Björn Stenberg | 4d4ec3a | 2002-05-03 15:36:52 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
Kevin Ferrare | 011a325 | 2007-07-20 17:06:55 +0000 | [diff] [blame] | 8 | * $Id: dir.c 13741 2007-06-30 02:08:27Z jethead71 $ |
Björn Stenberg | 4d4ec3a | 2002-05-03 15:36:52 +0000 | [diff] [blame] | 9 | * |
Nicolas Pennequin | 357ffb3 | 2008-05-05 10:32:46 +0000 | [diff] [blame] | 10 | * Copyright (C) 2002 by Björn Stenberg |
Björn Stenberg | 4d4ec3a | 2002-05-03 15:36:52 +0000 | [diff] [blame] | 11 | * |
Daniel Stenberg | 2acc0ac | 2008-06-28 18:10:04 +0000 | [diff] [blame^] | 12 | * This program is free software; you can redistribute it and/or |
| 13 | * modify it under the terms of the GNU General Public License |
| 14 | * as published by the Free Software Foundation; either version 2 |
| 15 | * of the License, or (at your option) any later version. |
Björn Stenberg | 4d4ec3a | 2002-05-03 15:36:52 +0000 | [diff] [blame] | 16 | * |
| 17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 18 | * KIND, either express or implied. |
| 19 | * |
| 20 | ****************************************************************************/ |
| 21 | #include <stdio.h> |
Björn Stenberg | 3d25f78 | 2002-05-27 09:13:24 +0000 | [diff] [blame] | 22 | #include <errno.h> |
Björn Stenberg | 4d4ec3a | 2002-05-03 15:36:52 +0000 | [diff] [blame] | 23 | #include <string.h> |
Björn Stenberg | 2382044 | 2002-05-13 12:29:34 +0000 | [diff] [blame] | 24 | #include <stdbool.h> |
Bertrik Sikken | 2843469 | 2008-04-28 16:18:04 +0000 | [diff] [blame] | 25 | #include <stdlib.h> |
Björn Stenberg | 4d4ec3a | 2002-05-03 15:36:52 +0000 | [diff] [blame] | 26 | #include "fat.h" |
| 27 | #include "dir.h" |
| 28 | #include "debug.h" |
Björn Stenberg | 4d4ec3a | 2002-05-03 15:36:52 +0000 | [diff] [blame] | 29 | |
Björn Stenberg | 3d25f78 | 2002-05-27 09:13:24 +0000 | [diff] [blame] | 30 | #define MAX_OPEN_DIRS 8 |
| 31 | |
Kevin Ferrare | 011a325 | 2007-07-20 17:06:55 +0000 | [diff] [blame] | 32 | static DIR_UNCACHED opendirs[MAX_OPEN_DIRS]; |
Björn Stenberg | 4d4ec3a | 2002-05-03 15:36:52 +0000 | [diff] [blame] | 33 | |
Jörg Hohensohn | da84857 | 2004-12-28 22:16:07 +0000 | [diff] [blame] | 34 | #ifdef HAVE_MULTIVOLUME |
| 35 | |
Jörg Hohensohn | da84857 | 2004-12-28 22:16:07 +0000 | [diff] [blame] | 36 | /* returns on which volume this is, and copies the reduced name |
| 37 | (sortof a preprocessor for volume-decorated pathnames) */ |
Miika Pekkarinen | 52d827a | 2008-03-11 19:39:26 +0000 | [diff] [blame] | 38 | int strip_volume(const char* name, char* namecopy) |
Jörg Hohensohn | da84857 | 2004-12-28 22:16:07 +0000 | [diff] [blame] | 39 | { |
| 40 | int volume = 0; |
Jens Arnold | f9b2091 | 2006-06-30 05:35:11 +0000 | [diff] [blame] | 41 | const char *temp = name; |
| 42 | |
Jens Arnold | 7bbf53e | 2006-06-30 05:43:08 +0000 | [diff] [blame] | 43 | while (*temp == '/') /* skip all leading slashes */ |
Jens Arnold | f9b2091 | 2006-06-30 05:35:11 +0000 | [diff] [blame] | 44 | ++temp; |
| 45 | |
Miika Pekkarinen | 52d827a | 2008-03-11 19:39:26 +0000 | [diff] [blame] | 46 | if (*temp && !strncmp(temp, VOL_NAMES, VOL_ENUM_POS)) |
Jörg Hohensohn | da84857 | 2004-12-28 22:16:07 +0000 | [diff] [blame] | 47 | { |
Jens Arnold | f9b2091 | 2006-06-30 05:35:11 +0000 | [diff] [blame] | 48 | temp += VOL_ENUM_POS; /* behind special name */ |
| 49 | volume = atoi(temp); /* number is following */ |
Jörg Hohensohn | da84857 | 2004-12-28 22:16:07 +0000 | [diff] [blame] | 50 | temp = strchr(temp, '/'); /* search for slash behind */ |
| 51 | if (temp != NULL) |
Jens Arnold | f9b2091 | 2006-06-30 05:35:11 +0000 | [diff] [blame] | 52 | name = temp; /* use the part behind the volume */ |
Jörg Hohensohn | da84857 | 2004-12-28 22:16:07 +0000 | [diff] [blame] | 53 | else |
Jens Arnold | f9b2091 | 2006-06-30 05:35:11 +0000 | [diff] [blame] | 54 | name = "/"; /* else this must be the root dir */ |
Jörg Hohensohn | da84857 | 2004-12-28 22:16:07 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | strncpy(namecopy, name, MAX_PATH); |
| 58 | namecopy[MAX_PATH-1] = '\0'; |
| 59 | |
| 60 | return volume; |
| 61 | } |
| 62 | #endif /* #ifdef HAVE_MULTIVOLUME */ |
| 63 | |
| 64 | |
Jörg Hohensohn | dc7534b | 2005-01-28 21:32:16 +0000 | [diff] [blame] | 65 | #ifdef HAVE_HOTSWAP |
| 66 | // release all dir handles on a given volume "by force", to avoid leaks |
| 67 | int release_dirs(int volume) |
| 68 | { |
Kevin Ferrare | 011a325 | 2007-07-20 17:06:55 +0000 | [diff] [blame] | 69 | DIR_UNCACHED* pdir = opendirs; |
Jörg Hohensohn | dc7534b | 2005-01-28 21:32:16 +0000 | [diff] [blame] | 70 | int dd; |
| 71 | int closed = 0; |
| 72 | for ( dd=0; dd<MAX_OPEN_DIRS; dd++, pdir++) |
| 73 | { |
| 74 | if (pdir->fatdir.file.volume == volume) |
| 75 | { |
| 76 | pdir->busy = false; /* mark as available, no further action */ |
| 77 | closed++; |
| 78 | } |
| 79 | } |
| 80 | return closed; /* return how many we did */ |
| 81 | } |
| 82 | #endif /* #ifdef HAVE_HOTSWAP */ |
| 83 | |
Kevin Ferrare | 011a325 | 2007-07-20 17:06:55 +0000 | [diff] [blame] | 84 | DIR_UNCACHED* opendir_uncached(const char* name) |
Björn Stenberg | 4d4ec3a | 2002-05-03 15:36:52 +0000 | [diff] [blame] | 85 | { |
Linus Nielsen Feltzing | 377725f | 2002-07-15 22:58:28 +0000 | [diff] [blame] | 86 | char namecopy[MAX_PATH]; |
Björn Stenberg | 4d4ec3a | 2002-05-03 15:36:52 +0000 | [diff] [blame] | 87 | char* part; |
Björn Stenberg | a7b8b00 | 2002-05-24 09:57:33 +0000 | [diff] [blame] | 88 | char* end; |
Björn Stenberg | 4d4ec3a | 2002-05-03 15:36:52 +0000 | [diff] [blame] | 89 | struct fat_direntry entry; |
Björn Stenberg | 3d25f78 | 2002-05-27 09:13:24 +0000 | [diff] [blame] | 90 | int dd; |
Kevin Ferrare | 011a325 | 2007-07-20 17:06:55 +0000 | [diff] [blame] | 91 | DIR_UNCACHED* pdir = opendirs; |
Jörg Hohensohn | da84857 | 2004-12-28 22:16:07 +0000 | [diff] [blame] | 92 | #ifdef HAVE_MULTIVOLUME |
| 93 | int volume; |
| 94 | #endif |
Björn Stenberg | 4d4ec3a | 2002-05-03 15:36:52 +0000 | [diff] [blame] | 95 | |
Jörg Hohensohn | dcdffe8 | 2005-01-03 20:23:42 +0000 | [diff] [blame] | 96 | if ( name[0] != '/' ) { |
| 97 | DEBUGF("Only absolute paths supported right now\n"); |
| 98 | return NULL; |
| 99 | } |
| 100 | |
Björn Stenberg | 3d25f78 | 2002-05-27 09:13:24 +0000 | [diff] [blame] | 101 | /* find a free dir descriptor */ |
Jörg Hohensohn | dcdffe8 | 2005-01-03 20:23:42 +0000 | [diff] [blame] | 102 | for ( dd=0; dd<MAX_OPEN_DIRS; dd++, pdir++) |
| 103 | if ( !pdir->busy ) |
Björn Stenberg | 3d25f78 | 2002-05-27 09:13:24 +0000 | [diff] [blame] | 104 | break; |
| 105 | |
| 106 | if ( dd == MAX_OPEN_DIRS ) { |
| 107 | DEBUGF("Too many dirs open\n"); |
| 108 | errno = EMFILE; |
Björn Stenberg | 4d4ec3a | 2002-05-03 15:36:52 +0000 | [diff] [blame] | 109 | return NULL; |
| 110 | } |
| 111 | |
Jörg Hohensohn | dcdffe8 | 2005-01-03 20:23:42 +0000 | [diff] [blame] | 112 | pdir->busy = true; |
Björn Stenberg | 4d4ec3a | 2002-05-03 15:36:52 +0000 | [diff] [blame] | 113 | |
Jörg Hohensohn | da84857 | 2004-12-28 22:16:07 +0000 | [diff] [blame] | 114 | #ifdef HAVE_MULTIVOLUME |
| 115 | /* try to extract a heading volume name, if present */ |
| 116 | volume = strip_volume(name, namecopy); |
Jörg Hohensohn | dcdffe8 | 2005-01-03 20:23:42 +0000 | [diff] [blame] | 117 | pdir->volumecounter = 0; |
Jörg Hohensohn | da84857 | 2004-12-28 22:16:07 +0000 | [diff] [blame] | 118 | #else |
| 119 | strncpy(namecopy,name,sizeof(namecopy)); /* just copy */ |
| 120 | namecopy[sizeof(namecopy)-1] = '\0'; |
| 121 | #endif |
| 122 | |
Jörg Hohensohn | dcdffe8 | 2005-01-03 20:23:42 +0000 | [diff] [blame] | 123 | if ( fat_opendir(IF_MV2(volume,) &pdir->fatdir, 0, NULL) < 0 ) { |
Björn Stenberg | 4d4ec3a | 2002-05-03 15:36:52 +0000 | [diff] [blame] | 124 | DEBUGF("Failed opening root dir\n"); |
Jörg Hohensohn | dcdffe8 | 2005-01-03 20:23:42 +0000 | [diff] [blame] | 125 | pdir->busy = false; |
Björn Stenberg | 4d4ec3a | 2002-05-03 15:36:52 +0000 | [diff] [blame] | 126 | return NULL; |
| 127 | } |
| 128 | |
Björn Stenberg | a7b8b00 | 2002-05-24 09:57:33 +0000 | [diff] [blame] | 129 | for ( part = strtok_r(namecopy, "/", &end); part; |
| 130 | part = strtok_r(NULL, "/", &end)) { |
Björn Stenberg | 4d4ec3a | 2002-05-03 15:36:52 +0000 | [diff] [blame] | 131 | /* scan dir for name */ |
| 132 | while (1) { |
Jörg Hohensohn | dcdffe8 | 2005-01-03 20:23:42 +0000 | [diff] [blame] | 133 | if ((fat_getnext(&pdir->fatdir,&entry) < 0) || |
Björn Stenberg | 3d25f78 | 2002-05-27 09:13:24 +0000 | [diff] [blame] | 134 | (!entry.name[0])) { |
Jörg Hohensohn | dcdffe8 | 2005-01-03 20:23:42 +0000 | [diff] [blame] | 135 | pdir->busy = false; |
Björn Stenberg | 4d4ec3a | 2002-05-03 15:36:52 +0000 | [diff] [blame] | 136 | return NULL; |
Björn Stenberg | 3d25f78 | 2002-05-27 09:13:24 +0000 | [diff] [blame] | 137 | } |
Björn Stenberg | 4d4ec3a | 2002-05-03 15:36:52 +0000 | [diff] [blame] | 138 | if ( (entry.attr & FAT_ATTR_DIRECTORY) && |
Linus Nielsen Feltzing | fae5b82 | 2002-08-15 11:26:12 +0000 | [diff] [blame] | 139 | (!strcasecmp(part, entry.name)) ) { |
Jörg Hohensohn | dcdffe8 | 2005-01-03 20:23:42 +0000 | [diff] [blame] | 140 | pdir->parent_dir = pdir->fatdir; |
Jörg Hohensohn | da84857 | 2004-12-28 22:16:07 +0000 | [diff] [blame] | 141 | if ( fat_opendir(IF_MV2(volume,) |
Jörg Hohensohn | dcdffe8 | 2005-01-03 20:23:42 +0000 | [diff] [blame] | 142 | &pdir->fatdir, |
Linus Nielsen Feltzing | bc9397d | 2004-04-16 08:58:29 +0000 | [diff] [blame] | 143 | entry.firstcluster, |
Jörg Hohensohn | dcdffe8 | 2005-01-03 20:23:42 +0000 | [diff] [blame] | 144 | &pdir->parent_dir) < 0 ) { |
Jean-Philippe Bernardy | cea551d | 2005-01-23 23:29:35 +0000 | [diff] [blame] | 145 | DEBUGF("Failed opening dir '%s' (%ld)\n", |
Björn Stenberg | 4d4ec3a | 2002-05-03 15:36:52 +0000 | [diff] [blame] | 146 | part, entry.firstcluster); |
Jörg Hohensohn | dcdffe8 | 2005-01-03 20:23:42 +0000 | [diff] [blame] | 147 | pdir->busy = false; |
Björn Stenberg | 4d4ec3a | 2002-05-03 15:36:52 +0000 | [diff] [blame] | 148 | return NULL; |
| 149 | } |
Jörg Hohensohn | da84857 | 2004-12-28 22:16:07 +0000 | [diff] [blame] | 150 | #ifdef HAVE_MULTIVOLUME |
Jörg Hohensohn | dcdffe8 | 2005-01-03 20:23:42 +0000 | [diff] [blame] | 151 | pdir->volumecounter = -1; /* n.a. to subdirs */ |
Jörg Hohensohn | da84857 | 2004-12-28 22:16:07 +0000 | [diff] [blame] | 152 | #endif |
Björn Stenberg | 4d4ec3a | 2002-05-03 15:36:52 +0000 | [diff] [blame] | 153 | break; |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | |
Jörg Hohensohn | dcdffe8 | 2005-01-03 20:23:42 +0000 | [diff] [blame] | 158 | return pdir; |
Björn Stenberg | 4d4ec3a | 2002-05-03 15:36:52 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Kevin Ferrare | 011a325 | 2007-07-20 17:06:55 +0000 | [diff] [blame] | 161 | int closedir_uncached(DIR_UNCACHED* dir) |
Björn Stenberg | 4d4ec3a | 2002-05-03 15:36:52 +0000 | [diff] [blame] | 162 | { |
Björn Stenberg | 3d25f78 | 2002-05-27 09:13:24 +0000 | [diff] [blame] | 163 | dir->busy=false; |
Björn Stenberg | 4d4ec3a | 2002-05-03 15:36:52 +0000 | [diff] [blame] | 164 | return 0; |
| 165 | } |
| 166 | |
Kevin Ferrare | 011a325 | 2007-07-20 17:06:55 +0000 | [diff] [blame] | 167 | struct dirent_uncached* readdir_uncached(DIR_UNCACHED* dir) |
Björn Stenberg | 4d4ec3a | 2002-05-03 15:36:52 +0000 | [diff] [blame] | 168 | { |
| 169 | struct fat_direntry entry; |
Kevin Ferrare | 011a325 | 2007-07-20 17:06:55 +0000 | [diff] [blame] | 170 | struct dirent_uncached* theent = &(dir->theent); |
Jörg Hohensohn | dc7534b | 2005-01-28 21:32:16 +0000 | [diff] [blame] | 171 | |
| 172 | if (!dir->busy) |
| 173 | return NULL; |
| 174 | |
Jörg Hohensohn | da84857 | 2004-12-28 22:16:07 +0000 | [diff] [blame] | 175 | #ifdef HAVE_MULTIVOLUME |
| 176 | /* Volumes (secondary file systems) get inserted into the root directory |
| 177 | of the first volume, since we have no separate top level. */ |
| 178 | if (dir->volumecounter >= 0 /* on a root dir */ |
| 179 | && dir->volumecounter < NUM_VOLUMES /* in range */ |
| 180 | && dir->fatdir.file.volume == 0) /* at volume 0 */ |
| 181 | { /* fake special directories, which don't really exist, but |
Kevin Ferrare | 011a325 | 2007-07-20 17:06:55 +0000 | [diff] [blame] | 182 | will get redirected upon opendir_uncached() */ |
Jörg Hohensohn | da84857 | 2004-12-28 22:16:07 +0000 | [diff] [blame] | 183 | while (++dir->volumecounter < NUM_VOLUMES) |
| 184 | { |
| 185 | if (fat_ismounted(dir->volumecounter)) |
| 186 | { |
| 187 | memset(theent, 0, sizeof(*theent)); |
| 188 | theent->attribute = FAT_ATTR_DIRECTORY | FAT_ATTR_VOLUME; |
| 189 | snprintf(theent->d_name, sizeof(theent->d_name), |
Miika Pekkarinen | 52d827a | 2008-03-11 19:39:26 +0000 | [diff] [blame] | 190 | VOL_NAMES, dir->volumecounter); |
Jörg Hohensohn | da84857 | 2004-12-28 22:16:07 +0000 | [diff] [blame] | 191 | return theent; |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | #endif |
| 196 | /* normal directory entry fetching follows here */ |
Björn Stenberg | 4d4ec3a | 2002-05-03 15:36:52 +0000 | [diff] [blame] | 197 | if (fat_getnext(&(dir->fatdir),&entry) < 0) |
| 198 | return NULL; |
| 199 | |
| 200 | if ( !entry.name[0] ) |
Björn Stenberg | b7b48fe | 2002-10-20 22:50:58 +0000 | [diff] [blame] | 201 | return NULL; |
Björn Stenberg | 4d4ec3a | 2002-05-03 15:36:52 +0000 | [diff] [blame] | 202 | |
Björn Stenberg | 3d25f78 | 2002-05-27 09:13:24 +0000 | [diff] [blame] | 203 | strncpy(theent->d_name, entry.name, sizeof( theent->d_name ) ); |
| 204 | theent->attribute = entry.attr; |
| 205 | theent->size = entry.filesize; |
| 206 | theent->startcluster = entry.firstcluster; |
Jörg Hohensohn | 74eb642 | 2004-06-20 16:34:29 +0000 | [diff] [blame] | 207 | theent->wrtdate = entry.wrtdate; |
| 208 | theent->wrttime = entry.wrttime; |
Björn Stenberg | 4d4ec3a | 2002-05-03 15:36:52 +0000 | [diff] [blame] | 209 | |
Björn Stenberg | 3d25f78 | 2002-05-27 09:13:24 +0000 | [diff] [blame] | 210 | return theent; |
Björn Stenberg | 4d4ec3a | 2002-05-03 15:36:52 +0000 | [diff] [blame] | 211 | } |
Linus Nielsen Feltzing | 60b1c4b | 2004-01-15 14:30:59 +0000 | [diff] [blame] | 212 | |
Kevin Ferrare | 011a325 | 2007-07-20 17:06:55 +0000 | [diff] [blame] | 213 | int mkdir_uncached(const char *name) |
Linus Nielsen Feltzing | 60b1c4b | 2004-01-15 14:30:59 +0000 | [diff] [blame] | 214 | { |
Kevin Ferrare | 011a325 | 2007-07-20 17:06:55 +0000 | [diff] [blame] | 215 | DIR_UNCACHED *dir; |
Linus Nielsen Feltzing | 60b1c4b | 2004-01-15 14:30:59 +0000 | [diff] [blame] | 216 | char namecopy[MAX_PATH]; |
| 217 | char* end; |
| 218 | char *basename; |
| 219 | char *parent; |
Kevin Ferrare | 011a325 | 2007-07-20 17:06:55 +0000 | [diff] [blame] | 220 | struct dirent_uncached *entry; |
Linus Nielsen Feltzing | 60b1c4b | 2004-01-15 14:30:59 +0000 | [diff] [blame] | 221 | struct fat_dir newdir; |
| 222 | int rc; |
Linus Nielsen Feltzing | ef7293f | 2004-01-21 14:58:40 +0000 | [diff] [blame] | 223 | |
Linus Nielsen Feltzing | 60b1c4b | 2004-01-15 14:30:59 +0000 | [diff] [blame] | 224 | if ( name[0] != '/' ) { |
Linus Nielsen Feltzing | 05f1fc4 | 2004-02-11 14:37:16 +0000 | [diff] [blame] | 225 | DEBUGF("mkdir: Only absolute paths supported right now\n"); |
Linus Nielsen Feltzing | 60b1c4b | 2004-01-15 14:30:59 +0000 | [diff] [blame] | 226 | return -1; |
| 227 | } |
| 228 | |
| 229 | strncpy(namecopy,name,sizeof(namecopy)); |
| 230 | namecopy[sizeof(namecopy)-1] = 0; |
| 231 | |
| 232 | /* Split the base name and the path */ |
| 233 | end = strrchr(namecopy, '/'); |
| 234 | *end = 0; |
| 235 | basename = end+1; |
| 236 | |
| 237 | if(namecopy == end) /* Root dir? */ |
| 238 | parent = "/"; |
| 239 | else |
| 240 | parent = namecopy; |
| 241 | |
| 242 | DEBUGF("mkdir: parent: %s, name: %s\n", parent, basename); |
| 243 | |
Kevin Ferrare | 011a325 | 2007-07-20 17:06:55 +0000 | [diff] [blame] | 244 | dir = opendir_uncached(parent); |
Linus Nielsen Feltzing | 60b1c4b | 2004-01-15 14:30:59 +0000 | [diff] [blame] | 245 | |
| 246 | if(!dir) { |
| 247 | DEBUGF("mkdir: can't open parent dir\n"); |
| 248 | return -2; |
| 249 | } |
| 250 | |
Linus Nielsen Feltzing | 05f1fc4 | 2004-02-11 14:37:16 +0000 | [diff] [blame] | 251 | if(basename[0] == 0) { |
| 252 | DEBUGF("mkdir: Empty dir name\n"); |
| 253 | errno = EINVAL; |
| 254 | return -3; |
| 255 | } |
| 256 | |
Linus Nielsen Feltzing | 60b1c4b | 2004-01-15 14:30:59 +0000 | [diff] [blame] | 257 | /* Now check if the name already exists */ |
Kevin Ferrare | 011a325 | 2007-07-20 17:06:55 +0000 | [diff] [blame] | 258 | while ((entry = readdir_uncached(dir))) { |
Linus Nielsen Feltzing | 60b1c4b | 2004-01-15 14:30:59 +0000 | [diff] [blame] | 259 | if ( !strcasecmp(basename, entry->d_name) ) { |
| 260 | DEBUGF("mkdir error: file exists\n"); |
| 261 | errno = EEXIST; |
Kevin Ferrare | 011a325 | 2007-07-20 17:06:55 +0000 | [diff] [blame] | 262 | closedir_uncached(dir); |
Linus Nielsen Feltzing | 05f1fc4 | 2004-02-11 14:37:16 +0000 | [diff] [blame] | 263 | return - 4; |
Linus Nielsen Feltzing | 60b1c4b | 2004-01-15 14:30:59 +0000 | [diff] [blame] | 264 | } |
| 265 | } |
Linus Nielsen Feltzing | ce1c93d | 2004-01-15 14:34:54 +0000 | [diff] [blame] | 266 | |
Linus Nielsen Feltzing | 54353e0 | 2004-01-15 15:56:19 +0000 | [diff] [blame] | 267 | memset(&newdir, sizeof(struct fat_dir), 0); |
Linus Nielsen Feltzing | 60b1c4b | 2004-01-15 14:30:59 +0000 | [diff] [blame] | 268 | |
| 269 | rc = fat_create_dir(basename, &newdir, &(dir->fatdir)); |
Kevin Ferrare | 011a325 | 2007-07-20 17:06:55 +0000 | [diff] [blame] | 270 | closedir_uncached(dir); |
Linus Nielsen Feltzing | 54353e0 | 2004-01-15 15:56:19 +0000 | [diff] [blame] | 271 | |
Linus Nielsen Feltzing | 60b1c4b | 2004-01-15 14:30:59 +0000 | [diff] [blame] | 272 | return rc; |
| 273 | } |
Linus Nielsen Feltzing | bc9397d | 2004-04-16 08:58:29 +0000 | [diff] [blame] | 274 | |
Kevin Ferrare | 011a325 | 2007-07-20 17:06:55 +0000 | [diff] [blame] | 275 | int rmdir_uncached(const char* name) |
Linus Nielsen Feltzing | bc9397d | 2004-04-16 08:58:29 +0000 | [diff] [blame] | 276 | { |
| 277 | int rc; |
Kevin Ferrare | 011a325 | 2007-07-20 17:06:55 +0000 | [diff] [blame] | 278 | DIR_UNCACHED* dir; |
| 279 | struct dirent_uncached* entry; |
Linus Nielsen Feltzing | bc9397d | 2004-04-16 08:58:29 +0000 | [diff] [blame] | 280 | |
Kevin Ferrare | 011a325 | 2007-07-20 17:06:55 +0000 | [diff] [blame] | 281 | dir = opendir_uncached(name); |
Linus Nielsen Feltzing | bc9397d | 2004-04-16 08:58:29 +0000 | [diff] [blame] | 282 | if (!dir) |
| 283 | { |
| 284 | errno = ENOENT; /* open error */ |
| 285 | return -1; |
| 286 | } |
| 287 | |
| 288 | /* check if the directory is empty */ |
Kevin Ferrare | 011a325 | 2007-07-20 17:06:55 +0000 | [diff] [blame] | 289 | while ((entry = readdir_uncached(dir))) |
Linus Nielsen Feltzing | bc9397d | 2004-04-16 08:58:29 +0000 | [diff] [blame] | 290 | { |
| 291 | if (strcmp(entry->d_name, ".") && |
| 292 | strcmp(entry->d_name, "..")) |
| 293 | { |
| 294 | DEBUGF("rmdir error: not empty\n"); |
| 295 | errno = ENOTEMPTY; |
Kevin Ferrare | 011a325 | 2007-07-20 17:06:55 +0000 | [diff] [blame] | 296 | closedir_uncached(dir); |
Linus Nielsen Feltzing | bc9397d | 2004-04-16 08:58:29 +0000 | [diff] [blame] | 297 | return -2; |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | rc = fat_remove(&(dir->fatdir.file)); |
| 302 | if ( rc < 0 ) { |
| 303 | DEBUGF("Failed removing dir: %d\n", rc); |
| 304 | errno = EIO; |
| 305 | rc = rc * 10 - 3; |
| 306 | } |
| 307 | |
Kevin Ferrare | 011a325 | 2007-07-20 17:06:55 +0000 | [diff] [blame] | 308 | closedir_uncached(dir); |
Linus Nielsen Feltzing | bc9397d | 2004-04-16 08:58:29 +0000 | [diff] [blame] | 309 | return rc; |
| 310 | } |