📄 fat16.c
字号:
if(!cluster_num) { cluster_num = cluster_new_chain; fd->dir_entry.cluster = cluster_num; } } /* write new directory entry */ fd->dir_entry.file_size = size; if(size == 0) fd->dir_entry.cluster = 0; if(!fat16_write_dir_entry(fd->fs, &fd->dir_entry)) return 0; if(size == 0) { /* free all clusters of file */ fat16_free_clusters(fd->fs, cluster_num); } else if(size_new <= cluster_size) { /* free all clusters no longer needed */ fat16_terminate_clusters(fd->fs, cluster_num); } } while(0); /* correct file position */ if(size < fd->pos) { fd->pos = size; fd->pos_cluster = 0; } return 1;#else return 0;#endif}/** * \ingroup fat16_dir * Opens a directory. * * \param[in] fs The filesystem on which the directory to open resides. * \param[in] dir_entry The directory entry which stands for the directory to open. * \returns An opaque directory descriptor on success, 0 on failure. * \see fat16_close_dir */struct fat16_dir_struct* fat16_open_dir(struct fat16_fs_struct* fs, const struct fat16_dir_entry_struct* dir_entry){
struct fat16_dir_struct* dd; uint8_t i;
if(!fs || !dir_entry || !(dir_entry->attributes & FAT16_ATTRIB_DIR)) return 0;#if USE_DYNAMIC_MEMORY dd = malloc(sizeof(*dd)); i=0;
if(!dd) return 0;#else dd = fat16_dir_handlers; for(i = 0; i < FAT16_DIR_COUNT; ++i) { if(!dd->fs) break; ++dd; } if(i >= FAT16_DIR_COUNT) return 0;#endif memcpy(&dd->dir_entry, dir_entry, sizeof(*dir_entry)); dd->fs = fs; dd->entry_next = 0; return dd;}/** * \ingroup fat16_dir * Closes a directory descriptor. * * This function destroys a directory descriptor which was * previously obtained by calling fat16_open_dir(). When this * function returns, the given descriptor will be invalid. * * \param[in] dd The directory descriptor to close. * \see fat16_open_dir */void fat16_close_dir(struct fat16_dir_struct* dd){ if(dd)#if USE_DYNAMIC_MEMORY free(dd);#else dd->fs = 0;#endif}/** * \ingroup fat16_dir * Reads the next directory entry contained within a parent directory. * * \param[in] dd The descriptor of the parent directory from which to read the entry. * \param[out] dir_entry Pointer to a buffer into which to write the directory entry information. * \returns 0 on failure, 1 on success. * \see fat16_reset_dir */uint8_t fat16_read_dir(struct fat16_dir_struct* dd, struct fat16_dir_entry_struct* dir_entry){ if(!dd || !dir_entry) return 0; if(dd->dir_entry.cluster == 0) { /* read entry from root directory */ if(fat16_read_root_dir_entry(dd->fs, dd->entry_next, dir_entry)) { ++dd->entry_next; return 1; } } 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 * Searches for space where to store a directory entry. * * \param[in] fs The filesystem on which to operate. * \param[in] dir_entry The directory entry for which to search space. * \returns 0 on failure, a device offset on success. */uint32_t fat16_find_offset_for_dir_entry(const struct fat16_fs_struct* fs, const struct fat16_dir_struct* parent, const struct fat16_dir_entry_struct* dir_entry){#if FAT16_WRITE_SUPPORT
uint8_t free_dir_entries_needed; uint8_t free_dir_entries_found; uint16_t cluster_num; uint32_t dir_entry_offset; uint32_t offset; uint32_t offset_to;
uint16_t cluster_next;
uint8_t first_char;
if(!fs || !dir_entry) return 0; /* search for a place where to write the directory entry to disk */ free_dir_entries_needed = (U8)((strlen(dir_entry->long_name) + 12) / 13 + 1); free_dir_entries_found = 0; cluster_num = parent->dir_entry.cluster; dir_entry_offset = 0; offset = 0; 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. */ cluster_next = fat16_get_next_cluster(fs, cluster_num); if(!cluster_next) { cluster_next = fat16_append_clusters(fs, cluster_num, 1); 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; /* clear cluster to avoid garbage directory entries */ fat16_clear_cluster(fs, cluster_next); 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 */ 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) { /* 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; } } return dir_entry_offset;#else return 0;#endif}/** * \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(const struct fat16_fs_struct* fs, struct fat16_dir_entry_struct* dir_entry){#if FAT16_WRITE_SUPPORT
device_write_t device_write; uint32_t offset; char* name; uint8_t name_len; uint8_t lfn_entry_count; uint8_t buffer[32];
char* name_ext;
uint8_t num;
uint8_t checksum;
uint8_t i;
uint8_t lfn_entry;
char* long_name_curr;
if(!fs || !dir_entry) return 0;
#if FAT16_DATETIME_SUPPORT { uint16_t year; uint8_t month; uint8_t day; uint8_t hour; uint8_t min; uint8_t sec; fat16_get_datetime(&year, &month, &day, &hour, &min, &sec); fat16_set_file_modification_date(dir_entry, year, month, day); fat16_set_file_modification_time(dir_entry, hour, min, sec); }#endif device_write = fs->partition->device_write; offset = dir_entry->entry_offset; name = dir_entry->long_name; name_len = (U8) strlen(name); lfn_entry_count = (name_len + 12) / 13; /* write 8.3 entry */ /* generate 8.3 file name */ memset(&buffer[0], ' ', 11); name_ext = strrchr(name, '.'); if(name_ext && *++name_ext) { uint8_t name_ext_len = (U8) 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, name, name_len); /* For now, we create lfn entries for all files, * except the "." and ".." directory references. * This is to avoid difficulties with capitalization, * as 8.3 filenames allow uppercase letters only. * * Theoretically it would be possible to leave * the 8.3 entry alone if the basename and the * extension have no mixed capitalization. */ if(name[0] == '.' && ((name[1] == '.' && name[2] == '\0') || name[1] == '\0') ) lfn_entry_count = 0; } else { memcpy(buffer, name, 8); /* Minimize 8.3 name clashes by appending * the lower byte of the cluster number. */ 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); } if(buffer[0] == FAT16_DIRENTRY_DELETED) buffer[0] = 0x05; /* fill directory entry buffer */ memset(&buffer[11], 0, sizeof(buffer) - 11); buffer[0x0b] = dir_entry->attributes;#if FAT16_DATETIME_SUPPORT buffer[0x16] = (U8) (dir_entry->modification_time >> 0);// & 0xff); buffer[0x17] = (U8) (dir_entry->modification_time >> 8);// & 0xff; buffer[0x18] = (U8) (dir_entry->modification_date >> 0);// & 0xff; buffer[0x19] = (U8) (dir_entry->modification_date >> 8);// & 0xff;#endif buffer[0x1a] = (U8) (dir_entry->cluster >> 0);// & 0xff; buffer[0x1b] = (U8) (dir_entry->cluster >> 8);// & 0xff; buffer[0x1c] = (U8) (dir_entry->file_size >> 0);// & 0xff; buffer[0x1d] = (U8) (dir_entry->file_size >> 8);// & 0xff; buffer[0x1e] = (U8) (dir_entry->file_size >> 16);// & 0xff; buffer[0x1f] = (U8) (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 */ checksum = buffer[0]; for(i = 1; i < 11; ++i) checksum = ((checksum >> 1) | (checksum << 7)) + buffer[i]; /* write lfn entries */ for(lfn_entry = lfn_entry_count; lfn_entry > 0; --lfn_entry) { memset(buffer, 0xff, sizeof(buffer)); /* set file name */ long_name_curr = name + (lfn_entry - 1) * 13; 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; } /* set index of lfn entry */ buffer[0x00] = lfn_entry; if(lfn_entry == lfn_entry_count) buffer[0x00] |= FAT16_DIRENTRY_LFNLAST; /* mark as lfn entry */ buffer[0x0b] = 0x0f;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -