fat相关结构及操作过程

上传人:第*** 文档编号:32685480 上传时间:2018-02-12 格式:DOC 页数:8 大小:56KB
返回 下载 相关 举报
fat相关结构及操作过程_第1页
第1页 / 共8页
fat相关结构及操作过程_第2页
第2页 / 共8页
fat相关结构及操作过程_第3页
第3页 / 共8页
fat相关结构及操作过程_第4页
第4页 / 共8页
fat相关结构及操作过程_第5页
第5页 / 共8页
点击查看更多>>
资源描述

《fat相关结构及操作过程》由会员分享,可在线阅读,更多相关《fat相关结构及操作过程(8页珍藏版)》请在金锄头文库上搜索。

1、第 1 章 数据结构1.1 关键数据结构struct fat_fs_structstruct partition_struct* partition; /分区类型,扇区偏移、总长度struct fat_header_struct header; /fat 的相关信息,包括分区起始扇区,总长度,fat表的大小,扇区大小,簇大小,数据区的偏移,根目录区的偏移;该文件结构代表了整个 fat 分区Partition 及 header 的具体的结构定义如下:struct fat_header_structoffset_t size; /磁盘空间大小,以字节来计算offset_t fat_offset;

2、/fat 的起始位置,以字节来计算uint32_t fat_size; /fat 有多大,以字节来计算uint16_t sector_size; /扇区大小,以字节来计算uint16_t cluster_size; /簇大小,以字节来计算offset_t cluster_zero_offset;/数据区域的地址,以字节来计算offset_t root_dir_offset; /根目录地址,以字节来计算#if FAT_FAT32_SUPPORTcluster_t root_dir_cluster;#endif; /以上数据是从分区第一个扇区读出来并整理的相关数据struct partition_

3、structdevice_read_t device_read;device_read_interval_t device_read_interval;device_write_t device_write; device_write_interval_t device_write_interval;uint8_t type; /分区类型uint32_t offset; /分区的偏移(单位为扇区)uint32_t length; /总长度(单位为扇区,每个 512 字节);以上信息是从第一扇区读出来的数据第 2 章 目录及文件的操作过程2.1 打开分区操作/* open first parti

4、tion */struct partition_struct* partition = partition_open(sd_raw_read,d_raw_read_interval,sd_raw_write,sd_raw_write_interval,0);/打开第 0 个分区,获取分区数据2.2 打开文件系统/* open file system */struct fat_fs_struct* fs = fat_open(partition);/获取文件系统信息2.3 打开根目录/* open root directory */struct fat_dir_entry_struct dire

