Subversion Repositories freemyipod

Rev

Rev 59 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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