Subversion Repositories freemyipod

Rev

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

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