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