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 strncpy(namecopy
, name
, MAX_PATH
);
62 namecopy
[MAX_PATH
-1] = '\0';
66 #endif /* #ifdef HAVE_MULTIVOLUME */
70 // release all dir handles on a given volume "by force", to avoid leaks
71 int release_dirs(int volume
)
73 DIR_UNCACHED
* pdir
= opendirs
;
76 for ( dd
=0; dd
<MAX_OPEN_DIRS
; dd
++, pdir
++)
78 if (pdir
->fatdir
.file
.volume
== volume
)
80 pdir
->busy
= false; /* mark as available, no further action */
84 return closed
; /* return how many we did */
86 #endif /* #ifdef HAVE_HOTSWAP */
88 DIR_UNCACHED
* opendir_uncached(const char* name
)
90 char namecopy
[MAX_PATH
];
93 struct fat_direntry entry
;
95 DIR_UNCACHED
* pdir
= opendirs
;
96 #ifdef HAVE_MULTIVOLUME
100 if ( name
[0] != '/' ) {
101 DEBUGF("Only absolute paths supported right now\n");
105 /* find a free dir descriptor */
106 for ( dd
=0; dd
<MAX_OPEN_DIRS
; dd
++, pdir
++)
110 if ( dd
== MAX_OPEN_DIRS
) {
111 DEBUGF("Too many dirs open\n");
118 #ifdef HAVE_MULTIVOLUME
119 /* try to extract a heading volume name, if present */
120 volume
= strip_volume(name
, namecopy
);
121 pdir
->volumecounter
= 0;
123 strncpy(namecopy
,name
,sizeof(namecopy
)); /* just copy */
124 namecopy
[sizeof(namecopy
)-1] = '\0';
127 if ( fat_opendir(IF_MV2(volume
,) &pdir
->fatdir
, 0, NULL
) < 0 ) {
128 DEBUGF("Failed opening root dir\n");
133 for ( part
= strtok_r(namecopy
, "/", &end
); part
;
134 part
= strtok_r(NULL
, "/", &end
)) {
135 /* scan dir for name */
137 if ((fat_getnext(&pdir
->fatdir
,&entry
) < 0) ||
142 if ( (entry
.attr
& FAT_ATTR_DIRECTORY
) &&
143 (!strcasecmp(part
, entry
.name
)) ) {
144 pdir
->parent_dir
= pdir
->fatdir
;
145 if ( fat_opendir(IF_MV2(volume
,)
148 &pdir
->parent_dir
) < 0 ) {
149 DEBUGF("Failed opening dir '%s' (%ld)\n",
150 part
, entry
.firstcluster
);
154 #ifdef HAVE_MULTIVOLUME
155 pdir
->volumecounter
= -1; /* n.a. to subdirs */
165 int closedir_uncached(DIR_UNCACHED
* dir
)
171 struct dirent_uncached
* readdir_uncached(DIR_UNCACHED
* dir
)
173 struct fat_direntry entry
;
174 struct dirent_uncached
* theent
= &(dir
->theent
);
179 #ifdef HAVE_MULTIVOLUME
180 /* Volumes (secondary file systems) get inserted into the root directory
181 of the first volume, since we have no separate top level. */
182 if (dir
->volumecounter
>= 0 /* on a root dir */
183 && dir
->volumecounter
< NUM_VOLUMES
/* in range */
184 && dir
->fatdir
.file
.volume
== 0) /* at volume 0 */
185 { /* fake special directories, which don't really exist, but
186 will get redirected upon opendir_uncached() */
187 while (++dir
->volumecounter
< NUM_VOLUMES
)
189 if (fat_ismounted(dir
->volumecounter
))
191 memset(theent
, 0, sizeof(*theent
));
192 theent
->attribute
= FAT_ATTR_DIRECTORY
| FAT_ATTR_VOLUME
;
193 snprintf(theent
->d_name
, sizeof(theent
->d_name
),
194 VOL_NAMES
, dir
->volumecounter
);
200 /* normal directory entry fetching follows here */
201 if (fat_getnext(&(dir
->fatdir
),&entry
) < 0)
204 if ( !entry
.name
[0] )
207 strncpy(theent
->d_name
, entry
.name
, sizeof( theent
->d_name
) );
208 theent
->attribute
= entry
.attr
;
209 theent
->size
= entry
.filesize
;
210 theent
->startcluster
= entry
.firstcluster
;
211 theent
->wrtdate
= entry
.wrtdate
;
212 theent
->wrttime
= entry
.wrttime
;
217 int mkdir_uncached(const char *name
)
220 char namecopy
[MAX_PATH
];
224 struct dirent_uncached
*entry
;
225 struct fat_dir newdir
;
228 if ( name
[0] != '/' ) {
229 DEBUGF("mkdir: Only absolute paths supported right now\n");
233 strncpy(namecopy
,name
,sizeof(namecopy
));
234 namecopy
[sizeof(namecopy
)-1] = 0;
236 /* Split the base name and the path */
237 end
= strrchr(namecopy
, '/');
241 if(namecopy
== end
) /* Root dir? */
246 DEBUGF("mkdir: parent: %s, name: %s\n", parent
, basename
);
248 dir
= opendir_uncached(parent
);
251 DEBUGF("mkdir: can't open parent dir\n");
255 if(basename
[0] == 0) {
256 DEBUGF("mkdir: Empty dir name\n");
261 /* Now check if the name already exists */
262 while ((entry
= readdir_uncached(dir
))) {
263 if ( !strcasecmp(basename
, entry
->d_name
) ) {
264 DEBUGF("mkdir error: file exists\n");
266 closedir_uncached(dir
);
271 memset(&newdir
, 0, sizeof(struct fat_dir
));
273 rc
= fat_create_dir(basename
, &newdir
, &(dir
->fatdir
));
274 closedir_uncached(dir
);
279 int rmdir_uncached(const char* name
)
283 struct dirent_uncached
* entry
;
285 dir
= opendir_uncached(name
);
288 errno
= ENOENT
; /* open error */
292 /* check if the directory is empty */
293 while ((entry
= readdir_uncached(dir
)))
295 if (strcmp(entry
->d_name
, ".") &&
296 strcmp(entry
->d_name
, ".."))
298 DEBUGF("rmdir error: not empty\n");
300 closedir_uncached(dir
);
305 rc
= fat_remove(&(dir
->fatdir
.file
));
307 DEBUGF("Failed removing dir: %d\n", rc
);
312 closedir_uncached(dir
);