... | ... | @@ -15,9 +15,10 @@ typedef uint32_t fat_entry_t; |
|
|
这里定义了一个名为**fat_entry_t** 的32位无符号整数类型别名,表示FAT表中的条目。
|
|
|
```c
|
|
|
#define SECTOR_SIZE 512
|
|
|
|
|
|
```
|
|
|
这里定义了一个名为**SECTOR_SIZE**的宏,来表示每个扇区的大小为512字节。
|
|
|
## 结构体
|
|
|
```c
|
|
|
struct fat32_dbr {
|
|
|
uint16_t bytes_per_sector; // 每个扇区的字节数
|
|
|
uint8_t sectors_per_cluster; // 每个簇的扇区数
|
... | ... | @@ -31,8 +32,9 @@ struct fat32_dbr { |
|
|
char file_system_type[9]; // 文件系统类型,最多8个字符,加一个结束符'\0'
|
|
|
// 其他 DBR 字段...
|
|
|
};
|
|
|
```
|
|
|
这个结构体定义了一个 FAT32 文件系统的 DBR(DOS Boot Record)结构,包含了各种与文件系统相关的信息,比如每个扇区的字节数、每个簇的扇区数、保留扇区数、FAT 表数量等。
|
|
|
|
|
|
```c
|
|
|
struct fat32_dir_entry {
|
|
|
char filename[11]; // 文件名
|
|
|
uint8_t attributes; // 属性
|
... | ... | @@ -41,19 +43,22 @@ struct fat32_dir_entry { |
|
|
uint16_t first_cluster_low; // 簇号的低16位
|
|
|
// 其他字段...
|
|
|
};
|
|
|
```
|
|
|
这个结构体定义了 FAT32 文件系统中的目录项结构,包含了文件名和文件属性等信息。
|
|
|
## 函数体
|
|
|
### allocate_cluster()
|
|
|
```c
|
|
|
fat_entry_t allocate_cluster(int fd, struct fat32_dbr dbr) {
|
|
|
...函数内容...
|
|
|
}
|
|
|
```
|
|
|
这个函数的作用是在fat32文件系统中分配一个空闲的簇。
|
|
|
#### 函数参数
|
|
|
int fd;
|
|
|
表示文件描述符,用于指定要操作的文件或设备。
|
|
|
struct fat32_dbr dbr
|
|
|
表示FAT32文件系统的DBR(DOS Boot Record),包含文件系统的关键参数。
|
|
|
```
|
|
|
|
|
|
#### 函数实现
|
|
|
1. 首先,计算FAT表在磁盘中的偏移量,即fat_offset = dbr.reserved_sector_count * dbr.bytes_per_sector。这里的reserved_sector_count表示保留的扇区数,bytes_per_sector表示每个扇区的字节数。
|
|
|
|
... | ... | @@ -121,10 +126,12 @@ filename: 要检查的文件名。 |
|
|
如果 memcmp 返回 0,表示找到了重复的文件名,函数立即返回 true。
|
|
|
6. 未找到重复文件名:
|
|
|
如果循环结束时都没有找到相同的文件名,则函数返回 false。
|
|
|
```c
|
|
|
### create_directory_entry()
|
|
|
bool create_directory_entry(int fd, off_t dir_offset, const char *filename, uint8_t attributes, fat_entry_t cluster_number) {
|
|
|
...函数内容...
|
|
|
}
|
|
|
```
|
|
|
这个是创建目录项的函数。
|
|
|
#### 函数参数
|
|
|
int fd, off_t dir_offset, const char *filename, uint8_t attributes, fat_entry_t cluster_number
|
... | ... | |