Use CLUSTER_ERROR when an error occurs with the FAT, not CLUSTER_FREE

This commit is contained in:
Michael Chisholm 2007-09-01 07:55:38 +00:00
parent 4a0c7c4046
commit 8cfa906c22
5 changed files with 50 additions and 29 deletions

View File

@ -46,6 +46,9 @@
2007-04-22 - Chishm
* Added space to list of illegal alias characters - fixes filename creation bug when filename contained a space
2007-09-01 - Chishm
* Use CLUSTER_ERROR when an error occurs with the FAT, not CLUSTER_FREE
*/
#include <string.h>
@ -220,7 +223,7 @@ static bool _FAT_directory_incrementDirEntryPosition (PARTITION* partition, DIR_
if (tempCluster == CLUSTER_EOF) {
if (extendDirectory) {
tempCluster = _FAT_fat_linkFreeClusterCleared (partition, position.cluster);
if (tempCluster == CLUSTER_FREE) {
if (!_FAT_fat_isValidCluster(partition, tempCluster)) {
return false; // This will only happen if the disc is full
}
} else {
@ -606,7 +609,9 @@ static bool _FAT_directory_findEntryGap (PARTITION* partition, DIR_ENTRY* entry,
endOfDirectory = false;
while (entryStillValid && !endOfDirectory && (dirEntryRemain > 0)) {
_FAT_cache_readPartialSector (partition->cache, entryData, _FAT_fat_clusterToSector(partition, gapEnd.cluster) + gapEnd.sector, gapEnd.offset * DIR_ENTRY_DATA_SIZE, DIR_ENTRY_DATA_SIZE);
_FAT_cache_readPartialSector (partition->cache, entryData,
_FAT_fat_clusterToSector(partition, gapEnd.cluster) + gapEnd.sector,
gapEnd.offset * DIR_ENTRY_DATA_SIZE, DIR_ENTRY_DATA_SIZE);
if (entryData[0] == DIR_ENTRY_LAST) {
gapStart = gapEnd;
-- dirEntryRemain;
@ -643,7 +648,9 @@ static bool _FAT_directory_findEntryGap (PARTITION* partition, DIR_ENTRY* entry,
entryStillValid = _FAT_directory_incrementDirEntryPosition (partition, &gapEnd, true);
-- dirEntryRemain;
// Fill the entry with blanks
_FAT_cache_writePartialSector (partition->cache, entryData, _FAT_fat_clusterToSector(partition, gapEnd.cluster) + gapEnd.sector, gapEnd.offset * DIR_ENTRY_DATA_SIZE, DIR_ENTRY_DATA_SIZE);
_FAT_cache_writePartialSector (partition->cache, entryData,
_FAT_fat_clusterToSector(partition, gapEnd.cluster) + gapEnd.sector,
gapEnd.offset * DIR_ENTRY_DATA_SIZE, DIR_ENTRY_DATA_SIZE);
}
if (!entryStillValid) {
return false;

View File

@ -42,6 +42,9 @@
2007-01-10 - Chishm
* Updated directory iterator functions for DevkitPro r20
2007-09-01 - Chishm
* Use CLUSTER_ERROR when an error occurs with the FAT, not CLUSTER_FREE
*/
#include <string.h>
@ -153,7 +156,7 @@ int _FAT_unlink_r (struct _reent *r, const char *path) {
}
}
if (cluster != CLUSTER_FREE) {
if (!_FAT_fat_isValidCluster(partition, cluster)) {
// Remove the cluster chain for this file
if (!_FAT_fat_clearLinks (partition, cluster)) {
r->_errno = EIO;
@ -390,7 +393,7 @@ int _FAT_mkdir_r (struct _reent *r, const char *path, int mode) {
// Get a cluster for the new directory
dirCluster = _FAT_fat_linkFreeClusterCleared (partition, CLUSTER_FREE);
if (dirCluster == CLUSTER_FREE) {
if (!_FAT_fat_isValidCluster(partition, dirCluster)) {
// No space left on disc for the cluster
r->_errno = ENOSPC;
return -1;

View File

@ -375,7 +375,7 @@ int _FAT_read_r (struct _reent *r, int fd, char *ptr, int len) {
tempNextCluster = _FAT_fat_nextCluster(partition, position.cluster);
if ((remain == 0) && (tempNextCluster == CLUSTER_EOF)) {
position.sector = partition->sectorsPerCluster;
} else if (tempNextCluster == CLUSTER_FREE) {
} else if (!_FAT_fat_isValidCluster(partition, tempNextCluster)) {
r->_errno = EIO;
flagNoError = false;
} else {
@ -401,7 +401,7 @@ int _FAT_read_r (struct _reent *r, int fd, char *ptr, int len) {
tempNextCluster = _FAT_fat_nextCluster(partition, position.cluster);
if ((remain == 0) && (tempNextCluster == CLUSTER_EOF)) {
position.sector = partition->sectorsPerCluster;
} else if (tempNextCluster == CLUSTER_FREE) {
} else if (!_FAT_fat_isValidCluster(partition, tempNextCluster)) {
r->_errno = EIO;
flagNoError = false;
} else {
@ -467,7 +467,7 @@ static bool file_extend_r (struct _reent *r, FILE_STRUCT* file) {
if ((remain > 0) && (file->filesize > 0) && (position.sector == 0)) {
// Get a new cluster on the edge of a cluster boundary
tempNextCluster = _FAT_fat_linkFreeCluster(partition, position.cluster);
if (tempNextCluster == CLUSTER_FREE) {
if (!_FAT_fat_isValidCluster(partition, tempNextCluster)) {
// Couldn't get a cluster, so abort
r->_errno = ENOSPC;
return false;
@ -496,7 +496,7 @@ static bool file_extend_r (struct _reent *r, FILE_STRUCT* file) {
position.sector = 0;
// Ran out of clusters so get a new one
tempNextCluster = _FAT_fat_linkFreeCluster(partition, position.cluster);
if (tempNextCluster == CLUSTER_FREE) {
if (!_FAT_fat_isValidCluster(partition, tempNextCluster)) {
// Couldn't get a cluster, so abort
r->_errno = ENOSPC;
return false;
@ -519,7 +519,7 @@ static bool file_extend_r (struct _reent *r, FILE_STRUCT* file) {
// Ran out of clusters so get a new one
tempNextCluster = _FAT_fat_linkFreeCluster(partition, position.cluster);
}
if (tempNextCluster == CLUSTER_FREE) {
if (!_FAT_fat_isValidCluster(partition, tempNextCluster)) {
// Couldn't get a cluster, so abort
r->_errno = ENOSPC;
return false;
@ -595,7 +595,7 @@ int _FAT_write_r (struct _reent *r,int fd, const char *ptr, int len) {
// Ran out of clusters so get a new one
tempNextCluster = _FAT_fat_linkFreeCluster(partition, position.cluster);
}
if (tempNextCluster == CLUSTER_FREE) {
if (!_FAT_fat_isValidCluster(partition, tempNextCluster)) {
// Couldn't get a cluster, so abort
r->_errno = ENOSPC;
flagNoError = false;
@ -655,7 +655,7 @@ int _FAT_write_r (struct _reent *r,int fd, const char *ptr, int len) {
// Ran out of clusters so get a new one
tempNextCluster = _FAT_fat_linkFreeCluster(partition, position.cluster);
}
if (tempNextCluster == CLUSTER_FREE) {
if (!_FAT_fat_isValidCluster(partition, tempNextCluster)) {
// Couldn't get a cluster, so abort
r->_errno = ENOSPC;
flagNoError = false;
@ -681,7 +681,7 @@ int _FAT_write_r (struct _reent *r,int fd, const char *ptr, int len) {
// Ran out of clusters so get a new one
tempNextCluster = _FAT_fat_linkFreeCluster(partition, position.cluster);
}
if (tempNextCluster == CLUSTER_FREE) {
if (!_FAT_fat_isValidCluster(partition, tempNextCluster)) {
// Couldn't get a cluster, so abort
r->_errno = ENOSPC;
flagNoError = false;

View File

@ -34,6 +34,9 @@
2006-10-01 - Chishm
* Added _FAT_fat_linkFreeClusterCleared to clear a cluster when it is allocated
2007-09-01 - Chishm
* Use CLUSTER_ERROR when an error occurs with the FAT, not CLUSTER_FREE
*/
@ -53,7 +56,7 @@ u32 _FAT_fat_nextCluster(PARTITION* partition, u32 cluster)
switch (partition->filesysType)
{
case FS_UNKNOWN:
nextCluster = CLUSTER_FREE;
return CLUSTER_ERROR;
break;
case FS_FAT12:
@ -110,7 +113,7 @@ u32 _FAT_fat_nextCluster(PARTITION* partition, u32 cluster)
break;
default:
nextCluster = CLUSTER_FREE;
return CLUSTER_ERROR;
break;
}
@ -126,7 +129,7 @@ static bool _FAT_fat_writeFatEntry (PARTITION* partition, u32 cluster, u32 value
int offset;
u8 oldValue;
if ((cluster < 0x0002) || (cluster > partition->fat.lastCluster))
if ((cluster < CLUSTER_FIRST) || (cluster > partition->fat.lastCluster /* This will catch CLUSTER_ERROR */))
{
return false;
}
@ -204,7 +207,7 @@ static bool _FAT_fat_writeFatEntry (PARTITION* partition, u32 cluster, u32 value
gets the first available free cluster, sets it
to end of file, links the input cluster to it then returns the
cluster number
If an error occurs, return CLUSTER_FREE
If an error occurs, return CLUSTER_ERROR
-----------------------------------------------------------------*/
u32 _FAT_fat_linkFreeCluster(PARTITION* partition, u32 cluster) {
u32 firstFree;
@ -215,7 +218,7 @@ u32 _FAT_fat_linkFreeCluster(PARTITION* partition, u32 cluster) {
lastCluster = partition->fat.lastCluster;
if (cluster > lastCluster) {
return CLUSTER_FREE;
return CLUSTER_ERROR;
}
// Check if the cluster already has a link, and return it if so
@ -236,9 +239,9 @@ u32 _FAT_fat_linkFreeCluster(PARTITION* partition, u32 cluster) {
firstFree++;
if (firstFree > lastCluster) {
if (loopedAroundFAT) {
// If couldn't get a free cluster then return, saying this fact
// If couldn't get a free cluster then return an error
partition->fat.firstFree = firstFree;
return CLUSTER_FREE;
return CLUSTER_ERROR;
} else {
// Try looping back to the beginning of the FAT
// This was suggested by loopy
@ -264,7 +267,7 @@ u32 _FAT_fat_linkFreeCluster(PARTITION* partition, u32 cluster) {
gets the first available free cluster, sets it
to end of file, links the input cluster to it, clears the new
cluster to 0 valued bytes, then returns the cluster number
If an error occurs, return CLUSTER_FREE
If an error occurs, return CLUSTER_ERROR
-----------------------------------------------------------------*/
u32 _FAT_fat_linkFreeClusterCleared (PARTITION* partition, u32 cluster) {
u32 newCluster;
@ -274,8 +277,8 @@ u32 _FAT_fat_linkFreeClusterCleared (PARTITION* partition, u32 cluster) {
// Link the cluster
newCluster = _FAT_fat_linkFreeCluster(partition, cluster);
if (newCluster == CLUSTER_FREE) {
return CLUSTER_FREE;
if (newCluster == CLUSTER_FREE || newCluster == CLUSTER_ERROR) {
return CLUSTER_ERROR;
}
// Clear all the sectors within the cluster
@ -297,7 +300,7 @@ frees any cluster used by a file
bool _FAT_fat_clearLinks (PARTITION* partition, u32 cluster) {
u32 nextCluster;
if ((cluster < 0x0002) || (cluster > partition->fat.lastCluster))
if ((cluster < CLUSTER_FIRST) || (cluster > partition->fat.lastCluster /* This will catch CLUSTER_ERROR */))
return false;
// If this clears up more space in the FAT before the current free pointer, move it backwards
@ -305,7 +308,7 @@ bool _FAT_fat_clearLinks (PARTITION* partition, u32 cluster) {
partition->fat.firstFree = cluster;
}
while ((cluster != CLUSTER_EOF) && (cluster != CLUSTER_FREE)) {
while ((cluster != CLUSTER_EOF) && (cluster != CLUSTER_FREE) && (cluster != CLUSTER_ERROR)) {
// Store next cluster before erasing the link
nextCluster = _FAT_fat_nextCluster (partition, cluster);

View File

@ -31,6 +31,9 @@
2006-10-01 - Chishm
* Added _FAT_fat_linkFreeClusterCleared to clear a cluster when it is allocated
2007-09-01 - Chishm
* Use CLUSTER_ERROR when an error occurs with the FAT, not CLUSTER_FREE
*/
#ifndef _FAT_H
@ -41,8 +44,9 @@
#define CLUSTER_EOF_16 0xFFFF
#define CLUSTER_EOF 0x0FFFFFFF
#define CLUSTER_FREE 0x0000
#define CLUSTER_FIRST 0x0002
#define CLUSTER_FREE 0x00000000
#define CLUSTER_FIRST 0x00000002
#define CLUSTER_ERROR 0xFFFFFFFF
#define CLUSTERS_PER_FAT12 4085
#define CLUSTERS_PER_FAT16 65525
@ -58,7 +62,11 @@ bool _FAT_fat_clearLinks (PARTITION* partition, u32 cluster);
u32 _FAT_fat_lastCluster (PARTITION* partition, u32 cluster);
static inline u32 _FAT_fat_clusterToSector (PARTITION* partition, u32 cluster) {
return (cluster >= 2) ? ((cluster - 2) * partition->sectorsPerCluster) + partition->dataStart : partition->rootDirStart;
return (cluster >= CLUSTER_FIRST) ? ((cluster - CLUSTER_FIRST) * partition->sectorsPerCluster) + partition->dataStart : partition->rootDirStart;
}
static inline bool _FAT_fat_isValidCluster (PARTITION* partition, u32 cluster) {
return (cluster >= CLUSTER_FIRST) && (cluster <= partition->fat.lastCluster /* This will catch CLUSTER_ERROR */);
}
#endif // _FAT_H