int fatfs_mark_file_deleted(struct fatfs *fs, uint32 Cluster, char *shortname) { 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) { // Mark as deleted directoryEntry->Name[0] = FILE_HEADER_DELETED; // 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_update_file_length(struct fatfs *fs, UINT32 Cluster, char *shortname, UINT32 fileLength) { unsigned char item=0; UINT16 recordoffset = 0; int x=0; struct fat_dir_entry *directoryEntry; // No write access? if (!fs->disk_io.write_sector) return 0; // Main cluster following loop while (TRUE) { // Read sector if (fatfs_sector_reader(fs, Cluster, x++, FALSE)) // 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 = fileLength; // TODO: Update last write time // Update sfn entry No need because th memcpy((unsigned char*)(fs->currentsector.sector+recordoffset), (unsigned char*)directoryEntry, sizeof(struct fat_dir_entry)); // Write sector back return fs->disk_io.write_sector(fs->currentsector.address, fs->currentsector.sector); } } } // End of if } else break; } // End of while loop return 0; }
int fatfs_sfn_exists(struct fatfs *fs, uint32 Cluster, char *shortname) { uint8 item=0; uint16 recordoffset = 0; int x=0; struct fat_dir_entry *directoryEntry; // 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) ) ; else #endif // Normal Entry, only 8.3 Text if (fatfs_entry_sfn_only(directoryEntry) ) { if (strncmp((const char*)directoryEntry->Name, shortname, 11)==0) return 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; }
//------------------------------------------------------------- // fatfs_get_file_entry: Find the file entry for a filename //------------------------------------------------------------- uint32 fatfs_get_file_entry(struct fatfs *fs, uint32 Cluster, char *name_to_find, struct fat_dir_entry *sfEntry) { uint8 item=0; uint16 recordoffset = 0; uint8 i=0; int x=0; char *long_filename = NULL; char short_filename[13]; struct lfn_cache lfn; int dotRequired = 0; struct fat_dir_entry *directoryEntry; fatfs_lfn_cache_init(&lfn, 1); // 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) ) 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) ) { long_filename = fatfs_lfn_cache_get(&lfn); // Compare names to see if they match if (fatfs_compare_names(long_filename, name_to_find)) { memcpy(sfEntry,directoryEntry,sizeof(struct fat_dir_entry)); return 1; } fatfs_lfn_cache_init(&lfn, 0); } else #endif // Normal Entry, only 8.3 Text if (fatfs_entry_sfn_only(directoryEntry) ) { 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] = ' '; // Compare names to see if they match if (fatfs_compare_names(short_filename, name_to_find)) { memcpy(sfEntry,directoryEntry,sizeof(struct fat_dir_entry)); return 1; } fatfs_lfn_cache_init(&lfn, 0); } } // End of if } else break; } // End of while loop return 0; }