Skip to content

Commit

Permalink
update FAT32 FSINFO for statvfs if invalid
Browse files Browse the repository at this point in the history
  • Loading branch information
WinterMute committed Jul 31, 2017
1 parent bc9f4c8 commit 42fe5af
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
13 changes: 5 additions & 8 deletions source/fatdir.c
Original file line number Diff line number Diff line change
Expand Up @@ -465,16 +465,13 @@ int _FAT_statvfs_r (struct _reent *r, const char *path, struct statvfs *buf)

_FAT_lock(&partition->lock);

if(memcmp(&buf->f_flag, "SCAN", 4) == 0)
{
//Special command was given to sync the numberFreeCluster
_FAT_partition_createFSinfo(partition);
}

if(partition->filesysType == FS_FAT32)
if(partition->filesysType == FS_FAT32) {
// Sync FSinfo block
_FAT_partition_readFSinfo(partition);
freeClusterCount = partition->fat.numberFreeCluster;
else
} else {
freeClusterCount = _FAT_fat_freeClusterCount (partition);
}

// FAT clusters = POSIX blocks
buf->f_bsize = partition->bytesPerCluster; // File system block size.
Expand Down
17 changes: 12 additions & 5 deletions source/partition.c
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,13 @@ PARTITION* _FAT_partition_getPartitionFromPath (const char* path) {
return (PARTITION*)devops->deviceData;
}

static void _FAT_updateFS_INFO(PARTITION * partition, uint8_t *sectorBuffer) {
partition->fat.numberFreeCluster = _FAT_fat_freeClusterCount(partition);
u32_to_u8array(sectorBuffer, FSIB_numberOfFreeCluster, partition->fat.numberFreeCluster);
u32_to_u8array(sectorBuffer, FSIB_numberLastAllocCluster, partition->fat.numberLastAllocCluster);
_FAT_disc_writeSectors (partition->disc, partition->fsInfoSector, 1, sectorBuffer);
}

void _FAT_partition_createFSinfo(PARTITION * partition)
{
if(partition->readOnly || partition->filesysType != FS_FAT32)
Expand All @@ -364,14 +371,10 @@ void _FAT_partition_createFSinfo(PARTITION * partition)
sectorBuffer[FSIB_SIG2+i] = FS_INFO_SIG2[i];
}

partition->fat.numberFreeCluster = _FAT_fat_freeClusterCount(partition);
u32_to_u8array(sectorBuffer, FSIB_numberOfFreeCluster, partition->fat.numberFreeCluster);
u32_to_u8array(sectorBuffer, FSIB_numberLastAllocCluster, partition->fat.numberLastAllocCluster);

sectorBuffer[FSIB_bootSig_55] = 0x55;
sectorBuffer[FSIB_bootSig_AA] = 0xAA;

_FAT_disc_writeSectors (partition->disc, partition->fsInfoSector, 1, sectorBuffer);
_FAT_updateFS_INFO(partition,sectorBuffer);

_FAT_mem_free(sectorBuffer);
}
Expand All @@ -398,6 +401,10 @@ void _FAT_partition_readFSinfo(PARTITION * partition)
_FAT_partition_createFSinfo(partition);
} else {
partition->fat.numberFreeCluster = u8array_to_u32(sectorBuffer, FSIB_numberOfFreeCluster);
if(partition->fat.numberFreeCluster == 0xffffffff) {
_FAT_updateFS_INFO(partition,sectorBuffer);
partition->fat.numberFreeCluster = u8array_to_u32(sectorBuffer, FSIB_numberOfFreeCluster);
}
partition->fat.numberLastAllocCluster = u8array_to_u32(sectorBuffer, FSIB_numberLastAllocCluster);
}
_FAT_mem_free(sectorBuffer);
Expand Down

0 comments on commit 42fe5af

Please sign in to comment.