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