Skip to content
This repository was archived by the owner on Nov 11, 2024. It is now read-only.

Commit 1087f6e

Browse files
authored
Add rmdir support (#17)
1 parent fef8efe commit 1087f6e

4 files changed

Lines changed: 22 additions & 4 deletions

File tree

source/fatdir.c

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ int _FAT_link_r (struct _reent *r, const char *existing, const char *newLink) {
8686
return -1;
8787
}
8888

89-
int _FAT_unlink_r (struct _reent *r, const char *path) {
89+
static int _FAT_unlinkCommon (struct _reent *r, const char *path, bool isRmDir) {
9090
PARTITION* partition = NULL;
9191
DIR_ENTRY dirEntry;
9292
DIR_ENTRY dirContents;
@@ -130,6 +130,12 @@ int _FAT_unlink_r (struct _reent *r, const char *path) {
130130

131131
// If this is a directory, make sure it is empty
132132
if (_FAT_directory_isDirectory (&dirEntry)) {
133+
if (!isRmDir) {
134+
_FAT_unlock(&partition->lock);
135+
r->_errno = EISDIR;
136+
return -1;
137+
}
138+
133139
nextEntry = _FAT_directory_getFirstEntry (partition, &dirContents, cluster);
134140

135141
while (nextEntry) {
@@ -141,6 +147,10 @@ int _FAT_unlink_r (struct _reent *r, const char *path) {
141147
}
142148
nextEntry = _FAT_directory_getNextEntry (partition, &dirContents);
143149
}
150+
} else if (isRmDir) {
151+
_FAT_unlock(&partition->lock);
152+
r->_errno = ENOTDIR;
153+
return -1;
144154
}
145155

146156
if (_FAT_fat_isValidCluster(partition, cluster)) {
@@ -171,6 +181,10 @@ int _FAT_unlink_r (struct _reent *r, const char *path) {
171181
}
172182
}
173183

184+
int _FAT_unlink_r (struct _reent *r, const char *path) {
185+
return _FAT_unlinkCommon (r, path, false);
186+
}
187+
174188
int _FAT_chdir_r (struct _reent *r, const char *path) {
175189
PARTITION* partition = NULL;
176190

@@ -451,6 +465,10 @@ int _FAT_mkdir_r (struct _reent *r, const char *path, int mode) {
451465
return 0;
452466
}
453467

468+
int _FAT_rmdir_r (struct _reent *r, const char *path) {
469+
return _FAT_unlinkCommon (r, path, true);
470+
}
471+
454472
int _FAT_statvfs_r (struct _reent *r, const char *path, struct statvfs *buf)
455473
{
456474
PARTITION* partition = NULL;

source/fatdir.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ extern int _FAT_rename_r (struct _reent *r, const char *oldName, const char *new
5959

6060
extern int _FAT_mkdir_r (struct _reent *r, const char *path, int mode);
6161

62+
extern int _FAT_rmdir_r (struct _reent *r, const char *path);
63+
6264
extern int _FAT_statvfs_r (struct _reent *r, const char *path, struct statvfs *buf);
6365

6466
/*

source/fatfile.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,6 @@ int _FAT_stat_r (struct _reent *r, const char *path, struct stat *st);
8585

8686
int _FAT_link_r (struct _reent *r, const char *existing, const char *newLink);
8787

88-
int _FAT_unlink_r (struct _reent *r, const char *name);
89-
9088
int _FAT_chdir_r (struct _reent *r, const char *name);
9189

9290
int _FAT_rename_r (struct _reent *r, const char *oldName, const char *newName);

source/libfat.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ static const devoptab_t dotab_fat = {
6666
NULL, /* Device data */
6767
NULL, // chmod_r
6868
NULL, // fchmod_r
69-
NULL // rmdir_r
69+
_FAT_rmdir_r,
7070
};
7171

7272
bool fatMount (const char* name, const DISC_INTERFACE* interface, sec_t startSector, uint32_t cacheSize, uint32_t SectorsPerPage) {

0 commit comments

Comments
 (0)