Subversion Repositories freemyipod

Rev

Rev 814 | Details | Compare with Previous | Last modification | View Log | RSS feed

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