From 1087f6ed6d7f8b322e91e9c4ab0af8b7232b086f Mon Sep 17 00:00:00 2001 From: mtheall Date: Sun, 19 Apr 2020 17:41:01 -0500 Subject: [PATCH] Add rmdir support (#17) --- source/fatdir.c | 20 +++++++++++++++++++- source/fatdir.h | 2 ++ source/fatfile.h | 2 -- source/libfat.c | 2 +- 4 files changed, 22 insertions(+), 4 deletions(-) diff --git a/source/fatdir.c b/source/fatdir.c index 1ef5aec..edb8141 100644 --- a/source/fatdir.c +++ b/source/fatdir.c @@ -86,7 +86,7 @@ int _FAT_link_r (struct _reent *r, const char *existing, const char *newLink) { return -1; } -int _FAT_unlink_r (struct _reent *r, const char *path) { +static int _FAT_unlinkCommon (struct _reent *r, const char *path, bool isRmDir) { PARTITION* partition = NULL; DIR_ENTRY dirEntry; DIR_ENTRY dirContents; @@ -130,6 +130,12 @@ int _FAT_unlink_r (struct _reent *r, const char *path) { // If this is a directory, make sure it is empty if (_FAT_directory_isDirectory (&dirEntry)) { + if (!isRmDir) { + _FAT_unlock(&partition->lock); + r->_errno = EISDIR; + return -1; + } + nextEntry = _FAT_directory_getFirstEntry (partition, &dirContents, cluster); while (nextEntry) { @@ -141,6 +147,10 @@ int _FAT_unlink_r (struct _reent *r, const char *path) { } nextEntry = _FAT_directory_getNextEntry (partition, &dirContents); } + } else if (isRmDir) { + _FAT_unlock(&partition->lock); + r->_errno = ENOTDIR; + return -1; } if (_FAT_fat_isValidCluster(partition, cluster)) { @@ -171,6 +181,10 @@ int _FAT_unlink_r (struct _reent *r, const char *path) { } } +int _FAT_unlink_r (struct _reent *r, const char *path) { + return _FAT_unlinkCommon (r, path, false); +} + int _FAT_chdir_r (struct _reent *r, const char *path) { PARTITION* partition = NULL; @@ -451,6 +465,10 @@ int _FAT_mkdir_r (struct _reent *r, const char *path, int mode) { return 0; } +int _FAT_rmdir_r (struct _reent *r, const char *path) { + return _FAT_unlinkCommon (r, path, true); +} + int _FAT_statvfs_r (struct _reent *r, const char *path, struct statvfs *buf) { PARTITION* partition = NULL; diff --git a/source/fatdir.h b/source/fatdir.h index 426dd30..7f5c71a 100644 --- a/source/fatdir.h +++ b/source/fatdir.h @@ -59,6 +59,8 @@ 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_rmdir_r (struct _reent *r, const char *path); + extern int _FAT_statvfs_r (struct _reent *r, const char *path, struct statvfs *buf); /* diff --git a/source/fatfile.h b/source/fatfile.h index 3d9836c..c39aa3f 100644 --- a/source/fatfile.h +++ b/source/fatfile.h @@ -85,8 +85,6 @@ int _FAT_stat_r (struct _reent *r, const char *path, struct stat *st); int _FAT_link_r (struct _reent *r, const char *existing, const char *newLink); -int _FAT_unlink_r (struct _reent *r, const char *name); - int _FAT_chdir_r (struct _reent *r, const char *name); int _FAT_rename_r (struct _reent *r, const char *oldName, const char *newName); diff --git a/source/libfat.c b/source/libfat.c index 4d323ca..11182f1 100644 --- a/source/libfat.c +++ b/source/libfat.c @@ -66,7 +66,7 @@ static const devoptab_t dotab_fat = { NULL, /* Device data */ NULL, // chmod_r NULL, // fchmod_r - NULL // rmdir_r + _FAT_rmdir_r, }; bool fatMount (const char* name, const DISC_INTERFACE* interface, sec_t startSector, uint32_t cacheSize, uint32_t SectorsPerPage) {