FS#8961 - Anti-Aliased Fonts.
[kugel-rb.git] / firmware / common / dir_uncached.c
blob25677a0903b3aa5f9295ec8db10464965b1b0a50
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 #if ((defined(MEMORYSIZE) && (MEMORYSIZE > 8)) || MEM > 8)
31 #define MAX_OPEN_DIRS 12
32 #else
33 #define MAX_OPEN_DIRS 8
34 #endif
36 static DIR_UNCACHED opendirs[MAX_OPEN_DIRS];
38 #ifdef HAVE_MULTIVOLUME
40 /* returns on which volume this is, and copies the reduced name
41 (sortof a preprocessor for volume-decorated pathnames) */
42 int strip_volume(const char* name, char* namecopy)
44 int volume = 0;
45 const char *temp = name;
47 while (*temp == '/') /* skip all leading slashes */
48 ++temp;
50 if (*temp && !strncmp(temp, VOL_NAMES, VOL_ENUM_POS))
52 temp += VOL_ENUM_POS; /* behind special name */
53 volume = atoi(temp); /* number is following */
54 temp = strchr(temp, '/'); /* search for slash behind */
55 if (temp != NULL)
56 name = temp; /* use the part behind the volume */
57 else
58 name = "/"; /* else this must be the root dir */
61 strlcpy(namecopy, name, MAX_PATH);
63 return volume;
65 #endif /* #ifdef HAVE_MULTIVOLUME */
68 #ifdef HAVE_HOTSWAP
69 // release all dir handles on a given volume "by force", to avoid leaks
70 int release_dirs(int volume)
72 DIR_UNCACHED* pdir = opendirs;
73 int dd;
74 int closed = 0;
75 for ( dd=0; dd<MAX_OPEN_DIRS; dd++, pdir++)
77 if (pdir->fatdir.file.volume == volume)
79 pdir->busy = false; /* mark as available, no further action */
80 closed++;
83 return closed; /* return how many we did */
85 #endif /* #ifdef HAVE_HOTSWAP */
87 DIR_UNCACHED* opendir_uncached(const char* name)
89 char namecopy[MAX_PATH];
90 char* part;
91 char* end;
92 struct fat_direntry entry;
93 int dd;
94 DIR_UNCACHED* pdir = opendirs;
95 #ifdef HAVE_MULTIVOLUME
96 int volume;
97 #endif
99 if ( name[0] != '/' ) {
100 DEBUGF("Only absolute paths supported right now\n");
101 return NULL;
104 /* find a free dir descriptor */
105 for ( dd=0; dd<MAX_OPEN_DIRS; dd++, pdir++)
106 if ( !pdir->busy )
107 break;
109 if ( dd == MAX_OPEN_DIRS ) {
110 DEBUGF("Too many dirs open\n");
111 errno = EMFILE;
112 return NULL;
115 pdir->busy = true;
117 #ifdef HAVE_MULTIVOLUME
118 /* try to extract a heading volume name, if present */
119 volume = strip_volume(name, namecopy);
120 pdir->volumecounter = 0;
121 #else
122 strlcpy(namecopy, name, sizeof(namecopy)); /* just copy */
123 #endif
125 if ( fat_opendir(IF_MV2(volume,) &pdir->fatdir, 0, NULL) < 0 ) {
126 DEBUGF("Failed opening root dir\n");
127 pdir->busy = false;
128 return NULL;
131 for ( part = strtok_r(namecopy, "/", &end); part;
132 part = strtok_r(NULL, "/", &end)) {
133 /* scan dir for name */
134 while (1) {
135 if ((fat_getnext(&pdir->fatdir,&entry) < 0) ||
136 (!entry.name[0])) {
137 pdir->busy = false;
138 return NULL;
140 if ( (entry.attr & FAT_ATTR_DIRECTORY) &&
141 (!strcasecmp(part, entry.name)) ) {
142 pdir->parent_dir = pdir->fatdir;
143 if ( fat_opendir(IF_MV2(volume,)
144 &pdir->fatdir,
145 entry.firstcluster,
146 &pdir->parent_dir) < 0 ) {
147 DEBUGF("Failed opening dir '%s' (%ld)\n",
148 part, entry.firstcluster);
149 pdir->busy = false;
150 return NULL;
152 #ifdef HAVE_MULTIVOLUME
153 pdir->volumecounter = -1; /* n.a. to subdirs */
154 #endif
155 break;
160 return pdir;
163 int closedir_uncached(DIR_UNCACHED* dir)
165 dir->busy=false;
166 return 0;
169 struct dirent_uncached* readdir_uncached(DIR_UNCACHED* dir)
171 struct fat_direntry entry;
172 struct dirent_uncached* theent = &(dir->theent);
174 if (!dir->busy)
175 return NULL;
177 #ifdef HAVE_MULTIVOLUME
178 /* Volumes (secondary file systems) get inserted into the root directory
179 of the first volume, since we have no separate top level. */
180 if (dir->volumecounter >= 0 /* on a root dir */
181 && dir->volumecounter < NUM_VOLUMES /* in range */
182 && dir->fatdir.file.volume == 0) /* at volume 0 */
183 { /* fake special directories, which don't really exist, but
184 will get redirected upon opendir_uncached() */
185 while (++dir->volumecounter < NUM_VOLUMES)
187 if (fat_ismounted(dir->volumecounter))
189 memset(theent, 0, sizeof(*theent));
190 theent->attribute = FAT_ATTR_DIRECTORY | FAT_ATTR_VOLUME;
191 snprintf(theent->d_name, sizeof(theent->d_name),
192 VOL_NAMES, dir->volumecounter);
193 return theent;
197 #endif
198 /* normal directory entry fetching follows here */
199 if (fat_getnext(&(dir->fatdir),&entry) < 0)
200 return NULL;
202 if ( !entry.name[0] )
203 return NULL;
205 strlcpy(theent->d_name, entry.name, sizeof(theent->d_name));
206 theent->attribute = entry.attr;
207 theent->size = entry.filesize;
208 theent->startcluster = entry.firstcluster;
209 theent->wrtdate = entry.wrtdate;
210 theent->wrttime = entry.wrttime;
212 return theent;
215 int mkdir_uncached(const char *name)
217 DIR_UNCACHED *dir;
218 char namecopy[MAX_PATH];
219 char* end;
220 char *basename;
221 char *parent;
222 struct dirent_uncached *entry;
223 struct fat_dir newdir;
224 int rc;
226 if ( name[0] != '/' ) {
227 DEBUGF("mkdir: Only absolute paths supported right now\n");
228 return -1;
231 strlcpy(namecopy, name, sizeof(namecopy));
233 /* Split the base name and the path */
234 end = strrchr(namecopy, '/');
235 *end = 0;
236 basename = end+1;
238 if(namecopy == end) /* Root dir? */
239 parent = "/";
240 else
241 parent = namecopy;
243 DEBUGF("mkdir: parent: %s, name: %s\n", parent, basename);
245 dir = opendir_uncached(parent);
247 if(!dir) {
248 DEBUGF("mkdir: can't open parent dir\n");
249 return -2;
252 if(basename[0] == 0) {
253 DEBUGF("mkdir: Empty dir name\n");
254 errno = EINVAL;
255 return -3;
258 /* Now check if the name already exists */
259 while ((entry = readdir_uncached(dir))) {
260 if ( !strcasecmp(basename, entry->d_name) ) {
261 DEBUGF("mkdir error: file exists\n");
262 errno = EEXIST;
263 closedir_uncached(dir);
264 return - 4;
268 memset(&newdir, 0, sizeof(struct fat_dir));
270 rc = fat_create_dir(basename, &newdir, &(dir->fatdir));
271 closedir_uncached(dir);
273 return rc;
276 int rmdir_uncached(const char* name)
278 int rc;
279 DIR_UNCACHED* dir;
280 struct dirent_uncached* entry;
282 dir = opendir_uncached(name);
283 if (!dir)
285 errno = ENOENT; /* open error */
286 return -1;
289 /* check if the directory is empty */
290 while ((entry = readdir_uncached(dir)))
292 if (strcmp(entry->d_name, ".") &&
293 strcmp(entry->d_name, ".."))
295 DEBUGF("rmdir error: not empty\n");
296 errno = ENOTEMPTY;
297 closedir_uncached(dir);
298 return -2;
302 rc = fat_remove(&(dir->fatdir.file));
303 if ( rc < 0 ) {
304 DEBUGF("Failed removing dir: %d\n", rc);
305 errno = EIO;
306 rc = rc * 10 - 3;
309 closedir_uncached(dir);
310 return rc;