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 ****************************************************************************/
29 #include "filefuncs.h"
31 #if ((defined(MEMORYSIZE) && (MEMORYSIZE > 8)) || MEM > 8)
32 #define MAX_OPEN_DIRS 12
34 #define MAX_OPEN_DIRS 8
37 static DIR_UNCACHED opendirs
[MAX_OPEN_DIRS
];
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
;
46 for ( dd
=0; dd
<MAX_OPEN_DIRS
; dd
++, pdir
++)
48 #ifdef HAVE_MULTIVOLUME
49 if (pdir
->fatdir
.file
.volume
== volume
)
54 pdir
->busy
= false; /* mark as available, no further action */
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
];
67 struct fat_direntry entry
;
69 DIR_UNCACHED
* pdir
= opendirs
;
70 #ifdef HAVE_MULTIVOLUME
74 if ( name
[0] != '/' ) {
75 DEBUGF("Only absolute paths supported right now\n");
79 /* find a free dir descriptor */
80 for ( dd
=0; dd
<MAX_OPEN_DIRS
; dd
++, pdir
++)
84 if ( dd
== MAX_OPEN_DIRS
) {
85 DEBUGF("Too many dirs open\n");
92 #ifdef HAVE_MULTIVOLUME
93 /* try to extract a heading volume name, if present */
94 volume
= strip_volume(name
, namecopy
);
95 pdir
->volumecounter
= 0;
97 strlcpy(namecopy
, name
, sizeof(namecopy
)); /* just copy */
100 if ( fat_opendir(IF_MV2(volume
,) &pdir
->fatdir
, 0, NULL
) < 0 ) {
101 DEBUGF("Failed opening root dir\n");
106 for ( part
= strtok_r(namecopy
, "/", &end
); part
;
107 part
= strtok_r(NULL
, "/", &end
)) {
108 /* scan dir for name */
110 if ((fat_getnext(&pdir
->fatdir
,&entry
) < 0) ||
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
127 if ( fat_opendir(IF_MV2(volume
,)
130 &pdir
->fatdir
) < 0 ) {
131 DEBUGF("Failed opening dir '%s' (%ld)\n",
132 part
, entry
.firstcluster
);
136 #ifdef HAVE_MULTIVOLUME
137 pdir
->volumecounter
= -1; /* n.a. to subdirs */
147 int closedir_uncached(DIR_UNCACHED
* dir
)
153 struct dirent_uncached
* readdir_uncached(DIR_UNCACHED
* dir
)
155 struct fat_direntry entry
;
156 struct dirent_uncached
* theent
= &(dir
->theent
);
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
);
182 /* normal directory entry fetching follows here */
183 if (fat_getnext(&(dir
->fatdir
),&entry
) < 0)
186 if ( !entry
.name
[0] )
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
;
199 int mkdir_uncached(const char *name
)
202 char namecopy
[MAX_PATH
];
206 struct dirent_uncached
*entry
;
207 struct fat_dir newdir
;
210 if ( name
[0] != '/' ) {
211 DEBUGF("mkdir: Only absolute paths supported right now\n");
215 strlcpy(namecopy
, name
, sizeof(namecopy
));
217 /* Split the base name and the path */
218 end
= strrchr(namecopy
, '/');
222 if(namecopy
== end
) /* Root dir? */
227 DEBUGF("mkdir: parent: %s, name: %s\n", parent
, basename
);
229 dir
= opendir_uncached(parent
);
232 DEBUGF("mkdir: can't open parent dir\n");
236 if(basename
[0] == 0) {
237 DEBUGF("mkdir: Empty dir name\n");
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");
247 closedir_uncached(dir
);
252 memset(&newdir
, 0, sizeof(struct fat_dir
));
254 rc
= fat_create_dir(basename
, &newdir
, &(dir
->fatdir
));
255 closedir_uncached(dir
);
260 int rmdir_uncached(const char* name
)
264 struct dirent_uncached
* entry
;
266 dir
= opendir_uncached(name
);
269 errno
= ENOENT
; /* open error */
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");
281 closedir_uncached(dir
);
286 rc
= fat_remove(&(dir
->fatdir
.file
));
288 DEBUGF("Failed removing dir: %d\n", rc
);
293 closedir_uncached(dir
);