mirror of
https://github.com/wiiu-env/libfat.git
synced 2024-11-01 00:05:06 +01:00
support to read/write FS info sector for FAT32 partitions. speeds up statvfs. patch submitted by dimok
This commit is contained in:
parent
875d47b8f3
commit
93ec6aad6a
@ -1 +1 @@
|
||||
<Project name="libfat"><MagicFolder excludeFolders="CVS;.svn" filter="*.c;*.h" name="source" path="source\"><MagicFolder excludeFolders="CVS;.svn" filter="*.c;*.h" name="disc_io" path="disc_io\"><File path="disc.c"></File><File path="io_cf_common.c"></File><File path="io_cf_common.h"></File><File path="io_dldi.h"></File><File path="io_efa2.c"></File><File path="io_efa2.h"></File><File path="io_fcsr.c"></File><File path="io_fcsr.h"></File><File path="io_m3cf.c"></File><File path="io_m3cf.h"></File><File path="io_m3sd.c"></File><File path="io_m3sd.h"></File><File path="io_m3_common.c"></File><File path="io_m3_common.h"></File><File path="io_mpcf.c"></File><File path="io_mpcf.h"></File><File path="io_njsd.c"></File><File path="io_njsd.h"></File><File path="io_nmmc.c"></File><File path="io_nmmc.h"></File><File path="io_sccf.c"></File><File path="io_sccf.h"></File><File path="io_scsd.c"></File><File path="io_scsd.h"></File><File path="io_sc_common.c"></File><File path="io_sc_common.h"></File><File path="io_sd_common.c"></File><File path="io_sd_common.h"></File></MagicFolder><MagicFolder excludeFolders="CVS;.svn" filter="*.c;*.h" name="ogc_io" path="ogc_io\"><File path="disc.c"></File><File path="gcsd.c"></File><File path="gcsd.h"></File><File path="wiisd.c"></File><File path="wiisd.h"></File></MagicFolder><File path="bit_ops.h"></File><File path="cache.c"></File><File path="cache.h"></File><File path="common.h"></File><File path="directory.c"></File><File path="directory.h"></File><File path="disc.h"></File><File path="fatdir.c"></File><File path="fatdir.h"></File><File path="fatfile.c"></File><File path="fatfile.h"></File><File path="filetime.c"></File><File path="filetime.h"></File><File path="file_allocation_table.c"></File><File path="file_allocation_table.h"></File><File path="libfat.c"></File><File path="mem_allocate.h"></File><File path="partition.c"></File><File path="partition.h"></File></MagicFolder><Folder name="nds"><Folder name="include"><File path="nds\include\fat.h"></File><File path="nds\include\nitrofs.h"></File></Folder><Folder name="source"><File path="nds\source\nitrofs.c"></File></Folder><File path="nds\Makefile"></File></Folder><Folder name="libogc"><Folder name="include"><File path="libogc\include\fat.h"></File></Folder><File path="libogc\Makefile"></File></Folder><Folder name="gba"><Folder name="include"><File path="gba\include\fat.h"></File></Folder><File path="gba\Makefile"></File></Folder><MagicFolder excludeFolders="CVS;.svn" filter="*.h" name="include" path="include\"><File path="fat.h"></File></MagicFolder><File path="Makefile"></File></Project>
|
||||
<Project name="libfat"><MagicFolder excludeFolders="CVS;.svn" filter="*.c;*.h" name="source" path="source\"><File path="bit_ops.h"></File><File path="cache.c"></File><File path="cache.h"></File><File path="common.h"></File><File path="directory.c"></File><File path="directory.h"></File><File path="disc.c"></File><File path="disc.h"></File><File path="fatdir.c"></File><File path="fatdir.h"></File><File path="fatfile.c"></File><File path="fatfile.h"></File><File path="filetime.c"></File><File path="filetime.h"></File><File path="file_allocation_table.c"></File><File path="file_allocation_table.h"></File><File path="libfat.c"></File><File path="lock.c"></File><File path="lock.h"></File><File path="mem_allocate.h"></File><File path="partition.c"></File><File path="partition.h"></File></MagicFolder><Folder name="nds"><Folder name="include"><File path="nds\include\fat.h"></File><File path="nds\include\nitrofs.h"></File></Folder><Folder name="source"><File path="nds\source\nitrofs.c"></File></Folder><File path="nds\Makefile"></File></Folder><Folder name="libogc"><Folder name="include"><File path="libogc\include\fat.h"></File></Folder><File path="libogc\Makefile"></File></Folder><Folder name="gba"><Folder name="include"><File path="gba\include\fat.h"></File></Folder><File path="gba\Makefile"></File></Folder><MagicFolder excludeFolders="CVS;.svn" filter="*.h" name="include" path="include\"><File path="fat.h"></File><File path="libfatversion.h"></File></MagicFolder><File path="Makefile"></File></Project>
|
@ -465,7 +465,16 @@ int _FAT_statvfs_r (struct _reent *r, const char *path, struct statvfs *buf)
|
||||
|
||||
_FAT_lock(&partition->lock);
|
||||
|
||||
freeClusterCount = _FAT_fat_freeClusterCount (partition);
|
||||
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)
|
||||
freeClusterCount = partition->fat.numberFreeCluster;
|
||||
else
|
||||
freeClusterCount = _FAT_fat_freeClusterCount (partition);
|
||||
|
||||
// FAT clusters = POSIX blocks
|
||||
buf->f_bsize = partition->bytesPerCluster; // File system block size.
|
||||
|
@ -245,6 +245,9 @@ uint32_t _FAT_fat_linkFreeCluster(PARTITION* partition, uint32_t cluster) {
|
||||
}
|
||||
}
|
||||
partition->fat.firstFree = firstFree;
|
||||
if(partition->fat.numberFreeCluster)
|
||||
partition->fat.numberFreeCluster--;
|
||||
partition->fat.numberLastAllocCluster = firstFree;
|
||||
|
||||
if ((cluster >= CLUSTER_FIRST) && (cluster <= lastCluster))
|
||||
{
|
||||
@ -309,6 +312,8 @@ bool _FAT_fat_clearLinks (PARTITION* partition, uint32_t cluster) {
|
||||
// Erase the link
|
||||
_FAT_fat_writeFatEntry (partition, cluster, CLUSTER_FREE);
|
||||
|
||||
if(partition->fat.numberFreeCluster < (partition->numberOfSectors/partition->sectorsPerCluster))
|
||||
partition->fat.numberFreeCluster++;
|
||||
// Move onto next cluster
|
||||
cluster = nextCluster;
|
||||
}
|
||||
|
@ -93,7 +93,20 @@ enum BPB {
|
||||
BPB_bootSig_AA = 0x1FF
|
||||
};
|
||||
|
||||
// File system information block offsets
|
||||
enum FSIB
|
||||
{
|
||||
FSIB_SIG1 = 0x00,
|
||||
FSIB_SIG2 = 0x1e4,
|
||||
FSIB_numberOfFreeCluster = 0x1e8,
|
||||
FSIB_numberLastAllocCluster = 0x1ec,
|
||||
FSIB_bootSig_55 = 0x1FE,
|
||||
FSIB_bootSig_AA = 0x1FF
|
||||
};
|
||||
|
||||
static const char FAT_SIG[3] = {'F', 'A', 'T'};
|
||||
static const char FS_INFO_SIG1[4] = {'R', 'R', 'a', 'A'};
|
||||
static const char FS_INFO_SIG2[4] = {'r', 'r', 'A', 'a'};
|
||||
|
||||
|
||||
sec_t FindFirstValidPartition(const DISC_INTERFACE* disc)
|
||||
@ -230,10 +243,15 @@ PARTITION* _FAT_partition_constructor (const DISC_INTERFACE* disc, uint32_t cach
|
||||
|
||||
partition->totalSize = ((uint64_t)partition->numberOfSectors - (partition->dataStart - startSector)) * (uint64_t)partition->bytesPerSector;
|
||||
|
||||
//FS info sector
|
||||
partition->fsInfoSector = startSector + (u8array_to_u16(sectorBuffer, BPB_FAT32_fsInfo) ? u8array_to_u16(sectorBuffer, BPB_FAT32_fsInfo) : 1);
|
||||
|
||||
// Store info about FAT
|
||||
uint32_t clusterCount = (partition->numberOfSectors - (uint32_t)(partition->dataStart - startSector)) / partition->sectorsPerCluster;
|
||||
partition->fat.lastCluster = clusterCount + CLUSTER_FIRST - 1;
|
||||
partition->fat.firstFree = CLUSTER_FIRST;
|
||||
partition->fat.numberFreeCluster = 0;
|
||||
partition->fat.numberLastAllocCluster = 0;
|
||||
|
||||
if (clusterCount < CLUSTERS_PER_FAT12) {
|
||||
partition->filesysType = FS_FAT12; // FAT12 volume
|
||||
@ -268,6 +286,8 @@ PARTITION* _FAT_partition_constructor (const DISC_INTERFACE* disc, uint32_t cach
|
||||
partition->openFileCount = 0;
|
||||
partition->firstOpenFile = NULL;
|
||||
|
||||
_FAT_partition_readFSinfo(partition);
|
||||
|
||||
return partition;
|
||||
}
|
||||
|
||||
@ -283,6 +303,9 @@ void _FAT_partition_destructor (PARTITION* partition) {
|
||||
nextFile = nextFile->nextOpenFile;
|
||||
}
|
||||
|
||||
// Write out the fs info sector
|
||||
_FAT_partition_writeFSinfo(partition);
|
||||
|
||||
// Free memory used by the cache, writing it to disc at the same time
|
||||
_FAT_cache_destructor (partition->cache);
|
||||
|
||||
@ -305,3 +328,77 @@ PARTITION* _FAT_partition_getPartitionFromPath (const char* path) {
|
||||
|
||||
return (PARTITION*)devops->deviceData;
|
||||
}
|
||||
|
||||
void _FAT_partition_createFSinfo(PARTITION * partition)
|
||||
{
|
||||
if(partition->readOnly || partition->filesysType != FS_FAT32)
|
||||
return;
|
||||
|
||||
uint8_t sectorBuffer[BYTES_PER_READ];
|
||||
memset(sectorBuffer, 0, sizeof(sectorBuffer));
|
||||
|
||||
int i;
|
||||
for(i = 0; i < 4; ++i)
|
||||
{
|
||||
sectorBuffer[FSIB_SIG1+i] = FS_INFO_SIG1[i];
|
||||
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);
|
||||
}
|
||||
|
||||
void _FAT_partition_readFSinfo(PARTITION * partition)
|
||||
{
|
||||
if(partition->filesysType != FS_FAT32)
|
||||
return;
|
||||
|
||||
uint8_t sectorBuffer[BYTES_PER_READ] = {0};
|
||||
|
||||
// Read first sector of disc
|
||||
if (!_FAT_disc_readSectors (partition->disc, partition->fsInfoSector, 1, sectorBuffer)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if(memcmp(sectorBuffer+FSIB_SIG1, FS_INFO_SIG1, 4) != 0 ||
|
||||
memcmp(sectorBuffer+FSIB_SIG2, FS_INFO_SIG2, 4) != 0 ||
|
||||
u8array_to_u32(sectorBuffer, FSIB_numberOfFreeCluster) == 0)
|
||||
{
|
||||
//sector does not yet exist, create one!
|
||||
_FAT_partition_createFSinfo(partition);
|
||||
return;
|
||||
}
|
||||
|
||||
partition->fat.numberFreeCluster = u8array_to_u32(sectorBuffer, FSIB_numberOfFreeCluster);
|
||||
partition->fat.numberLastAllocCluster = u8array_to_u32(sectorBuffer, FSIB_numberLastAllocCluster);
|
||||
}
|
||||
|
||||
void _FAT_partition_writeFSinfo(PARTITION * partition)
|
||||
{
|
||||
if(partition->filesysType != FS_FAT32)
|
||||
return;
|
||||
|
||||
uint8_t sectorBuffer[BYTES_PER_READ] = {0};
|
||||
|
||||
// Read first sector of disc
|
||||
if (!_FAT_disc_readSectors (partition->disc, partition->fsInfoSector, 1, sectorBuffer)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if(memcmp(sectorBuffer+FSIB_SIG1, FS_INFO_SIG1, 4) || memcmp(sectorBuffer+FSIB_SIG2, FS_INFO_SIG2, 4))
|
||||
return;
|
||||
|
||||
u32_to_u8array(sectorBuffer, FSIB_numberOfFreeCluster, partition->fat.numberFreeCluster);
|
||||
u32_to_u8array(sectorBuffer, FSIB_numberLastAllocCluster, partition->fat.numberLastAllocCluster);
|
||||
|
||||
// Read first sector of disc
|
||||
if (!_FAT_disc_writeSectors (partition->disc, partition->fsInfoSector, 1, sectorBuffer)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
@ -45,6 +45,8 @@ typedef struct {
|
||||
uint32_t sectorsPerFat;
|
||||
uint32_t lastCluster;
|
||||
uint32_t firstFree;
|
||||
uint32_t numberFreeCluster;
|
||||
uint32_t numberLastAllocCluster;
|
||||
} FAT;
|
||||
|
||||
typedef struct {
|
||||
@ -60,6 +62,7 @@ typedef struct {
|
||||
uint32_t bytesPerSector;
|
||||
uint32_t sectorsPerCluster;
|
||||
uint32_t bytesPerCluster;
|
||||
uint32_t fsInfoSector;
|
||||
FAT fat;
|
||||
// Values that may change after construction
|
||||
uint32_t cwdCluster; // Current working directory cluster
|
||||
@ -86,4 +89,19 @@ Return the partition specified in a path, as taken from the devoptab.
|
||||
*/
|
||||
PARTITION* _FAT_partition_getPartitionFromPath (const char* path);
|
||||
|
||||
/*
|
||||
Create the fs info sector.
|
||||
*/
|
||||
void _FAT_partition_createFSinfo(PARTITION * partition);
|
||||
|
||||
/*
|
||||
Read the fs info sector data.
|
||||
*/
|
||||
void _FAT_partition_readFSinfo(PARTITION * partition);
|
||||
|
||||
/*
|
||||
Write the fs info sector data.
|
||||
*/
|
||||
void _FAT_partition_writeFSinfo(PARTITION * partition);
|
||||
|
||||
#endif // _PARTITION_H
|
||||
|
Loading…
Reference in New Issue
Block a user