Skip to content
This repository was archived by the owner on Nov 11, 2024. It is now read-only.

Commit 42fe5af

Browse files
committed
update FAT32 FSINFO for statvfs if invalid
1 parent bc9f4c8 commit 42fe5af

2 files changed

Lines changed: 17 additions & 13 deletions

File tree

source/fatdir.c

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -465,16 +465,13 @@ int _FAT_statvfs_r (struct _reent *r, const char *path, struct statvfs *buf)
465465

466466
_FAT_lock(&partition->lock);
467467

468-
if(memcmp(&buf->f_flag, "SCAN", 4) == 0)
469-
{
470-
//Special command was given to sync the numberFreeCluster
471-
_FAT_partition_createFSinfo(partition);
472-
}
473-
474-
if(partition->filesysType == FS_FAT32)
468+
if(partition->filesysType == FS_FAT32) {
469+
// Sync FSinfo block
470+
_FAT_partition_readFSinfo(partition);
475471
freeClusterCount = partition->fat.numberFreeCluster;
476-
else
472+
} else {
477473
freeClusterCount = _FAT_fat_freeClusterCount (partition);
474+
}
478475

479476
// FAT clusters = POSIX blocks
480477
buf->f_bsize = partition->bytesPerCluster; // File system block size.

source/partition.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,13 @@ PARTITION* _FAT_partition_getPartitionFromPath (const char* path) {
348348
return (PARTITION*)devops->deviceData;
349349
}
350350

351+
static void _FAT_updateFS_INFO(PARTITION * partition, uint8_t *sectorBuffer) {
352+
partition->fat.numberFreeCluster = _FAT_fat_freeClusterCount(partition);
353+
u32_to_u8array(sectorBuffer, FSIB_numberOfFreeCluster, partition->fat.numberFreeCluster);
354+
u32_to_u8array(sectorBuffer, FSIB_numberLastAllocCluster, partition->fat.numberLastAllocCluster);
355+
_FAT_disc_writeSectors (partition->disc, partition->fsInfoSector, 1, sectorBuffer);
356+
}
357+
351358
void _FAT_partition_createFSinfo(PARTITION * partition)
352359
{
353360
if(partition->readOnly || partition->filesysType != FS_FAT32)
@@ -364,14 +371,10 @@ void _FAT_partition_createFSinfo(PARTITION * partition)
364371
sectorBuffer[FSIB_SIG2+i] = FS_INFO_SIG2[i];
365372
}
366373

367-
partition->fat.numberFreeCluster = _FAT_fat_freeClusterCount(partition);
368-
u32_to_u8array(sectorBuffer, FSIB_numberOfFreeCluster, partition->fat.numberFreeCluster);
369-
u32_to_u8array(sectorBuffer, FSIB_numberLastAllocCluster, partition->fat.numberLastAllocCluster);
370-
371374
sectorBuffer[FSIB_bootSig_55] = 0x55;
372375
sectorBuffer[FSIB_bootSig_AA] = 0xAA;
373376

374-
_FAT_disc_writeSectors (partition->disc, partition->fsInfoSector, 1, sectorBuffer);
377+
_FAT_updateFS_INFO(partition,sectorBuffer);
375378

376379
_FAT_mem_free(sectorBuffer);
377380
}
@@ -398,6 +401,10 @@ void _FAT_partition_readFSinfo(PARTITION * partition)
398401
_FAT_partition_createFSinfo(partition);
399402
} else {
400403
partition->fat.numberFreeCluster = u8array_to_u32(sectorBuffer, FSIB_numberOfFreeCluster);
404+
if(partition->fat.numberFreeCluster == 0xffffffff) {
405+
_FAT_updateFS_INFO(partition,sectorBuffer);
406+
partition->fat.numberFreeCluster = u8array_to_u32(sectorBuffer, FSIB_numberOfFreeCluster);
407+
}
401408
partition->fat.numberLastAllocCluster = u8array_to_u32(sectorBuffer, FSIB_numberLastAllocCluster);
402409
}
403410
_FAT_mem_free(sectorBuffer);

0 commit comments

Comments
 (0)