forked from martanne/ciopfs
/
ciopfs.c
1020 lines (921 loc) · 23 KB
/
ciopfs.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
999
1000
/*
* CIOPFS - The Case Insensitive On Purpose Filesystem for FUSE
*
* (c) 2008 Marc Andre Tanner <mat at brain-dump dot org>
* (c) 2001-2007 Miklos Szeredi <miklos@szeredi.hu>
*
* This program can be distributed under the terms of the GNU GPLv2.
*
* How it works:
* Before any operation takes place all filenames are
* converted to lower case. The original filenames are stored
* in extended attributes named user.filename. This value
* is returned upon request.
*
* Files or directories which aren't all lowercase in the
* underlying file system are ignored. You should probably
* start with an empty data directory and copy your contents
* over.
*
* Requirements:
* In order to compile ciopfs, you will need both
* libfuse and libattr. Furthermore if you want a case
* preserving file system you have to make sure that the
* underlying file system supports extended attributes
* (for example for ext{2,3} you need a kernel with
* CONFIG_EXT{2,3}_FS_XATTR enabled). You probably also
* want to mount the underlying filesystem with the
* user_xattr option which allows non root users to create
* extended attributes.
*
* If you want to work with unicode characters within file
* names, you will need libicu from www.icu-project.org.
* Otherwise disable it in config.mk, the file system will
* only work with ascii [a-zA-Z] file names.
*
* Compile & Install:
* $EDITOR config.mk
* make
* sudo make install
*
* Mount:
* ciopfs directory mountpoint [options]
*
*/
#ifdef __linux__
#define _XOPEN_SOURCE 500 /* For pread()/pwrite() */
#endif
#define _BSD_SOURCE /* for vsyslog() */
#include <fuse.h>
#include <ulockmgr.h>
#include <sys/xattr.h>
#include <sys/time.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdarg.h>
#include <string.h>
#include <errno.h>
#include <dirent.h>
#include <unistd.h>
#include <limits.h>
#include <syslog.h>
#include <grp.h>
#if __GNUC__ >= 3
# define likely(x) __builtin_expect(!!(x), 1)
# define unlikely(x) __builtin_expect(!!(x), 0)
#else
# define likely(x) (x)
# define unlikely(x) (x)
#endif
/* each *.c file implements the following two functions:
*
* static inline bool str_contains_upper(const char *s);
* static inline char *str_fold(const char *s);
*/
#ifdef HAVE_GLIB
# include "unicode-glib.c"
#elif defined HAVE_LIBICUUC
# include "unicode-icu.c"
#else
# include "ascii.c"
#endif
#define log_print(format, args...) (*dolog)(format, ## args)
#ifdef NDEBUG
# define debug(format, args...)
#else
# define debug log_print
#endif
#define CIOPFS_ATTR_NAME "user.filename"
#ifndef PATH_MAX
#define PATH_MAX 4096
#endif
#ifndef FILENAME_MAX
#define FILENAME_MAX 4096
#endif
static const char *dirname;
/* whether we have forced fuse in single threaded mode (`-s' option). This happens
* because we can't store uid/gids per thread and the file system is accessible for
* multiple users via the `-o allow_other' option.
*/
static bool single_threaded = false;
void stderr_print(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
fputs("ciopfs: ", stderr);
vfprintf(stderr, fmt, ap);
va_end(ap);
}
void syslog_print(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vsyslog(LOG_NOTICE, fmt, ap);
va_end(ap);
}
static void (*dolog)(const char *fmt, ...) = syslog_print;
static char *map_path(const char *path)
{
char *p;
/* XXX: memory fragmentation? */
if (path[0] == '/') {
if (path[1] == '\0')
return strdup(".");
path++;
}
p = str_fold(path);
debug("%s => %s\n", path, p);
return p;
}
/* Returns the supplementary group IDs of a calling process which
* isued the file system operation.
*
* As indicated by Miklos Szeredi the group list is available in
*
* /proc/$PID/task/$TID/status
*
* and fuse supplies TID in get_fuse_context()->pid.
*
* Jean-Pierre Andre found out that the same information is also
* available in
*
* /proc/$TID/task/$TID/status
*
* which is used in this implementation.
*/
static size_t get_groups(pid_t pid, gid_t **groups)
{
static char key[] = "\nGroups:\t";
char filename[64], buf[2048], *s, *t, c = '\0';
int fd, num_read, matched = 0;
size_t n = 0;
gid_t *gids, grp = 0;
sprintf(filename, "/proc/%u/task/%u/status", pid, pid);
fd = open(filename, O_RDONLY);
if (fd == -1)
return 0;
for (;;) {
if (!c) {
num_read = read(fd, buf, sizeof(buf) - 1);
if (num_read <= 0) {
close(fd);
return 0;
}
buf[num_read] = '\0';
s = buf;
}
c = *s++;
if (key[matched] == c) {
if (!key[++matched])
break;
} else
matched = (key[0] == c);
}
close(fd);
t = s;
n = 0;
while (*t != '\n') {
if (*t++ == ' ')
n++;
}
if (n == 0)
return 0;
*groups = gids = malloc(n * sizeof(gid_t));
if (!gids)
return 0;
n = 0;
while ((c = *s++) != '\n') {
if (c >= '0' && c <= '9')
grp = grp*10 + c - '0';
else if (c == ' ') {
gids[n++] = grp;
grp = 0;
}
}
return n;
}
/* This only works when the filesystem is mounted by root and fuse
* operates in single threaded mode. Because the euid/egid are stored
* per process this would otherwise cause all sorts of race condidtions
* and security issues when multiple users access the file system
* simultaneously.
*/
static inline void enter_user_context_effective()
{
gid_t *groups;
size_t ngroups;
struct fuse_context *c = fuse_get_context();
if (!single_threaded || getuid())
return;
if ((ngroups = get_groups(c->pid, &groups))) {
setgroups(ngroups, groups);
free(groups);
}
setegid(c->gid);
seteuid(c->uid);
}
static inline void leave_user_context_effective()
{
if (!single_threaded || getuid())
return;
seteuid(getuid());
setegid(getgid());
}
/* access(2) checks the real uid/gid not the effective one
* we therefore switch them if run as root and in single
* threaded mode.
*
* The real uid/gid are stored per process which is why we
* can't change them in multithreaded mode. This would lead
* to all sorts of race conditions and security issues when
* multiple users access the file system simultaneously.
*
*/
static inline void enter_user_context_real()
{
gid_t *groups;
size_t ngroups;
struct fuse_context *c = fuse_get_context();
if (!single_threaded || geteuid())
return;
if ((ngroups = get_groups(c->pid, &groups))) {
setgroups(ngroups, groups);
free(groups);
}
setregid(c->gid, -1);
setreuid(c->uid, -1);
}
static inline void leave_user_context_real()
{
if (!single_threaded || geteuid())
return;
setuid(geteuid());
setgid(getegid());
}
static ssize_t ciopfs_get_orig_name(const char *path, char *value, size_t size)
{
ssize_t attrlen;
debug("looking up original file name of %s ", path);
attrlen = lgetxattr(path, CIOPFS_ATTR_NAME, value, size);
if (attrlen > 0) {
value[attrlen] = '\0';
debug("found %s\n", value);
} else {
debug("nothing found\n");
}
return attrlen;
}
static int ciopfs_set_orig_name_fd(int fd, const char *origpath)
{
char *filename = strrchr(origpath, '/');
if (!filename)
filename = (char *)origpath;
else
filename++;
#ifndef NDEBUG
char *path = map_path(origpath);
if (likely(path != NULL)) {
log_print("storing original name '%s' in '%s'\n", filename, path);
free(path);
}
#endif
if (fsetxattr(fd, CIOPFS_ATTR_NAME, filename, strlen(filename), 0)) {
int ret = -errno;
debug("%s\n", strerror(errno));
return ret;
}
return 0;
}
static int ciopfs_set_orig_name_path(const char *path, const char *origpath)
{
char *filename = strrchr(origpath, '/');
if (!filename)
filename = (char *)origpath;
else
filename++;
debug("storing original name '%s' in '%s'\n", filename, path);
/* XXX: setting an extended attribute on a symlink doesn't seem to work (EPERM) */
if (lsetxattr(path, CIOPFS_ATTR_NAME, filename, strlen(filename), 0)) {
int ret = -errno;
debug("%s\n", strerror(errno));
return ret;
}
return 0;
}
static int ciopfs_remove_orig_name(const char *path)
{
debug("removing original file name of %s\n", path);
return lremovexattr(path, CIOPFS_ATTR_NAME);
}
static int ciopfs_getattr(const char *path, struct stat *st_data)
{
char *p = map_path(path);
if (unlikely(p == NULL))
return -ENOMEM;
enter_user_context_effective();
int res = lstat(p, st_data);
if (res == -1)
res = -errno;
leave_user_context_effective();
free(p);
return res;
}
static int ciopfs_fgetattr(const char *path, struct stat *stbuf,
struct fuse_file_info *fi)
{
enter_user_context_effective();
int res = fstat(fi->fh, stbuf);
if (res == -1)
res = -errno;
leave_user_context_effective();
return res;
}
static int ciopfs_readlink(const char *path, char *buf, size_t size)
{
int ret;
char *p = map_path(path);
if (unlikely(p == NULL))
return -ENOMEM;
enter_user_context_effective();
int res = readlink(p, buf, size - 1);
if (res == -1)
ret = -errno;
leave_user_context_effective();
free(p);
if (res == -1)
return ret;
buf[res] = '\0';
return 0;
}
static int ciopfs_opendir(const char *path, struct fuse_file_info *fi)
{
int ret;
char *p = map_path(path);
if (unlikely(p == NULL))
return -ENOMEM;
enter_user_context_effective();
DIR *dp = opendir(p);
if (dp == NULL)
ret = -errno;
leave_user_context_effective();
free(p);
if (dp == NULL)
return ret;
fi->fh = (uint64_t)(uintptr_t)dp;
return 0;
}
static int ciopfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
off_t offset, struct fuse_file_info *fi)
{
int ret = 0;
DIR *dp = (DIR *)(uintptr_t)fi->fh;
struct dirent *de;
char *p = map_path(path);
if (unlikely(p == NULL))
return -ENOMEM;
size_t pathlen = strlen(p);
char dnamebuf[PATH_MAX];
char attrbuf[FILENAME_MAX];
if (pathlen > PATH_MAX) {
ret = -ENAMETOOLONG;
goto out;
}
if (!dp) {
ret = -EBADF;
goto out;
}
seekdir(dp, offset);
while ((de = readdir(dp)) != NULL) {
struct stat st;
char *dname;
char *attrlower;
/* skip any entry which is not all lower case for now */
if (str_contains_upper(de->d_name))
continue;
memset(&st, 0, sizeof(st));
st.st_ino = de->d_ino;
st.st_mode = de->d_type << 12;
if (!strcmp(".", de->d_name) || !strcmp("..", de->d_name))
dname = de->d_name;
else {
/* check whether there is an original name associated with
* this path and if so return it instead of the all lower
* case one
*/
snprintf(dnamebuf, sizeof dnamebuf, "%s/%s", p, de->d_name);
debug("dnamebuf: %s de->d_name: %s\n", dnamebuf, de->d_name);
if (ciopfs_get_orig_name(dnamebuf, attrbuf, sizeof attrbuf) > 0) {
/* we found an original name now check whether it is
* still accurate and if not remove it
*/
attrlower = str_fold(attrbuf);
if (attrlower && !strcmp(attrlower, de->d_name))
dname = attrbuf;
else {
dname = de->d_name;
ciopfs_remove_orig_name(dnamebuf);
}
free(attrlower);
} else
dname = de->d_name;
}
debug("dname: %s\n", dname);
if (filler(buf, dname, &st, telldir(dp)))
break;
}
out:
free(p);
return ret;
}
static int ciopfs_releasedir(const char *path, struct fuse_file_info *fi)
{
if (fi->fh)
closedir((DIR *)(uintptr_t)fi->fh);
return 0;
}
static int ciopfs_mknod(const char *path, mode_t mode, dev_t rdev)
{
int res;
char *p = map_path(path);
if (unlikely(p == NULL))
return -ENOMEM;
enter_user_context_effective();
/* On Linux this could just be 'mknod(p, mode, rdev)' but this
is more portable */
if (S_ISREG(mode)) {
res = open(p, O_CREAT | O_EXCL | O_WRONLY, mode);
if (res >= 0) {
ciopfs_set_orig_name_fd(res, path);
close(res);
}
} else if (S_ISFIFO(mode)) {
res = mkfifo(p, mode);
} else {
res = mknod(p, mode, rdev);
}
if (res == -1)
res = -errno;
leave_user_context_effective();
free(p);
return res;
}
static int ciopfs_mkdir(const char *path, mode_t mode)
{
char *p = map_path(path);
if (unlikely(p == NULL))
return -ENOMEM;
enter_user_context_effective();
int res = mkdir(p, mode);
if (res == -1)
res = -errno;
leave_user_context_effective();
if (res == 0)
ciopfs_set_orig_name_path(p, path);
free(p);
return res;
}
static int ciopfs_unlink(const char *path)
{
char *p = map_path(path);
if (unlikely(p == NULL))
return -ENOMEM;
enter_user_context_effective();
int res = unlink(p);
if (res == -1)
res = -errno;
leave_user_context_effective();
free(p);
return res;
}
static int ciopfs_rmdir(const char *path)
{
char *p = map_path(path);
if (unlikely(p == NULL))
return -ENOMEM;
enter_user_context_effective();
int res = rmdir(p);
if (res == -1)
res = -errno;
leave_user_context_effective();
free(p);
return res;
}
static int ciopfs_symlink(const char *from, const char *to)
{
char *t = map_path(to);
if (unlikely(t == NULL))
return -ENOMEM;
enter_user_context_effective();
int res = symlink(from, t);
if (res == -1)
res = -errno;
leave_user_context_effective();
if (res == 0)
ciopfs_set_orig_name_path(t, to);
free(t);
return res;
}
static int ciopfs_rename(const char *from, const char *to)
{
char *f = map_path(from);
char *t = map_path(to);
if (unlikely(f == NULL || t == NULL))
return -ENOMEM;
enter_user_context_effective();
int res = rename(f, t);
if (res == -1)
res = -errno;
leave_user_context_effective();
if (res == 0)
ciopfs_set_orig_name_path(t, to);
free(f);
free(t);
return res;
}
static int ciopfs_link(const char *from, const char *to)
{
char *f = map_path(from);
char *t = map_path(to);
if (unlikely(f == NULL || t == NULL))
return -ENOMEM;
enter_user_context_effective();
int res = link(f, t);
if (res == -1)
res = -errno;
leave_user_context_effective();
if (res == 0)
ciopfs_set_orig_name_path(t, to);
free(f);
free(t);
return res;
}
static int ciopfs_chmod(const char *path, mode_t mode)
{
char *p = map_path(path);
if (unlikely(p == NULL))
return -ENOMEM;
enter_user_context_effective();
int res = chmod(p, mode);
if (res == -1)
res = -errno;
leave_user_context_effective();
free(p);
return res;
}
static int ciopfs_chown(const char *path, uid_t uid, gid_t gid)
{
char *p = map_path(path);
if (unlikely(p == NULL))
return -ENOMEM;
enter_user_context_effective();
int res = lchown(p, uid, gid);
if (res == -1)
res = -errno;
leave_user_context_effective();
free(p);
return res;
}
static int ciopfs_truncate(const char *path, off_t size)
{
char *p = map_path(path);
if (unlikely(p == NULL))
return -ENOMEM;
enter_user_context_effective();
int res = truncate(p, size);
if (res == -1)
res = -errno;
leave_user_context_effective();
free(p);
return res;
}
static int ciopfs_ftruncate(const char *path, off_t size, struct fuse_file_info *fi)
{
enter_user_context_effective();
int res = ftruncate(fi->fh, size);
if (res == -1)
res = -errno;
leave_user_context_effective();
return res;
}
static int ciopfs_utimens(const char *path, const struct timespec ts[2])
{
char *p = map_path(path);
if (unlikely(p == NULL))
return -ENOMEM;
struct timeval tv[2];
tv[0].tv_sec = ts[0].tv_sec;
tv[0].tv_usec = ts[0].tv_nsec / 1000;
tv[1].tv_sec = ts[1].tv_sec;
tv[1].tv_usec = ts[1].tv_nsec / 1000;
enter_user_context_effective();
int res = utimes(p, tv);
if (res == -1)
res = -errno;
leave_user_context_effective();
free(p);
return res;
}
static int ciopfs_create(const char *path, mode_t mode, struct fuse_file_info *fi)
{
int ret;
char *p = map_path(path);
if (unlikely(p == NULL))
return -ENOMEM;
enter_user_context_effective();
int fd = open(p, fi->flags, mode);
if (fd == -1)
ret = -errno;
leave_user_context_effective();
free(p);
if (fd == -1)
return ret;
ciopfs_set_orig_name_fd(fd, path);
fi->fh = fd;
return 0;
}
static int ciopfs_open(const char *path, struct fuse_file_info *fi)
{
int ret;
char *p = map_path(path);
if (unlikely(p == NULL))
return -ENOMEM;
enter_user_context_effective();
int fd = open(p, fi->flags);
if (fd == -1)
ret = -errno;
leave_user_context_effective();
free(p);
if (fd == -1)
return ret;
if (fi->flags & O_CREAT)
ciopfs_set_orig_name_fd(fd, path);
fi->fh = fd;
return 0;
}
static int ciopfs_read(const char *path, char *buf, size_t size, off_t offset,
struct fuse_file_info *fi)
{
int res = pread(fi->fh, buf, size, offset);
if (res == -1)
res = -errno;
return res;
}
static int ciopfs_write(const char *path, const char *buf, size_t size,
off_t offset, struct fuse_file_info *fi)
{
int res = pwrite(fi->fh, buf, size, offset);
if (res == -1)
res = -errno;
return res;
}
static int ciopfs_statfs(const char *path, struct statvfs *stbuf)
{
char *p = map_path(path);
if (unlikely(p == NULL))
return -ENOMEM;
enter_user_context_effective();
int res = statvfs(p, stbuf);
if (res == -1)
res = -errno;
leave_user_context_effective();
free(p);
return res;
}
static int ciopfs_flush(const char *path, struct fuse_file_info *fi)
{
/* This is called from every close on an open file, so call the
close on the underlying filesystem. But since flush may be
called multiple times for an open file, this must not really
close the file. This is important if used on a network
filesystem like NFS which flush the data/metadata on close() */
int res = close(dup(fi->fh));
if (res == -1)
return -errno;
return 0;
}
static int ciopfs_release(const char *path, struct fuse_file_info *fi)
{
close(fi->fh);
return 0;
}
static int ciopfs_fsync(const char *path, int isdatasync, struct fuse_file_info *fi)
{
int res;
#ifdef HAVE_FDATASYNC
if (isdatasync)
res = fdatasync(fi->fh);
else
#endif
res = fsync(fi->fh);
if (res == -1)
return -errno;
return 0;
}
static int ciopfs_access(const char *path, int mode)
{
char *p = map_path(path);
if (unlikely(p == NULL))
return -ENOMEM;
enter_user_context_real();
int res = access(p, mode);
if (res == -1)
res = -errno;
leave_user_context_real();
free(p);
return res;
}
static int ciopfs_setxattr(const char *path, const char *name, const char *value,
size_t size, int flags)
{
if (!strcmp(name, CIOPFS_ATTR_NAME)) {
debug("denying setting value of extended attribute '%s'\n", CIOPFS_ATTR_NAME);
return -EPERM;
}
char *p = map_path(path);
if (unlikely(p == NULL))
return -ENOMEM;
enter_user_context_effective();
int res = lsetxattr(p, name, value, size, flags);
if (res == -1)
res = -errno;
leave_user_context_effective();
free(p);
return res;
}
static int ciopfs_getxattr(const char *path, const char *name, char *value, size_t size)
{
char *p = map_path(path);
if (unlikely(p == NULL))
return -ENOMEM;
enter_user_context_effective();
int res = lgetxattr(p, name, value, size);
if (res == -1)
res = -errno;
leave_user_context_effective();
free(p);
return res;
}
static int ciopfs_listxattr(const char *path, char *list, size_t size)
{
char *p = map_path(path);
if (unlikely(p == NULL))
return -ENOMEM;
enter_user_context_effective();
int res = llistxattr(p, list, size);
if (res == -1)
res = -errno;
leave_user_context_effective();
free(p);
return res;
}
static int ciopfs_removexattr(const char *path, const char *name)
{
if (!strcmp(name, CIOPFS_ATTR_NAME)) {
debug("denying removal of extended attribute '%s'\n", CIOPFS_ATTR_NAME);
return -EPERM;
}
char *p = map_path(path);
if (unlikely(p == NULL))
return -ENOMEM;
enter_user_context_effective();
int res = lremovexattr(p, name);
if (res == -1)
res = -errno;
leave_user_context_effective();
free(p);
return res;
}
static int ciopfs_lock(const char *path, struct fuse_file_info *fi, int cmd,
struct flock *lock)
{
return ulockmgr_op(fi->fh, cmd, lock, &fi->lock_owner,
sizeof(fi->lock_owner));
}
static void *ciopfs_init(struct fuse_conn_info *conn)
{
if (chdir(dirname) == -1) {
log_print("init: %s\n", strerror(errno));
exit(1);
}
if (lsetxattr(".", "user.ciopfs", VERSION, sizeof(VERSION) -1, 0) == -1 && errno == ENOTSUP)
log_print("warning underlying filesystem does not support extended attributes, "
"converting all filenames to lower case\n");
return NULL;
}
struct fuse_operations ciopfs_operations = {
.getattr = ciopfs_getattr,
.fgetattr = ciopfs_fgetattr,
.readlink = ciopfs_readlink,
.opendir = ciopfs_opendir,
.readdir = ciopfs_readdir,
.releasedir = ciopfs_releasedir,
.mknod = ciopfs_mknod,
.mkdir = ciopfs_mkdir,
.symlink = ciopfs_symlink,
.unlink = ciopfs_unlink,
.rmdir = ciopfs_rmdir,
.rename = ciopfs_rename,
.link = ciopfs_link,
.chmod = ciopfs_chmod,
.chown = ciopfs_chown,
.truncate = ciopfs_truncate,
.ftruncate = ciopfs_ftruncate,
.utimens = ciopfs_utimens,
.create = ciopfs_create,
.open = ciopfs_open,
.read = ciopfs_read,
.write = ciopfs_write,
.statfs = ciopfs_statfs,
.flush = ciopfs_flush,
.release = ciopfs_release,
.fsync = ciopfs_fsync,
.access = ciopfs_access,
.setxattr = ciopfs_setxattr,
.getxattr = ciopfs_getxattr,
.listxattr = ciopfs_listxattr,
.removexattr = ciopfs_removexattr,
.lock = ciopfs_lock,
.init = ciopfs_init
};
static void usage(const char *name)
{
fprintf(stderr, "usage: %s directory mountpoint [options]\n"
"\n"
"Mounts the content of directory at mountpoint in case insensitiv fashion.\n"
"\n"
"general options:\n"
" -o opt,[opt...] mount options\n"
" -h|--help print help\n"
" --version print version\n"
"\n", name);
}
enum {
CIOPFS_OPT_HELP,
CIOPFS_OPT_VERSION
};
static int ciopfs_opt_parse(void *data, const char *arg, int key, struct fuse_args *outargs)
{
switch (key) {
case FUSE_OPT_KEY_NONOPT:
if (!dirname) {
/* realpath(char *s, NULL) is a POSIX.1-2008 extension, originally from GLIBC,
and might be unavailible on older non-glibc systems. */
if (!(dirname = realpath(arg, NULL))) {
perror(outargs->argv[0]);
exit(1);
}
return 0;
}
return 1;
case FUSE_OPT_KEY_OPT:
if (arg[0] == '-') {
switch (arg[1]) {
case 'd':
case 'f':
dolog = stderr_print;
}
} else if (!strcmp("allow_other", arg)) {
/* disable multithreaded mode if the file system
* is accessible to multiple users simultanousely
* because we can't store uid/gid per thread and
* this leads to all sorts of race conditions and
* security issues.
*/
single_threaded = (getuid() == 0);
}
return 1;
case CIOPFS_OPT_HELP:
usage(outargs->argv[0]);
fuse_opt_add_arg(outargs, "-ho");
fuse_main(outargs->argc, outargs->argv, &ciopfs_operations, NULL);
exit(0);
case CIOPFS_OPT_VERSION:
fprintf(stderr, "%s: "VERSION" fuse: %d\n", outargs->argv[0], fuse_version());
exit(0);
default:
fprintf(stderr, "see `%s -h' for usage\n", outargs->argv[0]);
exit(1);
}
return 1;
}
static struct fuse_opt ciopfs_opts[] = {
FUSE_OPT_KEY("-h", CIOPFS_OPT_HELP),