Subversion Repositories freemyipod

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
770 user890104 1
//
2
//
3
//    Copyright 2011 user890104
4
//
5
//
6
//    This file is part of emCORE.
7
//
8
//    emCORE is free software: you can redistribute it and/or
9
//    modify it under the terms of the GNU General Public License as
10
//    published by the Free Software Foundation, either version 2 of the
11
//    License, or (at your option) any later version.
12
//
13
//    emCORE is distributed in the hope that it will be useful,
14
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
15
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16
//    See the GNU General Public License for more details.
17
//
18
//    You should have received a copy of the GNU General Public License along
19
//    with emCORE.  If not, see <http://www.gnu.org/licenses/>.
20
//
21
//
22
 
23
 
24
#include "global.h"
25
 
26
#include "fuse.h"
27
#include "util.h"
28
#include "cache.h"
29
#include "emcore.h"
30
 
31
 
32
int emcorefs_getattr(const char* path, struct stat* stbuf)
33
{
34
    int res = 0;
35
    struct emcore_dir_entry *entry = NULL, curr_entry;
36
    uint32_t dir_handle;
37
    char *parent, *filename;
38
 
39
    memset(stbuf, 0, sizeof(*stbuf));
40
 
41
    entry = cache_get(path);
42
 
43
    if (NULL == entry)
44
    {
45
        parent = strdup(path);
46
        dirname(parent);
47
        filename = basename((char*)path);
48
 
49
        res = emcore_dir_open(&dir_handle, parent);
50
 
51
        if (EMCORE_SUCCESS == res)
52
        {
53
            while (1)
54
            {
55
                res = emcore_dir_read(&curr_entry, dir_handle);
56
 
57
                if (EMCORE_ERROR_NO_MORE_ENTRIES == res)
58
                {
59
                    break;
60
                }
61
 
62
                if (EMCORE_SUCCESS != res)
63
                {
64
                    break;
65
                }
66
 
67
                cache_insert(parent, &curr_entry);
68
 
69
                if (0 == strcmp(filename, curr_entry.name))
70
                {
71
                    entry = malloc(sizeof(*entry));
72
 
73
                    memcpy(entry, &curr_entry, sizeof(curr_entry));
74
 
75
                    break;
76
                }
77
            };
78
 
79
            emcore_dir_close(dir_handle);
80
        }
81
 
82
        free(parent);
83
    }
84
 
85
    if (NULL == entry) {
86
        if (EMCORE_ERROR_NO_MORE_ENTRIES == res)
87
        {
88
            return -ENOENT;
89
        }
90
 
91
        return -EIO;
92
    }
93
    else
94
    {
95
        stbuf->st_uid = getuid();
96
        stbuf->st_gid = getgid();
97
        stbuf->st_mtime = fat_time_to_unix_ts(entry->wrttime, entry->wrtdate);
98
 
99
        if (entry->attributes & 0x10)
100
        {
101
            stbuf->st_mode = S_IFDIR | 0755;
102
            stbuf->st_nlink = 2;
103
            stbuf->st_size = 0x1000;
104
        }
105
        else
106
        {
107
            stbuf->st_mode = S_IFREG | 0644;
108
            stbuf->st_nlink = 1;
109
            stbuf->st_size = entry->size;
110
        }
111
    }
112
 
113
    return 0;
114
}
115
 
116
int emcorefs_opendir(const char* path, struct fuse_file_info* fi)
117
{
118
    int res;
119
    uint32_t handle;
120
 
121
    res = emcore_dir_open(&handle, path);
122
 
123
    if (EMCORE_SUCCESS != res)
124
    {
125
        return -EIO;
126
    }
127
 
128
    fi->fh = handle;
129
 
130
    return 0;
131
}
132
 
133
int emcorefs_readdir(const char* path, void* buf, fuse_fill_dir_t filler, off_t offset, struct fuse_file_info* fi)
134
{
135
    int res;
136
    struct emcore_dir_entry entry;
137
    (void) offset;
138
    (void) fi;
139
 
140
    if (strcmp(path, "/") == 0)
141
    {
142
        filler(buf, ".", NULL, 0);
143
        filler(buf, "..", NULL, 0);
144
    }
145
 
146
    while (1)
147
    {
148
        res = emcore_dir_read(&entry, fi->fh);
149
 
150
        if (EMCORE_ERROR_NO_MORE_ENTRIES == res)
151
        {
152
            break;
153
        }
154
 
155
        if (EMCORE_SUCCESS != res)
156
        {
157
            return -EIO;
158
        }
159
 
160
        cache_insert(path, &entry);
161
 
162
        filler(buf, entry.name, NULL, 0);
163
    }
164
 
165
    return 0;
166
}
167
 
