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