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 #define MAX_OPEN_DIRS 8
32 static DIR_UNCACHED opendirs
[MAX_OPEN_DIRS
];
34 #ifdef HAVE_MULTIVOLUME
36 /* returns on which volume this is, and copies the reduced name
37 (sortof a preprocessor for volume-decorated pathnames) */
38 int strip_volume(const char* name
, char* namecopy
)
41 const char *temp
= name
;
43 while (*temp
== '/') /* skip all leading slashes */
46 if (*temp
&& !strncmp(temp
, VOL_NAMES
, VOL_ENUM_POS
))
48 temp
+= VOL_ENUM_POS
; /* behind special name */
49 volume
= atoi(temp
); /* number is following */
50 temp
= strchr(temp
, '/'); /* search for slash behind */
52 name
= temp
; /* use the part behind the volume */
54 name
= "/"; /* else this must be the root dir */
57 strncpy(namecopy
, name
, MAX_PATH
);
58 namecopy
[MAX_PATH
-1] = '\0';
62 #endif /* #ifdef HAVE_MULTIVOLUME */
66 // release all dir handles on a given volume "by force", to avoid leaks
67 int release_dirs(int volume
)
69 DIR_UNCACHED
* pdir
= opendirs
;
72 for ( dd
=0; dd
<MAX_OPEN_DIRS
; dd
++, pdir
++)
74 if (pdir
->fatdir
.file
.volume
== volume
)
76 pdir
->busy
= false; /* mark as available, no further action */
80 return closed
; /* return how many we did */
82 #endif /* #ifdef HAVE_HOTSWAP */
84 DIR_UNCACHED
* opendir_uncached(const char* name
)
86 char namecopy
[MAX_PATH
];
89 struct fat_direntry entry
;
91 DIR_UNCACHED
* pdir
= opendirs
;
92 #ifdef HAVE_MULTIVOLUME
96 if ( name
[0] != '/' ) {
97 DEBUGF("Only absolute paths supported right now\n");
101 /* find a free dir descriptor */
102 for ( dd
=0; dd
<MAX_OPEN_DIRS
; dd
++, pdir
++)
106 if ( dd
== MAX_OPEN_DIRS
) {
107 DEBUGF("Too many dirs open\n");
114 #ifdef HAVE_MULTIVOLUME
115 /* try to extract a heading volume name, if present */
116 volume
= strip_volume(name
, namecopy
);
117 pdir
->volumecounter
= 0;
119 strncpy(namecopy
,name
,sizeof(namecopy
)); /* just copy */
120 namecopy
[sizeof(namecopy
)-1] = '\0';
123 if ( fat_opendir(IF_MV2(volume
,) &pdir
->fatdir
, 0, NULL
) < 0 ) {
124 DEBUGF("Failed opening root dir\n");
129 for ( part
= strtok_r(namecopy
, "/", &end
); part
;
130 part
= strtok_r(NULL
, "/", &end
)) {
131 /* scan dir for name */
133 if ((fat_getnext(&pdir
->fatdir
,&entry
) < 0) ||
138 if ( (entry
.attr
& FAT_ATTR_DIRECTORY
) &&
139 (!strcasecmp(part
, entry
.name
)) ) {
140 pdir
->parent_dir
= pdir
->fatdir
;
141 if ( fat_opendir(IF_MV2(volume
,)
144 &pdir
->parent_dir
) < 0 ) {
145 DEBUGF("Failed opening dir '%s' (%ld)\n",
146 part
, entry
.firstcluster
);
150 #ifdef HAVE_MULTIVOLUME
151 pdir
->volumecounter
= -1; /* n.a. to subdirs */
161 int closedir_uncached(DIR_UNCACHED
* dir
)
167 struct dirent_uncached
* readdir_uncached(DIR_UNCACHED
* dir
)
169 struct fat_direntry entry
;
170 struct dirent_uncached
* theent
= &(dir
->theent
);
175 #ifdef HAVE_MULTIVOLUME
176 /* Volumes (secondary file systems) get inserted into the root directory
177 of the first volume, since we have no separate top level. */
178 if (dir
->volumecounter
>= 0 /* on a root dir */
179 && dir
->volumecounter
< NUM_VOLUMES
/* in range */
180 && dir
->fatdir
.file
.volume
== 0) /* at volume 0 */
181 { /* fake special directories, which don't really exist, but
182 will get redirected upon opendir_uncached() */
183 while (++dir
->volumecounter
< NUM_VOLUMES
)
185 if (fat_ismounted(dir
->volumecounter
))
187 memset(theent
, 0, sizeof(*theent
));
188 theent
->attribute
= FAT_ATTR_DIRECTORY
| FAT_ATTR_VOLUME
;
189 snprintf(theent
->d_name
, sizeof(theent
->d_name
),
190 VOL_NAMES
, dir
->volumecounter
);
196 /* normal directory entry fetching follows here */
197 if (fat_getnext(&(dir
->fatdir
),&entry
) < 0)
200 if ( !entry
.name
[0] )
203 strncpy(theent
->d_name
, entry
.name
, sizeof( theent
->d_name
) );
204 theent
->attribute
= entry
.attr
;
205 theent
->size
= entry
.filesize
;
206 theent
->startcluster
= entry
.firstcluster
;
207 theent
->wrtdate
= entry
.wrtdate
;
208 theent
->wrttime
= entry
.wrttime
;
213 int mkdir_uncached(const char *name
)
216 char namecopy
[MAX_PATH
];
220 struct dirent_uncached
*entry
;
221 struct fat_dir newdir
;
224 if ( name
[0] != '/' ) {
225 DEBUGF("mkdir: Only absolute paths supported right now\n");
229 strncpy(namecopy
,name
,sizeof(namecopy
));
230 namecopy
[sizeof(namecopy
)-1] = 0;
232 /* Split the base name and the path */
233 end
= strrchr(namecopy
, '/');
237 if(namecopy
== end
) /* Root dir? */
242 DEBUGF("mkdir: parent: %s, name: %s\n", parent
, basename
);
244 dir
= opendir_uncached(parent
);
247 DEBUGF("mkdir: can't open parent dir\n");
251 if(basename
[0] == 0) {
252 DEBUGF("mkdir: Empty dir name\n");
257 /* Now check if the name already exists */
258 while ((entry
= readdir_uncached(dir
))) {
259 if ( !strcasecmp(basename
, entry
->d_name
) ) {
260 DEBUGF("mkdir error: file exists\n");
262 closedir_uncached(dir
);
267 memset(&newdir
, sizeof(struct fat_dir
), 0);
269 rc
= fat_create_dir(basename
, &newdir
, &(dir
->fatdir
));
270 closedir_uncached(dir
);
275 int rmdir_uncached(const char* name
)
279 struct dirent_uncached
* entry
;
281 dir
= opendir_uncached(name
);
284 errno
= ENOENT
; /* open error */
288 /* check if the directory is empty */
289 while ((entry
= readdir_uncached(dir
)))
291 if (strcmp(entry
->d_name
, ".") &&
292 strcmp(entry
->d_name
, ".."))
294 DEBUGF("rmdir error: not empty\n");
296 closedir_uncached(dir
);
301 rc
= fat_remove(&(dir
->fatdir
.file
));
303 DEBUGF("Failed removing dir: %d\n", rc
);
308 closedir_uncached(dir
);