Fix FS#111395. Call settings_apply_skins() after changing theme colours.
[kugel-rb.git] / firmware / common / dir_uncached.c
blobe3e33a4c112a68bb02f9c55f68f888f799fd0318
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"
29 #include "filefuncs.h"
31 #if ((defined(MEMORYSIZE) && (MEMORYSIZE > 8)) || MEM > 8)
32 #define MAX_OPEN_DIRS 12
33 #else
34 #define MAX_OPEN_DIRS 8
35 #endif
37 static DIR_UNCACHED opendirs[MAX_OPEN_DIRS];
39 #ifdef HAVE_HOTSWAP
40 // release all dir handles on a given volume "by force", to avoid leaks
41 int release_dirs(int volume)
43 DIR_UNCACHED* pdir = opendirs;
44 int dd;
45 int closed = 0;
46 for ( dd=0; dd<MAX_OPEN_DIRS; dd++, pdir++)
48 #ifdef HAVE_MULTIVOLUME
49 if (pdir->fatdir.file.volume == volume)
50 #else
51 (void)volume;
52 #endif
54 pdir->busy = false; /* mark as available, no further action */
55 closed++;
58 return closed; /* return how many we did */
60 #endif /* #ifdef HAVE_HOTSWAP */
62 DIR_UNCACHED* opendir_uncached(const char* name)
64 char namecopy[MAX_PATH];
65 char* part;
66 char* end;
67 struct fat_direntry entry;
68 int dd;
69 DIR_UNCACHED* pdir = opendirs;
70 #ifdef HAVE_MULTIVOLUME
71 int volume;
72 #endif
74 if ( name[0] != '/' ) {
75 DEBUGF("Only absolute paths supported right now\n");
76 return NULL;
79 /* find a free dir descriptor */
80 for ( dd=0; dd<MAX_OPEN_DIRS; dd++, pdir++)
81 if ( !pdir->busy )
82 break;
84 if ( dd == MAX_OPEN_DIRS ) {
85 DEBUGF("Too many dirs open\n");
86 errno = EMFILE;
87 return NULL;
90 pdir->busy = true;
92 #ifdef HAVE_MULTIVOLUME
93 /* try to extract a heading volume name, if present */
94 volume = strip_volume(name, namecopy);
95 pdir->volumecounter = 0;
96 #else
97 strlcpy(namecopy, name, sizeof(namecopy)); /* just copy */
98 #endif
100 if ( fat_opendir(IF_MV2(volume,) &pdir->fatdir, 0, NULL) < 0 ) {
101 DEBUGF("Failed opening root dir\n");
102 pdir->busy = false;
103 return NULL;
106 for ( part = strtok_r(namecopy, "/", &end); part;
107 part = strtok_r(NULL, "/", &end)) {
108 /* scan dir for name */
109 while (1) {
110 if ((fat_getnext(&pdir->fatdir,&entry) < 0) ||
111 (!entry.name[0])) {
112 pdir->busy = false;
113 return NULL;
115 if ( (entry.attr & FAT_ATTR_DIRECTORY) &&
116 (!strcasecmp(part, entry.name)) ) {
117 /* In reality, the parent_dir parameter of fat_opendir seems
118 * useless because it's sole purpose it to have a way to
119 * update the file metadata, but here we are only reading
120 * a directory so there's no need for that kind of stuff.
121 * However, the rmdir_uncached function uses a ugly hack to
122 * avoid opening a directory twice when deleting it and thus
123 * needs those information. That's why we pass pdir->fatdir both
124 * as the parent directory and the resulting one (this is safe,
125 * in doubt, check fat_open(dir) code) which will allow this kind of
126 * (ugly) things */
127 if ( fat_opendir(IF_MV2(volume,)
128 &pdir->fatdir,
129 entry.firstcluster,
130 &pdir->fatdir) < 0 ) {
131 DEBUGF("Failed opening dir '%s' (%ld)\n",
132 part, entry.firstcluster);
133 pdir->busy = false;
134 return NULL;
136 #ifdef HAVE_MULTIVOLUME
137 pdir->volumecounter = -1; /* n.a. to subdirs */
138 #endif
139 break;
144 return pdir;
147 int closedir_uncached(DIR_UNCACHED* dir)
149 dir->busy=false;
150 return 0;
153 struct dirent_uncached* readdir_uncached(DIR_UNCACHED* dir)
155 struct fat_direntry entry;
156 struct dirent_uncached* theent = &(dir->theent);
158 if (!dir->busy)
159 return NULL;
161 #ifdef HAVE_MULTIVOLUME
162 /* Volumes (secondary file systems) get inserted into the root directory
163 of the first volume, since we have no separate top level. */
164 if (dir->volumecounter >= 0 /* on a root dir */
165 && dir->volumecounter < NUM_VOLUMES /* in range */
166 && dir->fatdir.file.volume == 0) /* at volume 0 */
167 { /* fake special directories, which don't really exist, but
168 will get redirected upon opendir_uncached() */
169 while (++dir->volumecounter < NUM_VOLUMES)
171 if (fat_ismounted(dir->volumecounter))
173 memset(theent, 0, sizeof(*theent));
174 theent->attribute = FAT_ATTR_DIRECTORY | FAT_ATTR_VOLUME;
175 snprintf(theent->d_name, sizeof(theent->d_name),
176 VOL_NAMES, dir->volumecounter);
177 return theent;
181 #endif
182 /* normal directory entry fetching follows here */
183 if (fat_getnext(&(dir->fatdir),&entry) < 0)
184 return NULL;
186 if ( !entry.name[0] )
187 return NULL;
189 strlcpy(theent->d_name, entry.name, sizeof(theent->d_name));
190 theent->attribute = entry.attr;
191 theent->size = entry.filesize;
192 theent->startcluster = entry.firstcluster;
193 theent->wrtdate = entry.wrtdate;
194 theent->wrttime = entry.wrttime;
196 return theent;
199 int mkdir_uncached(const char *name)
201 DIR_UNCACHED *dir;
202 char namecopy[MAX_PATH];
203 char* end;
204 char *basename;
205 char *parent;
206 struct dirent_uncached *entry;
207 struct fat_dir newdir;
208 int rc;
210 if ( name[0] != '/' ) {
211 DEBUGF("mkdir: Only absolute paths supported right now\n");
212 return -1;
215 strlcpy(namecopy, name, sizeof(namecopy));
217 /* Split the base name and the path */
218 end = strrchr(namecopy, '/');
219 *end = 0;
220 basename = end+1;
222 if(namecopy == end) /* Root dir? */
223 parent = "/";
224 else
225 parent = namecopy;
227 DEBUGF("mkdir: parent: %s, name: %s\n", parent, basename);
229 dir = opendir_uncached(parent);
231 if(!dir) {
232 DEBUGF("mkdir: can't open parent dir\n");
233 return -2;
236 if(basename[0] == 0) {
237 DEBUGF("mkdir: Empty dir name\n");
238 errno = EINVAL;
239 return -3;
242 /* Now check if the name already exists */
243 while ((entry = readdir_uncached(dir))) {
244 if ( !strcasecmp(basename, entry->d_name) ) {
245 DEBUGF("mkdir error: file exists\n");
246 errno = EEXIST;
247 closedir_uncached(dir);
248 return - 4;
252 memset(&newdir, 0, sizeof(struct fat_dir));
254 rc = fat_create_dir(basename, &newdir, &(dir->fatdir));
255 closedir_uncached(dir);
257 return rc;
260 int rmdir_uncached(const char* name)
262 int rc;
263 DIR_UNCACHED* dir;
264 struct dirent_uncached* entry;
266 dir = opendir_uncached(name);
267 if (!dir)
269 errno = ENOENT; /* open error */
270 return -1;
273 /* check if the directory is empty */
274 while ((entry = readdir_uncached(dir)))
276 if (strcmp(entry->d_name, ".") &&
277 strcmp(entry->d_name, ".."))
279 DEBUGF("rmdir error: not empty\n");
280 errno = ENOTEMPTY;
281 closedir_uncached(dir);
282 return -2;
286 rc = fat_remove(&(dir->fatdir.file));
287 if ( rc < 0 ) {
288 DEBUGF("Failed removing dir: %d\n", rc);
289 errno = EIO;
290 rc = rc * 10 - 3;
293 closedir_uncached(dir);
294 return rc;