Colour targets: Revert an optimisation from almost 18 months ago that actually turned...
[Rockbox.git] / firmware / common / dir_uncached.c
blobe036c9584020910f2f91963e5c162413207f6e4b
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id: dir.c 13741 2007-06-30 02:08:27Z jethead71 $
10 * Copyright (C) 2002 by Björn Stenberg
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.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
21 #include <stdio.h>
22 #include <errno.h>
23 #include <string.h>
24 #include <stdbool.h>
25 #include <stdlib.h>
26 #include "fat.h"
27 #include "dir.h"
28 #include "debug.h"
30 #define MAX_OPEN_DIRS 8
32 static DIR_UNCACHED opendirs[MAX_OPEN_DIRS];
34 #ifdef HAVE_MULTIVOLUME
36 /* returns on which volume this is, and copies the reduced name
37 (sortof a preprocessor for volume-decorated pathnames) */
38 int strip_volume(const char* name, char* namecopy)
40 int volume = 0;
41 const char *temp = name;
43 while (*temp == '/') /* skip all leading slashes */
44 ++temp;
46 if (*temp && !strncmp(temp, VOL_NAMES, VOL_ENUM_POS))
48 temp += VOL_ENUM_POS; /* behind special name */
49 volume = atoi(temp); /* number is following */
50 temp = strchr(temp, '/'); /* search for slash behind */
51 if (temp != NULL)
52 name = temp; /* use the part behind the volume */
53 else
54 name = "/"; /* else this must be the root dir */
57 strncpy(namecopy, name, MAX_PATH);
58 namecopy[MAX_PATH-1] = '\0';
60 return volume;
62 #endif /* #ifdef HAVE_MULTIVOLUME */
65 #ifdef HAVE_HOTSWAP
66 // release all dir handles on a given volume "by force", to avoid leaks
67 int release_dirs(int volume)
69 DIR_UNCACHED* pdir = opendirs;
70 int dd;
71 int closed = 0;
72 for ( dd=0; dd<MAX_OPEN_DIRS; dd++, pdir++)
74 if (pdir->fatdir.file.volume == volume)
76 pdir->busy = false; /* mark as available, no further action */
77 closed++;
80 return closed; /* return how many we did */
82 #endif /* #ifdef HAVE_HOTSWAP */
84 DIR_UNCACHED* opendir_uncached(const char* name)
86 char namecopy[MAX_PATH];
87 char* part;
88 char* end;
89 struct fat_direntry entry;
90 int dd;
91 DIR_UNCACHED* pdir = opendirs;
92 #ifdef HAVE_MULTIVOLUME
93 int volume;
94 #endif
96 if ( name[0] != '/' ) {
97 DEBUGF("Only absolute paths supported right now\n");
98 return NULL;
101 /* find a free dir descriptor */
102 for ( dd=0; dd<MAX_OPEN_DIRS; dd++, pdir++)
103 if ( !pdir->busy )
104 break;
106 if ( dd == MAX_OPEN_DIRS ) {
107 DEBUGF("Too many dirs open\n");
108 errno = EMFILE;
109 return NULL;
112 pdir->busy = true;
114 #ifdef HAVE_MULTIVOLUME
115 /* try to extract a heading volume name, if present */
116 volume = strip_volume(name, namecopy);
117 pdir->volumecounter = 0;
118 #else
119 strncpy(namecopy,name,sizeof(namecopy)); /* just copy */
120 namecopy[sizeof(namecopy)-1] = '\0';
121 #endif
123 if ( fat_opendir(IF_MV2(volume,) &pdir->fatdir, 0, NULL) < 0 ) {
124 DEBUGF("Failed opening root dir\n");
125 pdir->busy = false;
126 return NULL;
129 for ( part = strtok_r(namecopy, "/", &end); part;
130 part = strtok_r(NULL, "/", &end)) {
131 /* scan dir for name */
132 while (1) {
133 if ((fat_getnext(&pdir->fatdir,&entry) < 0) ||
134 (!entry.name[0])) {
135 pdir->busy = false;
136 return NULL;
138 if ( (entry.attr & FAT_ATTR_DIRECTORY) &&
139 (!strcasecmp(part, entry.name)) ) {
140 pdir->parent_dir = pdir->fatdir;
141 if ( fat_opendir(IF_MV2(volume,)
142 &pdir->fatdir,
143 entry.firstcluster,
144 &pdir->parent_dir) < 0 ) {
145 DEBUGF("Failed opening dir '%s' (%ld)\n",
146 part, entry.firstcluster);
147 pdir->busy = false;
148 return NULL;
150 #ifdef HAVE_MULTIVOLUME
151 pdir->volumecounter = -1; /* n.a. to subdirs */
152 #endif
153 break;
158 return pdir;
161 int closedir_uncached(DIR_UNCACHED* dir)
163 dir->busy=false;
164 return 0;
167 struct dirent_uncached* readdir_uncached(DIR_UNCACHED* dir)
169 struct fat_direntry entry;
170 struct dirent_uncached* theent = &(dir->theent);
172 if (!dir->busy)
173 return NULL;
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
182 will get redirected upon opendir_uncached() */
183 while (++dir->volumecounter < NUM_VOLUMES)
185 if (fat_ismounted(dir->volumecounter))
187 memset(theent, 0, sizeof(*theent));
188 theent->attribute = FAT_ATTR_DIRECTORY | FAT_ATTR_VOLUME;
189 snprintf(theent->d_name, sizeof(theent->d_name),
190 VOL_NAMES, dir->volumecounter);
191 return theent;
195 #endif
196 /* normal directory entry fetching follows here */
197 if (fat_getnext(&(dir->fatdir),&entry) < 0)
198 return NULL;
200 if ( !entry.name[0] )
201 return NULL;
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;
207 theent->wrtdate = entry.wrtdate;
208 theent->wrttime = entry.wrttime;
210 return theent;
213 int mkdir_uncached(const char *name)
215 DIR_UNCACHED *dir;
216 char namecopy[MAX_PATH];
217 char* end;
218 char *basename;
219 char *parent;
220 struct dirent_uncached *entry;
221 struct fat_dir newdir;
222 int rc;
224 if ( name[0] != '/' ) {
225 DEBUGF("mkdir: Only absolute paths supported right now\n");
226 return -1;
229 strncpy(namecopy,name,sizeof(namecopy));
230 namecopy[sizeof(namecopy)-1] = 0;
232 /* Split the base name and the path */
233 end = strrchr(namecopy, '/');
234 *end = 0;
235 basename = end+1;
237 if(namecopy == end) /* Root dir? */
238 parent = "/";
239 else
240 parent = namecopy;
242 DEBUGF("mkdir: parent: %s, name: %s\n", parent, basename);
244 dir = opendir_uncached(parent);
246 if(!dir) {
247 DEBUGF("mkdir: can't open parent dir\n");
248 return -2;
251 if(basename[0] == 0) {
252 DEBUGF("mkdir: Empty dir name\n");
253 errno = EINVAL;
254 return -3;
257 /* Now check if the name already exists */
258 while ((entry = readdir_uncached(dir))) {
259 if ( !strcasecmp(basename, entry->d_name) ) {
260 DEBUGF("mkdir error: file exists\n");
261 errno = EEXIST;
262 closedir_uncached(dir);
263 return - 4;
267 memset(&newdir, sizeof(struct fat_dir), 0);
269 rc = fat_create_dir(basename, &newdir, &(dir->fatdir));
270 closedir_uncached(dir);
272 return rc;
275 int rmdir_uncached(const char* name)
277 int rc;
278 DIR_UNCACHED* dir;
279 struct dirent_uncached* entry;
281 dir = opendir_uncached(name);
282 if (!dir)
284 errno = ENOENT; /* open error */
285 return -1;
288 /* check if the directory is empty */
289 while ((entry = readdir_uncached(dir)))
291 if (strcmp(entry->d_name, ".") &&
292 strcmp(entry->d_name, ".."))
294 DEBUGF("rmdir error: not empty\n");
295 errno = ENOTEMPTY;
296 closedir_uncached(dir);
297 return -2;
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;
308 closedir_uncached(dir);
309 return rc;