Moved all externally visible directory related functions to fatdir

This commit is contained in:
Michael Chisholm 2006-08-13 09:43:38 +00:00
parent cae34ed566
commit a8bbf71829
4 changed files with 11 additions and 259 deletions

View File

@ -36,6 +36,9 @@
2006-08-02 - Chishm 2006-08-02 - Chishm
* Fixed _FAT_seek_r * Fixed _FAT_seek_r
2006-08-13 - Chishm
* Moved all externally visible directory related functions to fatdir
*/ */
@ -810,262 +813,3 @@ int _FAT_fstat_r (struct _reent *r, int fd, struct stat *st) {
return 0; return 0;
} }
int _FAT_stat_r (struct _reent *r, const char *path, struct stat *st) {
PARTITION* partition = NULL;
DIR_ENTRY dirEntry;
// Get the partition this file is on
partition = _FAT_partition_getPartitionFromPath (path);
if (partition == NULL) {
r->_errno = ENODEV;
return -1;
}
// Move the path pointer to the start of the actual path
if (strchr (path, ':') != NULL) {
path = strchr (path, ':') + 1;
}
if (strchr (path, ':') != NULL) {
r->_errno = EINVAL;
return -1;
}
// Search for the file on the disc
if (!_FAT_directory_entryFromPath (partition, &dirEntry, path, NULL)) {
r->_errno = ENOENT;
return -1;
}
// Fill in the stat struct
_FAT_directory_entryStat (partition, &dirEntry, st);
return 0;
}
int _FAT_link_r (struct _reent *r, const char *existing, const char *newLink) {
r->_errno = ENOTSUP;
return -1;
}
int _FAT_unlink_r (struct _reent *r, const char *path) {
PARTITION* partition = NULL;
DIR_ENTRY dirEntry;
DIR_ENTRY dirContents;
u32 cluster;
bool nextEntry;
bool errorOccured = false;
// Get the partition this directory is on
partition = _FAT_partition_getPartitionFromPath (path);
if (partition == NULL) {
r->_errno = ENODEV;
return -1;
}
// Make sure we aren't trying to write to a read-only disc
if (partition->readOnly) {
r->_errno = EROFS;
return -1;
}
// Move the path pointer to the start of the actual path
if (strchr (path, ':') != NULL) {
path = strchr (path, ':') + 1;
}
if (strchr (path, ':') != NULL) {
r->_errno = EINVAL;
return -1;
}
// Search for the file on the disc
if (!_FAT_directory_entryFromPath (partition, &dirEntry, path, NULL)) {
r->_errno = ENOENT;
return -1;
}
cluster = _FAT_directory_entryGetCluster (dirEntry.entryData);
// If this is a directory, make sure it is empty
if (_FAT_directory_isDirectory (&dirEntry)) {
nextEntry = _FAT_directory_getFirstEntry (partition, &dirContents, cluster);
while (nextEntry) {
if (!_FAT_directory_isDot (&dirContents)) {
// The directory had something in it that isn't a reference to itself or it's parent
r->_errno = EPERM;
return -1;
}
nextEntry = _FAT_directory_getNextEntry (partition, &dirContents);
}
}
if (cluster != CLUSTER_FREE) {
// Remove the cluster chain for this file
if (!_FAT_fat_clearLinks (partition, cluster)) {
r->_errno = EIO;
errorOccured = true;
}
}
// Remove the directory entry for this file
if (!_FAT_directory_removeEntry (partition, &dirEntry)) {
r->_errno = EIO;
errorOccured = true;
}
// Flush any sectors in the disc cache
if (!_FAT_cache_flush(partition->cache)) {
r->_errno = EIO;
errorOccured = true;
}
if (errorOccured) {
return -1;
} else {
return 0;
}
}
int _FAT_chdir_r (struct _reent *r, const char *path) {
PARTITION* partition = NULL;
// Get the partition this directory is on
partition = _FAT_partition_getPartitionFromPath (path);
if (partition == NULL) {
r->_errno = ENODEV;
return -1;
}
// Move the path pointer to the start of the actual path
if (strchr (path, ':') != NULL) {
path = strchr (path, ':') + 1;
}
if (strchr (path, ':') != NULL) {
r->_errno = EINVAL;
return -1;
}
// Set the default device to match this one
if (!_FAT_partition_setDefaultPartition (partition)) {
r->_errno = ENOENT;
return -1;
}
// Try changing directory
if (_FAT_directory_chdir (partition, path)) {
// Successful
return 0;
} else {
// Failed
r->_errno = ENOTDIR;
return -1;
}
}
int _FAT_rename_r (struct _reent *r, const char *oldName, const char *newName) {
PARTITION* partition = NULL;
DIR_ENTRY oldDirEntry;
DIR_ENTRY newDirEntry;
const char *pathEnd;
u32 dirCluster;
// Get the partition this directory is on
partition = _FAT_partition_getPartitionFromPath (oldName);
if (partition == NULL) {
r->_errno = ENODEV;
return -1;
}
// Make sure the same partition is used for the old and new names
if (partition != _FAT_partition_getPartitionFromPath (newName)) {
r->_errno = EXDEV;
return -1;
}
// Make sure we aren't trying to write to a read-only disc
if (partition->readOnly) {
r->_errno = EROFS;
return -1;
}
// Move the path pointer to the start of the actual path
if (strchr (oldName, ':') != NULL) {
oldName = strchr (oldName, ':') + 1;
}
if (strchr (oldName, ':') != NULL) {
r->_errno = EINVAL;
return -1;
}
if (strchr (newName, ':') != NULL) {
newName = strchr (newName, ':') + 1;
}
if (strchr (newName, ':') != NULL) {
r->_errno = EINVAL;
return -1;
}
// Search for the file on the disc
if (!_FAT_directory_entryFromPath (partition, &oldDirEntry, oldName, NULL)) {
r->_errno = ENOENT;
return -1;
}
// Make sure there is no existing file / directory with the new name
if (_FAT_directory_entryFromPath (partition, &newDirEntry, newName, NULL)) {
r->_errno = EEXIST;
return -1;
}
// Create the new file entry
// Get the directory it has to go in
pathEnd = strrchr (newName, DIR_SEPARATOR);
if (pathEnd == NULL) {
// No path was specified
dirCluster = partition->cwdCluster;
pathEnd = newName;
} else {
// Path was specified -- get the right dirCluster
// Recycling newDirEntry, since it needs to be recreated anyway
if (!_FAT_directory_entryFromPath (partition, &newDirEntry, newName, pathEnd) ||
!_FAT_directory_isDirectory(&newDirEntry)) {
r->_errno = ENOTDIR;
return -1;
}
dirCluster = _FAT_directory_entryGetCluster (newDirEntry.entryData);
// Move the pathEnd past the last DIR_SEPARATOR
pathEnd += 1;
}
// Copy the entry data
memcpy (&newDirEntry, &oldDirEntry, sizeof(DIR_ENTRY));
// Set the new name
strncpy (newDirEntry.filename, pathEnd, MAX_FILENAME_LENGTH - 1);
// Write the new entry
if (!_FAT_directory_addEntry (partition, &newDirEntry, dirCluster)) {
r->_errno = ENOSPC;
return -1;
}
// Remove the old entry
if (!_FAT_directory_removeEntry (partition, &oldDirEntry)) {
r->_errno = EIO;
return -1;
}
// Flush any sectors in the disc cache
if (!_FAT_cache_flush (partition->cache)) {
r->_errno = EIO;
return -1;
}
return 0;
}

