From 42fe5af38d0d48a1a3a874c96ee89ecb62f7a4a1 Mon Sep 17 00:00:00 2001 From: Dave Murphy Date: Mon, 31 Jul 2017 14:45:57 +0100 Subject: [PATCH] update FAT32 FSINFO for statvfs if invalid --- source/fatdir.c | 13 +++++-------- source/partition.c | 17 ++++++++++++----- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/source/fatdir.c b/source/fatdir.c index 4c3aaca..1ef5aec 100644 --- a/source/fatdir.c +++ b/source/fatdir.c @@ -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. diff --git a/source/partition.c b/source/partition.c index 108776a..1584e0e 100644 --- a/source/partition.c +++ b/source/partition.c @@ -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) @@ -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); } @@ -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);