1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
30 #if ((defined(MEMORYSIZE) && (MEMORYSIZE > 8)) || MEM > 8)
31 #define MAX_OPEN_DIRS 12
33 #define MAX_OPEN_DIRS 8
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
)
45 const char *temp
= name
;
47 while (*temp
== '/') /* skip all leading slashes */
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 */
56 name
= temp
; /* use the part behind the volume */
58 name
= "/"; /* else this must be the root dir */
61 strlcpy(namecopy
, name
, MAX_PATH
);
65 #endif /* #ifdef HAVE_MULTIVOLUME */
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
;
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 */
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
];
92 struct fat_direntry entry
;
94 DIR_UNCACHED
* pdir
= opendirs
;
95 #ifdef HAVE_MULTIVOLUME
99 if ( name
[0] != '/' ) {
100 DEBUGF("Only absolute paths supported right now\n");
104 /* find a free dir descriptor */
105 for ( dd
=0; dd
<MAX_OPEN_DIRS
; dd
++, pdir
++)
109 if ( dd
== MAX_OPEN_DIRS
) {
110 DEBUGF("Too many dirs open\n");
117 #ifdef HAVE_MULTIVOLUME
118 /* try to extract a heading volume name, if present */
119 volume
= strip_volume(name
, namecopy
);
120 pdir
->volumecounter
= 0;
122 strlcpy(namecopy
, name
, sizeof(namecopy
)); /* just copy */
125 if ( fat_opendir(IF_MV2(volume
,) &pdir
->fatdir
, 0, NULL
) < 0 ) {
126 DEBUGF("Failed opening root dir\n");
131 for ( part
= strtok_r(namecopy
, "/", &end
); part
;
132 part
= strtok_r(NULL
, "/", &end
)) {
133 /* scan dir for name */
135 if ((fat_getnext(&pdir
->fatdir
,&entry
) < 0) ||
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
,)
146 &pdir
->parent_dir
) < 0 ) {
147 DEBUGF("Failed opening dir '%s' (%ld)\n",
148 part
, entry
.firstcluster
);
152 #ifdef HAVE_MULTIVOLUME
153 pdir
->volumecounter
= -1; /* n.a. to subdirs */
163 int closedir_uncached(DIR_UNCACHED
* dir
)
169 struct dirent_uncached
* readdir_uncached(DIR_UNCACHED
* dir
)
171 struct fat_direntry entry
;
172 struct dirent_uncached
* theent
= &(dir
->theent
);
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
);
198 /* normal directory entry fetching follows here */
199 if (fat_getnext(&(dir
->fatdir
),&entry
) < 0)
202 if ( !entry
.name
[0] )
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
;
215 int mkdir_uncached(const char *name
)
218 char namecopy
[MAX_PATH
];
222 struct dirent_uncached
*entry
;
223 struct fat_dir newdir
;
226 if ( name
[0] != '/' ) {
227 DEBUGF("mkdir: Only absolute paths supported right now\n");
231 strlcpy(namecopy
, name
, sizeof(namecopy
));
233 /* Split the base name and the path */
234 end
= strrchr(namecopy
, '/');
238 if(namecopy
== end
) /* Root dir? */
243 DEBUGF("mkdir: parent: %s, name: %s\n", parent
, basename
);
245 dir
= opendir_uncached(parent
);
248 DEBUGF("mkdir: can't open parent dir\n");
252 if(basename
[0] == 0) {
253 DEBUGF("mkdir: Empty dir name\n");
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");
263 closedir_uncached(dir
);
268 memset(&newdir
, 0, sizeof(struct fat_dir
));
270 rc
= fat_create_dir(basename
, &newdir
, &(dir
->fatdir
));
271 closedir_uncached(dir
);
276 int rmdir_uncached(const char* name
)
280 struct dirent_uncached
* entry
;
282 dir
= opendir_uncached(name
);
285 errno
= ENOENT
; /* open error */
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");
297 closedir_uncached(dir
);
302 rc
= fat_remove(&(dir
->fatdir
.file
));
304 DEBUGF("Failed removing dir: %d\n", rc
);
309 closedir_uncached(dir
);