fat writing optimization

This commit is contained in:
koolkdev 2017-03-26 22:03:01 +03:00
parent 1258dbb2fe
commit 7396fe9009
4 changed files with 6 additions and 3 deletions

View File

@ -131,6 +131,7 @@ int fatfs_init(struct fatfs *fs)
// For FAT32 (which this may be)
fs->rootdir_first_cluster = GET_32BIT_WORD(fs->currentsector.sector, BPB_FAT32_ROOTCLUS);
fs->last_free_cluster = fs->rootdir_first_cluster + 1;
fs->fs_info_sector = GET_16BIT_WORD(fs->currentsector.sector, BPB_FAT32_FSINFO);
// For FAT16 (which this may be), rootdir_first_cluster is actuall rootdir_first_sector

View File

@ -60,6 +60,7 @@ struct fatfs
uint8 sectors_per_cluster;
uint32 cluster_begin_lba;
uint32 rootdir_first_cluster;
uint32 last_free_cluster;
uint32 rootdir_first_sector;
uint32 rootdir_sectors;
uint32 fat_begin_lba;

View File

@ -337,6 +337,7 @@ int fatfs_find_blank_cluster(struct fatfs *fs, uint32 start_cluster, uint32 *fre
while (nextcluster != 0x0);
// Found blank entry
fs->last_free_cluster = current_cluster + 1;
*free_cluster = current_cluster;
return 1;
}

View File

@ -55,7 +55,7 @@ int fatfs_add_free_space(struct fatfs *fs, uint32 *startCluster, uint32 clusters
for (i=0;i<clusters;i++)
{
// Start looking for free clusters from the beginning
if (fatfs_find_blank_cluster(fs, fs->rootdir_first_cluster, &nextcluster))
if (fatfs_find_blank_cluster(fs, fs->last_free_cluster, &nextcluster))
{
// Point last to this
fatfs_fat_set_cluster(fs, start, nextcluster);
@ -103,7 +103,7 @@ int fatfs_allocate_free_space(struct fatfs *fs, int newFile, uint32 *startCluste
// Allocated first link in the chain if a new file
if (newFile)
{
if (!fatfs_find_blank_cluster(fs, fs->rootdir_first_cluster, &nextcluster))
if (!fatfs_find_blank_cluster(fs, fs->last_free_cluster, &nextcluster))
return 0;
// If this is all that is needed then all done
@ -230,7 +230,7 @@ static int fatfs_find_free_dir_offset(struct fatfs *fs, uint32 dirCluster, int e
uint32 newCluster;
// Get a new cluster for directory
if (!fatfs_find_blank_cluster(fs, fs->rootdir_first_cluster, &newCluster))
if (!fatfs_find_blank_cluster(fs, fs->last_free_cluster, &newCluster))
return 0;
// Add cluster to end of directory tree