-
Notifications
You must be signed in to change notification settings - Fork 11
/
rofs-filtered.c
998 lines (841 loc) · 25.6 KB
/
rofs-filtered.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
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
/* vi:ai:tabstop=8:shiftwidth=4:softtabstop=4:expandtab
*
* Author: Gabriel Burca (gburca dash fuse at ebixio dot com)
* Version: 1.7
* Latest version:
* https://github.com/gburca/rofs-filtered
*
* This FUSE file system allows the user to mount a directory read-only and filter
* the files shown in the read-only directory based on regular expressions found in
* the optional /etc/rofs-filtered.rc configuration file. See the rofs-filtered.rc
* file for more details.
*
* What's the use of such a file system? Say you have a ton of *.flac music
* files, along with the transcoded *.mp3 files in the same directory tree
* structure. Maybe you want to show only one of the formats to music players
* that can play both flac and mp3 so that the songs don't show up twice. You
* might also want to show only mp3 files to players that don't understand the
* flac format.
*
* Based on:
* ROFS - The read-only filesystem for FUSE.
*
* On Ubuntu/Debian install: libfuse2, libfuse-dev, fuse-utils
* Version 2.5 or later of FUSE is required. If needed, it can be obtained from
* debuntu.org by adding the following line to /etc/apt/sources.list:
* deb http://repository.debuntu.org/ dapper multiverse
*
* Compile using:
* make
* make install
*
* Mount by adding the following line to /etc/fstab:
* /full/path/to/rofs-filtered#/the/read/write/device /the/read/only/mount/point fuse defaults,allow_other 0 0
*
* Unmount: fusermount -u /the/read/only/mount/point
* OR
* Unmount: umount /the/read/only/mount/point
*
* The user might need to be in the "fuse" UNIX group.
*
*********************************************************************************
* Copyright (C) 2006-2014 Gabriel Burca (gburca dash fuse at ebixio dot com)
*
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*********************************************************************************
*/
#if HAVE_CONFIG_H
#include <config.h>
#endif
#if __APPLE__
#define llistxattr(path, list, size) (listxattr(path, list, size, XATTR_NOFOLLOW))
#define lgetxattr(path, name, value, size) (getxattr(path, name, value, size, 0, XATTR_NOFOLLOW))
#define lsetxattr(path, name, value, size, flags) (setxattr(path, name, value, size, 0, flags | XATTR_NOFOLLOW))
#endif
// We depend on version 2.5 of FUSE because it provides an "access" callback.
// Some applications would call "access" and figure out a file is writable
// (which was the default behavior of "access" prior to 2.5), then attempt to
// open the file "rw", fail, and bomb out because of the conflicting info.
#define FUSE_USE_VERSION 25
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/statvfs.h>
#include <strings.h>
#include <unistd.h>
#include <stdio.h>
#include <assert.h>
#include <errno.h>
#include <stddef.h>
#include <fcntl.h>
#include <sys/xattr.h>
#include <regex.h>
#include <syslog.h>
#include <fuse.h>
// AC_HEADER_STDC
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
// AC_HEADER_DIRENT
#if HAVE_DIRENT_H
# include <dirent.h>
# define NAMLEN(dirent) strlen((dirent)->d_name)
#else
# define dirent direct
# define NAMLEN(dirent) (dirent)->d_namlen
# if HAVE_SYS_NDIR_H
# include <sys/ndir.h>
# endif
# if HAVE_SYS_DIR_H
# include <sys/dir.h>
# endif
# if HAVE_NDIR_H
# include <ndir.h>
# endif
#endif
// Some hard-coded values for use with syslog
static const char *EXEC_NAME = "rofs-filtered";
static const int log_facility = LOG_DAEMON;
struct rofs_config {
char *rw_path;
char *config;
int invert;
int debug;
int preserve_perms;
};
// Global to store our configuration (the option parsing results)
struct rofs_config conf;
enum {
KEY_HELP,
KEY_VERSION,
KEY_DEBUG,
};
#ifdef SYSCONF_DIR
char *default_config_file = SYSCONF_DIR "/rofs-filtered.rc";
#else
char *default_config_file = "/etc/rofs-filtered.rc";
#endif
regex_t pattern;
mode_t *modes = NULL;
int modes_count = 0;
/** Log a message to syslog */
static inline void log_msg(const int level, const char *format, ... /*args*/) {
if (level == LOG_DEBUG && !conf.debug) return;
va_list ap;
va_start(ap, format);
vsyslog(log_facility | level, format, ap);
va_end(ap);
va_start(ap, format);
vfprintf(stderr, format, ap);
va_end(ap);
fprintf(stderr, "\n");
}
static char* concat_path(const char* p1, const char* p2) {
assert(p1 && p2);
size_t p1len = strlen(p1);
size_t p2len = strlen(p2);
assert(p1len && p2len);
// Need room for path separator and null terminator
char *path = malloc(p1len + p2len + 2);
if (!path) return NULL;
strcpy(path, p1);
if ((path[p1len - 1] != '/') && (p2[0] != '/')) {
// Add a "/" if neither p1 nor p2 has it.
strcat(path, "/");
} else if (path[p1len - 1] == '/') {
// If p1 ends in '/', we don't need it from p2
while (p2[0] == '/') p2++;
}
strcat(path, p2);
return path;
}
/** Translate an rofs path into its underlying filesystem path.
*
* @param path The full path, relative to the rofs mount point. For example, if
* the rofs is mounted at /a/path and there's a /a/path/file, the 'ls /a/path'
* command will result in calls to this function with the path argument set to
* "/" and "/file". */
static char* translate_path(const char* path)
{
return concat_path(conf.rw_path, path);
}
/** Report user-friendly regex errors */
static void log_regex_error(int error, regex_t *regex, const char* pattern) {
size_t msg_len;
char *err_msg;
msg_len = regerror(error, regex, NULL, 0);
err_msg = (char *)malloc(msg_len);
if (err_msg) {
regerror(error, regex, err_msg, msg_len);
log_msg(LOG_ERR, "RegEx error: \"%s\" while parsing pattern: \"%s\"",
err_msg, pattern);
//printf("Error: %s %s\n", err_msg, pattern);
free(err_msg);
}
regfree(regex);
}
static int add_mode(mode_t mode) {
mode &= S_IFMT;
for (int i = 0; i < modes_count; ++i)
if (mode == modes[i])
return 1;
int n = modes_count + 1;
mode_t *m = (mode_t*)realloc(modes, n * sizeof(*modes));
if (!m) {
log_msg(LOG_ERR, "Out of memory for additional type.");
return 0;
}
modes = m;
modes[modes_count] = mode;
modes_count = n;
return 1;
}
typedef struct {
char * str;
size_t capacity;
size_t length;
} buffer_t;
int buffer_init(buffer_t * buffer, size_t size)
{
buffer->capacity = size + 1;
buffer->length = 0;
buffer->str = malloc(buffer->capacity * sizeof(*buffer->str));
if (!buffer->str) return -1;
*buffer->str = '\0';
return 0;
}
static int regex_append(buffer_t * buffer, char * regex, size_t len)
{
int new_capacity = buffer->capacity;
while (new_capacity <= buffer->length + len + 3) {
new_capacity *= 2;
}
if (new_capacity > buffer->capacity) {
char* str = realloc(buffer->str, new_capacity * sizeof(*buffer->str));
if (!str) return -1;
buffer->str = str;
buffer->capacity = new_capacity;
}
char * pos = buffer->str + buffer->length;
if (buffer->length > 0) *pos++ = '|';
*pos++ = '(';
strncpy(pos, regex, len); pos += len;
*pos++ = ')';
*pos = '\0';
buffer->length = pos - buffer->str;
return 0;
}
/** Read the RegEx configuration file */
static int read_config(const char *conf_file) {
char *line = NULL;
size_t line_size = 0;
int regcomp_res, pcount = 0;
int ret = 0;
FILE *fh = fopen(conf_file, "r");
if (fh == NULL) {
log_msg(LOG_ERR, "Failed to open config file: %s", conf_file);
ret = -1;
goto exit;
}
// File types we want to ignore
regex_t type_pattern;
regcomp_res = regcomp(&type_pattern, "^\\|\\s*type:\\s*(CHR|BLK|FIFO|LNK|SOCK)\\s*$", REG_EXTENDED);
if (regcomp_res) {
log_msg(LOG_ERR, "Failed compiling config parser regex.");
ret = -3;
goto free_fh;
}
// Config file lines we want to ignore
regex_t ignore_pattern;
regcomp_res = regcomp(&ignore_pattern, "^#|^\\s*$",
REG_EXTENDED | REG_NOSUB);
if (regcomp_res) {
log_msg(LOG_ERR, "Failed compiling config parser regex.");
ret = -3;
goto free_type;
}
// Buffer to store the merged patterns
buffer_t buffer;
if (buffer_init(&buffer, 256)) {
log_msg(LOG_ERR, "Out of memory while scanning config.");
ret = -3;
goto free_ignore;
}
while ( getline(&line, &line_size, fh) >= 0 ) {
// Ignore comments or empty lines in the config file
if (! regexec(&ignore_pattern, line, 0, NULL, 0)) continue;
// Remove the \n EOL
char *eol = index(line, '\n');
if (eol) *eol = '\0';
// Process types
regmatch_t match[2];
if (! regexec(&type_pattern, line, sizeof(match) / sizeof(*match), match, 0)) {
log_msg(LOG_DEBUG, "Type: %s", line+5);
if (strncmp(line + match[1].rm_so, "CHR", 3) == 0) {
if (!add_mode(S_IFCHR)) goto free_all;
} else if (strncmp(line + match[1].rm_so, "BLK", 3) == 0) {
if (!add_mode(S_IFBLK)) goto free_all;
} else if (strncmp(line + match[1].rm_so, "LNK", 3) == 0) {
if (!add_mode(S_IFLNK)) goto free_all;
} else if (strncmp(line + match[1].rm_so, "FIFO", 4) == 0) {
if (!add_mode(S_IFIFO)) goto free_all;
} else if (strncmp(line + match[1].rm_so, "SOCK", 4) == 0) {
if (!add_mode(S_IFSOCK)) goto free_all;
}
continue;
}
// Test if standalone regex compiles before concatenating.
regcomp_res = regcomp(&pattern, line, REG_EXTENDED | REG_NOSUB);
if ( regcomp_res ) {
// This one failed, we verbosely ignore it.
log_regex_error(regcomp_res, &pattern, line);
} else {
log_msg(LOG_DEBUG, "Pattern: %s", line);
pcount++;
regfree(&pattern);
// Add regex to the buffer
if ( regex_append(&buffer, line, eol - line) ) {
log_msg(LOG_ERR, "Out of memory while scanning config.");
ret = -5;
goto free_all;
}
}
}
if (pcount == 0) {
log_msg(LOG_ERR, "Config file contains no valid pattern.");
ret = -1;
goto free_all;
}
regcomp_res = regcomp(&pattern, buffer.str, REG_EXTENDED | REG_NOSUB);
if ( regcomp_res ) {
log_regex_error(regcomp_res, &pattern, buffer.str);
ret = -1;
goto free_all;
}
ret = 0;
goto free_norm;
free_all:
free(modes);
modes = NULL;
modes_count = 0;
free_norm:
free(buffer.str);
buffer.str = NULL;
free(line);
free_ignore:
regfree(&ignore_pattern);
free_type:
regfree(&type_pattern);
free_fh:
fclose(fh);
exit:
return ret;
}
/** If the file name matches one of the RegEx patterns, hide it. */
static int should_hide(const char *name, mode_t mode) {
mode &= S_IFMT;
log_msg(LOG_DEBUG, "should_hide: %s %07o", name, mode);
for (int i = 0; i < modes_count; ++i)
if (mode == modes[i]) {
log_msg(LOG_DEBUG, "type: %07o %s", mode, name);
return !conf.invert;
}
if (conf.invert && mode != S_IFREG && mode != S_IFDIR)
return conf.invert;
if (!regexec(&pattern, name, 0, NULL, 0)) {
// We have a match.
log_msg(LOG_DEBUG, "match: %s", name);
return !conf.invert;
}
return conf.invert;
}
/******************************
*
* Callbacks for FUSE
*
******************************/
static int callback_getattr(const char *path, struct stat *st_data) {
char *trpath=translate_path(path);
if (!trpath) {
errno = ENOMEM;
return -errno;
}
int res = lstat(trpath, st_data);
free(trpath);
if (should_hide(path, st_data->st_mode)) return -ENOENT;
if (res == -1) {
return -errno;
}
// Remove write permissions = chmod a-w
if (!conf.preserve_perms) {
st_data->st_mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
}
return 0;
}
static int callback_readlink(const char *path, char *buf, size_t size) {
if (should_hide(path, S_IFLNK)) return -ENOENT;
char *trpath=translate_path(path);
if (!trpath) {
errno = ENOMEM;
return -errno;
}
int res = readlink(trpath, buf, size - 1);
free(trpath);
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)
{
if (should_hide(path, S_IFREG)) return -ENOENT;
DIR *dp;
struct dirent *de;
(void) offset;
(void) fi;
char *trpath = translate_path(path);
if (!trpath) {
errno = ENOMEM;
return -errno;
}
dp = opendir(trpath);
free(trpath);
if (dp == NULL) {
return -errno;
}
while((de = readdir(dp)) != NULL) {
char *fullPath = concat_path(path, de->d_name);
if (!fullPath) {
closedir(dp);
errno = ENOMEM;
return -errno;
}
int stmode = DTTOIF(de->d_type);
if (stmode == DT_UNKNOWN) {
struct stat stdata;
trpath = translate_path(fullPath);
if (!trpath) {
free(fullPath);
closedir(dp);
errno = ENOMEM;
return -errno;
}
int const res = lstat(trpath, &stdata);
free(trpath);
if (res) {
log_msg(LOG_ERR, "%s: unexpected lstat() error %d for %s", PACKAGE_STRING, errno, fullPath);
stmode = 0;
} else {
stmode = stdata.st_mode;
}
}
int hide = should_hide(fullPath, stmode);
free(fullPath);
if (hide) {
// hide some files and directories
} else {
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 -EPERM;
}
static int callback_mkdir(const char *path, mode_t mode) {
(void)path;
(void)mode;
return -EPERM;
}
static int callback_unlink(const char *path) {
(void)path;
return -EPERM;
}
static int callback_rmdir(const char *path) {
(void)path;
return -EPERM;
}
static int callback_symlink(const char *from, const char *to)
{
(void)from;
(void)to;
return -EPERM;
}
static int callback_rename(const char *from, const char *to) {
if (should_hide(from, S_IFREG)) return -ENOENT;
(void)from;
(void)to;
return -EPERM;
}
static int callback_link(const char *from, const char *to) {
if (should_hide(from, S_IFREG)) return -ENOENT;
(void)from;
(void)to;
return -EPERM;
}
static int callback_chmod(const char *path, mode_t mode) {
if (should_hide(path, S_IFREG)) return -ENOENT;
(void)path;
(void)mode;
return -EPERM;
}
static int callback_chown(const char *path, uid_t uid, gid_t gid) {
if (should_hide(path, S_IFREG)) return -ENOENT;
(void)path;
(void)uid;
(void)gid;
return -EPERM;
}
static int callback_truncate(const char *path, off_t size) {
if (should_hide(path, S_IFREG)) return -ENOENT;
(void)path;
(void)size;
return -EPERM;
}
static int callback_utime(const char *path, struct utimbuf *buf)
{
(void)path;
(void)buf;
return -EPERM;
}
/** This function should just check if the operation is permitted for the given
* flags. FUSE will provide its own file descriptor to the calling
* application.
*/
static int callback_open(const char *path, struct fuse_file_info *finfo) {
char *trpath=translate_path(path);
if (!trpath) {
errno = ENOMEM;
return -errno;
}
struct stat st;
if (lstat(trpath, &st)) {
free(trpath);
return -errno;
}
if (should_hide(path, st.st_mode)) {
free(trpath);
return -ENOENT;
}
/* 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)) {
free(trpath);
return -EPERM;
}
int res = open(trpath, flags);
free(trpath);
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) {
char *trpath=translate_path(path);
if (!trpath) {
errno = ENOMEM;
return -errno;
}
struct stat st;
if (lstat(trpath, &st)) {
free(trpath);
return -errno;
}
if (should_hide(path, st.st_mode)) {
free(trpath);
return -ENOENT;
}
int fd;
int res;
(void)finfo;
errno = 0;
fd = open(trpath, O_RDONLY);
free(trpath);
if (fd == -1) return -errno;
errno = 0;
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) {
char *trpath=translate_path(path);
if (!trpath) {
errno = ENOMEM;
return -errno;
}
struct stat st;
if (lstat(trpath, &st)) {
free(trpath);
return -errno;
}
if (should_hide(path, st.st_mode)) {
free(trpath);
return -ENOENT;
}
free(trpath);
(void)path;
(void)buf;
(void)size;
(void)offset;
(void)finfo;
return -EPERM;
}
static int callback_statfs(const char *path, struct statvfs *st_buf) {
char *trpath=translate_path(path);
if (!trpath) {
errno = ENOMEM;
return -errno;
}
struct stat st;
if (lstat(trpath, &st)) {
free(trpath);
return -errno;
}
if (should_hide(path, st.st_mode)) {
free(trpath);
return -ENOENT;
}
int res = statvfs(trpath, st_buf);
free(trpath);
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) {
char *trpath=translate_path(path);
if (!trpath) {
errno = ENOMEM;
return -errno;
}
struct stat st;
if (lstat(trpath, &st)) {
free(trpath);
return -errno;
}
if (should_hide(path, st.st_mode)) {
free(trpath);
return -ENOENT;
}
if (mode & W_OK) {
free(trpath);
return -1; // We are ReadOnly
}
errno = 0;
int res = access(trpath, mode);
free(trpath);
if (res == -1 && errno != 0) {
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) {
if (should_hide(path, S_IFREG)) return -ENOENT;
(void)path;
(void)name;
(void)value;
(void)size;
(void)flags;
return -EPERM;
}
/**
* Get the value of an extended attribute.
*/
static int callback_getxattr(const char *path, const char *name, char *value, size_t size) {
char *trpath=translate_path(path);
if (!trpath) {
errno = ENOMEM;
return -errno;
}
struct stat st;
if (lstat(trpath, &st)) {
free(trpath);
return -errno;
}
if (should_hide(path, st.st_mode)) {
free(trpath);
return -ENOENT;
}
int res = lgetxattr(trpath, name, value, size);
free(trpath);
if(res == -1) {
return -errno;
}
return res;
}
/**
* List the supported extended attributes.
*/
static int callback_listxattr(const char *path, char *list, size_t size) {
char *trpath=translate_path(path);
if (!trpath) {
errno = ENOMEM;
return -errno;
}
struct stat st;
if (lstat(trpath, &st)) {
free(trpath);
return -errno;
}
if (should_hide(path, st.st_mode)) {
free(trpath);
return -ENOENT;
}
int res = llistxattr(trpath, list, size);
free(trpath);
if(res == -1) {
return -errno;
}
return res;
}
/**
* Remove an extended attribute.
*/
static int callback_removexattr(const char *path, const char *name) {
if (should_hide(path, S_IFREG)) return -ENOENT;
(void)path;
(void)name;
return -EPERM;
}
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
};
#define ROFS_OPT(t, p, v) { t, offsetof(struct rofs_config, p), v }
static struct fuse_opt rofs_opts[] = {
ROFS_OPT("source=%s", rw_path, 0),
ROFS_OPT("config=%s", config, 0),
ROFS_OPT("-c %s", config, 0),
ROFS_OPT("invert", invert, 1),
ROFS_OPT("preserve-perms", preserve_perms, 1),
FUSE_OPT_KEY("-V", KEY_VERSION),
FUSE_OPT_KEY("--version", KEY_VERSION),
FUSE_OPT_KEY("-h", KEY_HELP),
FUSE_OPT_KEY("--help", KEY_HELP),
FUSE_OPT_KEY("-d", KEY_DEBUG),
FUSE_OPT_KEY("--debug", KEY_DEBUG),
FUSE_OPT_END
};
static int rofs_opt_proc(void *data, const char *arg, int key, struct fuse_args *outargs) {
switch (key) {
case KEY_HELP:
fprintf(stderr, "Usage: %s /mount/point -o source=/some/dir [-o config=/some/config.rc] [options]\n"
"\n"
"General options:\n"
" -o opt,[opt...] mount options\n"
" -h --help print help\n"
" -V --version print version\n"
"\n"
"rofs-filtered options:\n"
" -o source=DIR directory to mount as read-only and filter\n"
" -o config=CONFIG_FILE config file path (default: %s)\n"
" -o invert the config file specifies files to allow\n"
" -o preserve-perms do not clear write permission\n"
"\n"
, outargs->argv[0], default_config_file);
// Let fuse print out its help text as well...
fuse_opt_add_arg(outargs, "-ho");
fuse_main(outargs->argc, outargs->argv, &callback_oper);
exit(1);
case KEY_VERSION:
fprintf(stderr, "%s version: %s\n", EXEC_NAME, PACKAGE_VERSION);
// Let fuse also print its version
fuse_opt_add_arg(outargs, "--version");
fuse_main(outargs->argc, outargs->argv, &callback_oper);
exit(0);
case KEY_DEBUG:
fprintf(stderr, "Enable extra logging\n");
conf.debug = 1;
break;
}
return 1;
}
int main(int argc, char *argv[]) {
openlog(EXEC_NAME, LOG_PID, log_facility);
for (int i = 0; i < argc; i++) log_msg(LOG_DEBUG, " arg %i = %s", i, argv[i]);
struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
memset(&conf, 0, sizeof(conf));
fuse_opt_parse(&args, &conf, rofs_opts, rofs_opt_proc);
if (conf.config == NULL) conf.config = default_config_file;
if (conf.rw_path == NULL) {
log_msg(LOG_ERR, "%s: A source directory was not provided.", PACKAGE_STRING);
log_msg(LOG_ERR, "%s: See '%s -h' for usage.", PACKAGE_STRING, argv[0]);
exit(2);
}
if (access(conf.rw_path, F_OK)) {
log_msg(LOG_ERR, "%s: The following source directory does not exist: %s", PACKAGE_STRING, conf.rw_path);
exit(2);
}
log_msg(LOG_INFO, "%s: Starting up. Using source: %s and config: %s", PACKAGE_STRING, conf.rw_path, conf.config);
if (read_config(conf.config)) {
log_msg(LOG_ERR, "%s: Error parsing config file: %s", PACKAGE_STRING, conf.config);
exit(3);
}
return fuse_main(args.argc, args.argv, &callback_oper);
}