Home | History | Annotate | Line # | Download | only in icfs
icfs.c revision 1.3
      1  1.3  pooka /*	$NetBSD: icfs.c,v 1.3 2007/06/24 18:57:26 pooka Exp $	*/
      2  1.1  pooka 
      3  1.1  pooka /*
      4  1.1  pooka  * Copyright (c) 2007  Antti Kantee.  All Rights Reserved.
      5  1.1  pooka  *
      6  1.1  pooka  * Redistribution and use in source and binary forms, with or without
      7  1.1  pooka  * modification, are permitted provided that the following conditions
      8  1.1  pooka  * are met:
      9  1.1  pooka  * 1. Redistributions of source code must retain the above copyright
     10  1.1  pooka  *    notice, this list of conditions and the following disclaimer.
     11  1.1  pooka  * 2. Redistributions in binary form must reproduce the above copyright
     12  1.1  pooka  *    notice, this list of conditions and the following disclaimer in the
     13  1.1  pooka  *    documentation and/or other materials provided with the distribution.
     14  1.1  pooka  *
     15  1.1  pooka  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     16  1.1  pooka  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     17  1.1  pooka  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     18  1.1  pooka  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     19  1.1  pooka  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     20  1.1  pooka  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     21  1.1  pooka  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22  1.1  pooka  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     23  1.1  pooka  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     24  1.1  pooka  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     25  1.1  pooka  * SUCH DAMAGE.
     26  1.1  pooka  */
     27  1.1  pooka 
     28  1.1  pooka /*
     29  1.1  pooka  * icfs: layer everything in lowercase
     30  1.1  pooka  * (unfinished, as is probably fairly easy to tell)
     31  1.1  pooka  *
     32  1.1  pooka  * Now, this "layered" file system demostrates a nice gotcha with
     33  1.1  pooka  * mangling readdir entries.  In case we can't unmangle the name
     34  1.1  pooka  * (cf. rot13fs), we need to map the mangled name back to the real
     35  1.1  pooka  * name in lookup and store the actual lower layer name instead of
     36  1.1  pooka  * the mangled name.
     37  1.1  pooka  *
     38  1.1  pooka  * This is mounted nocache now (to disable the namecache since a
     39  1.1  pooka  * separate switch doesn't exist).  Otherwise we might have
     40  1.1  pooka  * two different nodes for e.g. FoO and foo.
     41  1.1  pooka  */
     42  1.1  pooka 
     43  1.1  pooka #include <sys/types.h>
     44  1.1  pooka 
     45  1.1  pooka #include <assert.h>
     46  1.1  pooka #include <ctype.h>
     47  1.1  pooka #include <dirent.h>
     48  1.1  pooka #include <err.h>
     49  1.1  pooka #include <errno.h>
     50  1.1  pooka #include <puffs.h>
     51  1.1  pooka #include <stdio.h>
     52  1.1  pooka #include <stdlib.h>
     53  1.1  pooka #include <unistd.h>
     54  1.1  pooka 
     55  1.1  pooka PUFFSOP_PROTOS(ic)
     56  1.1  pooka 
     57  1.1  pooka static void usage(void);
     58  1.1  pooka 
     59  1.1  pooka static void
     60  1.1  pooka usage()
     61  1.1  pooka {
     62  1.1  pooka 
     63  1.1  pooka 	errx(1, "usage: %s [-s][-o mntopts]icfs mountpath",
     64  1.1  pooka 	    getprogname());
     65  1.1  pooka }
     66  1.1  pooka 
     67  1.1  pooka static void
     68  1.1  pooka dotolower(char *buf, size_t buflen)
     69  1.1  pooka {
     70  1.1  pooka 
     71  1.1  pooka 	while (buflen--) {
     72  1.3  pooka 		*buf = tolower((unsigned char)*buf);
     73  1.1  pooka 		buf++;
     74  1.1  pooka 	}
     75  1.1  pooka }
     76  1.1  pooka 
     77  1.1  pooka static int
     78  1.1  pooka icpathcmp(struct puffs_usermount *pu, struct puffs_pathobj *c1,
     79  1.1  pooka 	struct puffs_pathobj *c2, size_t clen, int checkprefix)
     80  1.1  pooka {
     81  1.1  pooka 	struct puffs_pathobj *po_root;
     82  1.1  pooka 	char *cp1, *cp2;
     83  1.1  pooka 	size_t rplen;
     84  1.1  pooka 
     85  1.1  pooka 	po_root = puffs_getrootpathobj(pu);
     86  1.1  pooka 	rplen = po_root->po_len;
     87  1.1  pooka 
     88  1.1  pooka 	assert(clen >= rplen);
     89  1.1  pooka 
     90  1.1  pooka 	cp1 = c1->po_path;
     91  1.1  pooka 	cp2 = c2->po_path;
     92  1.1  pooka 
     93  1.1  pooka 	if (strncasecmp(cp1 + rplen, cp2 + rplen, clen - rplen) != 0)
     94  1.1  pooka 		return 1;
     95  1.1  pooka 
     96  1.1  pooka 	if (checkprefix == 0)
     97  1.1  pooka 		return 0;
     98  1.1  pooka 
     99  1.1  pooka 	if (c1->po_len < c2->po_len)
    100  1.1  pooka 		return 1;
    101  1.1  pooka 
    102  1.1  pooka 	if (*(cp1 + clen) != '/')
    103  1.1  pooka 		return 1;
    104  1.1  pooka 
    105  1.1  pooka 	return 0;
    106  1.1  pooka }
    107  1.1  pooka 
    108  1.1  pooka static int
    109  1.1  pooka icpathxform(struct puffs_usermount *pu, const struct puffs_pathobj *po_base,
    110  1.1  pooka 	const struct puffs_cn *pcn, struct puffs_pathobj *po_new)
    111  1.1  pooka {
    112  1.1  pooka 	struct dirent entry, *result;
    113  1.1  pooka 	char *src;
    114  1.1  pooka 	size_t srclen;
    115  1.1  pooka 	DIR *dp;
    116  1.1  pooka 
    117  1.1  pooka 	dp = opendir(po_base->po_path);
    118  1.1  pooka 	if (dp == NULL)
    119  1.1  pooka 		return errno;
    120  1.1  pooka 
    121  1.1  pooka 	src = pcn->pcn_name;
    122  1.1  pooka 	srclen = pcn->pcn_namelen;
    123  1.1  pooka 	for (;;) {
    124  1.1  pooka 		if (readdir_r(dp, &entry, &result) != 0)
    125  1.1  pooka 			break;
    126  1.1  pooka 		if (!result)
    127  1.1  pooka 			break;
    128  1.1  pooka 		if (strcasecmp(result->d_name, pcn->pcn_name) == 0) {
    129  1.1  pooka 			src = result->d_name;
    130  1.1  pooka 			srclen = strlen(result->d_name);
    131  1.1  pooka 			break;
    132  1.1  pooka 		}
    133  1.1  pooka 	}
    134  1.1  pooka 
    135  1.1  pooka 	po_new->po_path = strdup(src);
    136  1.1  pooka 	po_new->po_len = srclen;
    137  1.1  pooka 	closedir(dp);
    138  1.1  pooka 
    139  1.1  pooka 	return 0;
    140  1.1  pooka }
    141  1.1  pooka 
    142  1.1  pooka int
    143  1.1  pooka main(int argc, char *argv[])
    144  1.1  pooka {
    145  1.1  pooka 	struct puffs_usermount *pu;
    146  1.1  pooka 	struct puffs_ops *pops;
    147  1.1  pooka 	struct puffs_pathobj *po_root;
    148  1.1  pooka 	struct puffs_node *pn_root;
    149  1.1  pooka 	struct stat sb;
    150  1.1  pooka 	mntoptparse_t mp;
    151  1.1  pooka 	int mntflags, pflags, lflags;
    152  1.1  pooka 	int ch;
    153  1.1  pooka 
    154  1.1  pooka 	setprogname(argv[0]);
    155  1.1  pooka 
    156  1.1  pooka 	if (argc < 3)
    157  1.1  pooka 		usage();
    158  1.1  pooka 
    159  1.1  pooka 	pflags = lflags = mntflags = 0;
    160  1.1  pooka 	while ((ch = getopt(argc, argv, "o:s")) != -1) {
    161  1.1  pooka 		switch (ch) {
    162  1.1  pooka 		case 'o':
    163  1.1  pooka 			mp = getmntopts(optarg, puffsmopts, &mntflags, &pflags);
    164  1.1  pooka 			if (mp == NULL)
    165  1.1  pooka 				err(1, "getmntopts");
    166  1.1  pooka 			freemntopts(mp);
    167  1.1  pooka 			break;
    168  1.1  pooka 		case 's':
    169  1.1  pooka 			lflags |= PUFFSLOOP_NODAEMON;
    170  1.1  pooka 			break;
    171  1.1  pooka 		}
    172  1.1  pooka 	}
    173  1.1  pooka 	pflags |= PUFFS_FLAG_BUILDPATH;
    174  1.1  pooka 	pflags |= PUFFS_KFLAG_NOCACHE;
    175  1.1  pooka 	argv += optind;
    176  1.1  pooka 	argc -= optind;
    177  1.1  pooka 
    178  1.1  pooka 	if (pflags & PUFFS_FLAG_OPDUMP)
    179  1.1  pooka 		lflags = PUFFSLOOP_NODAEMON;
    180  1.1  pooka 
    181  1.1  pooka 	if (argc != 2)
    182  1.1  pooka 		usage();
    183  1.1  pooka 
    184  1.1  pooka 	if (lstat(argv[0], &sb) == -1)
    185  1.1  pooka 		err(1, "stat %s", argv[0]);
    186  1.1  pooka 	if ((sb.st_mode & S_IFDIR) == 0)
    187  1.1  pooka 		errx(1, "%s is not a directory", argv[0]);
    188  1.1  pooka 
    189  1.1  pooka 	PUFFSOP_INIT(pops);
    190  1.2  pooka 	puffs_null_setops(pops);
    191  1.1  pooka 
    192  1.1  pooka 	PUFFSOP_SET(pops, ic, node, readdir);
    193  1.1  pooka 
    194  1.1  pooka 	if ((pu = puffs_init(pops, "ic", NULL, pflags)) == NULL)
    195  1.1  pooka 		err(1, "mount");
    196  1.1  pooka 
    197  1.1  pooka 	pn_root = puffs_pn_new(pu, NULL);
    198  1.1  pooka 	if (pn_root == NULL)
    199  1.1  pooka 		err(1, "puffs_pn_new");
    200  1.1  pooka 	puffs_setroot(pu, pn_root);
    201  1.1  pooka 
    202  1.1  pooka 	po_root = puffs_getrootpathobj(pu);
    203  1.1  pooka 	if (po_root == NULL)
    204  1.1  pooka 		err(1, "getrootpathobj");
    205  1.1  pooka 	po_root->po_path = argv[0];
    206  1.1  pooka 	po_root->po_len = strlen(argv[0]);
    207  1.1  pooka 	puffs_stat2vattr(&pn_root->pn_va, &sb);
    208  1.1  pooka 
    209  1.1  pooka 	puffs_set_pathcmp(pu, icpathcmp);
    210  1.1  pooka 	puffs_set_pathtransform(pu, icpathxform);
    211  1.1  pooka 
    212  1.1  pooka 	if (puffs_mount(pu, argv[1], mntflags, pn_root) == -1)
    213  1.1  pooka 		err(1, "puffs_mount");
    214  1.1  pooka 
    215  1.1  pooka 	return puffs_mainloop(pu, lflags);
    216  1.1  pooka }
    217  1.1  pooka 
    218  1.1  pooka int
    219  1.1  pooka ic_node_readdir(struct puffs_cc *pcc, void *opc, struct dirent *dent,
    220  1.1  pooka 	off_t *readoff, size_t *reslen, const struct puffs_cred *pcr,
    221  1.1  pooka 	int *eofflag, off_t *cookies, size_t *ncookies)
    222  1.1  pooka {
    223  1.1  pooka 	struct dirent *dp;
    224  1.1  pooka 	size_t rl;
    225  1.1  pooka 	int rv;
    226  1.1  pooka 
    227  1.1  pooka 	dp = dent;
    228  1.1  pooka 	rl = *reslen;
    229  1.1  pooka 
    230  1.1  pooka 	rv = puffs_null_node_readdir(pcc, opc, dent, readoff, reslen, pcr,
    231  1.1  pooka 	    eofflag, cookies, ncookies);
    232  1.1  pooka 	if (rv)
    233  1.1  pooka 		return rv;
    234  1.1  pooka 
    235  1.1  pooka 	while (rl > *reslen) {
    236  1.1  pooka 		dotolower(dp->d_name, dp->d_namlen);
    237  1.1  pooka 		rl -= _DIRENT_SIZE(dp);
    238  1.1  pooka 		dp = _DIRENT_NEXT(dp);
    239  1.1  pooka 	}
    240  1.1  pooka 
    241  1.1  pooka 	return 0;
    242  1.1  pooka }
    243