mirror of
https://github.com/wiiu-env/libfat.git
synced 2024-11-25 11:26:53 +01:00
Added directory iterator functions
This commit is contained in:
parent
1288d8f8ec
commit
11140aaa33
104
source/fatdir.c
104
source/fatdir.c
@ -30,6 +30,9 @@
|
|||||||
2006-08-13 - Chishm
|
2006-08-13 - Chishm
|
||||||
* Moved all externally visible directory related functions to fatdir
|
* Moved all externally visible directory related functions to fatdir
|
||||||
* Added _FAT_mkdir_r
|
* Added _FAT_mkdir_r
|
||||||
|
|
||||||
|
2006-08-14 - Chishm
|
||||||
|
* Added directory iterator functions
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@ -37,6 +40,8 @@
|
|||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#include "fatdir.h"
|
||||||
|
|
||||||
#include "cache.h"
|
#include "cache.h"
|
||||||
#include "file_allocation_table.h"
|
#include "file_allocation_table.h"
|
||||||
#include "partition.h"
|
#include "partition.h"
|
||||||
@ -421,3 +426,102 @@ int _FAT_mkdir_r (struct _reent *r, const char *path, int mode) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
dir_iter_t* _FAT_diropen_r(struct _reent *r, dir_iter_t *dirState, const char *path) {
|
||||||
|
DIR_ENTRY dirEntry;
|
||||||
|
DIR_STATE_STRUCT* state = (DIR_STATE_STRUCT*) dirState;
|
||||||
|
bool fileExists;
|
||||||
|
|
||||||
|
state->partition = _FAT_partition_getPartitionFromPath (path);
|
||||||
|
|
||||||
|
if (state->partition == NULL) {
|
||||||
|
r->_errno = ENODEV;
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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 NULL;
|
||||||
|
}
|
||||||
|
// Get the start cluster of the directory
|
||||||
|
fileExists = _FAT_directory_entryFromPath (state->partition, &dirEntry, path, NULL);
|
||||||
|
|
||||||
|
if (!fileExists) {
|
||||||
|
r->_errno = ENOENT;
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Make sure it is a directory
|
||||||
|
if (! _FAT_directory_isDirectory (&dirEntry)) {
|
||||||
|
r->_errno = ENOTDIR;
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Save the start cluster for use when resetting the directory data
|
||||||
|
state->startCluster = _FAT_directory_entryGetCluster (dirEntry.entryData);
|
||||||
|
|
||||||
|
// Get the first entry for use with a call to dirnext
|
||||||
|
state->validEntry =
|
||||||
|
_FAT_directory_getFirstEntry (state->partition, &(state->currentEntry), state->startCluster);
|
||||||
|
|
||||||
|
// We are now using this entry
|
||||||
|
state->inUse = true;
|
||||||
|
return (dir_iter_t*) state;
|
||||||
|
}
|
||||||
|
|
||||||
|
int _FAT_dirreset_r (struct _reent *r, dir_iter_t *dirState) {
|
||||||
|
DIR_STATE_STRUCT* state = (DIR_STATE_STRUCT*) dirState;
|
||||||
|
|
||||||
|
// Make sure we are still using this entry
|
||||||
|
if (!state->inUse) {
|
||||||
|
r->_errno = EBADF;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get the first entry for use with a call to dirnext
|
||||||
|
state->validEntry =
|
||||||
|
_FAT_directory_getFirstEntry (state->partition, &(state->currentEntry), state->startCluster);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int _FAT_dirnext_r (struct _reent *r, dir_iter_t *dirState, char *filename, struct stat *filestat) {
|
||||||
|
DIR_STATE_STRUCT* state = (DIR_STATE_STRUCT*) dirState;
|
||||||
|
|
||||||
|
// Make sure we are still using this entry
|
||||||
|
if (!state->inUse) {
|
||||||
|
r->_errno = EBADF;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Make sure there is another file to report on
|
||||||
|
if (! state->validEntry) {
|
||||||
|
r->_errno = ENOENT;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get the filename
|
||||||
|
strncpy (filename, state->currentEntry.filename, MAX_FILENAME_LENGTH);
|
||||||
|
// Get the stats, if requested
|
||||||
|
if (filestat != NULL) {
|
||||||
|
_FAT_directory_entryStat (state->partition, &(state->currentEntry), filestat);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Look for the next entry for use next time
|
||||||
|
state->validEntry =
|
||||||
|
_FAT_directory_getNextEntry (state->partition, &(state->currentEntry));
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int _FAT_dirclose_r (struct _reent *r, dir_iter_t *dirState) {
|
||||||
|
DIR_STATE_STRUCT* state = (DIR_STATE_STRUCT*) dirState;
|
||||||
|
|
||||||
|
// We are no longer using this entry
|
||||||
|
state->inUse = false;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
@ -30,6 +30,9 @@
|
|||||||
2006-08-13 - Chishm
|
2006-08-13 - Chishm
|
||||||
* Moved all externally visible directory related functions to fatdir
|
* Moved all externally visible directory related functions to fatdir
|
||||||
* Added _FAT_mkdir_r
|
* Added _FAT_mkdir_r
|
||||||
|
|
||||||
|
2006-08-14 - Chishm
|
||||||
|
* Added directory iterator functions
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
@ -38,7 +41,17 @@
|
|||||||
|
|
||||||
#include <sys/reent.h>
|
#include <sys/reent.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
|
#include <sys/iosupport.h>
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
|
#include "directory.h"
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
PARTITION* partition;
|
||||||
|
DIR_ENTRY currentEntry;
|
||||||
|
u32 startCluster;
|
||||||
|
bool inUse;
|
||||||
|
bool validEntry;
|
||||||
|
} DIR_STATE_STRUCT;
|
||||||
|
|
||||||
extern int _FAT_stat_r (struct _reent *r, const char *path, struct stat *st);
|
extern int _FAT_stat_r (struct _reent *r, const char *path, struct stat *st);
|
||||||
|
|
||||||
@ -52,4 +65,13 @@ extern int _FAT_rename_r (struct _reent *r, const char *oldName, const char *new
|
|||||||
|
|
||||||
extern int _FAT_mkdir_r (struct _reent *r, const char *path, int mode);
|
extern int _FAT_mkdir_r (struct _reent *r, const char *path, int mode);
|
||||||
|
|
||||||
|
/*
|
||||||
|
Directory iterator functions
|
||||||
|
*/
|
||||||
|
extern dir_iter_t* _FAT_diropen_r(struct _reent *r, dir_iter_t *dirState, const char *path);
|
||||||
|
extern int _FAT_dirreset_r (struct _reent *r, dir_iter_t *dirState);
|
||||||
|
extern int _FAT_dirnext_r (struct _reent *r, dir_iter_t *dirState, char *filename, struct stat *filestat);
|
||||||
|
extern int _FAT_dirclose_r (struct _reent *r, dir_iter_t *dirState);
|
||||||
|
|
||||||
|
|
||||||
#endif // _FATDIR_H
|
#endif // _FATDIR_H
|
||||||
|
Loading…
Reference in New Issue
Block a user