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
This commit is contained in:
Linus Nielsen Feltzing 2004-04-16 08:58:29 +00:00
parent b8beff39e9
commit bc9397d1fa
4 changed files with 54 additions and 6 deletions

View file

@ -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;
}

View file

@ -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"

View file

@ -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);

View file

@ -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);