summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--firmware/common/dir.c44
-rw-r--r--firmware/drivers/fat.c11
-rw-r--r--firmware/export/fat.h3
-rw-r--r--firmware/include/dir.h2
4 files changed, 54 insertions, 6 deletions
diff --git a/firmware/common/dir.c b/firmware/common/dir.c
index c3965c2418..6ca3a76e3b 100644
--- a/firmware/common/dir.c
+++ b/firmware/common/dir.c
@@ -55,7 +55,7 @@ DIR* opendir(char* name)
return NULL;
}
- if ( fat_opendir(&(opendirs[dd].fatdir), 0) < 0 ) {
+ if ( fat_opendir(&(opendirs[dd].fatdir), 0, NULL) < 0 ) {
DEBUGF("Failed opening root dir\n");
opendirs[dd].busy = false;
return NULL;
@@ -75,8 +75,10 @@ DIR* opendir(char* name)
}
if ( (entry.attr & FAT_ATTR_DIRECTORY) &&
(!strcasecmp(part, entry.name)) ) {
+ opendirs[dd].parent_dir = opendirs[dd].fatdir;
if ( fat_opendir(&(opendirs[dd].fatdir),
- entry.firstcluster) < 0 ) {
+ entry.firstcluster,
+ &(opendirs[dd].parent_dir)) < 0 ) {
DEBUGF("Failed opening dir '%s' (%d)\n",
part, entry.firstcluster);
opendirs[dd].busy = false;
@@ -179,3 +181,41 @@ int mkdir(char *name, int mode)
return rc;
}
+
+int rmdir(char* name)
+{
+ int rc;
+ DIR* dir;
+ struct dirent* entry;
+
+ dir = opendir(name);
+ if (!dir)
+ {
+ errno = ENOENT; /* open error */
+ return -1;
+ }
+
+ /* check if the directory is empty */
+ while ((entry = readdir(dir)))
+ {
+ if (strcmp(entry->d_name, ".") &&
+ strcmp(entry->d_name, ".."))
+ {
+ DEBUGF("rmdir error: not empty\n");
+ errno = ENOTEMPTY;
+ closedir(dir);
+ return -2;
+ }
+ }
+
+ rc = fat_remove(&(dir->fatdir.file));
+ if ( rc < 0 ) {
+ DEBUGF("Failed removing dir: %d\n", rc);
+ errno = EIO;
+ rc = rc * 10 - 3;
+ }
+
+ closedir(dir);
+
+ return rc;
+}
diff --git a/firmware/drivers/fat.c b/firmware/drivers/fat.c
index 0c8997d94a..bb10c5f223 100644
--- a/firmware/drivers/fat.c
+++ b/firmware/drivers/fat.c
@@ -1543,6 +1543,10 @@ int fat_remove(struct fat_file* file)
file->firstcluster = 0;
file->dircluster = 0;
+ rc = flush_fat();
+ if (rc < 0)
+ return rc * 10 - 2;
+
return 0;
}
@@ -1561,7 +1565,7 @@ int fat_rename(struct fat_file* file,
}
/* create a temporary file handle */
- rc = fat_opendir(&dir, file->dircluster);
+ rc = fat_opendir(&dir, file->dircluster, NULL);
if (rc < 0)
return rc * 10 - 2;
@@ -1796,14 +1800,15 @@ int fat_seek(struct fat_file *file, unsigned int seeksector )
return 0;
}
-int fat_opendir(struct fat_dir *dir, unsigned int startcluster)
+int fat_opendir(struct fat_dir *dir, unsigned int startcluster,
+ struct fat_dir *parent_dir)
{
int rc;
if (startcluster == 0)
startcluster = sec2cluster(fat_bpb.rootdirsector);
- rc = fat_open(startcluster, &dir->file, NULL);
+ rc = fat_open(startcluster, &dir->file, parent_dir);
if(rc)
{
DEBUGF( "fat_opendir() - Couldn't open dir"
diff --git a/firmware/export/fat.h b/firmware/export/fat.h
index db8737cfd3..24c894ff35 100644
--- a/firmware/export/fat.h
+++ b/firmware/export/fat.h
@@ -92,7 +92,8 @@ extern int fat_rename(struct fat_file* file,
unsigned char* newname,
int size, int attr);
-extern int fat_opendir(struct fat_dir *ent, unsigned int currdir);
+extern int fat_opendir(struct fat_dir *ent, unsigned int currdir,
+ struct fat_dir *parent_dir);
extern int fat_getnext(struct fat_dir *ent, struct fat_direntry *entry);
extern int fat_get_cluster_size(void);
diff --git a/firmware/include/dir.h b/firmware/include/dir.h
index ab8e15ffa6..7de0276840 100644
--- a/firmware/include/dir.h
+++ b/firmware/include/dir.h
@@ -48,6 +48,7 @@ typedef struct {
bool busy;
int startcluster;
struct fat_dir fatdir;
+ struct fat_dir parent_dir;
struct dirent theent;
} DIR;
@@ -73,6 +74,7 @@ typedef struct DIRtag
extern DIR* opendir(char* name);
extern int closedir(DIR* dir);
extern int mkdir(char* name, int mode);
+extern int rmdir(char* name);
extern struct dirent* readdir(DIR* dir);