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