void fatfs_sfn_create_entry(char *shortfilename, uint32 size, uint32 startCluster, struct fat_dir_entry *entry, int dir) { int i; // Copy short filename for (i=0;i<FAT_SFN_SIZE_FULL;i++) entry->Name[i] = shortfilename[i]; // Unless we have a RTC we might as well set these to 1980 entry->CrtTimeTenth = 0x00; entry->CrtTime[1] = entry->CrtTime[0] = 0x00; entry->CrtDate[1] = 0x00; entry->CrtDate[0] = 0x20; entry->LstAccDate[1] = 0x00; entry->LstAccDate[0] = 0x20; entry->WrtTime[1] = entry->WrtTime[0] = 0x00; entry->WrtDate[1] = 0x00; entry->WrtDate[0] = 0x20; if (!dir) entry->Attr = FILE_TYPE_FILE; else entry->Attr = FILE_TYPE_DIR; entry->NTRes = 0x00; entry->FstClusHI = FAT_HTONS((uint16)((startCluster>>16) & 0xFFFF)); entry->FstClusLO = FAT_HTONS((uint16)((startCluster>>0) & 0xFFFF)); entry->FileSize = FAT_HTONL(size); }
int fatfs_update_file_length(struct fatfs *fs, uint32 Cluster, char *shortname, uint32 fileLength) { uint8 item=0; uint16 recordoffset = 0; int x=0; struct fat_dir_entry *directoryEntry; // No write access? if (!fs->disk_io.write_media) return 0; // Main cluster following loop while (1) { // Read sector if (fatfs_sector_reader(fs, Cluster, x++, 0)) // If sector read was successfull { // Analyse Sector for (item = 0; item < FAT_DIR_ENTRIES_PER_SECTOR; item++) { // Create the multiplier for sector access recordoffset = FAT_DIR_ENTRY_SIZE * item; // Overlay directory entry over buffer directoryEntry = (struct fat_dir_entry*)(fs->currentsector.sector+recordoffset); #if FATFS_INC_LFN_SUPPORT // Long File Name Text Found if (fatfs_entry_lfn_text(directoryEntry) ) ; // If Invalid record found delete any long file name information collated else if (fatfs_entry_lfn_invalid(directoryEntry) ) ; // Normal Entry, only 8.3 Text else #endif if (fatfs_entry_sfn_only(directoryEntry) ) { if (strncmp((const char*)directoryEntry->Name, shortname, 11)==0) { directoryEntry->FileSize = FAT_HTONL(fileLength); // TODO: Update last write time // Update sfn entry memcpy((uint8*)(fs->currentsector.sector+recordoffset), (uint8*)directoryEntry, sizeof(struct fat_dir_entry)); // Write sector back return fs->disk_io.write_media(fs->currentsector.address, fs->currentsector.sector, 1); } } } // End of if } else break; } // End of while loop return 0; }
int fatfs_list_directory_next(struct fatfs *fs, struct fs_dir_list_status *dirls, struct fs_dir_ent *entry) { uint8 i,item; uint16 recordoffset; struct fat_dir_entry *directoryEntry; char *long_filename = NULL; char short_filename[13]; struct lfn_cache lfn; int dotRequired = 0; int result = 0; // Initialise LFN cache first fatfs_lfn_cache_init(&lfn, 0); while (1) { // If data read OK if (fatfs_sector_reader(fs, dirls->cluster, dirls->sector, 0)) { // Maximum of 16 directory entries for (item = dirls->offset; item < FAT_DIR_ENTRIES_PER_SECTOR; item++) { // Increase directory offset recordoffset = FAT_DIR_ENTRY_SIZE * item; // Overlay directory entry over buffer directoryEntry = (struct fat_dir_entry*)(fs->currentsector.sector+recordoffset); #if FATFS_INC_LFN_SUPPORT // Long File Name Text Found if ( fatfs_entry_lfn_text(directoryEntry) ) fatfs_lfn_cache_entry(&lfn, fs->currentsector.sector+recordoffset); // If Invalid record found delete any long file name information collated else if ( fatfs_entry_lfn_invalid(directoryEntry) ) fatfs_lfn_cache_init(&lfn, 0); // Normal SFN Entry and Long text exists else if (fatfs_entry_lfn_exists(&lfn, directoryEntry) ) { // Get text long_filename = fatfs_lfn_cache_get(&lfn); strncpy(entry->filename, long_filename, FATFS_MAX_LONG_FILENAME-1); if (fatfs_entry_is_dir(directoryEntry)) entry->is_dir = 1; else entry->is_dir = 0; #if FATFS_INC_TIME_DATE_SUPPORT // Get time / dates entry->create_time = ((uint16)directoryEntry->CrtTime[1] << 8) | directoryEntry->CrtTime[0]; entry->create_date = ((uint16)directoryEntry->CrtDate[1] << 8) | directoryEntry->CrtDate[0]; entry->access_date = ((uint16)directoryEntry->LstAccDate[1] << 8) | directoryEntry->LstAccDate[0]; entry->write_time = ((uint16)directoryEntry->WrtTime[1] << 8) | directoryEntry->WrtTime[0]; entry->write_date = ((uint16)directoryEntry->WrtDate[1] << 8) | directoryEntry->WrtDate[0]; #endif entry->size = FAT_HTONL(directoryEntry->FileSize); entry->cluster = (FAT_HTONS(directoryEntry->FstClusHI)<<16) | FAT_HTONS(directoryEntry->FstClusLO); // Next starting position dirls->offset = item + 1; result = 1; return 1; } // Normal Entry, only 8.3 Text else #endif if ( fatfs_entry_sfn_only(directoryEntry) ) { fatfs_lfn_cache_init(&lfn, 0); memset(short_filename, 0, sizeof(short_filename)); // Copy name to string for (i=0; i<8; i++) short_filename[i] = directoryEntry->Name[i]; // Extension dotRequired = 0; for (i=8; i<11; i++) { short_filename[i+1] = directoryEntry->Name[i]; if (directoryEntry->Name[i] != ' ') dotRequired = 1; } // Dot only required if extension present if (dotRequired) { // If not . or .. entry if (short_filename[0]!='.') short_filename[8] = '.'; else short_filename[8] = ' '; } else short_filename[8] = ' '; fatfs_get_sfn_display_name(entry->filename, short_filename); if (fatfs_entry_is_dir(directoryEntry)) entry->is_dir = 1; else entry->is_dir = 0; #if FATFS_INC_TIME_DATE_SUPPORT // Get time / dates entry->create_time = ((uint16)directoryEntry->CrtTime[1] << 8) | directoryEntry->CrtTime[0]; entry->create_date = ((uint16)directoryEntry->CrtDate[1] << 8) | directoryEntry->CrtDate[0]; entry->access_date = ((uint16)directoryEntry->LstAccDate[1] << 8) | directoryEntry->LstAccDate[0]; entry->write_time = ((uint16)directoryEntry->WrtTime[1] << 8) | directoryEntry->WrtTime[0]; entry->write_date = ((uint16)directoryEntry->WrtDate[1] << 8) | directoryEntry->WrtDate[0]; #endif entry->size = FAT_HTONL(directoryEntry->FileSize); entry->cluster = (FAT_HTONS(directoryEntry->FstClusHI)<<16) | FAT_HTONS(directoryEntry->FstClusLO); // Next starting position dirls->offset = item + 1; result = 1; return 1; } }// end of for // If reached end of the dir move onto next sector dirls->sector++; dirls->offset = 0; } else break; } return result; }