-
Notifications
You must be signed in to change notification settings - Fork 0
/
fusefatfs.c
611 lines (563 loc) · 16.2 KB
/
fusefatfs.c
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
/**
* Copyright (c) 2020 Renzo Davoli <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; If not, see <http://www.gnu.org/licenses/>.
*
*/
#if FUSE == 2
#define FUSE_USE_VERSION 29
#define FUSE3_ONLY(...)
#else
#define FUSE_USE_VERSION FUSE_MAKE_VERSION(3, 14)
#define FUSE3_ONLY(...) __VA_ARGS__
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fuse.h>
#include <time.h>
#include <stddef.h>
#include <pthread.h>
#include <ff.h>
#include <fftable.h>
#include <config.h>
int fuse_reentrant_tag = 0;
#if FF_DEFINED == 80286
#define FF_VERSION "0.15"
#else
#error FuseFat version mismaatch
#endif
#define FAT_DEFAULT_CODEPAGE 850
static pthread_mutex_t fff_mutex = PTHREAD_MUTEX_INITIALIZER;
#define mutex_in() pthread_mutex_lock(&fff_mutex)
#define mutex_out() pthread_mutex_unlock(&fff_mutex)
#define mutex_out_return(RETVAL) do {mutex_out(); return(RETVAL); } while (0)
#define fffpath(index, path) \
*fffpath; \
ssize_t __fffpathlen = (index == 0) ? 0 : strlen(path) + 3; \
char __fffpath[__fffpathlen]; \
if (index != 0) { \
snprintf(__fffpath, __fffpathlen, "%d:%s", index, path); \
fffpath = __fffpath; \
} else \
fffpath = path
static int fr2errno(FRESULT fres) {
switch (fres) {
case FR_OK: return 0;
case FR_NO_FILE:
case FR_NO_PATH:
return -ENOENT;
case FR_INVALID_NAME:
case FR_INVALID_PARAMETER:
return -EINVAL;
case FR_DENIED:
return -EACCES;
case FR_WRITE_PROTECTED:
return -EROFS;
case FR_EXIST:
return -EEXIST;
case FR_NOT_ENOUGH_CORE:
return -ENOMEM;
default:
return -EIO;
}
}
static BYTE flags2ffmode(int flags) {
// O_RDONLY -> FA_READ, O_WRONLY -> FA_WRITE, O_RDWR -> FA_READ | FA_WRITE
BYTE ffmode = ((flags & O_ACCMODE) + 1) & O_ACCMODE;
if (flags & O_CREAT) {
if (flags & O_EXCL)
ffmode |= FA_CREATE_NEW;
else if (flags & O_TRUNC)
ffmode |= FA_CREATE_ALWAYS;
else
ffmode |= FA_OPEN_ALWAYS;
}
if (flags & O_APPEND) {
ffmode |= FA_OPEN_APPEND;
}
return ffmode;
}
static time_t fftime2time(WORD fdate, WORD ftime) {
if (fdate == 0 && ftime == 0)
return 0;
else {
struct tm tm = {0};
tm.tm_year = ((fdate >> 9) & 0x7f) + 80;
tm.tm_mon = ((fdate >> 5) & 0xf) - 1;
tm.tm_mday = fdate & 0x1f;
tm.tm_hour = (ftime >> 11) & 0x1f;
tm.tm_min = (ftime >> 5) & 0x3f;
tm.tm_sec = (ftime & 0x1f) * 2;
return mktime(&tm);
}
}
static int fff_getattr(const char *path, struct stat *stbuf FUSE3_ONLY(, struct fuse_file_info *fi))
{
FUSE3_ONLY((void) fi);
mutex_in();
struct fuse_context *cntx=fuse_get_context();
struct fftab *ffentry = cntx->private_data;
FRESULT fres;
// f_stat path: The object must not be the root directory */
if (strcmp(path, "/") == 0) {
memset(stbuf, 0, sizeof(struct stat));
stbuf->st_mode = 0755 | S_IFDIR;
stbuf->st_nlink = 2;
mutex_out_return(0);
} else {
const char fffpath(ffentry->index, path);
FILINFO fileinfo;
fres = f_stat(fffpath, &fileinfo);
//printf("getattr %s %s -> %d\n", path, fffpath, fres);
if (fres != FR_OK) goto err;
memset(stbuf, 0, sizeof(struct stat));
stbuf->st_size = fileinfo.fsize;
stbuf->st_ctime = stbuf->st_mtime =
fftime2time(fileinfo.fdate, fileinfo.ftime);
if (fileinfo.fattrib & AM_DIR) {
stbuf->st_mode = 0755 | S_IFDIR;
stbuf->st_nlink = 2;
} else {
stbuf->st_nlink = 1;
stbuf->st_mode = 0755 | S_IFREG;
}
if (fileinfo.fattrib & AM_RDO)
stbuf->st_mode &= ~0222;
}
mutex_out_return(0);
err:
mutex_out_return(fr2errno(fres));
}
static int fff_open(const char *path, struct fuse_file_info *fi){
mutex_in();
struct fuse_context *cntx=fuse_get_context();
struct fftab *ffentry = cntx->private_data;
const char fffpath(ffentry->index, path);
if ((ffentry->flags & FFFF_RDONLY) && (fi->flags & O_ACCMODE) != O_RDONLY)
mutex_out_return(-EROFS);
FIL fp;
FRESULT fres = f_open(&fp, fffpath, flags2ffmode(fi->flags));
if (fres == FR_OK)
f_close(&fp);
mutex_out_return(fr2errno(fres));
}
static int fff_create(const char *path, mode_t mode, struct fuse_file_info *fi){
(void) fi;
(void) mode; // XXX set readonly?
mutex_in();
struct fuse_context *cntx=fuse_get_context();
struct fftab *ffentry = cntx->private_data;
const char fffpath(ffentry->index, path);
if (ffentry->flags & FFFF_RDONLY)
mutex_out_return(-EROFS);
FIL fp;
FRESULT fres = f_open(&fp, fffpath, flags2ffmode(fi->flags | O_CREAT));
if (fres == FR_OK)
f_close(&fp);
mutex_out_return(fr2errno(fres));
}
static int fff_release(const char *path, struct fuse_file_info *fi){
(void) path;
(void) fi;
return 0;
}
static int fff_read(const char *path, char *buf, size_t size, off_t offset, struct fuse_file_info *fi){
mutex_in();
struct fuse_context *cntx=fuse_get_context();
struct fftab *ffentry = cntx->private_data;
const char fffpath(ffentry->index, path);
FIL fp;
UINT br;
FRESULT fres = f_open(&fp, fffpath, flags2ffmode(fi->flags));
if (fres != FR_OK)
goto earlyerr;
fres = f_lseek(&fp, offset);
if (fres != FR_OK) goto err;
fres = f_read(&fp, buf, size, &br);
if (fres != FR_OK) goto err;
f_close(&fp);
mutex_out_return(br);
err:
f_close(&fp);
earlyerr:
mutex_out_return(fr2errno(fres));
}
static int fff_write(const char *path, const char *buf, size_t size, off_t offset, struct fuse_file_info *fi){
mutex_in();
struct fuse_context *cntx=fuse_get_context();
struct fftab *ffentry = cntx->private_data;
const char fffpath(ffentry->index, path);
FIL fp;
UINT bw;
if (ffentry->flags & FFFF_RDONLY)
mutex_out_return(-EROFS);
FRESULT fres = f_open(&fp, fffpath, flags2ffmode(fi->flags));
if (fres != FR_OK)
goto earlyerr;
fres = f_lseek(&fp, offset);
if (fres != FR_OK) goto err;
fres = f_write(&fp, buf, size, &bw);
if (fres != FR_OK) goto err;
fres = f_sync(&fp);
if (fres != FR_OK) goto err;
f_close(&fp);
mutex_out_return(bw);
err:
f_close(&fp);
earlyerr:
mutex_out_return(fr2errno(fres));
}
static int fff_opendir(const char *path, struct fuse_file_info *fi){
(void) fi;
mutex_in();
struct fuse_context *cntx=fuse_get_context();
struct fftab *ffentry = cntx->private_data;
const char fffpath(ffentry->index, path);
DIR dp;
FRESULT fres = f_opendir(&dp, fffpath);
f_closedir(&dp);
mutex_out_return(fr2errno(fres));
}
static int fff_releasedir(const char *path, struct fuse_file_info *fi){
(void) path;
(void) fi;
return 0;
}
static int fff_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
off_t offset, struct fuse_file_info *fi FUSE3_ONLY(, enum fuse_readdir_flags fl)){
(void) offset;
(void) fi;
FUSE3_ONLY((void) fl);
mutex_in();
struct fuse_context *cntx=fuse_get_context();
struct fftab *ffentry = cntx->private_data;
const char fffpath(ffentry->index, path);
DIR dp;
FRESULT fres = f_opendir(&dp, fffpath);
if (fres != FR_OK)
goto mutexout_leave;
filler(buf, ".", NULL, 0 FUSE3_ONLY(, 0));
filler(buf, "..", NULL, 0 FUSE3_ONLY(, 0));
while(1) {
FILINFO fileinfo;
fres = f_readdir(&dp, &fileinfo);
if (fres != FR_OK) break;
if (fileinfo.fname[0] == 0) break;
filler(buf, fileinfo.fname, NULL, 0 FUSE3_ONLY(, 0));
}
f_closedir(&dp);
mutexout_leave:
mutex_out_return(fr2errno(fres));
}
static int fff_mkdir(const char *path, mode_t mode) {
(void) mode; // XXX set readonly
mutex_in();
struct fuse_context *cntx=fuse_get_context();
struct fftab *ffentry = cntx->private_data;
const char fffpath(ffentry->index, path);
if (ffentry->flags & FFFF_RDONLY)
mutex_out_return(-EROFS);
FRESULT fres = f_mkdir(fffpath);
if (fres != FR_OK) return fr2errno(fres);
// XXX mode?
mutex_out_return(0);
}
static int fff_unlink(const char *path) {
mutex_in();
struct fuse_context *cntx=fuse_get_context();
struct fftab *ffentry = cntx->private_data;
const char fffpath(ffentry->index, path);
if (ffentry->flags & FFFF_RDONLY)
mutex_out_return(-EROFS);
// XXX ck is it reg file ?
FRESULT fres = f_unlink(fffpath);
mutex_out_return(fr2errno(fres));
}
static int fff_rmdir(const char *path) {
mutex_in();
struct fuse_context *cntx=fuse_get_context();
struct fftab *ffentry = cntx->private_data;
const char fffpath(ffentry->index, path);
if (ffentry->flags & FFFF_RDONLY)
mutex_out_return(-EROFS);
// XXX ck is it a dir ?
FRESULT fres = f_unlink(fffpath);
mutex_out_return(fr2errno(fres));
}
static int fff_rename(const char *path, const char *newpath FUSE3_ONLY(, unsigned int flags)) {
FUSE3_ONLY(if(flags) return -ENOSYS;)
mutex_in();
struct fuse_context *cntx=fuse_get_context();
struct fftab *ffentry = cntx->private_data;
const char fffpath(ffentry->index, path);
if (ffentry->flags & FFFF_RDONLY)
mutex_out_return(-EROFS);
FRESULT fres = f_rename(fffpath, newpath);
mutex_out_return(fr2errno(fres));
}
static int fff_truncate(const char *path, off_t size FUSE3_ONLY(, struct fuse_file_info *fi)) {
FUSE3_ONLY((void) fi);
mutex_in();
struct fuse_context *cntx=fuse_get_context();
struct fftab *ffentry = cntx->private_data;
const char fffpath(ffentry->index, path);
if (ffentry->flags & FFFF_RDONLY)
mutex_out_return(-EROFS);
FIL fp;
memset(&fp, 0, sizeof(fp));
FRESULT fres = f_open(&fp, fffpath, FA_WRITE);
if (fres != FR_OK) goto openerr;
fres = f_lseek(&fp, size);
if (fres != FR_OK) goto err;
fres = f_truncate(&fp);
if (fres != FR_OK) goto err;
fres = f_close(&fp);
openerr:
mutex_out_return(fr2errno(fres));
err:
f_close(&fp);
mutex_out_return(fr2errno(fres));
}
static int fff_utimens(const char *path, const struct timespec tv[2] FUSE3_ONLY(, struct fuse_file_info *fi)) {
FUSE3_ONLY((void) fi);
mutex_in();
struct fuse_context *cntx=fuse_get_context();
struct fftab *ffentry = cntx->private_data;
const char fffpath(ffentry->index, path);
if (ffentry->flags & FFFF_RDONLY)
mutex_out_return(-EROFS);
FILINFO fno;
struct tm tm;
time_t newtime = tv[1].tv_sec;
if (gmtime_r(&newtime, &tm) == NULL)
mutex_out_return(-EINVAL);
fno.fdate =
/* bit15:9: Year origin from the 1980 (0..127, e.g. 37 for 2017) */
(((tm.tm_year - 80) & 0x7f) << 9) |
/* bit8:5: Month (1..12) */
(((tm.tm_mon + 1) & 0xf) << 5) |
/* bit4:0: Day of the month (1..31) */
(tm.tm_mday & 0x1f);
fno.ftime =
/* bit15:11: Hour (0..23)) */
((tm.tm_hour & 0x1f) << 11) |
/* bit10:5: Minute (0..59) */
((tm.tm_min & 0x3f) << 5) |
/* bit4:0 Second / 2 (0..29, e.g. 25 for 50) */
((tm.tm_sec & 0x3f) / 2);
FRESULT fres = f_utime(fffpath, &fno);
mutex_out_return(fr2errno(fres));
}
static int fff_statfs(const char *path, struct statvfs *buf) {
(void) path;
mutex_in();
struct fuse_context *cntx=fuse_get_context();
struct fftab *ffentry = cntx->private_data;
const char fffpath(ffentry->index, "");
memset(buf, 0, sizeof(*buf));
FATFS *fs;
DWORD fre_clust;
FRESULT fres = f_getfree(fffpath, &fre_clust, &fs);
if (fres == FR_OK) {
WORD ssize =
#if FF_MAX_SS != FF_MIN_SS
fs->ssize
#else
FF_MAX_SS
#endif
;
buf->f_bsize = buf->f_frsize = fs->csize * ssize;
buf->f_blocks = ((fs->n_fatent - 2) * ssize) / S_BLKSIZE;
buf->f_bfree = buf->f_bavail = (fre_clust * ssize) / S_BLKSIZE;
buf->f_namemax = 255;
}
mutex_out_return(fr2errno(fres));
}
static struct fftab *fff_init(const char *source, int codepage, int flags) {
int index = fftab_new(source, flags);
if (index >= 0) {
struct fftab *ffentry = fftab_get(index);
char sdrv[12];
snprintf(sdrv, 12, "%d:", index);
FRESULT fres = f_mount(&ffentry->fs, sdrv, 1);
if (fres != FR_OK) {
fftab_del(index);
return NULL;
}
if (codepage != 0) {
if (f_setcp(codepage) != FR_OK) {
fprintf(stderr, "codepage %d unavailable\n", codepage);
f_setcp(FAT_DEFAULT_CODEPAGE);
}
} else
f_setcp(FAT_DEFAULT_CODEPAGE);
return ffentry;
} else
return NULL;
}
static void fff_destroy(struct fftab *ffentry) {
char sdrv[12];
snprintf(sdrv, 12, "%d:", ffentry->index);
f_mount(0, sdrv, 1);
fftab_del(ffentry->index);
}
int fff_access (const char *path, int mode) {
(void) path;
(void) mode;
return 0;
}
static const struct fuse_operations fusefat_ops = {
.getattr = fff_getattr,
.open = fff_open,
.create = fff_create,
.read = fff_read,
.write = fff_write,
.release = fff_release,
.opendir = fff_opendir,
.readdir = fff_readdir,
.releasedir = fff_releasedir,
.mkdir = fff_mkdir,
.unlink = fff_unlink,
.rmdir = fff_rmdir,
.rename = fff_rename,
.truncate = fff_truncate,
.utimens = fff_utimens,
.statfs = fff_statfs,
.access = fff_access,
};
static void usage(void)
{
fprintf(stderr,
"usage: " PROGNAME " image mountpoint [options]\n"
"\n"
"general options:\n"
" -o opt,[opt...] mount options\n"
" -h --help print help\n"
" -V --version print version\n"
"\n"
PROGNAME " options:\n"
" -o ro disable write support\n"
" -o rw+ enable write support\n"
" -o rw enable write support only together with -force\n"
" -o force enable write support only together with -rw\n"
" -o codepage=XXX set codepage (default 850)\n"
"\n"
" this software is still experimental\n"
"\n");
}
struct options {
const char *source;
const char *mountpoint;
int ro;
int rw;
int rwplus;
int force;
int codepage;
};
#define FFF_OPT(t, p, v) { t, offsetof(struct options, p), v }
static struct fuse_opt fff_opts[] =
{
FFF_OPT("ro", ro, 1),
FFF_OPT("rw", rw, 1),
FFF_OPT("rw+", rwplus, 1),
FFF_OPT("force", force, 1),
FFF_OPT("codepage=%u", codepage, 1),
FUSE_OPT_KEY("-V", 'V'),
FUSE_OPT_KEY("--version", 'V'),
FUSE_OPT_KEY("-h", 'h'),
FUSE_OPT_KEY("--help", 'h'),
FUSE_OPT_END
};
static int
fff_opt_proc(void *data, const char *arg, int key, struct fuse_args *outargs)
{
struct options *options = data;
switch(key) {
case FUSE_OPT_KEY_OPT:
return 1;
case FUSE_OPT_KEY_NONOPT:
if (!options->source) {
options->source = arg;
return 0;
} else if(!options->mountpoint) {
options->mountpoint = arg;
return 1;
} else
return -1;
break;
case 'h':
usage();
fuse_opt_add_arg(outargs, "-ho");
fuse_main(outargs->argc, outargs->argv, &fusefat_ops, NULL);
return -1;
case 'V':
fprintf(stderr, PROGNAME " version %s -- FatFS %s\n", VERSION, FF_VERSION);
fuse_opt_add_arg(outargs, "--version");
fuse_main(outargs->argc, outargs->argv, &fusefat_ops, NULL);
return -1;
default:
return -1;
}
}
int main(int argc, char *argv[])
{
int err;
struct options options = {0};
struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
struct fftab *ffentry;
int flags = 0;
struct stat sbuf;
putenv("TZ=UTC0");
if (fuse_opt_parse(&args, &options, fff_opts, fff_opt_proc) == -1) {
fuse_opt_free_args(&args);
return -1;
}
if (options.rw == 0 && options.rwplus == 0)
options.ro = 1;
if (options.rw == 1 && options.force == 0) {
fprintf(stderr,
"The file system will be mounted in read-only mode.\n"
"This is still experimental code.\n"
"The option to mount in read-write mode is: -o rw+\n"
"or: -o rw,force\n\n");
options.ro = 1;
}
if (options.source == NULL || options.mountpoint == NULL) {
usage();
goto returnerr;
}
if (stat(options.source, &sbuf) < 0) {
fprintf(stderr, "%s: %s\n", options.source, strerror(errno));
goto returnerr;
}
if (! S_ISREG(sbuf.st_mode) && ! S_ISBLK(sbuf.st_mode)) {
fprintf(stderr, "%s: source must be a block device or a regular file (image)\n", options.source);
goto returnerr;
}
if (options.ro) flags |= FFFF_RDONLY;
if ((ffentry = fff_init(options.source, options.codepage, flags)) == NULL) {
fprintf(stderr, "Fuse init error\n");
goto returnerr;
}
err = fuse_main(args.argc, args.argv, &fusefat_ops, ffentry);
fff_destroy(ffentry);
fuse_opt_free_args(&args);
if (err) fprintf(stderr, "Fuse error %d\n", err);
return err;
returnerr:
fuse_opt_free_args(&args);
return -1;
}