summaryrefslogtreecommitdiffstats
path: root/firmware/common/file.c
blob: a5d9443aeb11da65091a32e688fe6a1c33769a47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2002 by Bjrn Stenberg
 *
 * All files in this archive are subject to the GNU General Public License.
 * See the file COPYING in the source tree root for full license agreement.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
#include <string.h>
#include <errno.h>
#include "file.h"
#include "fat.h"
#include "types.h"
#include "dir.h"
#include "debug.h"

#define MAX_OPEN_FILES 4

struct filedesc {
    unsigned char cache[SECTOR_SIZE];
    int cacheoffset;
    int fileoffset;
    int size;
    struct fat_file fatfile;
    bool busy;
};

static struct filedesc openfiles[MAX_OPEN_FILES];

int open(char* pathname, int flags)
{
    DIR* dir;
    struct dirent* entry;
    int fd;
    char* name;
    int namelen;

    if ( pathname[0] != '/' ) {
        DEBUGF("'%s' is not an absolute path.\n",pathname);
        DEBUGF("Only absolute pathnames supported at the moment\n");
        errno = EINVAL;
        return -1;
    }

    /* find a free file descriptor */
    for ( fd=0; fd<MAX_OPEN_FILES; fd++ )
        if ( !openfiles[fd].busy )
            break;

    if ( fd == MAX_OPEN_FILES ) {
        DEBUGF("Too many files open\n");
        errno = EMFILE;
        return -1;
    }

    /* locate filename */
    name=strrchr(pathname+1,'/');
    if ( name ) {
        *name = 0;
        dir = opendir(pathname);
        *name = '/';
        name++;
    }
    else {
        dir = opendir("/");
        name = pathname+1;
    }
    if (!dir) {
        DEBUGF("Failed opening dir\n");
        errno = EIO;
        return -1;
    }

    /* scan dir for name */
    namelen = strlen(name);
    while ((entry = readdir(dir))) {
        if ( !strncmp(name, entry->d_name, namelen) ) {
            fat_open(entry->startcluster, &(openfiles[fd].fatfile));
            openfiles[fd].size = entry->size;
            break;
        }
        else {
            DEBUGF("entry: %s\n",entry->d_name);
        }
    }
    closedir(dir);
    if ( !entry ) {
        DEBUGF("Couldn't find %s in %s\n",name,pathname);
        errno = ENOENT;
        return -1;
    }

    openfiles[fd].cacheoffset = -1;
    openfiles[fd].fileoffset = 0;
    openfiles[fd].busy = TRUE;
    return fd;
}

int close(int fd)
{
    openfiles[fd].busy = FALSE;
    return 0;
}

int read(int fd, void* buf, int count)
{
    int sectors;
    int nread=0;

    if ( !openfiles[fd].busy ) {
        errno = EBADF;
        return -1;
    }

    /* attempt to read past EOF? */
    if ( count > openfiles[fd].size - openfiles[fd].fileoffset )
        count = openfiles[fd].size - openfiles[fd].fileoffset;

    /* any head bytes? */
    if ( openfiles[fd].cacheoffset != -1 ) {
        int headbytes;
        int offs = openfiles[fd].cacheoffset;
        if ( count <= SECTOR_SIZE - openfiles[fd].cacheoffset ) {
            headbytes = count;
            openfiles[fd].cacheoffset += count;
            if ( openfiles[fd].cacheoffset >= SECTOR_SIZE )
                openfiles[fd].cacheoffset = 0;
        }
        else {
            headbytes = SECTOR_SIZE - openfiles[fd].cacheoffset;
            openfiles[fd].cacheoffset = -1;
        }

        /* eof? */
        if ( openfiles[fd].fileoffset + headbytes > openfiles[fd].size )
            headbytes = openfiles[fd].size - openfiles[fd].fileoffset;

        memcpy( buf, openfiles[fd].cache + offs, headbytes );
        nread = headbytes;
        count -= headbytes;
    }

    /* read whole sectors right into the supplied buffer */
    sectors = count / SECTOR_SIZE;
    if ( sectors ) {
        int rc = fat_read(&(openfiles[fd].fatfile), sectors, buf+nread );
        if ( rc < 0 ) {
            DEBUGF("Failed reading %d sectors\n",sectors);
            errno = EIO;
            return -1;
        }
        else {
            if ( rc > 0 ) {
                nread += sectors * SECTOR_SIZE;
                count -= sectors * SECTOR_SIZE;
            }
            else {
                /* eof */
                count=0;
            }

            openfiles[fd].cacheoffset = -1;
        }
    }

    /* any tail bytes? */
    if ( count ) {
        if ( fat_read(&(openfiles[fd].fatfile), 1,
                      &(openfiles[fd].cache)) < 0 ) {
            DEBUGF("Failed caching sector\n");
            errno = EIO;
            return -1;
        }

        /* eof? */
        if ( openfiles[fd].fileoffset + count > openfiles[fd].size )
            count = openfiles[fd].size - openfiles[fd].fileoffset;

        memcpy( buf + nread, openfiles[fd].cache, count );
        nread += count;
        openfiles[fd].cacheoffset = count;
    }

    openfiles[fd].fileoffset += nread;
    return nread;
}

int lseek(int fd, int offset, int whence)
{
    int pos;
    int newsector;
    int oldsector;
    int sectoroffset;
    int rc;

    if ( !openfiles[fd].busy ) {
        errno = EBADF;
        return -1;
    }

    switch ( whence ) {
        case SEEK_SET:
            pos = offset;
            break;

        case SEEK_CUR:
            pos = openfiles[fd].fileoffset + offset;
            break;

        case SEEK_END:
            pos = openfiles[fd].size - offset;
            break;

        default:
            errno = EINVAL;
            return -1;
    }
    if ( (pos < 0) ||
         (pos > openfiles[fd].size) ) {
        errno = EINVAL;
        return -1;
    }

    /* new sector? */
    newsector = pos / SECTOR_SIZE;
    oldsector = openfiles[fd].fileoffset / SECTOR_SIZE;
    sectoroffset = pos % SECTOR_SIZE;

    if ( (newsector != oldsector) ||
         ((openfiles[fd].cacheoffset==-1) && sectoroffset) ) {
        if ( newsector != oldsector ) {
            rc = fat_seek(&(openfiles[fd].fatfile), newsector);
            if ( rc < 0 ) {
                errno = EIO;
                return -1;
            }
        }
        rc = fat_read(&(openfiles[fd].fatfile), 1,
                      &(openfiles[fd].cache));
        if ( rc < 0 ) {
            errno = EIO;
            return -1;
        }
    }

    openfiles[fd].cacheoffset = sectoroffset;
    openfiles[fd].fileoffset = pos;

    return pos;
}

/*
 * local variables:
 * eval: (load-file "../rockbox-mode.el")
 * end:
 */