📄 fat_dir.c
字号:
fat_get_dir_entry(&fat_cache.current); /* update current directory info */
/* parent dir is also root */
if(dir_is_root==TRUE)
{
if(fat_type_id == FAT_IS_32)
{
fat_cache.parent.start_cluster.l = fat32_root_cluster;
}
else
{
fat_cache.parent.start_cluster.l = 0;
}
fat_cache.parent.attributes = ATTR_ROOT_DIR; // mark as root dir //
}
else
{
fat_cache.parent.start_cluster.l = cluster.l;
fat_cache.parent.attributes = ATTR_DIRECTORY ; // mark as root dir //
}
return OK;
}
#endif //COMPILE_FAT_DIR_CREATE
#if COMPILE_FAT_DELETE == TRUE
/*F**************************************************************************
* NAME: fat_dirdelete
*----------------------------------------------------------------------------
* PURPOSE:
* Delete a selected directory
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
* This function works: have no file or subdirectory in the directory
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
Byte fat_dirdelete (void)
{
Uint16 temp;
Uint16 temp_all;
temp = fat_dir_list_index;
temp_all = fat_dir_list_last;
fat_get_clusters(&dclusters,MAX_DIR_FRAGMENT_NUMBER);
dir_is_root = FALSE;
fat_global_id = FILE_DIR;
fat_scan_dir(TRUE);
if( fat_dir_all_count > 2 ) //this file have subdir or file.
return KO;
/* FAT update */
fat_dclust_byte_count = 0; /* byte 0 of cluster */
/* reset the allocation list variable */
fat_dchain_index = 0;
fat_dchain_nb_clust = 0; /* start on first contiguous cl */
/* get file allocation list */
fat_dir_update_fats(TRUE,&dclusters);
if(fat_cache.parent.attributes == ATTR_ROOT_DIR)
{
dir_is_root = TRUE;
if(fat_type_id == FAT_IS_32)
{
fat_cache.current.start_cluster.l = 2;
fat_get_clusters(&dclusters,MAX_DIR_FRAGMENT_NUMBER);
}
}
else
{
fat_cache.current = fat_cache.parent;
//fat_dir_start_sect = (fat_cache.current.start_cluster - 2)*fat_cluster_size + fat_ptr_data ;
fat_get_clusters(&dclusters,MAX_DIR_FRAGMENT_NUMBER);
}
fat_dir_sector_offset = 0;
fat_phy_del();
/* Reconstruct list file */
if(temp_all == 0)
return OK;
else
{
if(temp == temp_all - 1)
{
temp -= 1;
}
}
fat_dir_list_index = temp;
fat_scan_dir(FALSE);
fat_dir_seek(((Uint32)fat_dir_all_count) * DIR_SIZE );
fat_get_dir_entry(&fat_cache.current); // update current file info
return OK;
}
/*F**************************************************************************
* NAME: fat_phy_del
*----------------------------------------------------------------------------
* PURPOSE:
* Delete a selected directory
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
* This function works: have no file or subdirectory in the directory
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
void fat_phy_del(void)
{
Uint16 i;
// Uint32 para;
Uint16 count;
Uint32 root_sector;
fat_scan_dir(FALSE);
fat_dir_seek(fat_dir_all_count * 32);
i = fat_dir_sector_offset ;//% fat_sector_size; /*number of bytes to seek in a cluster*/
phy_read_sector();
phy_read_close();
root_sector = fat_dir_current_sect;
count = 0;
while (gl_buffer[i+11] == ATTR_LFN_ENTRY)
{
/* mark file as deleted */
gl_buffer[i] = FILE_DELETED;
i += 32;
count++;
if (i == fat_sector_size)
{
fat_write_sector(root_sector);
if((dir_is_root == TRUE)&&(fat_type_id != FAT_IS_32))
{
root_sector++;
phy_read_open(root_sector); // read entry's sector //
phy_read_sector();
}
else
{
fat_dir_seek(((Uint32)count)*32);
count = 0;
phy_read_sector();
root_sector = fat_dir_current_sect;
}
phy_read_close();
i = 0;
}
}
gl_buffer[i] = FILE_DELETED;
fat_write_sector(root_sector);
}
#endif // #if COMPILE_FAT_DELETE == TRUE
/*F**************************************************************************
* NAME: fat_dir_seek
*----------------------------------------------------------------------------
* PARAMS:
* offset: offset to current position in signed word value
*
* return:
*----------------------------------------------------------------------------
* PURPOSE:
* Seek from the current position to a new offset computing relative
* poisition +/- scan size limited to a 16 bit offset
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
* We consider here that the seek size is minor to the cluster size !!!
* if you want to do a more than a cluster seek, issue two successive
* dseek commands
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
Byte fat_dir_seek (Uint32 offset)
{
Uint16 nb_sect; /* number of sectors to seek */
Uint32 next_cluster; /* the possible next cluster to load */
Uint16 target_cluster; /* the cluster to reach */
Uint16 i;
// fat_dir_current_offs = 0;
fat_dir_current_offs = offset; /* Calculate the absolute byte pos */
nb_sect = (Uint16)((fat_dir_current_offs) / fat_sector_size);
if(fat_type_id != FAT_IS_32)
fat_dir_current_sect = fat_ptr_rdir + nb_sect;
fat_dir_sector_offset = fat_dir_current_offs % (((Uint32)fat_sector_size) * fat_cluster_size);
fat_dclust_byte_count = fat_dir_sector_offset; //offset in a cluster
fat_dir_sector_offset %= fat_sector_size;
if ((dir_is_root == FALSE) || (fat_type_id == FAT_IS_32)) // Sub-directory /
{
// Find the # cluster
target_cluster = (nb_sect / fat_cluster_size);
fat_dchain_index = 0;
if(fat_type_id == FAT_IS_32)
next_cluster = dclusters[fat_dchain_index].cluster.l;
else
next_cluster = dclusters[fat_dchain_index].cluster.w[1];
fat_dchain_nb_clust = 1;
for (i = 0; i < target_cluster; i++)
{
if (dclusters[fat_dchain_index].number <= fat_dchain_nb_clust)
{ // new fragment
fat_dchain_index++;
fat_dchain_nb_clust = 1;
if(fat_type_id == FAT_IS_32)
next_cluster = dclusters[fat_dchain_index].cluster.l;
else
next_cluster = dclusters[fat_dchain_index].cluster.w[1];
}
else
{ // not new fragment
//next_cluster = dclusters[fat_dchain_index].cluster + fat_dchain_nb_clust;
if(fat_type_id == FAT_IS_32)
next_cluster = dclusters[fat_dchain_index].cluster.l;
else
next_cluster = dclusters[fat_dchain_index].cluster.w[1];
next_cluster += fat_dchain_nb_clust;
fat_dchain_nb_clust++;
}
}
fat_dir_current_sect = (((Uint32)(next_cluster) * fat_cluster_size)
+ fat_ptr_data + (nb_sect % fat_cluster_size));
}
phy_read_open(fat_dir_current_sect);
return OK;
}
/*F**************************************************************************
* NAME: fat_dgetsector
*----------------------------------------------------------------------------
* PARAMS:
*
* return:
*----------------------------------------------------------------------------
* PURPOSE:
* Return the directory data page at the current position
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
void fat_dgetsector (void)
{
Uint32 next_cluster;
/* new cluster in allocation list */
// if((fat_dclust_byte_count % (fat_sector_size * fat_cluster_size)) == 0 )
if(((fat_dclust_byte_count / fat_sector_size) % fat_cluster_size) == 0 )
{
//fat_dclust_byte_count=0;
/* extract if necessary the next cluster from the allocation list */
if ((dclusters[fat_dchain_index].number == fat_dchain_nb_clust)&&(dclusters[0].number != 0))
{ /* new fragment */
fat_dchain_index++;
fat_dchain_nb_clust = 1;
next_cluster = dclusters[fat_dchain_index].cluster.l;
fat_dir_current_sect = fat_ptr_data + ((Uint32)(next_cluster) * fat_cluster_size);
phy_read_open(fat_dir_current_sect);
}
else
{ /* no new fragment */
if(dclusters[fat_dchain_index].number == 0)
fat_dchain_nb_clust = 0;
//next_cluster = dclusters[fat_dchain_index].cluster + fat_dchain_nb_clust;
next_cluster = dclusters[fat_dchain_index].cluster.l + fat_dchain_nb_clust;
fat_dir_current_sect = fat_ptr_data + ((Uint32)(next_cluster) * fat_cluster_size);
//if((fat_dchain_index == 0)&&(fat_dchain_nb_clust == 0))
//{
phy_read_open(fat_dir_current_sect);
//}
fat_dchain_nb_clust++; /* one more cluster read */
}
//fat_dir_current_sect = fat_ptr_data + ((Uint32)(next_cluster) * fat_cluster_size);
//phy_read_open(fat_dir_current_sect);
}
fat_dclust_byte_count += fat_sector_size; /* one more byte read */
phy_read_sector();
}
/*F**************************************************************************
* NAME: fat_get_dir_entry
*----------------------------------------------------------------------------
* PARAMS:
* entry: directory entry structure
* return: none
*----------------------------------------------------------------------------
* PURPOSE:
* Get from directory all information about a directory or file entry
*----------------------------------------------------------------------------
* NOTE:
* This function reads directly datas from sectors
* It automaticaly computes difference between LFN and normal entries
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
void fat_get_dir_entry (fat_st_dir_entry xdata *entry)
{
Byte exit_flag = FALSE;
Byte lfn_entry_found = FALSE;
Byte i;
Uint16 num;
Uint16 j;
Byte flag;
byte count;
num = fat_dir_sector_offset; /*number of bytes to seek in a cluster*/
flag = FALSE;
count = 0;
for (i = MAX_FILENAME_LEN; i != 0; lfn_name[--i] = '\0');
phy_read_sector();
//num = num%fat_sector_size;
while (!exit_flag)
/* loop while the entry is not a normal one. */
{
/* read the directory entry */
if(flag == TRUE)
{
if ((dir_is_root == TRUE)&&(fat_type_id != FAT_IS_32))
{
if((num%fat_sector_size)==0)
{
//phy_read_open(fat_ptr_rdir+n);
phy_read_sector();
num=0;
}
}
else
{ /* subdir can be fragmented -> dgetc() */
if(num % fat_sector_size == 0 )
{
fat_dgetsector();
num=0;
}
}
}
j = num;
num += DIR_SIZE;
flag = TRUE;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -