1. FATFS 分析之——数据结构

 1. FATFS 分析之——数据结构

文件系统主要数据结构全放在ff.h里面。

1.    File system object structure

文件系统最主要的数据结构,用于存储文件系统的基本信息如:文件系统类型、扇区大小、簇的扇区数、FAT表的位置及大小等一些重要信息。

typedefstruct {

   BYTE    fs_type;         /*  FAT子类型,在Mount时,置0表示未挂载 */

   BYTE   drv;              /*  物理驱动号 */

   BYTE   csize;              /* Sectors per cluster (1,2,4...128) */ 每个簇扇区数目

   BYTE    n_fats;          /* FAT表数目 */

   BYTE   wflag;            /* win[] dirtyflag (1:must be written back) */ 标记文件是否被改动过

   BYTE    fsi_flag;       /* fsinfodirty flag (1:must be written back) */标记文件系统时否被改动过

   WORD   id;             /*File system mount ID */文件系统挂载ID

   WORD    n_rootdir;    /* Number ofroot directory entries (FAT12/16) */  FAT12/16所用,这里不作深究

#if_MAX_SS != 512

   WORD    ssize;          /* Bytes per sector (512,1024,2048,4096) */每个扇区字节数

#endif

#if_FS_REENTRANTfs_reentrant,允许重入

   _SYNC_t sobj;           /*Identifier of sync object */// 创建同步对像

#endif

#if!_FS_READONLY // 只读模式

   DWORD   last_clust;    /* Last allocated cluster*/

   DWORD   free_clust;    /* Number of freeclusters */

   DWORD   fsi_sector;     /* fsinfo sector (FAT32)*/

#endif

#if_FS_RPATH // 允讲相对路径

   DWORD  cdir;          /* Currentdirectory start cluster (0:root) */

#endif

   DWORD   n_fatent;      /* Number ofFAT entries (= number of clusters + 2) */FAT入口

   DWORD   fsize;         /* Sectors per FAT */每个FAT所占扇区

   DWORD   fatbase;       /* FATstart sector */FAT表起始扇区

   DWORD   dirbase;       /* Rootdirectory start sector (FAT32:Cluster#) */根目录扇区

   DWORD   database;      /* Data startsector */数据目录扇区

   DWORD   winsect;       /* Currentsector appearing in the win[] */当前缓冲区中存储的扇区号

   BYTE    win[_MAX_SS];  /* Disk access window forDirectory, FAT (and Data on tiny cfg) */

}FATFS;

2.    File object structure

这个结构体存储一个文件的相关信息()要调用文件系统上层接口函数必须创建的一个实体。

typedefstruct {

   FATFS*fs;             /*Pointer to the owner file system object */文件所在FS

   WORD   id;            /*Owner file system mount ID */所在的FS挂载编号

   BYTE   flag;            /* File statusflags */文件状态标志

   BYTE   pad1;          //未查到有何作用,以前版本此位:扇区偏移

   DWORD  fptr;          /* Fileread/write pointer */文件读写指针

   DWORD   fsize;         /* File size */文件大小

   DWORD   org_clust;     /* File startcluster (0 when fsize==0) */文件起始扇区

   DWORD   curr_clust;    /* Current cluster */当前簇

   DWORD   dsect;         /* Current data sector */数据当前扇区

#if!_FS_READONLY只读模式

   DWORD   dir_sect;      /* Sectorcontaining the directory entry */

   BYTE*   dir_ptr;       /* Ponter to the directory entry in the window */

#endif

#if_USE_FASTSEEK

   DWORD* cltbl;         /* Pointer to the cluster link map table (null on file open) */指向簇链映射表

#endif

#if_FS_SHARE

   UINT    lockid;        /* File lock ID (index of file semaphore table) */

#endif

#if!_FS_TINY

   BYTE    buf[_MAX_SS];  /* File data read/write buffer*/

#endif

}FIL;

3.   Directory object structure

    文件目录结构体,主要作用是在文件系统处理目录操作时所用到的结构体。

typedefstruct {

   FATFS*fs;            /*Pointer to the owner file system object */

   WORD    id;            /* Owner file system mount ID */

   WORD   index;         /* Currentread/write index number */

   DWORD   sclust;        /*Table start cluster (0:Root dir) */

   DWORD   clust;         /* Current cluster */

   DWORD  sect;           /* Currentsector */

   BYTE*  dir;           /*Pointer to the current SFN entry in the win[] */

   BYTE*  fn;            /*Pointer to the SFN (in/out) {file[8],ext[3],status[1]} */

#if_USE_LFN

   WCHAR* lfn;           /* Pointer to the LFN working buffer */

   WORD    lfn_idx;       /*Last matched LFN index number (0xFFFF:No LFN) */

#endif

}DIR;

4.   File status structure (FILINFO)

记录文件目录项信息的数据结构,这个结构主要描述文件的状态信息,包括文件名13个字符(8+.+3+\0)、属性、修改时间

typedefstruct {

   DWORD   fsize;         /* File size */

   WORD   fdate;          /* Last modifieddate */

   WORD   ftime;         /* Last modifiedtime */

   BYTE    fattrib;       /*Attribute */

   TCHAR   fname[13];      /* Short file name(8.3 format) */

#if_USE_LFN

   TCHAR* lfname;          /* Pointer tothe LFN buffer */

   UINT    lfsize;        /* Size of LFN buffer in TCHAR */

#endif

}FILINFO;

5.   用于共享控制的结构体。

typedefstruct {

   FATFS*fs;             /* File ID 1, volume (NULL:blank entry) */

   DWORDclu;               /* File ID 2, directory */

   WORDidx;              /* File ID 3, directory index */

   WORDctr;               /* File open counter, 0:none, 0x01..0xFF:read open count, 0x100:write mode */

}FILESEM;


6.  File function return code

typedefenum {

   FR_OK =0,             /* (0) Succeeded */

   FR_DISK_ERR,           /* (1) A hard error occured in the low level disk I/O layer */

   FR_INT_ERR,            /* (2) Assertion failed */

   FR_NOT_READY,       /* (3) The physical drive cannot work */

   FR_NO_FILE,            /* (4) Could not find the file */

   FR_NO_PATH,            /* (5) Could not find the path */

   FR_INVALID_NAME,    /* (6) The path nameformat is invalid */

   FR_DENIED,             /* (7) Acces denied due to prohibited access or directory full */

   FR_EXIST,               /* (8) Acces denied due to prohibited access */

   FR_INVALID_OBJECT,     /* (9) The file/directoryobject is invalid */

   FR_WRITE_PROTECTED,    /* (10) The physical drive is writeprotected */

   FR_INVALID_DRIVE,      /* (11) The logical drivenumber is invalid */

   FR_NOT_ENABLED,        /* (12) Thevolume has no work area */

   FR_NO_FILESYSTEM,       /* (13) There is no validFAT volume on the physical drive */

   FR_MKFS_ABORTED,        /* (14) The f_mkfs()aborted due to any parameter error */

   FR_TIMEOUT,            /* (15) Could not get a grant to access the volume within defined period */

   FR_LOCKED,             /* (16) The operation is rejected according to the file shareing policy */

   FR_NOT_ENOUGH_CORE,     /* (17) LFN working buffer could not be allocated */

   FR_TOO_MANY_OPEN_FILES /* (18) Number of open files > _FS_SHARE */

}FRESULT;

 

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值