📄 fat_dir.c
字号:
//****************************************************************
// FileName: fat_dir.c
// Description: file system control
// Version: 1.0
// Histrory:
// 1.0 chanjl 2003.11.10 create the module
//****************************************************************
#include "DataType.h"
#include "FTC32t.h"
#include "Define.h"
#include "ftc32t_m.h"
#include "register.h"
#include "global_variable.h"
#include "fat.h"
#include "fat_h.h"
#include <string.h>
/*F**************************************************************************
* NAME: fat_goto_subdir
*----------------------------------------------------------------------------
* PARAMS:
* id: file extension to select
*
* return:
* - OK: subdir selected
* - KO: current entry not a directory
* - KO: low level error
*----------------------------------------------------------------------------
* PURPOSE:
* Go to the subdir if current is a directory
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
* Also called by goto_parentdir() with current info from parent info
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
Byte fat_goto_subdir (void)
{
fat_save_file_info(GOTO_CHILD);
/* check if current file is a directory */
if ((fat_cache.current.attributes & ATTR_DIRECTORY) == ATTR_DIRECTORY)
{
/* computes the sector address (RELATIVE) */
if (fat_cache.current.attributes != ATTR_ROOT_DIR)
{ /* go to not root dir */
dir_is_root = FALSE; /* not the root dir */
fat_cache.parent = fat_cache.current;
/* get directory allocation table */
fat_get_clusters(&dclusters, MAX_DIR_FRAGMENT_NUMBER);
/* initialize fat pointers */
fat_dchain_nb_clust = 0;
fat_dchain_index = 0;
fat_dclust_byte_count = 0;
fat_dir_sector_offset = 0;
}
else
{ /* go to root dir */
fat_goto_rootdir();
}
return OK;
}
else
{
return KO;
}
}
/*F**************************************************************************
* NAME: fat_goto_parent
*----------------------------------------------------------------------------
* PARAMS:
* id: file extension to select
*
* return:
*----------------------------------------------------------------------------
* PURPOSE:
* Go to the parent directory
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
Byte fat_goto_parent(void)
{
/* goto the parent directory */
fat_cache.current = fat_cache.parent;
if((fat_cache.current.attributes & ATTR_DIRECTORY ) == ATTR_DIRECTORY )
{
if(fat_cache.current.attributes == ATTR_ROOT_DIR)
{
dir_is_root = TRUE;
fat_cache.parent.attributes = 0;
fat_cache.parent.start_cluster.l = 0;
fat_goto_rootdir();
}
else
{
fat_dir_sector_offset = 0;
dir_is_root = FALSE;
fat_dclust_byte_count = 0;
/* create list of entries */
fat_global_id = FILE_DIR;
fat_dir_list_index = 1; /* point ".." entry */
fat_scan_dir(FALSE);
if (fat_dir_seek(DIR_SIZE) == OK)
{
fat_get_dir_entry(&fat_cache.parent); /* update parent dir info */
if(fat_cache.parent.attributes == ATTR_ROOT_DIR)
{
dir_is_root = TRUE;
return OK;
}
}
fat_get_clusters(&dclusters, MAX_DIR_FRAGMENT_NUMBER);
/* initialize fat pointers */
fat_dchain_nb_clust = 0;
fat_dchain_index = 0;
fat_dclust_byte_count = 0;
fat_dir_sector_offset = 0;
}
return OK;
}
else
return KO;
}
/*F**************************************************************************
* NAME: fat_goto_parent
*----------------------------------------------------------------------------
* PARAMS:
* id: file extension to select
*
* return:
*----------------------------------------------------------------------------
* PURPOSE:
* Go to the parent directory
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
Byte fat_goto_parentdir(void)
{
fat_save_file_info(GOTO_PARENT);
if((fat_cache.current.attributes & ATTR_DIRECTORY ) == ATTR_DIRECTORY)
{
if(fat_goto_parent() == OK)
return OK;
}
else
{
if(fat_goto_parent() == OK)
{
if(fat_goto_parent() == OK)
return OK;
}
}
return KO;
}
#if COMPILE_FAT_DIR_CREATE == TRUE
/*F**************************************************************************
* NAME: fat_dircreate
*----------------------------------------------------------------------------
* NOTE:
* This function creates first the free cluster chain from fat1 and then
* creates an entry in current directory
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
Byte fat_dircreate (char chDirName[8])
{
Byte flag;
Uint32 sectorNum;
Union32 clusterNum;
Union32 cluster;
Uint32 cluster_number;
Byte j;
Uint16 i;
flag = FALSE;
if(dir_is_root == FALSE)
{
cluster.l = fat_cache.parent.start_cluster.l; //dclusters[0].cluster + 2;//2004.3.30modify
fat_cache.current = fat_cache.parent;
fat_get_clusters(&dclusters,MAX_DIR_FRAGMENT_NUMBER);
}
else
{
if(fat_type_id == FAT_IS_32)
{
fat_cache.current.start_cluster.l = fat32_root_cluster;
cluster.l = fat32_root_cluster;
fat_get_clusters(&dclusters,MAX_DIR_FRAGMENT_NUMBER);
}
}
////////////////////////////////////////////////////////////////////////////////////////////
fat_global_id = FILE_DIR;
fat_scan_dir(TRUE);
/* get free clusters list */
if ( fat_set_clusters(&dclusters,2) == KO )
return KO;
if((dir_is_root == TRUE) && (fat_type_id != FAT_IS_32)) //&&(fat_cache.current.attributes != ATTR_DIRECTORY))
{
if(fat_dir_all_count == ((fat_ptr_data - fat_ptr_rdir)*fat_sector_size/32))
return KO;
}
else
{
if(((fat_dir_all_count%(fat_cluster_size * (fat_sector_size/32))) == 0) && (fat_dir_all_count != 0))
{
//fat_set_clusters(&dclusters,1);
if(fat_type_id != FAT_IS_32)
dclusters[0].cluster.w[0] = 0;
cluster_number = dclusters[0].cluster.l;
if(dclusters[0].number == 1)
{
dclusters[0] = dclusters[1];
}
else
{
dclusters[0].cluster.l++;
dclusters[0].number--;
}
flag = TRUE;
phy_write_open(cluster_number * fat_cluster_size + fat_ptr_data,fat_cluster_size);
for(j=0;j<fat_cluster_size;j++)
{
for(i=0;i<fat_sector_size;i++)
gl_buffer[i]=0x00;
phy_write_sector();
}
phy_write_close();
}
}
if(fat_type_id == FAT_IS_32)
{
clusterNum.l = dclusters[0].cluster.l + 2 ;
}
else
{
clusterNum.l = dclusters[0].cluster.w[1] + 2 ;
}
sectorNum = (clusterNum.l - 2) * fat_cluster_size + fat_ptr_data;
phy_write_open(sectorNum,fat_cluster_size);
gl_buffer[0] = '.';
for(i=1;i<11;i++)
gl_buffer[i] = 0x20;
gl_buffer[11] = ATTR_DIRECTORY; /* Attribute : directory */
for(i=12;i<32;i++)
gl_buffer[i] = 0x00;
gl_buffer[20] = clusterNum.b[1];//(Byte)(clusterNum >> 16); /* Low word first cluster number */
gl_buffer[21] = clusterNum.b[0];//(Byte)((clusterNum) >> 24);
gl_buffer[26] = clusterNum.b[3];//(Byte)(clusterNum); /* Low word first cluster number */
gl_buffer[27] = clusterNum.b[2];//(Byte)((clusterNum) >> 8);
gl_buffer[32] = '.';
gl_buffer[33] = '.';
for(i=34;i<43;i++)
gl_buffer[i] = 0x20;
if(dir_is_root==TRUE)
{
gl_buffer[43] = ATTR_ROOT_DIR;
}
else
{
gl_buffer[43] = ATTR_DIRECTORY; /* Attribute : directory */
}
for(i=44;i<64;i++)
gl_buffer[i] = 0x00;
if(dir_is_root)
{
if(fat_type_id != FAT_IS_32)
{
gl_buffer[58] = 0x00; /* Low word first cluster number */
gl_buffer[59] = 0x00;
}
else
{
gl_buffer[58] = 0x02; /* Low word first cluster number */
gl_buffer[59] = 0x00;
}
}
else
{
gl_buffer[52] = cluster.b[1]; //(Byte)(cluster>>16); /* Low word first cluster number */
gl_buffer[53] = cluster.b[0]; //(Byte)(cluster>>24);
gl_buffer[58] = cluster.b[3]; //(Byte)(cluster); /* Low word first cluster number */
gl_buffer[59] = cluster.b[2]; //(Byte)(cluster>>8);
}
for(i=64;i<fat_sector_size;i++)
gl_buffer[i]=0x00;
phy_write_sector();
for(j=1;j<fat_cluster_size;j++)
{
for(i=0;i<fat_sector_size;i++)
gl_buffer[i]=0x00;
phy_write_sector();
}
phy_write_close();
dclusters[0].number = 0;
fat_dir_update_fats(FALSE,&dclusters);
//////////////////////////////////////
if(dir_is_root == FALSE)
{
fat_cache.current = fat_cache.parent;
fat_get_clusters(&dclusters,MAX_DIR_FRAGMENT_NUMBER);
}
else
{
if(fat_type_id == FAT_IS_32)
{
fat_cache.current.start_cluster.l = 2;
fat_get_clusters(&dclusters,MAX_DIR_FRAGMENT_NUMBER);
}
}
/* no more place in file list */
if(flag == TRUE)
{
dclusters[fat_last_clust_index].number = 1;
fat_last_clust_index++;
dclusters[fat_last_clust_index].cluster.l = cluster_number;
dclusters[fat_last_clust_index].number = 0;
fat_dir_update_fats(FALSE,&dclusters);
}
fat_dchain_nb_clust = 0;
fat_dchain_index = 0;
fat_global_id = FILE_DIR;
//dir name save to lfn_name
memcpy(lfn_name,chDirName,8);
fat_create_entry(clusterNum.l);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -