📄 fat16.c
字号:
} } else { /* read entry from a subdirectory */ if(fat16_read_sub_dir_entry(dd->fs, dd->entry_next, &dd->dir_entry, dir_entry)) { ++dd->entry_next; return 1; } } /* restart reading */ dd->entry_next = 0; return 0;}/** * \ingroup fat16_dir * Resets a directory handle. * * Resets the directory handle such that reading restarts * with the first directory entry. * * \param[in] dd The directory handle to reset. * \returns 0 on failure, 1 on success. * \see fat16_read_dir */uint8_t fat16_reset_dir(struct fat16_dir_struct* dd){ if(!dd) return 0; dd->entry_next = 0; return 1;}/** * \ingroup fat16_fs * Writes a directory entry to disk. * * \note The file name is not checked for invalid characters. * * \note The generation of the short 8.3 file name is quite * simple. The first eight characters are used for the filename. * The extension, if any, is made up of the first three characters * following the last dot within the long filename. If the * filename (without the extension) is longer than eight characters, * the lower byte of the cluster number replaces the last two * characters to avoid name clashes. In any other case, it is your * responsibility to avoid name clashes. * * \param[in] fs The filesystem on which to operate. * \param[in] dir_entry The directory entry to write. * \returns 0 on failure, 1 on success. */uint8_t fat16_write_dir_entry(struct fat16_fs_struct* fs, const struct fat16_dir_entry_struct* dir_entry){#if FAT16_WRITE_SUPPORT if(!fs || !dir_entry) return 0; device_write_t device_write = fs->partition->device_write; uint32_t offset = dir_entry->entry_offset; uint8_t name_len = strlen(dir_entry->long_name); uint8_t lfn_entry_count = (name_len + 12) / 13; uint8_t buffer[32]; /* write 8.3 entry */ /* generate 8.3 file name */ memset(&buffer[0], ' ', 11); char* name_ext = strrchr(dir_entry->long_name, '.'); if(name_ext) { ++name_ext; uint8_t name_ext_len = strlen(name_ext); name_len -= name_ext_len + 1; if(name_ext_len > 3) name_ext_len = 3; memcpy(&buffer[8], name_ext, name_ext_len); } if(name_len <= 8) { memcpy(buffer, dir_entry->long_name, name_len); } else { memcpy(buffer, dir_entry->long_name, 8); /* Minimize 8.3 name clashes by appending * the lower byte of the cluster number. */ uint8_t num = dir_entry->cluster & 0xff; buffer[6] = (num < 0xa0) ? ('0' + (num >> 4)) : ('a' + (num >> 4)); num &= 0x0f; buffer[7] = (num < 0x0a) ? ('0' + num) : ('a' + num); } /* fill directory entry buffer */ memset(&buffer[11], 0, sizeof(buffer) - 11); buffer[0x0b] = dir_entry->attributes; buffer[0x1a] = (dir_entry->cluster >> 0) & 0xff; buffer[0x1b] = (dir_entry->cluster >> 8) & 0xff; buffer[0x1c] = (dir_entry->file_size >> 0) & 0xff; buffer[0x1d] = (dir_entry->file_size >> 8) & 0xff; buffer[0x1e] = (dir_entry->file_size >> 16) & 0xff; buffer[0x1f] = (dir_entry->file_size >> 24) & 0xff; /* write to disk */ if(!device_write(offset + (uint32_t) lfn_entry_count * 32, buffer, sizeof(buffer))) return 0; /* calculate checksum of 8.3 name */ uint8_t checksum = buffer[0]; for(uint8_t i = 1; i < 11; ++i) checksum = ((checksum >> 1) | (checksum << 7)) + buffer[i]; /* write lfn entries */ for(uint8_t lfn_entry = lfn_entry_count; lfn_entry > 0; --lfn_entry) { memset(buffer, 0, sizeof(buffer)); memset(&buffer[0x01], 0xff, 10); memset(&buffer[0x0e], 0xff, 12); memset(&buffer[0x1c], 0xff, 4); buffer[0x00] = lfn_entry; if(lfn_entry == lfn_entry_count) buffer[0x00] |= FAT16_DIRENTRY_LFNLAST; /* set file name */ const char* long_name_curr = dir_entry->long_name + (lfn_entry - 1) * 13; uint8_t i = 1; while(i < 0x1f) { buffer[i++] = *long_name_curr; buffer[i++] = 0; switch(i) { case 0x0b: i = 0x0e; break; case 0x1a: i = 0x1c; break; } if(!*long_name_curr++) break; } /* mark as lfn entry */ buffer[0x0b] = 0x0f; /* set checksum */ buffer[0x0d] = checksum; /* write entry */ device_write(offset, buffer, sizeof(buffer)); offset += sizeof(buffer); } return 1;#else return 0;#endif}/** * \ingroup fat16_file * Creates a file. * * Creates a file and obtains the directory entry of the * new file. If the file to create already exists, the * directory entry of the existing file will be returned * within the dir_entry parameter. * * \note The file name is not checked for invalid characters. * * \note The generation of the short 8.3 file name is quite * simple. The first eight characters are used for the filename. * The extension, if any, is made up of the first three characters * following the last dot within the long filename. If the * filename (without the extension) is longer than eight characters, * the lower byte of the cluster number replaces the last two * characters to avoid name clashes. In any other case, it is your * responsibility to avoid name clashes. * * \param[in] parent The handle of the directory in which to create the file. * \param[in] file The name of the file to create. * \param[out] dir_entry The directory entry to fill for the new file. * \returns 0 on failure, 1 on success. * \see fat16_delete_file */uint8_t fat16_create_file(struct fat16_dir_struct* parent, const char* file, struct fat16_dir_entry_struct* dir_entry){#if FAT16_WRITE_SUPPORT if(!parent || !file || !file[0]) return 0; /* check if the file already exists */ while(1) { if(!fat16_read_dir(parent, dir_entry)) break; if(strcmp(file, dir_entry->long_name) == 0) { fat16_reset_dir(parent); return 1; } } memset(dir_entry, 0, sizeof(*dir_entry)); strncpy(dir_entry->long_name, file, sizeof(dir_entry->long_name)); dir_entry->long_name[sizeof(dir_entry->long_name)] = '\0'; /* search for a place where to write the directory entry to disk */ uint8_t free_dir_entries_needed = strlen(file) / 13 + 1 + 1; uint8_t free_dir_entries_found = 0; struct fat16_fs_struct* fs = parent->fs; uint16_t cluster_num = parent->dir_entry.cluster; uint32_t dir_entry_offset = 0; uint32_t offset = 0; uint32_t offset_to = 0; if(cluster_num == 0) { /* we read/write from the root directory entry */ offset = fs->header.root_dir_offset; offset_to = fs->header.cluster_zero_offset; dir_entry_offset = offset; } while(1) { if(offset == offset_to) { if(cluster_num == 0) /* We iterated through the whole root directory entry * and could not find enough space for the directory entry. */ return 0; if(offset) { /* We reached a cluster boundary and have to * switch to the next cluster. */ uint16_t cluster_next = fat16_get_next_cluster(fs, cluster_num); if(!cluster_next) { cluster_next = fat16_append_cluster(fs, cluster_num); if(!cluster_next) return 0; /* we appended a new cluster and know it is free */ dir_entry_offset = fs->header.cluster_zero_offset + (uint32_t) (cluster_next - 2) * fs->header.cluster_size; break; } cluster_num = cluster_next; } offset = fs->header.cluster_zero_offset + (uint32_t) (cluster_num - 2) * fs->header.cluster_size; offset_to = offset + fs->header.cluster_size; dir_entry_offset = offset; free_dir_entries_found = 0; } /* read next lfn or 8.3 entry */ uint8_t first_char; if(!fs->partition->device_read(offset, &first_char, sizeof(first_char))) return 0; /* check if we found a free directory entry */ if(first_char == FAT16_DIRENTRY_DELETED || !first_char) { if(!first_char) { /* Mark the directory entries as deleted. * This is needed if our new directory entry is * too large to fit into the current cluster. */ first_char = FAT16_DIRENTRY_DELETED; if(!fs->partition->device_write(offset, &first_char, sizeof(first_char))) return 0; } /* check if we have the needed number of available entries */ ++free_dir_entries_found; if(free_dir_entries_found >= free_dir_entries_needed) break; offset += 32; } else { offset += 32; dir_entry_offset = offset; free_dir_entries_found = 0; } } /* write directory entry to disk */ dir_entry->entry_offset = dir_entry_offset; if(!fat16_write_dir_entry(fs, dir_entry)) return 0; return 1; #else return 0;#endif}/** * \ingroup fat16_file * Deletes a file or directory. * * It is not checked if the file to delete is a directory. * If a directory is deleted without first deleting its * subdirectories and files, disk space occupied by these * files will get wasted as there is no chance to release * it and mark it as free. * * \param[in] fs The filesystem on which to operate. * \param[in] dir_entry The directory entry of the file to delete. * \returns 0 on failure, 1 on success. * \see fat16_create_file */uint8_t fat16_delete_file(struct fat16_fs_struct* fs, struct fat16_dir_entry_struct* dir_entry){#if FAT16_WRITE_SUPPORT if(!fs || !dir_entry) return 0; /* get offset of the file's directory entry */ uint32_t dir_entry_offset = dir_entry->entry_offset; if(!dir_entry_offset) return 0; uint8_t buffer[12]; while(1) { /* read directory entry */ if(!fs->partition->device_read(dir_entry_offset, buffer, sizeof(buffer))) return 0; /* mark the directory entry as deleted */ buffer[0] = FAT16_DIRENTRY_DELETED; /* write back entry */ if(!fs->partition->device_write(dir_entry_offset, buffer, sizeof(buffer))) return 0; /* check if we deleted the whole entry */ if(buffer[11] != 0x0f) break; dir_entry_offset += 32; } /* We deleted the directory entry. The next thing to do is * marking all occupied clusters as free. */ uint16_t cluster_num = dir_entry->cluster; uint16_t cluster_num_next; while(cluster_num) { /* get next cluster before freeing the previous one */ cluster_num_next = fat16_get_next_cluster(fs, cluster_num); /* free cluster */ fat16_free_cluster(fs, cluster_num); cluster_num = cluster_num_next; } return 1;#else return 0;#endif}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -