-
Notifications
You must be signed in to change notification settings - Fork 3
/
rofs.c
488 lines (419 loc) · 9.72 KB
/
rofs.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
/*
* ROFS - The read-only filesystem for FUSE.
* Copyright 2005,2006,2008 Matthew Keller. [email protected] and others.
* v2008.09.24
*
* Mount any filesytem, or folder tree read-only, anywhere else.
* No warranties. No guarantees. No lawyers.
*
* I read (and borrowed) a lot of other FUSE code to write this.
* Similarities possibly exist- Wholesale reuse as well of other GPL code.
* Special mention to Rémi Flament and his loggedfs.
*
* Consider this code GPLv2.
*
* Compile: gcc -o rofs -Wall -ansi -W -std=c99 -g -ggdb -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -lfuse main.c
* Mount: rofs readwrite_filesystem mount_point
*
*/
#define FUSE_USE_VERSION 26
static const char* rofsVersion = "2008.09.24";
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/statvfs.h>
#include <stdio.h>
#include <strings.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/xattr.h>
#include <dirent.h>
#include <unistd.h>
#include <fuse.h>
// Global to store our read-write path
char *rw_path;
// Translate an rofs path into it's underlying filesystem path
static char* translate_path(const char* path)
{
char *rPath= malloc(sizeof(char)*(strlen(path)+strlen(rw_path)+1));
strcpy(rPath,rw_path);
if (rPath[strlen(rPath)-1]=='/') {
rPath[strlen(rPath)-1]='\0';
}
strcat(rPath,path);
return rPath;
}
/******************************
*
* Callbacks for FUSE
*
*
*
******************************/
static int callback_getattr(const char *path, struct stat *st_data)
{
int res;
char * ipath;
ipath=translate_path(path);
res = lstat(ipath, st_data);
free(ipath);
if(res == -1) {
return -errno;
}
return 0;
}
static int callback_readlink(const char *path, char *buf, size_t size)
{
int res;
char *ipath;
ipath=translate_path(path);
res = readlink(ipath, buf, size - 1);
free(ipath);
if(res == -1) {
return -errno;
}
buf[res] = '\0';
return 0;
}
static int callback_readdir(const char *path, void *buf, fuse_fill_dir_t filler,off_t offset, struct fuse_file_info *fi)
{
DIR *dp;
struct dirent *de;
int res;
(void) offset;
(void) fi;
char *ipath;
ipath=translate_path(path);
dp = opendir(ipath);
free(ipath);
if(dp == NULL) {
res = -errno;
return res;
}
while((de = readdir(dp)) != NULL) {
struct stat st;
memset(&st, 0, sizeof(st));
st.st_ino = de->d_ino;
st.st_mode = de->d_type << 12;
if (filler(buf, de->d_name, &st, 0))
break;
}
closedir(dp);
return 0;
}
static int callback_mknod(const char *path, mode_t mode, dev_t rdev)
{
(void)path;
(void)mode;
(void)rdev;
return -EROFS;
}
static int callback_mkdir(const char *path, mode_t mode)
{
(void)path;
(void)mode;
return -EROFS;
}
static int callback_unlink(const char *path)
{
(void)path;
return -EROFS;
}
static int callback_rmdir(const char *path)
{
(void)path;
return -EROFS;
}
static int callback_symlink(const char *from, const char *to)
{
(void)from;
(void)to;
return -EROFS;
}
static int callback_rename(const char *from, const char *to)
{
(void)from;
(void)to;
return -EROFS;
}
static int callback_link(const char *from, const char *to)
{
(void)from;
(void)to;
return -EROFS;
}
static int callback_chmod(const char *path, mode_t mode)
{
(void)path;
(void)mode;
return -EROFS;
}
static int callback_chown(const char *path, uid_t uid, gid_t gid)
{
(void)path;
(void)uid;
(void)gid;
return -EROFS;
}
static int callback_truncate(const char *path, off_t size)
{
(void)path;
(void)size;
return -EROFS;
}
static int callback_utime(const char *path, struct utimbuf *buf)
{
(void)path;
(void)buf;
return -EROFS;
}
static int callback_open(const char *path, struct fuse_file_info *finfo)
{
int res;
/* We allow opens, unless they're tring to write, sneaky
* people.
*/
int flags = finfo->flags;
if ((flags & O_WRONLY) || (flags & O_RDWR) || (flags & O_CREAT) || (flags & O_EXCL) || (flags & O_TRUNC) || (flags & O_APPEND)) {
return -EROFS;
}
char *ipath;
ipath=translate_path(path);
res = open(ipath, flags);
free(ipath);
if(res == -1) {
return -errno;
}
close(res);
return 0;
}
static int callback_read(const char *path, char *buf, size_t size, off_t offset, struct fuse_file_info *finfo)
{
int fd;
int res;
(void)finfo;
char *ipath;
ipath=translate_path(path);
fd = open(ipath, O_RDONLY);
free(ipath);
if(fd == -1) {
res = -errno;
return res;
}
res = pread(fd, buf, size, offset);
if(res == -1) {
res = -errno;
}
close(fd);
return res;
}
static int callback_write(const char *path, const char *buf, size_t size, off_t offset, struct fuse_file_info *finfo)
{
(void)path;
(void)buf;
(void)size;
(void)offset;
(void)finfo;
return -EROFS;
}
static int callback_statfs(const char *path, struct statvfs *st_buf)
{
int res;
char *ipath;
ipath=translate_path(path);
res = statvfs(path, st_buf);
free(ipath);
if (res == -1) {
return -errno;
}
return 0;
}
static int callback_release(const char *path, struct fuse_file_info *finfo)
{
(void) path;
(void) finfo;
return 0;
}
static int callback_fsync(const char *path, int crap, struct fuse_file_info *finfo)
{
(void) path;
(void) crap;
(void) finfo;
return 0;
}
static int callback_access(const char *path, int mode)
{
int res;
char *ipath;
ipath=translate_path(path);
/* Don't pretend that we allow writing
* Chris AtLee <[email protected]>
*/
if (mode & W_OK)
return -EROFS;
res = access(ipath, mode);
free(ipath);
if (res == -1) {
return -errno;
}
return res;
}
/*
* Set the value of an extended attribute
*/
static int callback_setxattr(const char *path, const char *name, const char *value, size_t size, int flags)
{
(void)path;
(void)name;
(void)value;
(void)size;
(void)flags;
return -EROFS;
}
/*
* Get the value of an extended attribute.
*/
static int callback_getxattr(const char *path, const char *name, char *value, size_t size)
{
int res;
char *ipath;
ipath=translate_path(path);
res = lgetxattr(ipath, name, value, size);
free(ipath);
if(res == -1) {
return -errno;
}
return res;
}
/*
* List the supported extended attributes.
*/
static int callback_listxattr(const char *path, char *list, size_t size)
{
int res;
char *ipath;
ipath=translate_path(path);
res = llistxattr(ipath, list, size);
free(ipath);
if(res == -1) {
return -errno;
}
return res;
}
/*
* Remove an extended attribute.
*/
static int callback_removexattr(const char *path, const char *name)
{
(void)path;
(void)name;
return -EROFS;
}
struct fuse_operations callback_oper = {
.getattr = callback_getattr,
.readlink = callback_readlink,
.readdir = callback_readdir,
.mknod = callback_mknod,
.mkdir = callback_mkdir,
.symlink = callback_symlink,
.unlink = callback_unlink,
.rmdir = callback_rmdir,
.rename = callback_rename,
.link = callback_link,
.chmod = callback_chmod,
.chown = callback_chown,
.truncate = callback_truncate,
.utime = callback_utime,
.open = callback_open,
.read = callback_read,
.write = callback_write,
.statfs = callback_statfs,
.release = callback_release,
.fsync = callback_fsync,
.access = callback_access,
/* Extended attributes support for userland interaction */
.setxattr = callback_setxattr,
.getxattr = callback_getxattr,
.listxattr = callback_listxattr,
.removexattr= callback_removexattr
};
enum {
KEY_HELP,
KEY_VERSION,
};
static void usage(const char* progname)
{
fprintf(stdout,
"usage: %s readwritepath mountpoint [options]\n"
"\n"
" Mounts readwritepath as a read-only mount at mountpoint\n"
"\n"
"general options:\n"
" -o opt,[opt...] mount options\n"
" -h --help print help\n"
" -V --version print version\n"
"\n", progname);
}
static int rofs_parse_opt(void *data, const char *arg, int key,
struct fuse_args *outargs)
{
(void) data;
switch (key)
{
case FUSE_OPT_KEY_NONOPT:
if (rw_path == 0)
{
rw_path = strdup(arg);
return 0;
}
else
{
return 1;
}
case FUSE_OPT_KEY_OPT:
return 1;
case KEY_HELP:
usage(outargs->argv[0]);
exit(0);
case KEY_VERSION:
fprintf(stdout, "ROFS version %s\n", rofsVersion);
exit(0);
default:
fprintf(stderr, "see `%s -h' for usage\n", outargs->argv[0]);
exit(1);
}
return 1;
}
static struct fuse_opt rofs_opts[] = {
FUSE_OPT_KEY("-h", KEY_HELP),
FUSE_OPT_KEY("--help", KEY_HELP),
FUSE_OPT_KEY("-V", KEY_VERSION),
FUSE_OPT_KEY("--version", KEY_VERSION),
FUSE_OPT_END
};
int main(int argc, char *argv[])
{
struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
int res;
res = fuse_opt_parse(&args, &rw_path, rofs_opts, rofs_parse_opt);
if (res != 0)
{
fprintf(stderr, "Invalid arguments\n");
fprintf(stderr, "see `%s -h' for usage\n", argv[0]);
exit(1);
}
if (rw_path == 0)
{
fprintf(stderr, "Missing readwritepath\n");
fprintf(stderr, "see `%s -h' for usage\n", argv[0]);
exit(1);
}
#if FUSE_VERSION >= 26
fuse_main(args.argc, args.argv, &callback_oper, NULL);
#else
fuse_main(args.argc, args.argv, &callback_oper);
#endif
return 0;
}