Subversion Repositories freemyipod

Rev

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