




已阅读5页,还剩3页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
第1章 数据结构1.1 关键数据结构struct fat_fs_struct struct partition_struct* partition; /分区类型,扇区偏移、总长度 struct fat_header_struct header; /fat的相关信息,包括分区起始扇区,总长度,fat表的大小,扇区大小,簇大小,数据区的偏移,根目录区的偏移;该文件结构代表了整个fat分区Partition及header的具体的结构定义如下:struct fat_header_struct offset_t size; /磁盘空间大小,以字节来计算 offset_t fat_offset; /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_SUPPORT cluster_t root_dir_cluster;#endif; /以上数据是从分区第一个扇区读出来并整理的相关数据struct partition_struct device_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 partition */ 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 directory; fat_get_dir_entry_of_path(fs, /, &directory); struct fat_dir_struct* dd = fat_open_dir(fs, &directory);打开目录分为两个动作1 打开根目录,获取到目录项数据fat_dir_entry_struct2 根据获得目录项数据,打开相应的目录,并获得目录描述符fat_dir_struct如何找到目录项:n 打开根目录,获取到根目录的描述符n 使用根目录的描述符,根据名称通过fat_read_dir获取fat_dir_entry_structn 检查fat_dir_entry_struct中的名称是否与制定目录名称匹配n 匹配则则返回相应的目录项查看目录项的数据结构: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; /修改时间 /* Compressed 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;/字节偏移;该目录项包含了名称/属性/目录的具体位置等信息/目录描述符struct fat_dir_struct struct fat_fs_struct* fs; struct fat_dir_entry_struct dir_entry; /目录项 cluster_t entry_cluster; /首簇地址 uint16_t entry_offset;/包含的信息基本和目录项一致。代表了一个目录信息。当要搜索该目录下的文件,就需要使用到该描述符。2.4 打开其他目录打开其他目录分成以下步骤:n 打开根目录”/”n 在根目录下找到对应名称的目录项fat_dir_entry_structn 打开目录获得描述符fat_dir_struct文件操作的有两个元素:目录项,目录描述符。首先查找到目录项,然后才能获取目录描述符。目录的操作函数如下: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_struct *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_struct *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* 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, struct fat_dir_entry_struct* dir_entry)2.5 删除目录删除目录,必须先删除其下的所有文件和目录,否则存储空间无法释放。2.6 打开文件文件操作主要涉及到两个概念:n 目录项n 文件描述符文件项在目录区域文件描述符用于文件操作打开文件分成以下步骤进行:n 打开分区n 打开文件系统n 打开根目录n 进入相应的目录n 查找是否存在指定名称的文件目录项n 打开目录项,获取文件描述符封装好的打开文件的函数如下:struct fat_file_struct* open_file_in_dir(struct fat_fs_struct* fs, struct fat_dir_struct* 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, &file_entry);2.7 读取文件读取文件只需要文件描述符while(fat_read_file(fd, buffer, sizeof(buffer) 0)fat_close_file(fd);2.8 写文件写文件分成几步:n 打开文件,获取文件描述符n 调整文件指针n 写数据uint8_t fat_seek_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_
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
评论
0/150
提交评论