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