Subversion Repositories freemyipod

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
60 theseven 1
/***************************************************************************
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
    struct fat_dir newdir;
221
    int rc;
222
 
223
    if ( name[0] != '/' ) {
224
        DEBUGF("mkdir: Only absolute paths supported right now");
225
        return -1;
226
    }
227
 
228
    strlcpy(namecopy, name, sizeof(namecopy));
229
 
230
    /* Split the base name and the path */
231
    end = strrchr(namecopy, '/');
232
    *end = 0;
233
    basename = end+1;
234
 
235
    if(namecopy == end) /* Root dir? */
236
        parent = "/";
237
    else
238
        parent = namecopy;
239
 
240
    DEBUGF("mkdir: parent: %s, name: %s", parent, basename);
241
 
242
    dir = opendir(parent);
243
 
244
    if(!dir) {
245
        DEBUGF("mkdir: can't open parent dir");
246
        return -2;
247
    }    
248
 
249
    if(basename[0] == 0) {
250
        DEBUGF("mkdir: Empty dir name");
251
        errno = EINVAL;
252
        return -3;
253
    }
254
 
255
    /* Now check if the name already exists */
256
    while ((entry = readdir(dir))) {
257
        if ( !strcasecmp(basename, entry->d_name) ) {
258
            DEBUGF("mkdir error: file exists");
259
            errno = EEXIST;
260
            closedir(dir);
261
            return - 4;
262
        }
263
    }
264
 
265
    memset(&newdir, 0, sizeof(struct fat_dir));
266
 
267
    rc = fat_create_dir(basename, &newdir, &(dir->fatdir));
268
    closedir(dir);
269
 
270
    return rc;
271
}
272
 
273
int rmdir(const char* name)
274
{
275
    int rc;
276
    DIR* dir;
277
    struct dirent* entry;
278
 
279
    dir = opendir(name);
280
    if (!dir)
281
    {
282
        errno = ENOENT; /* open error */
283
        return -1;
284
    }
285
 
286
    /* check if the directory is empty */
287
    while ((entry = readdir(dir)))
288
    {
289
        if (strcmp(entry->d_name, ".") &&
290
            strcmp(entry->d_name, ".."))
291
        {
292
            DEBUGF("rmdir error: not empty");
293
            errno = ENOTEMPTY;
294
            closedir(dir);
295
            return -2;
296
        }
297
    }
298
 
299
    rc = fat_remove(&(dir->fatdir.file));
300
    if ( rc < 0 ) {
301
        DEBUGF("Failed removing dir: %d", rc);
302
        errno = EIO;
303
        rc = rc * 10 - 3;
304
    }
305
 
306
    closedir(dir);
307
    return rc;
308
}
309
 
310
#ifdef HAVE_MULTIVOLUME
311
/* returns on which volume this is, and copies the reduced name
312
   (sortof a preprocessor for volume-decorated pathnames) */
313
int strip_volume(const char* name, char* namecopy)
314
{
315
    int volume = 0;
316
    const char *temp = name;
317
 
318
    while (*temp == '/')          /* skip all leading slashes */
319
        ++temp;
320
 
321
    if (*temp && !strncmp(temp, VOL_NAMES, VOL_ENUM_POS))
322
    {
323
        temp += VOL_ENUM_POS;     /* behind special name */
324
        volume = atoi(temp);      /* number is following */
325
        temp = strchr(temp, '/'); /* search for slash behind */
326
        if (temp != NULL)
327
            name = temp;          /* use the part behind the volume */
328
        else
329
            name = "/";           /* else this must be the root dir */
330
    }
331
 
332
    strlcpy(namecopy, name, MAX_PATH);
333
 
334
    return volume;
335
}
336
#endif /* #ifdef HAVE_MULTIVOLUME */