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