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 is
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 * Consequently, we can safely pass NULL of it because
122 * fat_opendir and fat_open are NULL-protected. */
123 if ( fat_opendir(IF_MV2(volume
,)
127 DEBUGF("Failed opening dir '%s' (%ld)\n",
128 part
, entry
.firstcluster
);
132 #ifdef HAVE_MULTIVOLUME
133 pdir
->volumecounter
= -1; /* n.a. to subdirs */
143 int closedir_uncached(DIR_UNCACHED
* dir
)
149 struct dirent_uncached
* readdir_uncached(DIR_UNCACHED
* dir
)
151 struct fat_direntry entry
;
152 struct dirent_uncached
* theent
= &(dir
->theent
);
157 #ifdef HAVE_MULTIVOLUME
158 /* Volumes (secondary file systems) get inserted into the root directory
159 of the first volume, since we have no separate top level. */
160 if (dir
->volumecounter
>= 0 /* on a root dir */
161 && dir
->volumecounter
< NUM_VOLUMES
/* in range */
162 && dir
->fatdir
.file
.volume
== 0) /* at volume 0 */
163 { /* fake special directories, which don't really exist, but
164 will get redirected upon opendir_uncached() */
165 while (++dir
->volumecounter
< NUM_VOLUMES
)
167 if (fat_ismounted(dir
->volumecounter
))
169 memset(theent
, 0, sizeof(*theent
));
170 theent
->attribute
= FAT_ATTR_DIRECTORY
| FAT_ATTR_VOLUME
;
171 snprintf(theent
->d_name
, sizeof(theent
->d_name
),
172 VOL_NAMES
, dir
->volumecounter
);
178 /* normal directory entry fetching follows here */
179 if (fat_getnext(&(dir
->fatdir
),&entry
) < 0)
182 if ( !entry
.name
[0] )
185 strlcpy(theent
->d_name
, entry
.name
, sizeof(theent
->d_name
));
186 theent
->attribute
= entry
.attr
;
187 theent
->size
= entry
.filesize
;
188 theent
->startcluster
= entry
.firstcluster
;
189 theent
->wrtdate
= entry
.wrtdate
;
190 theent
->wrttime
= entry
.wrttime
;
195 int mkdir_uncached(const char *name
)
198 char namecopy
[MAX_PATH
];
202 struct dirent_uncached
*entry
;
203 struct fat_dir newdir
;
206 if ( name
[0] != '/' ) {
207 DEBUGF("mkdir: Only absolute paths supported right now\n");
211 strlcpy(namecopy
, name
, sizeof(namecopy
));
213 /* Split the base name and the path */
214 end
= strrchr(namecopy
, '/');
218 if(namecopy
== end
) /* Root dir? */
223 DEBUGF("mkdir: parent: %s, name: %s\n", parent
, basename
);
225 dir
= opendir_uncached(parent
);
228 DEBUGF("mkdir: can't open parent dir\n");
232 if(basename
[0] == 0) {
233 DEBUGF("mkdir: Empty dir name\n");
238 /* Now check if the name already exists */
239 while ((entry
= readdir_uncached(dir
))) {
240 if ( !strcasecmp(basename
, entry
->d_name
) ) {
241 DEBUGF("mkdir error: file exists\n");
243 closedir_uncached(dir
);
248 memset(&newdir
, 0, sizeof(struct fat_dir
));
250 rc
= fat_create_dir(basename
, &newdir
, &(dir
->fatdir
));
251 closedir_uncached(dir
);
256 int rmdir_uncached(const char* name
)
260 struct dirent_uncached
* entry
;
262 dir
= opendir_uncached(name
);
265 errno
= ENOENT
; /* open error */
269 /* check if the directory is empty */
270 while ((entry
= readdir_uncached(dir
)))
272 if (strcmp(entry
->d_name
, ".") &&
273 strcmp(entry
->d_name
, ".."))
275 DEBUGF("rmdir error: not empty\n");
277 closedir_uncached(dir
);
282 rc
= fat_remove(&(dir
->fatdir
.file
));
284 DEBUGF("Failed removing dir: %d\n", rc
);
289 closedir_uncached(dir
);