168
int emcorefs_releasedir(const char* path, struct fuse_file_info* fi)
169
{
170
    int res;
171
    uint32_t emcore_errno_value;
172
    (void)path;
173
 
174
    res = emcore_dir_close(fi->fh);
175
 
176
    if (EMCORE_ERROR_IO == res)
177
    {
178
        res = emcore_errno(&emcore_errno_value);
179
 
180
        if (EMCORE_SUCCESS != res)
181
        {
182
            return -EIO;
183
        }
184
 
185
        if (EMCORE_SUCCESS != emcore_errno_value)
186
        {
187
            return -emcore_errno_value;
188
        }
189
    }
190
 
191
    if (EMCORE_SUCCESS != res)
192
    {
193
        return -EIO;
194
    }
195
 
196
    return 0;
197
}
198
 
199
int emcorefs_open(const char* path, struct fuse_file_info* fi)
200
{
201
    int res;
202
    uint32_t handle, emcore_errno_value;
203
 
204
    res = emcore_file_open(&handle, path, fi->flags);
205
 
206
    if (EMCORE_ERROR_IO == res)
207
    {
208
        res = emcore_errno(&emcore_errno_value);
209
 
210
        if (EMCORE_SUCCESS != res)
211
        {
212
            return -EIO;
213
        }
214
 
215
        if (EMCORE_SUCCESS != emcore_errno_value)
216
        {
217
            return -emcore_errno_value;
218
        }
219
    }
220
 
221
    if (EMCORE_SUCCESS != res)
222
    {
223
        return -EIO;
224
    }
225
 
226
    fi->fh = handle;
227
 
228
    return 0;
229
}
230
 
231
int emcorefs_read(const char* path, char* buf, uint32_t size, off_t offset, struct fuse_file_info* fi) {
232
    fprintf(stderr, "FUSE_READ: path=[%s] size=[%d] offset=[%jd] fi->flags=[%d]\n", path, size, offset, fi->flags);
233
 
234
    int res;
235
    uint32_t emcore_errno_value, addr, nread = size;
236
 
237
    if (!fi->fh)
238
    {
239
        return -EIO;
240
    }
241
 
242
    res = emcore_malloc(&addr, size);
243
 
244
    if (EMCORE_SUCCESS != res)
245
    {
246
        return -EIO;
247
    }
248
 
249
    do {
250
        if (offset) {
251
            res = emcore_file_seek(fi->fh, offset, SEEK_SET);
252
 
253
            if (EMCORE_ERROR_IO == res)
254
            {
255
                res = emcore_errno(&emcore_errno_value);
256
 
257
                if (EMCORE_SUCCESS != res)
258
                {
259
                    nread = -EIO;
260
                    break;
261
                }
262
 
263
                if (EMCORE_SUCCESS != emcore_errno_value)
264
                {
265
                    nread = -emcore_errno_value;
266
                    break;
267
                }
268
            }
269
 
270
            if (EMCORE_SUCCESS != res)
271
            {
272
                nread = -EIO;
273
                break;
274
            }
275
        }
276
 
277
        res = emcore_file_read(&nread, fi->fh, addr, size);
278
 
279
        if (EMCORE_ERROR_IO == res)
280
        {
281
            res = emcore_errno(&emcore_errno_value);
282
 
283
            if (EMCORE_SUCCESS != res)
284
            {
285
                nread = -EIO;
286
                break;
287
            }
288
 
289
            if (EMCORE_SUCCESS != emcore_errno_value)
290
            {
291
                nread = -emcore_errno_value;
292
                break;
293
            }
294
        }
295
 
296
        if (EMCORE_SUCCESS != res)
297
        {
298
            nread = -EIO;
299
            break;
300
        }
301
 
302
        res = emcore_read(buf, addr, nread);
303
 
304
        if (EMCORE_SUCCESS != res)
305
        {
306
            nread = -EIO;
307
        }
308
 
309
    }
310
    while(0);
311
 
312
    res = emcore_free(addr);
313
 
314
    if (EMCORE_SUCCESS != res)
315
    {
316
        return -EIO;
317
    }
318
 
319
    return nread;
320
}
321
 
322
int emcorefs_release(const char* path, struct fuse_file_info* fi)
323
{
324
    int res;
325
    uint32_t emcore_errno_value;
326
    (void)path;
327
 
328
    if (!fi->fh)
329
    {
330
        return -EIO;
331
    }
332
 
333
    res = emcore_file_close(fi->fh);
334
 
335
    if (EMCORE_ERROR_IO == res)
336
    {
337
        res = emcore_errno(&emcore_errno_value);
338
 
339
        if (EMCORE_SUCCESS != res)
340
        {
341
            return -EIO;
342
        }
343
 
344
        if (EMCORE_SUCCESS != emcore_errno_value)
345
        {
346
            return -emcore_errno_value;
347
        }
348
    }
349
 
350
    if (EMCORE_SUCCESS != res)
351
    {
352
        return -EIO;
353
    }
354
 
355
    return 0;
356
}