View File

@ -36,6 +36,9 @@
2006-07-24 - Chishm 2006-07-24 - Chishm
* Removed padding workaround from FILE_STRUCT * Removed padding workaround from FILE_STRUCT
2006-08-13 - Chishm
* Moved all externally visible directory related functions to fatdir
*/ */

View File

@ -200,6 +200,7 @@ static bool _FAT_fat_writeFatEntry (PARTITION* partition, u32 cluster, u32 value
gets the first available free cluster, sets it gets the first available free cluster, sets it
to end of file, links the input cluster to it then returns the to end of file, links the input cluster to it then returns the
cluster number cluster number
If an error occurs, return CLUSTER_FREE
-----------------------------------------------------------------*/ -----------------------------------------------------------------*/
u32 _FAT_fat_linkFreeCluster(PARTITION* partition, u32 cluster) { u32 _FAT_fat_linkFreeCluster(PARTITION* partition, u32 cluster) {
u32 firstFree; u32 firstFree;

View File

@ -27,6 +27,9 @@
2006-07-11 - Chishm 2006-07-11 - Chishm
* Original release * Original release
2006-08-13 - Chishm
* Moved all externally visible directory related functions to fatdir
*/ */
#include <sys/iosupport.h> #include <sys/iosupport.h>
@ -34,6 +37,7 @@
#include "common.h" #include "common.h"
#include "partition.h" #include "partition.h"
#include "fatfile.h" #include "fatfile.h"
#include "fatdir.h"
#define GBA_DEFAULT_CACHE_PAGES 2 #define GBA_DEFAULT_CACHE_PAGES 2
#define NDS_DEFAULT_CACHE_PAGES 8 #define NDS_DEFAULT_CACHE_PAGES 8