~martanne/ciopfs

ref: 09ec0f6013093cb9b5a6b810538af83167e2dafd ciopfs/ciopfs.c -rw-r--r-- 15.1 KiB
09ec0f60Marc Andre Tanner Skip any directory entry which isn't all lower case. 13 years ago
                                                                                
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
/*
 * 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.
 *
 * Requirements:
 * 	In order to compile ciopfs, you will need both
 * 	libfuse and libattr. Furthermore if you want a case
 * 	preserving filesystem you have to make sure that the
 * 	underlaying filesystem 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 underlaying filesystem with the
 * 	user_xattr option which allows non root users to create
 * 	extended attributes.
 *
 * 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 <assert.h>
#include <stdio.h>
#include <ctype.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>

#define logp(format, args...) (*dolog)(format, ## args)

#ifdef NDEBUG
# define debug(format, args...)
#else
# define debug logp
#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;

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 inline bool str_contains_upper(const char *s)
{
	while(*s) {
		if (isupper(*s++))
			return true;
	}
	return false;
}

static inline char *strtolower(char *s)
{
	char *t = s;
	while(*t) {
		*t = tolower(*t);
		t++;
	}
	return s;
}

static char* map_path(const char *path)
{
	// XXX: malloc failure, memory fragmentation?
	if (path[0] == '/') {
		if (path[1] == '\0')
			return strdup(".");
		path++;
	}
	char *p = strdup(path);
	strtolower(p);
	debug("%s => %s\n", path, p);
	return p;
}

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++;
	//XXX: map_path memory leak
	debug("storing original name '%s' in '%s'\n", filename, map_path(origpath));
	if (fsetxattr(fd, CIOPFS_ATTR_NAME, filename, strlen(filename), 0)) {
		debug("%s\n", strerror(errno));
		return -errno;
	}
	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)) {
		debug("%s\n", strerror(errno));
		return -errno;
	}
	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);
	int res = lstat(p, st_data);
	free(p);
	return (res == -1) ? -errno : 0;
}

static int ciopfs_fgetattr(const char *path, struct stat *stbuf,
                           struct fuse_file_info *fi)
{
	int res = fstat(fi->fh, stbuf);
	if (res == -1)
		return -errno;
	return 0;
}


static int ciopfs_readlink(const char *path, char *buf, size_t size)
{
	char *p = map_path(path);
	int res = readlink(p, buf, size - 1);
	free(p);
	if (res == -1)
		return -errno;
	buf[res] = '\0';
	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;
	struct dirent *de;
	char *p = map_path(path);
	size_t pathlen = strlen(p);
	char dnamebuf[PATH_MAX];
	char attrbuf[FILENAME_MAX];

	if (pathlen > PATH_MAX) {
		ret = -ENAMETOOLONG;
		goto out;
	}

	strcpy(dnamebuf, p);

	(void) offset;
	(void) fi;

	dp = opendir(p);
	if (dp == NULL) {
		ret = -errno;
		goto out;
	}

	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 = strtolower(strdup(attrbuf));
				if(!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, 0))
			break;
	}

	closedir(dp);
out:
	free(p);
	return ret;
}

static int ciopfs_mknod(const char *path, mode_t mode, dev_t rdev)
{
	int res;
	char *p = map_path(path);
	/* 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);
	free(p);
	if (res == -1)
		return -errno;

	return 0;
}

static int ciopfs_mkdir(const char *path, mode_t mode)
{
	int ret = 0;
	char *p = map_path(path);
	int res = mkdir(p, mode);

	if (res == -1) {
		ret =  -errno;
		goto out;
	}

	ciopfs_set_orig_name_path(p, path);
out:
	free(p);
	return ret;
}

static int ciopfs_unlink(const char *path)
{
	char *p = map_path(path);
	int res = unlink(p);
	free(p);
	if (res == -1)
		return -errno;
	return 0;
}

static int ciopfs_rmdir(const char *path)
{
	char *p = map_path(path);
	int res = rmdir(p);
	free(p);
	if (res == -1)
		return -errno;
	return 0;
}

static int ciopfs_symlink(const char *from, const char *to)
{
	int ret = 0;
	char *f = map_path(from);
	char *t = map_path(to);
	int res = symlink(f, t);
	debug("symlink from %s to %s\n", from, to);
	if (res == -1) {
		ret = -errno;
		goto out;
	}
	ciopfs_set_orig_name_path(t, to);
out:
	free(f);
	free(t);
	return ret;
}

static int ciopfs_rename(const char *from, const char *to)
{
	int ret = 0;
	char *f = map_path(from);
	char *t = map_path(to);
	int res = rename(f, t);
	if (res == -1) {
		ret = -errno;
		goto out;
	}
	ciopfs_set_orig_name_path(t, to);
out:
	free(f);
	free(t);
	return ret;
}

static int ciopfs_link(const char *from, const char *to)
{
	int ret = 0;
	char *f = map_path(from);
	char *t = map_path(to);
	int res = link(from, to);
	if (res == -1) {
		ret = -errno;
		goto out;
	}
	ciopfs_set_orig_name_path(t, to);
out:
	free(f);
	free(t);
	return ret;
}

static int ciopfs_chmod(const char *path, mode_t mode)
{
	char *p = map_path(path);
	int res = chmod(p, mode);
	free(p);
	if (res == -1)
		return -errno;
	return 0;
}

static int ciopfs_chown(const char *path, uid_t uid, gid_t gid)
{
	char *p = map_path(path);
	int res = lchown(p, uid, gid);
	free(p);
	if (res == -1)
		return -errno;
	return 0;
}

static int ciopfs_truncate(const char *path, off_t size)
{
	char *p = map_path(path);
	int res = truncate(p, size);
	free(p);
	if (res == -1)
		return -errno;
	return 0;
}

static int ciopfs_ftruncate(const char *path, off_t size, struct fuse_file_info *fi)
{
	int res = ftruncate(fi->fh, size);
	if (res == -1)
		return -errno;

	return 0;
}

static int ciopfs_utime(const char *path, struct utimbuf *buf)
{
	char *p = map_path(path);
	int res = utime(p, buf);
	free(p);
	if (res == -1)
		return -errno;
	return 0;
}

static int ciopfs_create(const char *path, mode_t mode, struct fuse_file_info *fi)
{
	char *p = map_path(path);
	int fd = open(p, fi->flags, mode);
	free(p);
	if(fd == -1)
		return -errno;
	ciopfs_set_orig_name_fd(fd, path);
	fi->fh = fd;
	return 0;
}

static int ciopfs_open(const char *path, struct fuse_file_info *fi)
{
	char *p = map_path(path);
	int fd = open(p, fi->flags);
	free(p);
	if (fd == -1)
		return -errno;
	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);
	int res = statvfs(p, stbuf);
	free(p);
	if (res == -1)
		return -errno;

	return 0;
}

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);
  	int res = access(p, mode);
	free(p);
  	if (res == -1)
    		return -errno;
  	return 0;
}

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);
	int res = lsetxattr(p, name, value, size, flags);
	free(p);
	if (res == -1)
		return -errno;
	return 0;
}

static int ciopfs_getxattr(const char *path, const char *name, char *value, size_t size)
{
	char *p = map_path(path);
	int res = lgetxattr(p, name, value, size);
	free(p);
	if (res == -1)
		return -errno;
	return res;
}

static int ciopfs_listxattr(const char *path, char *list, size_t size)
{
	char *p = map_path(path);
	int res = llistxattr(p, list, size);
	free(p);
	if (res == -1)
		return -errno;
	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);
	int res = lremovexattr(p, name);
	free(p);
	if (res == -1)
		return -errno;
	return 0;
}

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));
}


struct fuse_operations ciopfs_operations = {
	.getattr	= ciopfs_getattr,
	.fgetattr	= ciopfs_fgetattr,
	.readlink	= ciopfs_readlink,
	.readdir	= ciopfs_readdir,
	.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,
	.utime		= ciopfs_utime,
	.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
/*
 *	what about:
 *
 *	opendir
 *	releasedir
 */
};

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) {
				// XXX: realpath(char *s, NULL) is a glibc extension
				if (!(dirname = realpath(arg, NULL)) || chdir(arg)) {
					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;
				}
			}
			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),
	FUSE_OPT_KEY("--help",		CIOPFS_OPT_HELP),
	FUSE_OPT_KEY("--version",	CIOPFS_OPT_VERSION),
	FUSE_OPT_END
};

int main(int argc, char *argv[])
{
	struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
	if (fuse_opt_parse(&args, &dirname, ciopfs_opts, ciopfs_opt_parse)) {
		fprintf(stderr, "Invalid arguments, see `%s -h' for usage\n", argv[0]);
		exit(1);
	}
	debug("dir: %s\n", dirname);
	fuse_main(args.argc, args.argv, &ciopfs_operations, NULL);
	return 0;
}