5、ctory;fat_get_dir_entry_of_path(fs, /, struct fat_dir_struct* dd = fat_open_dir(fs, 打开目录分为两个动作1 打开根目录,获取到目录项数据 fat_dir_entry_struct2 根据获得目录项数据,打开相应的目录,并获得目录描述符 fat_dir_struct如何找到目录项: 打开根目录,获取到根目录的描述符 使用根目录的描述符,根据名称通过 fat_read_dir 获取 fat_dir_entry_struct 检查 fat_dir_entry_struct 中的名称是否与制定目录名称匹配 匹配则则返回

6、相应的目录项查看目录项的数据结构:struct fat_dir_entry_struct/* The files name, truncated to 31 characters. */char long_name32; /文件名/* The files attributes. Mask of the FAT_ATTRIB_* constants. */uint8_t attributes; /目录属性,只读/* Compressed representation of modification time. */uint16_t modification_time; /修改时间/* Compr

7、essed representation of modification date. */uint16_t modification_date;/修改日期/* The cluster in which the files first byte resides. */cluster_t cluster;/在哪一簇/* The files size. */uint32_t file_size;/文件大小/* The total disk offset of this directory entry. */offset_t entry_offset;/字节偏移;该目录项包含了名称/属性/目录的具体位

8、置等信息/目录描述符struct fat_dir_structstruct fat_fs_struct* fs;struct fat_dir_entry_struct dir_entry; /目录项cluster_t entry_cluster; /首簇地址uint16_t entry_offset;/包含的信息基本和目录项一致。代表了一个目录信息。当要搜索该目录下的文件,就需要使用到该描述符。2.4 打开其他目录打开其他目录分成以下步骤: 打开根目录”/” 在根目录下找到对应名称的目录项 fat_dir_entry_struct 打开目录获得描述符 fat_dir_struct文件操作的有两

9、个元素:目录项,目录描述符。首先查找到目录项,然后才能获取目录描述符。目录的操作函数如下:struct fat_dir_struct * fat_open_dir (struct fat_fs_struct *fs, const struct fat_dir_entry_struct *dir_entry) Opens a directory. (获取目录描述符)void fat_close_dir (struct fat_dir_struct *dd) Closes a directory descriptor. uint8_t fat_read_dir (struct fat_dir_st

10、ruct *dd, struct fat_dir_entry_struct *dir_entry) Reads the next directory entry contained within a parent directory. (一般该函数封装在查询目录的函数中) ,不以接口暴露在应用程序内uint8_t fat_reset_dir (struct fat_dir_struct *dd) Resets a directory handle. (一般该函数封装在查询目录的函数中) ,不以接口暴露在应用程序内uint8_t fat_create_dir (struct fat_dir_st

11、ruct *parent, const char *dir, struct fat_dir_entry_struct *dir_entry) Creates a directory. uint8_t fat_delete_dir (struct fat_fs_struct *fs, struct fat_dir_entry_struct *dir_entry) Deletes a directory. /目录的封装函数/在一个目录中查找对应名称的目录uint8_t find_file_in_dir(struct fat_fs_struct* fs, struct fat_dir_struct*

12、 dd, const char* name, struct fat_dir_entry_struct* dir_entry)while(fat_read_dir(dd, dir_entry)if(strcmp(dir_entry-long_name, name) = 0) /文件名完全相同fat_reset_dir(dd);return 1; return 0;/根据路径名查找文件目录的形式为”/TEMP/G.TXT” 必须采用大写的方式uint8_t fat_get_dir_entry_of_path(struct fat_fs_struct* fs, const char* path, s

13、truct fat_dir_entry_struct* dir_entry)2.5 删除目录删除目录,必须先删除其下的所有文件和目录,否则存储空间无法释放。2.6 打开文件文件操作主要涉及到两个概念: 目录项 文件描述符文件项在目录区域文件描述符用于文件操作打开文件分成以下步骤进行: 打开分区 打开文件系统 打开根目录 进入相应的目录 查找是否存在指定名称的文件目录项 打开目录项,获取文件描述符封装好的打开文件的函数如下:struct fat_file_struct* open_file_in_dir(struct fat_fs_struct* fs, struct fat_dir_struc

14、t* dd, const char* name)struct fat_dir_entry_struct file_entry;if(!find_file_in_dir(fs, dd, name, &file_entry) /查找是否存在该名称的目录项return 0;return fat_open_file(fs, 2.7 读取文件读取文件只需要文件描述符while(fat_read_file(fd, buffer, sizeof(buffer) 0)fat_close_file(fd);2.8 写文件写文件分成几步: 打开文件,获取文件描述符 调整文件指针 写数据uint8_t fat_se

15、ek_file ( struct fat_file_struct * fd, int32_t * offset, uint8_t whence )除非想改写文件内容,否则都需要从文件末尾开始写数据所以 whence 通常为 FAT_SEEK_ENDOffset 通常为 0写文件的过程如下:if(!fat_seek_file(fd, NULL, FAT_SEEK_END) /调整文件指针fat_close_file(fd);/* write text to file */if(fat_write_file(fd, (uint8_t*) buffer, data_len) != data_len)/写文件fat_close_file(fd);2.9 删除文件删除文件需要分以下步骤: 遍历目录,获取目录项 执行删除文件操作struct fat_dir_entry_struct file_entry;if(find_file_in_dir(fs, dd, command, &file_entry) /查找目录项if(fat_delete_file(fs, &file_entry)/删除文件

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 中学教育 > 职业教育

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号