Home | History | Annotate | Line # | Download | only in icfs
icfs.c revision 1.12
      1  1.12    andvar /*	$NetBSD: icfs.c,v 1.12 2025/05/04 20:18:26 andvar 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.12    andvar  * Now, this "layered" file system demonstrates 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.4     pooka  * This is mounted without namecache.  Otherwise we might have
     39   1.4     pooka  * two different nodes for e.g. FoO and foo.  It might be possible
     40   1.4     pooka  * support name cache by having all namespace-altering operations
     41   1.4     pooka  * flush the directory namecache ...
     42   1.1     pooka  */
     43   1.1     pooka 
     44   1.1     pooka #include <sys/types.h>
     45   1.1     pooka 
     46   1.1     pooka #include <assert.h>
     47   1.1     pooka #include <ctype.h>
     48   1.1     pooka #include <dirent.h>
     49   1.1     pooka #include <err.h>
     50   1.1     pooka #include <errno.h>
     51   1.1     pooka #include <puffs.h>
     52   1.1     pooka #include <stdio.h>
     53   1.1     pooka #include <stdlib.h>
     54   1.1     pooka #include <unistd.h>
     55   1.1     pooka 
     56   1.1     pooka PUFFSOP_PROTOS(ic)
     57   1.1     pooka 
     58   1.1     pooka static void usage(void);
     59   1.1     pooka 
     60   1.1     pooka static void
     61   1.1     pooka usage()
     62   1.1     pooka {
     63   1.1     pooka 
     64  1.11  christos 	errx(1, "usage: %s [-sp] [-o mntopts] icfs mountpath",
     65   1.1     pooka 	    getprogname());
     66   1.1     pooka }
     67   1.1     pooka 
     68   1.1     pooka static void
     69   1.1     pooka dotolower(char *buf, size_t buflen)
     70   1.1     pooka {
     71   1.1     pooka 
     72   1.1     pooka 	while (buflen--) {
     73   1.3     pooka 		*buf = tolower((unsigned char)*buf);
     74   1.1     pooka 		buf++;
     75   1.1     pooka 	}
     76   1.1     pooka }
     77   1.1     pooka 
     78   1.1     pooka static int
     79   1.1     pooka icpathcmp(struct puffs_usermount *pu, struct puffs_pathobj *c1,
     80   1.1     pooka 	struct puffs_pathobj *c2, size_t clen, int checkprefix)
     81   1.1     pooka {
     82   1.1     pooka 	struct puffs_pathobj *po_root;
     83   1.1     pooka 	char *cp1, *cp2;
     84   1.1     pooka 	size_t rplen;
     85   1.1     pooka 
     86   1.1     pooka 	po_root = puffs_getrootpathobj(pu);
     87   1.1     pooka 	rplen = po_root->po_len;
     88   1.1     pooka 
     89   1.1     pooka 	assert(clen >= rplen);
     90   1.1     pooka 
     91   1.1     pooka 	cp1 = c1->po_path;
     92   1.1     pooka 	cp2 = c2->po_path;
     93   1.1     pooka 
     94   1.1     pooka 	if (strncasecmp(cp1 + rplen, cp2 + rplen, clen - rplen) != 0)
     95   1.1     pooka 		return 1;
     96   1.1     pooka 
     97   1.1     pooka 	if (checkprefix == 0)
     98   1.1     pooka 		return 0;
     99   1.1     pooka 
    100   1.1     pooka 	if (c1->po_len < c2->po_len)
    101   1.1     pooka 		return 1;
    102   1.1     pooka 
    103   1.1     pooka 	if (*(cp1 + clen) != '/')
    104   1.1     pooka 		return 1;
    105   1.1     pooka 
    106   1.1     pooka 	return 0;
    107   1.1     pooka }
    108   1.1     pooka 
    109   1.1     pooka static int
    110   1.1     pooka icpathxform(struct puffs_usermount *pu, const struct puffs_pathobj *po_base,
    111   1.1     pooka 	const struct puffs_cn *pcn, struct puffs_pathobj *po_new)
    112   1.1     pooka {
    113   1.1     pooka 	struct dirent entry, *result;
    114   1.1     pooka 	char *src;
    115   1.1     pooka 	size_t srclen;
    116   1.1     pooka 	DIR *dp;
    117   1.1     pooka 
    118   1.1     pooka 	dp = opendir(po_base->po_path);
    119   1.1     pooka 	if (dp == NULL)
    120   1.1     pooka 		return errno;
    121   1.1     pooka 
    122   1.1     pooka 	src = pcn->pcn_name;
    123   1.1     pooka 	srclen = pcn->pcn_namelen;
    124   1.1     pooka 	for (;;) {
    125   1.1     pooka 		if (readdir_r(dp, &entry, &result) != 0)
    126   1.1     pooka 			break;
    127   1.1     pooka 		if (!result)
    128   1.1     pooka 			break;
    129   1.1     pooka 		if (strcasecmp(result->d_name, pcn->pcn_name) == 0) {
    130   1.1     pooka 			src = result->d_name;
    131   1.1     pooka 			srclen = strlen(result->d_name);
    132   1.1     pooka 			break;
    133   1.1     pooka 		}
    134   1.1     pooka 	}
    135   1.1     pooka 
    136   1.1     pooka 	po_new->po_path = strdup(src);
    137   1.1     pooka 	po_new->po_len = srclen;
    138   1.1     pooka 	closedir(dp);
    139   1.1     pooka 
    140   1.1     pooka 	return 0;
    141   1.1     pooka }
    142   1.1     pooka 
    143   1.1     pooka int
    144   1.1     pooka main(int argc, char *argv[])
    145   1.1     pooka {
    146   1.1     pooka 	struct puffs_usermount *pu;
    147   1.1     pooka 	struct puffs_ops *pops;
    148   1.1     pooka 	struct puffs_pathobj *po_root;
    149   1.1     pooka 	struct puffs_node *pn_root;
    150   1.1     pooka 	struct stat sb;
    151   1.1     pooka 	mntoptparse_t mp;
    152   1.6     pooka 	int mntflags, pflags;
    153  1.10     pooka 	int detach, dpres;
    154   1.1     pooka 	int ch;
    155   1.1     pooka 
    156   1.1     pooka 	setprogname(argv[0]);
    157   1.1     pooka 
    158   1.1     pooka 	if (argc < 3)
    159   1.1     pooka 		usage();
    160   1.1     pooka 
    161  1.10     pooka 	pflags = mntflags = dpres = 0;
    162   1.6     pooka 	detach = 1;
    163  1.10     pooka 	while ((ch = getopt(argc, argv, "o:sp")) != -1) {
    164   1.1     pooka 		switch (ch) {
    165   1.1     pooka 		case 'o':
    166   1.1     pooka 			mp = getmntopts(optarg, puffsmopts, &mntflags, &pflags);
    167   1.1     pooka 			if (mp == NULL)
    168   1.1     pooka 				err(1, "getmntopts");
    169   1.1     pooka 			freemntopts(mp);
    170   1.1     pooka 			break;
    171  1.10     pooka 		case 'p':
    172  1.10     pooka 			dpres = 1;
    173  1.10     pooka 			break;
    174   1.1     pooka 		case 's':
    175   1.6     pooka 			detach = 0;
    176   1.1     pooka 			break;
    177   1.1     pooka 		}
    178   1.1     pooka 	}
    179   1.1     pooka 	pflags |= PUFFS_FLAG_BUILDPATH;
    180   1.4     pooka 	pflags |= PUFFS_KFLAG_NOCACHE_NAME;
    181   1.1     pooka 	argv += optind;
    182   1.1     pooka 	argc -= optind;
    183   1.1     pooka 
    184   1.1     pooka 	if (pflags & PUFFS_FLAG_OPDUMP)
    185   1.6     pooka 		detach = 0;
    186   1.1     pooka 
    187   1.1     pooka 	if (argc != 2)
    188   1.1     pooka 		usage();
    189   1.1     pooka 
    190   1.1     pooka 	if (lstat(argv[0], &sb) == -1)
    191   1.1     pooka 		err(1, "stat %s", argv[0]);
    192   1.1     pooka 	if ((sb.st_mode & S_IFDIR) == 0)
    193   1.1     pooka 		errx(1, "%s is not a directory", argv[0]);
    194   1.1     pooka 
    195   1.1     pooka 	PUFFSOP_INIT(pops);
    196   1.2     pooka 	puffs_null_setops(pops);
    197   1.1     pooka 
    198  1.10     pooka 	if (dpres == 0)
    199  1.10     pooka 		PUFFSOP_SET(pops, ic, node, readdir);
    200   1.1     pooka 
    201   1.5     pooka 	if ((pu = puffs_init(pops, argv[0], "ic", NULL, pflags)) == NULL)
    202   1.1     pooka 		err(1, "mount");
    203   1.1     pooka 
    204   1.1     pooka 	pn_root = puffs_pn_new(pu, NULL);
    205   1.1     pooka 	if (pn_root == NULL)
    206   1.1     pooka 		err(1, "puffs_pn_new");
    207   1.1     pooka 	puffs_setroot(pu, pn_root);
    208   1.1     pooka 
    209   1.1     pooka 	po_root = puffs_getrootpathobj(pu);
    210   1.1     pooka 	if (po_root == NULL)
    211   1.1     pooka 		err(1, "getrootpathobj");
    212   1.1     pooka 	po_root->po_path = argv[0];
    213   1.1     pooka 	po_root->po_len = strlen(argv[0]);
    214   1.1     pooka 	puffs_stat2vattr(&pn_root->pn_va, &sb);
    215   1.1     pooka 
    216   1.1     pooka 	puffs_set_pathcmp(pu, icpathcmp);
    217   1.1     pooka 	puffs_set_pathtransform(pu, icpathxform);
    218   1.1     pooka 
    219   1.6     pooka 	if (detach)
    220   1.8     pooka 		if (puffs_daemon(pu, 1, 1) == -1)
    221   1.8     pooka 			err(1, "puffs_daemon");
    222   1.6     pooka 
    223   1.7     pooka 	if (puffs_mount(pu, argv[1], mntflags, pn_root) == -1)
    224   1.7     pooka 		err(1, "puffs_mount");
    225   1.6     pooka 	if (puffs_mainloop(pu) == -1)
    226   1.7     pooka 		err(1, "mainloop");
    227   1.6     pooka 
    228   1.6     pooka 	return 0;
    229   1.1     pooka }
    230   1.1     pooka 
    231   1.1     pooka int
    232   1.9     pooka ic_node_readdir(struct puffs_usermount *pu, void *opc, struct dirent *dent,
    233   1.1     pooka 	off_t *readoff, size_t *reslen, const struct puffs_cred *pcr,
    234   1.1     pooka 	int *eofflag, off_t *cookies, size_t *ncookies)
    235   1.1     pooka {
    236   1.1     pooka 	struct dirent *dp;
    237   1.1     pooka 	size_t rl;
    238   1.1     pooka 	int rv;
    239   1.1     pooka 
    240   1.1     pooka 	dp = dent;
    241   1.1     pooka 	rl = *reslen;
    242   1.1     pooka 
    243   1.9     pooka 	rv = puffs_null_node_readdir(pu, opc, dent, readoff, reslen, pcr,
    244   1.1     pooka 	    eofflag, cookies, ncookies);
    245   1.1     pooka 	if (rv)
    246   1.1     pooka 		return rv;
    247   1.1     pooka 
    248   1.1     pooka 	while (rl > *reslen) {
    249   1.1     pooka 		dotolower(dp->d_name, dp->d_namlen);
    250   1.1     pooka 		rl -= _DIRENT_SIZE(dp);
    251   1.1     pooka 		dp = _DIRENT_NEXT(dp);
    252   1.1     pooka 	}
    253   1.1     pooka 
    254   1.1     pooka 	return 0;
    255   1.1     pooka }
    256