| 231 |
theseven |
1 |
/***************************************************************************
|
| 60 |
theseven |
2 |
* __________ __ ___.
|
|
|
3 |
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
4 |
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
5 |
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
6 |
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
7 |
* \/ \/ \/ \/ \/
|
|
|
8 |
* $Id: dir.c 13741 2007-06-30 02:08:27Z jethead71 $
|
|
|
9 |
*
|
|
|
10 |
* Copyright (C) 2002 by Björn Stenberg
|
|
|
11 |
*
|
|
|
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.
|
|
|
16 |
*
|
|
|
17 |
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
18 |
* KIND, either express or implied.
|
|
|
19 |
*
|
|
|
20 |
****************************************************************************/
|
|
|
21 |
#include "global.h"
|
| 114 |
theseven |
22 |
#include "libc/include/errno.h"
|
|
|
23 |
#include "libc/include/string.h"
|
| 60 |
theseven |
24 |
#include "fat.h"
|
|
|
25 |
#include "dir.h"
|
|
|
26 |
#include "debug.h"
|
|
|
27 |
|
|
|
28 |
#ifndef MAX_OPEN_DIRS
|
|
|
29 |
#define MAX_OPEN_DIRS 16
|
|
|
30 |
#endif
|
|
|
31 |
|
| 58 |
theseven |
32 |
extern struct scheduler_thread* current_thread;
|
| 60 |
theseven |
33 |
static DIR opendirs[MAX_OPEN_DIRS];
|
|
|
34 |
|
|
|
35 |
#ifdef HAVE_HOTSWAP
|
|
|
36 |
// release all dir handles on a given volume "by force", to avoid leaks
|
|
|
37 |
int release_dirs(int volume)
|
|
|
38 |
{
|
|
|
39 |
DIR* pdir = opendirs;
|
|
|
40 |
int dd;
|
|
|
41 |
int closed = 0;
|
|
|
42 |
for ( dd=0; dd<MAX_OPEN_DIRS; dd++, pdir++)
|
|
|
43 |
{
|
|
|
44 |
#ifdef HAVE_MULTIVOLUME
|
|
|
45 |
if (pdir->fatdir.file.volume == volume)
|
|
|
46 |
#else
|
|
|
47 |
(void)volume;
|
|
|
48 |
#endif
|
|
|
49 |
{
|
|
|
50 |
pdir->busy = false; /* mark as available, no further action */
|
|
|
51 |
closed++;
|
|
|
52 |
}
|
|
|
53 |
}
|
|
|
54 |
return closed; /* return how many we did */
|
|
|
55 |
}
|
|
|
56 |
#endif /* #ifdef HAVE_HOTSWAP */
|
|
|
57 |
|
|
|
58 |
DIR* opendir(const char* name)
|
|
|
59 |
{
|
|
|
60 |
char namecopy[MAX_PATH];
|
|
|
61 |
char* part;
|
|
|
62 |
char* end;
|
|
|
63 |
struct fat_direntry entry;
|
|
|
64 |
int dd;
|
|
|
65 |
DIR* pdir = opendirs;
|
|
|
66 |
#ifdef HAVE_MULTIVOLUME
|
|
|
67 |
int volume;
|
|
|
68 |
#endif
|
|
|
69 |
|
|
|
70 |
if ( name[0] != '/' ) {
|
|
|
71 |
DEBUGF("Only absolute paths supported right now");
|
|
|
72 |
return NULL;
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
/* find a free dir descriptor */
|
|
|
76 |
for ( dd=0; dd<MAX_OPEN_DIRS; dd++, pdir++)
|
|
|
77 |
if ( !pdir->busy )
|
|
|
78 |
break;
|
|
|
79 |
|
|
|
80 |
if ( dd == MAX_OPEN_DIRS ) {
|
|
|
81 |
DEBUGF("Too many dirs open");
|
|
|
82 |
errno = EMFILE;
|
|
|
83 |
return NULL;
|
|
|
84 |
}
|
|
|
85 |
|
|
|
86 |
pdir->busy = true;
|
|
|
87 |
pdir->process = current_thread;
|
|
|
88 |
|
|
|
89 |
#ifdef HAVE_MULTIVOLUME
|
|
|
90 |
/* try to extract a heading volume name, if present */
|
|
|
91 |
volume = strip_volume(name, namecopy);
|
|
|
92 |
pdir->volumecounter = 0;
|
|
|
93 |
#else
|
|
|
94 |
strlcpy(namecopy, name, sizeof(namecopy)); /* just copy */
|
|
|
95 |
#endif
|
|
|
96 |
|
|
|
97 |
if ( fat_opendir(IF_MV2(volume,) &pdir->fatdir, 0, NULL) < 0 ) {
|
|
|
98 |
DEBUGF("Failed opening root dir");
|
|
|
99 |
pdir->busy = false;
|
|
|
100 |
return NULL;
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
for ( part = strtok_r(namecopy, "/", &end); part;
|
|
|
104 |
part = strtok_r(NULL, "/", &end)) {
|
|
|
105 |
/* scan dir for name */
|
|
|
106 |
while (1) {
|
|
|
107 |
if ((fat_getnext(&pdir->fatdir,&entry) < 0) ||
|
|
|
108 |
(!entry.name[0])) {
|
|
|
109 |
pdir->busy = false;
|
|
|
110 |
return NULL;
|
|
|
111 |
}
|
|
|
112 |
if ( (entry.attr & FAT_ATTR_DIRECTORY) &&
|
|
|
113 |
(!strcasecmp(part, entry.name)) ) {
|
|
|
114 |
/* In reality, the parent_dir parameter of fat_opendir seems
|
|
|
115 |
* useless because it's sole purpose it to have a way to
|
|
|
116 |
* update the file metadata, but here we are only reading
|
|
|
117 |
* a directory so there's no need for that kind of stuff.
|
|
|
118 |
* However, the rmdir function uses a ugly hack to
|
|
|
119 |
* avoid opening a directory twice when deleting it and thus
|
|
|
120 |
* needs those information. That's why we pass pdir->fatdir both
|
|
|
121 |
* as the parent directory and the resulting one (this is safe,
|
|
|
122 |
* in doubt, check fat_open(dir) code) which will allow this kind of
|
|
|
123 |
* (ugly) things */
|
|
|
124 |
if ( fat_opendir(IF_MV2(volume,)
|
|
|
125 |
&pdir->fatdir,
|
|
|
126 |
entry.firstcluster,
|
|
|
127 |
&pdir->fatdir) < 0 ) {
|
|
|
128 |
DEBUGF("Failed opening dir '%s' (%ld)",
|
|
|
129 |
part, entry.firstcluster);
|
|
|
130 |
pdir->busy = false;
|
|
|
131 |
return NULL;
|
|
|
132 |
}
|
|
|
133 |
#ifdef HAVE_MULTIVOLUME
|
|
|
134 |
pdir->volumecounter = -1; /* n.a. to subdirs */
|
|
|
135 |
#endif
|
|
|
136 |
break;
|
|
|
137 |
}
|
|
|
138 |
}
|
|
|
139 |
}
|
|
|
140 |
|
|
|
141 |
return pdir;
|
|
|
142 |
}
|
|
|
143 |
|
|
|
144 |
int closedir(DIR* dir)
|
|
|
145 |
{
|
|
|
146 |
dir->busy=false;
|
|
|
147 |
return 0;
|
|
|
148 |
}
|
|
|
149 |
|
|
|
150 |
int closedir_all_of_process(struct scheduler_thread* process)
|
|
|
151 |
{
|
|
|
152 |
DIR* pdir = opendirs;
|
|
|
153 |
int dd;
|
|
|
154 |
int closed = 0;
|
|
|
155 |
for ( dd=0; dd<MAX_OPEN_DIRS; dd++, pdir++)
|
|
|
156 |
{
|
|
|
157 |
if (pdir->process == process)
|
|
|
158 |
{
|
|
|
159 |
pdir->busy = false; /* mark as available, no further action */
|
|
|
160 |
closed++;
|
|
|
161 |
}
|
|
|
162 |
}
|
|
|
163 |
return closed; /* return how many we did */
|
|
|
164 |
}
|
|
|
165 |
|
|
|
166 |
struct dirent* readdir(DIR* dir)
|
|
|
167 |
{
|
|
|
168 |
struct fat_direntry entry;
|
|
|
169 |
struct dirent* theent = &(dir->theent);
|
|
|
170 |
|
|
|
171 |
if (!dir->busy)
|
|
|
172 |
return NULL;
|
|
|
173 |
|
|
|
174 |
#ifdef HAVE_MULTIVOLUME
|
|
|
175 |
/* Volumes (secondary file systems) get inserted into the root directory
|
|
|
176 |
of the first volume, since we have no separate top level. */
|
|
|
177 |
if (dir->volumecounter >= 0 /* on a root dir */
|
|
|
178 |
&& dir->volumecounter < NUM_VOLUMES /* in range */
|
|
|
179 |
&& dir->fatdir.file.volume == 0) /* at volume 0 */
|
|
|
180 |
{ /* fake special directories, which don't really exist, but
|
|
|
181 |
will get redirected upon opendir() */
|
|
|
182 |
while (++dir->volumecounter < NUM_VOLUMES)
|
|
|
183 |
{
|
|
|
184 |
if (fat_ismounted(dir->volumecounter))
|
|
|
185 |
{
|
|
|
186 |
memset(theent, 0, sizeof(*theent));
|
|
|
187 |
theent->attribute = FAT_ATTR_DIRECTORY | FAT_ATTR_VOLUME;
|
|
|
188 |
snprintf(theent->d_name, sizeof(theent->d_name),
|
|
|
189 |
VOL_NAMES, dir->volumecounter);
|
|
|
190 |
return theent;
|
|
|
191 |
}
|
|
|
192 |
}
|
|
|
193 |
}
|
|
|
194 |
#endif
|
|
|
195 |
/* normal directory entry fetching follows here */
|
|
|
196 |
if (fat_getnext(&(dir->fatdir),&entry) < 0)
|
|
|
197 |
return NULL;
|
|
|
198 |
|
|
|
199 |
if ( !entry.name[0] )
|
|
|
200 |
return NULL;
|
|
|
201 |
|
|
|
202 |
strlcpy(theent->d_name, entry.name, sizeof(theent->d_name));
|
|
|
203 |
theent->attribute = entry.attr;
|
|
|
204 |
theent->size = entry.filesize;
|
|
|
205 |
theent->startcluster = entry.firstcluster;
|
|
|
206 |
theent->wrtdate = entry.wrtdate;
|
|
|
207 |
theent->wrttime = entry.wrttime;
|
|
|
208 |
|
|
|
209 |
return theent;
|
|
|
210 |
}
|
|
|
211 |
|
|
|
212 |
int mkdir(const char *name)
|
|
|
213 |
{
|
|
|
214 |
DIR *dir;
|
|
|
215 |
char namecopy[MAX_PATH];
|
|
|
216 |
char* end;
|
|
|
217 |
char *basename;
|
|
|
218 |
char *parent;
|
|
|
219 |
struct dirent *entry;
|
|
|
220 |
int rc;
|
|
|
221 |
|
|
|
222 |
if ( name[0] != '/' ) {
|
|
|
223 |
DEBUGF("mkdir: Only absolute paths supported right now");
|
|
|
224 |
return -1;
|
|
|
225 |
}
|
|
|
226 |
|
|
|
227 |
strlcpy(namecopy, name, sizeof(namecopy));
|
|
|
228 |
|
|
|
229 |
/* Split the base name and the path */
|
|
|
230 |
end = strrchr(namecopy, '/');
|
|
|
231 |
*end = 0;
|
|
|
232 |
basename = end+1;
|
|
|
233 |
|
|
|
234 |
if(namecopy == end) /* Root dir? */
|
|
|
235 |
parent = "/";
|
|
|
236 |
else
|
|
|
237 |
parent = namecopy;
|
|
|
238 |
|
|
|
239 |
DEBUGF("mkdir: parent: %s, name: %s", parent, basename);
|
|
|
240 |
|
|
|
241 |
dir = opendir(parent);
|
|
|
242 |
|
|
|
243 |
if(!dir) {
|
|
|
244 |
DEBUGF("mkdir: can't open parent dir");
|
|
|
245 |
return -2;
|
|
|
246 |
}
|
|
|
247 |
|
|
|
248 |
if(basename[0] == 0) {
|
|
|
249 |
DEBUGF("mkdir: Empty dir name");
|
|
|
250 |
errno = EINVAL;
|
|
|
251 |
return -3;
|
|
|
252 |
}
|
|
|
253 |
|
|
|
254 |
/* Now check if the name already exists */
|
|
|
255 |
while ((entry = readdir(dir))) {
|
|
|
256 |
if ( !strcasecmp(basename, entry->d_name) ) {
|
|
|
257 |
DEBUGF("mkdir error: file exists");
|
|
|
258 |
errno = EEXIST;
|
|
|
259 |
closedir(dir);
|
|
|
260 |
return - 4;
|
|
|
261 |
}
|
|
|
262 |
}
|
|
|
263 |
|
| 211 |
theseven |
264 |
rc = fat_create_dir(basename, &(dir->fatdir));
|
| 60 |
theseven |
265 |
closedir(dir);
|
|
|
266 |
|
|
|
267 |
return rc;
|
|
|
268 |
}
|
|
|
269 |
|
|
|
270 |
int rmdir(const char* name)
|
|
|
271 |
{
|
|
|
272 |
int rc;
|
|
|
273 |
DIR* dir;
|
|
|
274 |
struct dirent* entry;
|
|
|
275 |
|
|
|
276 |
dir = opendir(name);
|
|
|
277 |
if (!dir)
|
|
|
278 |
{
|
|
|
279 |
errno = ENOENT; /* open error */
|
|
|
280 |
return -1;
|
|
|
281 |
}
|
|
|
282 |
|
|
|
283 |
/* check if the directory is empty */
|
|
|
284 |
while ((entry = readdir(dir)))
|
|
|
285 |
{
|
|
|
286 |
if (strcmp(entry->d_name, ".") &&
|
|
|
287 |
strcmp(entry->d_name, ".."))
|
|
|
288 |
{
|
|
|
289 |
DEBUGF("rmdir error: not empty");
|
|
|
290 |
errno = ENOTEMPTY;
|
|
|
291 |
closedir(dir);
|
|
|
292 |
return -2;
|
|
|
293 |
}
|
|
|
294 |
}
|
|
|
295 |
|
|
|
296 |
rc = fat_remove(&(dir->fatdir.file));
|
|
|
297 |
if ( rc < 0 ) {
|
|
|
298 |
DEBUGF("Failed removing dir: %d", rc);
|
|
|
299 |
errno = EIO;
|
|
|
300 |
rc = rc * 10 - 3;
|
|
|
301 |
}
|
|
|
302 |
|
|
|
303 |
closedir(dir);
|
|
|
304 |
return rc;
|
|
|
305 |
}
|
|
|
306 |
|
|
|
307 |
#ifdef HAVE_MULTIVOLUME
|
|
|
308 |
/* returns on which volume this is, and copies the reduced name
|
|
|
309 |
(sortof a preprocessor for volume-decorated pathnames) */
|
|
|
310 |
int strip_volume(const char* name, char* namecopy)
|
|
|
311 |
{
|
|
|
312 |
int volume = 0;
|
|
|
313 |
const char *temp = name;
|
|
|
314 |
|
|
|
315 |
while (*temp == '/') /* skip all leading slashes */
|
|
|
316 |
++temp;
|
|
|
317 |
|
|
|
318 |
if (*temp && !strncmp(temp, VOL_NAMES, VOL_ENUM_POS))
|
|
|
319 |
{
|
|
|
320 |
temp += VOL_ENUM_POS; /* behind special name */
|
|
|
321 |
volume = atoi(temp); /* number is following */
|
|
|
322 |
temp = strchr(temp, '/'); /* search for slash behind */
|
|
|
323 |
if (temp != NULL)
|
|
|
324 |
name = temp; /* use the part behind the volume */
|
|
|
325 |
else
|
|
|
326 |
name = "/"; /* else this must be the root dir */
|
|
|
327 |
}
|
|
|
328 |
|
|
|
329 |
strlcpy(namecopy, name, MAX_PATH);
|
|
|
330 |
|
|
|
331 |
return volume;
|
|
|
332 |
}
|
|
|
333 |
#endif /* #ifdef HAVE_MULTIVOLUME */
|