Add note to Bookmarking sections that bookmarking only works from the file browser...
[kugel-rb.git] / firmware / common / dir_uncached.c
blob5ec5c7fb41aec50d0eeb5d786f3a0beb5079f34b
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 pdir->parent_dir = pdir->fatdir;
118 if ( fat_opendir(IF_MV2(volume,)
119 &pdir->fatdir,
120 entry.firstcluster,
121 &pdir->parent_dir) < 0 ) {
122 DEBUGF("Failed opening dir '%s' (%ld)\n",
123 part, entry.firstcluster);
124 pdir->busy = false;
125 return NULL;
127 #ifdef HAVE_MULTIVOLUME
128 pdir->volumecounter = -1; /* n.a. to subdirs */
129 #endif
130 break;
135 return pdir;
138 int closedir_uncached(DIR_UNCACHED* dir)
140 dir->busy=false;
141 return 0;
144 struct dirent_uncached* readdir_uncached(DIR_UNCACHED* dir)
146 struct fat_direntry entry;
147 struct dirent_uncached* theent = &(dir->theent);
149 if (!dir->busy)
150 return NULL;
152 #ifdef HAVE_MULTIVOLUME
153 /* Volumes (secondary file systems) get inserted into the root directory
154 of the first volume, since we have no separate top level. */
155 if (dir->volumecounter >= 0 /* on a root dir */
156 && dir->volumecounter < NUM_VOLUMES /* in range */
157 && dir->fatdir.file.volume == 0) /* at volume 0 */
158 { /* fake special directories, which don't really exist, but
159 will get redirected upon opendir_uncached() */
160 while (++dir->volumecounter < NUM_VOLUMES)
162 if (fat_ismounted(dir->volumecounter))
164 memset(theent, 0, sizeof(*theent));
165 theent->attribute = FAT_ATTR_DIRECTORY | FAT_ATTR_VOLUME;
166 snprintf(theent->d_name, sizeof(theent->d_name),
167 VOL_NAMES, dir->volumecounter);
168 return theent;
172 #endif
173 /* normal directory entry fetching follows here */
174 if (fat_getnext(&(dir->fatdir),&entry) < 0)
175 return NULL;
177 if ( !entry.name[0] )
178 return NULL;
180 strlcpy(theent->d_name, entry.name, sizeof(theent->d_name));
181 theent->attribute = entry.attr;
182 theent->size = entry.filesize;
183 theent->startcluster = entry.firstcluster;
184 theent->wrtdate = entry.wrtdate;
185 theent->wrttime = entry.wrttime;
187 return theent;
190 int mkdir_uncached(const char *name)
192 DIR_UNCACHED *dir;
193 char namecopy[MAX_PATH];
194 char* end;
195 char *basename;
196 char *parent;
197 struct dirent_uncached *entry;
198 struct fat_dir newdir;
199 int rc;
201 if ( name[0] != '/' ) {
202 DEBUGF("mkdir: Only absolute paths supported right now\n");
203 return -1;
206 strlcpy(namecopy, name, sizeof(namecopy));
208 /* Split the base name and the path */
209 end = strrchr(namecopy, '/');
210 *end = 0;
211 basename = end+1;
213 if(namecopy == end) /* Root dir? */
214 parent = "/";
215 else
216 parent = namecopy;
218 DEBUGF("mkdir: parent: %s, name: %s\n", parent, basename);
220 dir = opendir_uncached(parent);
222 if(!dir) {
223 DEBUGF("mkdir: can't open parent dir\n");
224 return -2;
227 if(basename[0] == 0) {
228 DEBUGF("mkdir: Empty dir name\n");
229 errno = EINVAL;
230 return -3;
233 /* Now check if the name already exists */
234 while ((entry = readdir_uncached(dir))) {
235 if ( !strcasecmp(basename, entry->d_name) ) {
236 DEBUGF("mkdir error: file exists\n");
237 errno = EEXIST;
238 closedir_uncached(dir);
239 return - 4;
243 memset(&newdir, 0, sizeof(struct fat_dir));
245 rc = fat_create_dir(basename, &newdir, &(dir->fatdir));
246 closedir_uncached(dir);
248 return rc;
251 int rmdir_uncached(const char* name)
253 int rc;
254 DIR_UNCACHED* dir;
255 struct dirent_uncached* entry;
257 dir = opendir_uncached(name);
258 if (!dir)
260 errno = ENOENT; /* open error */
261 return -1;
264 /* check if the directory is empty */
265 while ((entry = readdir_uncached(dir)))
267 if (strcmp(entry->d_name, ".") &&
268 strcmp(entry->d_name, ".."))
270 DEBUGF("rmdir error: not empty\n");
271 errno = ENOTEMPTY;
272 closedir_uncached(dir);
273 return -2;
277 rc = fat_remove(&(dir->fatdir.file));
278 if ( rc < 0 ) {
279 DEBUGF("Failed removing dir: %d\n", rc);
280 errno = EIO;
281 rc = rc * 10 - 3;
284 closedir_uncached(dir);
285 return rc;