diff options
| author | Linus Nielsen Feltzing <linus@haxx.se> | 2004-04-16 08:58:29 +0000 |
|---|---|---|
| committer | Linus Nielsen Feltzing <linus@haxx.se> | 2004-04-16 08:58:29 +0000 |
| commit | bc9397d1fa1e05a09f24f5999fa2e85a23916ab9 (patch) | |
| tree | 5cd8d1f4abc1f40627662c61cdd833d3cc9e768b | |
| parent | b8beff39e92ebe8c417ef6b9d1a7d467bf84826a (diff) | |
| download | rockbox-bc9397d1fa1e05a09f24f5999fa2e85a23916ab9.zip rockbox-bc9397d1fa1e05a09f24f5999fa2e85a23916ab9.tar.gz rockbox-bc9397d1fa1e05a09f24f5999fa2e85a23916ab9.tar.bz2 rockbox-bc9397d1fa1e05a09f24f5999fa2e85a23916ab9.tar.xz | |
New function: rmdir(). Also some changes in the fat code, to track the parent directory in opendir(), to be able to delete directories
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@4509 a1c6a512-1295-4272-9138-f99709370657
| -rw-r--r-- | firmware/common/dir.c | 44 | ||||
| -rw-r--r-- | firmware/drivers/fat.c | 11 | ||||
| -rw-r--r-- | firmware/export/fat.h | 3 | ||||
| -rw-r--r-- | firmware/include/dir.h | 2 |
4 files changed, 54 insertions, 6 deletions
diff --git a/firmware/common/dir.c b/firmware/common/dir.c index c3965c2..6ca3a76 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 0c8997d..bb10c5f 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 db8737c..24c894f 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 ab8e15f..7de0276 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); |