📄 libfat.h
字号:
WORD DIR_CrtTime; /* 14 creation time */ WORD DIR_CrtDate; /* 16 creation date */ WORD DIR_LstAccDate; /* 18 last access date */ WORD DIR_FstClusHI; /* 20 start cluster, Hi */ WORD DIR_WrtTime; /* 22 time stamp */ WORD DIR_WrtDate; /* 24 date stamp */ WORD DIR_FstClusLO; /* 26 starting cluster number */ DWORD DIR_FileSize; /* 28 size of the file */}__attribute__ ((packed)) DirEntry_t;// sfentry == DirEntry_t, value = DWORD#define FIRST_CLUSTER(sfnentry, value) ((WORD *) &value)[0] = ((DirEntry_t) sfnentry).DIR_FstClusLO; ((WORD *) &value)[1] = ((DirEntry_t) sfnentry).DIR_FstClusHI/* Special codes for the first byte of a directory entry (DIR_Name[0] */#define FREEENT 0xE5 /* The directory entry is free */#define ENDOFDIR 0x00 /* This and the following entries are free */#define DIRENT_ISFREE(D) (((D) == FREEENT) || ((D) == ENDOFDIR))#define DIRENT_ISLAST(D) (D == ENDOFDIR)/* 2bytes trailing signature that occupies the byte 510 and 511 of the boot sector,bkbootsector and FSinfo sector */#define BPB_TRAILSIG 0x55AA/* Attributes for DIR_Attr */#define ATTR_READ_ONLY 0x1#define ATTR_HIDDEN 0x2#define ATTR_SYSTEM 0x4#define ATTR_VOLUME_ID 0x8#define ATTR_DIRECTORY 0x10#define ATTR_ARCHIVE 0x20#define ATTR_LONG_NAME ( ATTR_READ_ONLY | ATTR_HIDDEN | ATTR_SYSTEM | ATTR_VOLUME_ID )#define ATTR_ISDIR(D) ( ( (D) & ATTR_DIRECTORY ) == ATTR_DIRECTORY )/* values used by libfat. not correlated with fat file system values */#define LIBFAT_DIRENT_FREE 0x01 //(0001)#define LIBFAT_DIRENT_LASTFREE 0x09 //(1001)#define LIBFAT_DIRENT_SFN 0x02 //(0010)#define LIBFAT_DIRENT_LFN 0x04 //(0100)#define LIBFAT_DIRENT_LFN_LAST 0x0C //(1100)#define LIBFAT_DIRENT_ISFREE(res) ((res & LIBFAT_DIRENT_FREE) == LIBFAT_DIRENT_FREE)#define LIBFAT_DIRENT_ISLASTFREE(res) ((res & LIBFAT_DIRENT_LASTFREE) == LIBFAT_DIRENT_LASTFREE)#define LIBFAT_DIRENT_ISSFN(res) ((res & LIBFAT_DIRENT_SFN) == LIBFAT_DIRENT_SFN)#define LIBFAT_DIRENT_ISLFN(res) ((res & LIBFAT_DIRENT_LFN) == LIBFAT_DIRENT_LFN)#define LIBFAT_DIRENT_ISLFNLAST(res) ((res & LIBFAT_DIRENT_LFN_LAST) == LIBFAT_DIRENT_LFN_LAST)/* values used in fat file system */#define LFN_LASTENTRY 0x40 /* Last entry of a LFN set */#define LFN_PADDING 0xFF /* padding for last LFN entry, if needed */#define LFN_NULL 0x00 /* null terminator for last LFN entry */#define LFN_ISNULL(D) ( D == LFN_NULL )#define LFN_ISLAST(D) ((D & LFN_LASTENTRY) == LFN_LASTENTRY)/* FAT 32-byte Long File Name Directory Entry structure */typedef struct{ BYTE LDIR_Ord; /* Sequence number for slot */ WORD LDIR_Name1[5]; /* First 5 Unicode characters */ BYTE LDIR_Attr; /* Attributes, always 0x0F */ BYTE LDIR_Type; /* Reserved, always 0x00 */ BYTE LDIR_Chksum; /* Checksum of 8.3 name */ WORD LDIR_Name2[6]; /* 6 more Unicode characters */ WORD LDIR_FstClusLO; /* First cluster number, must be 0 */ WORD LDIR_Name3[2]; /* Last 2 Unicode characters */}__attribute__ ((packed)) LfnEntry_t;/* Linked likst structure for free clusters */struct llist { int n; struct llist *next;};typedef struct llist FreeClus_t;/* This structure stores all informations about a FAT volume. *//* It is the P structure (private data) for file system volume ops. */typedef struct{ /* Data referring to the hosting block device */ DWORD blkDevFd; /* File Descriptor of the block device hosting the volume */ mode_t mode; /* Permissions for the volume */ uid_t uid; /* user ID of owner */ gid_t gid; /* group ID of owner */ /* Some precalculated data */ DWORD VolSig; /* Must be FAT_VOLSIG for a valid volume */ FatType_t FatType; /* Can be FAT12, FAT16 or FAT32 */ DWORD DataClusters; /* The total number of valid not reserved data clusters (last valid data cluster is dataclusters + 1 */ DWORD FirstDataSector; /* The first sector of the data region, usually the beginning of rootdir in fat32 */ DWORD FirstRootCluster; /* The first sector of FAT12/FAT16 root cluster, usually 2 (useless at the moment) */ DWORD freecnt; /* The count of free clusters */ DWORD nextfree; /* The cluster number from which to start */ /* to search for free clusters, if known */ int numfats; DWORD freeclus[FCLUS_BUFSZ]; int fstfclus; int fclusz; int bps; int spc; int bpc; DWORD fatsz; // It's ok to have an int here, cause a fat can have up to 2^28 DWORDs int rsvdbytecnt; // count of reserved bytes before fat 0 off64_t bps64; // bytes per sector = V->Bpb.BPB_BytsPerSec; off64_t spc64; // sectors per cluster d=V->Bpb.BPB_SecPerClus; off64_t bpc64; // bytes per cluster = off64_t fds64; // first data serctor e=V->FirstDataSector; off64_t fdb64; // first data byte off64_t rootdir16off; int rootdir16sz; /* a pthread mutex */ pthread_mutex_t fat_mutex; /* for write0data (in seek) */ char zerobuf[ZERO_BFSZ]; /* FAT in fat12/16 volumes */ char *fat; /* The BIOS Parameter Block of the volume (the long entry) */ Bpb_t Bpb; FSInfo_t Fsi;} Volume_t;/* Structure for dirents */typedef struct{ DWORD clus; DWORD off; off64_t off1; off64_t off2; off64_t direntoff; int len1; int len2; int len; LfnEntry_t entry[21]; int last;} DirEnt_t;/* The file structure stores all the informations about an open file. */ // VA CAMBIATA!!!!!!!!!!!!!!!!typedef struct{ Volume_t *V; /* Pointer to the volume hosting the file */ DWORD ParentFstClus; /* First cluster of the parent directory */ DWORD ParentOffset; /* Offset of the direntry in parent file */ DWORD DirEntryClus; /* Cluster containing the direntry */ DWORD DirEntryOffset; /* Offset of the dir entry in the cluster */// DWORD DirEntrySector; /* Sector containing the directory entry */// DWORD DirEntrySecOff; /* Byte offset of the 1st dir entry in sector */// off64_t AbsOffset; /* Absolute offset in the fs of the 1st dir entry */ DirEnt_t D; /* The file's directory entry */ DirEntry_t *DirEntry; /* Pointer to sfn entry at the end of the chain */ int Mode; /* File opening mode */ char FileName[511]; /* Utf8 filename */ int rootdir; /* 1 if the file refers to rootdir */ /* The following fields refer to the byte position into the file */ DWORD CurClus; /* Cluster where the file offset is atm */ DWORD CurOff; /* Offset in the current cluster */ off64_t CurAbsOff; /* Absolute offset related to the beginning of the file */ /* Useful to know if we have to go ahead or restart from the beginning */} File_t;/* Prototypes *//* FAT access functions . these should be static so we dont have to declare them here */int fat32_read_entry(Volume_t *V, DWORD N, int FatNum, DWORD *Value);#ifdef FATWRITEint fat32_write_entry(Volume_t *V, DWORD N, int FatNum, DWORD Value);int fat32_writen_entry(Volume_t *V, DWORD N, DWORD Value);#endiftime_t fat_mktime2(DirEntry_t *D);int fat_fill_time(WORD *Date, WORD *Time, time_t t);int fat_isfree(Volume_t *V,DWORD value);int fat_isbad(Volume_t *V,DWORD value);int fat_iseoc(Volume_t *V,DWORD value);int fat_legalclus(Volume_t *V,DWORD value);DWORD fat_eocvalue(Volume_t *V);/* Directory entry functions */int fat_populate_freelist(Volume_t *V);DWORD fat_getFreeCluster(Volume_t *V);BYTE lfn_checksum(BYTE *name);int analize_dirent(LfnEntry_t *D);int check_cluster_bound(Volume_t *V, DWORD *Cluster, DWORD *Offset);int fetch_entry(Volume_t *V, DWORD *Cluster, DWORD *Offset, LfnEntry_t *D);int fetch_next_direntry(Volume_t *V, DirEnt_t *D, DWORD *Cluster, DWORD *Offset);int check_lfn_order(LfnEntry_t *Buffer, int bufsize);int check_lfn_checksum(LfnEntry_t *Buffer, int bufsize);WORD fetch_lfn_char(LfnEntry_t *D, int n);int find_lfn_length( LfnEntry_t *D, int bufsize);int extract_lfn_name( LfnEntry_t *Buffer, int bufsize, WORD *dest, int length);int find_sfn_length( DirEntry_t *D, int bufsize);int extract_sfn_name(DirEntry_t *D, int bufsize, char *name);int fatentry_to_dirent(Volume_t *V, DirEnt_t *D, struct dirent *dirp);int find_direntry(Volume_t *V, char *name, DWORD *Cluster, DWORD *Offset);int traverse_path(Volume_t *V, gchar **parts, guint parts_len, DWORD *Cluster);int find_file(Volume_t *V, const char *path, File_t *F, DWORD *Cluster, DWORD *Offset);int fat_fat_sync(Volume_t *V);#endif /* #ifdef _BITS_LIBFAT_H */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -