summaryrefslogtreecommitdiff
path: root/share/examples/puffs/icfs/icfs.c
blob: 5f9f69103fdebb16a3fcde3e27a397e2212bd01b (plain)
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
/*	$NetBSD: icfs.c,v 1.11 2008/09/12 14:40:46 christos Exp $	*/

/*
 * Copyright (c) 2007  Antti Kantee.  All Rights Reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

/*
 * icfs: layer everything in lowercase
 * (unfinished, as is probably fairly easy to tell)
 *
 * Now, this "layered" file system demostrates a nice gotcha with
 * mangling readdir entries.  In case we can't unmangle the name
 * (cf. rot13fs), we need to map the mangled name back to the real
 * name in lookup and store the actual lower layer name instead of
 * the mangled name.
 *
 * This is mounted without namecache.  Otherwise we might have
 * two different nodes for e.g. FoO and foo.  It might be possible
 * support name cache by having all namespace-altering operations
 * flush the directory namecache ...
 */

#include <sys/types.h>

#include <assert.h>
#include <ctype.h>
#include <dirent.h>
#include <err.h>
#include <errno.h>
#include <puffs.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

PUFFSOP_PROTOS(ic)

static void usage(void);

static void
usage()
{

	errx(1, "usage: %s [-sp] [-o mntopts] icfs mountpath",
	    getprogname());
}

static void
dotolower(char *buf, size_t buflen)
{

	while (buflen--) {
		*buf = tolower((unsigned char)*buf);
		buf++;
	}
}

static int
icpathcmp(struct puffs_usermount *pu, struct puffs_pathobj *c1,
	struct puffs_pathobj *c2, size_t clen, int checkprefix)
{
	struct puffs_pathobj *po_root;
	char *cp1, *cp2;
	size_t rplen;

	po_root = puffs_getrootpathobj(pu);
	rplen = po_root->po_len;

	assert(clen >= rplen);

	cp1 = c1->po_path;
	cp2 = c2->po_path;

	if (strncasecmp(cp1 + rplen, cp2 + rplen, clen - rplen) != 0)
		return 1;

	if (checkprefix == 0)
		return 0;

	if (c1->po_len < c2->po_len)
		return 1;

	if (*(cp1 + clen) != '/')
		return 1;

	return 0;
}

static int
icpathxform(struct puffs_usermount *pu, const struct puffs_pathobj *po_base,
	const struct puffs_cn *pcn, struct puffs_pathobj *po_new)
{
	struct dirent entry, *result;
	char *src;
	size_t srclen;
	DIR *dp;

	dp = opendir(po_base->po_path);
	if (dp == NULL)
		return errno;

	src = pcn->pcn_name;
	srclen = pcn->pcn_namelen;
	for (;;) {
		if (readdir_r(dp, &entry, &result) != 0)
			break;
		if (!result)
			break;
		if (strcasecmp(result->d_name, pcn->pcn_name) == 0) {
			src = result->d_name;
			srclen = strlen(result->d_name);
			break;
		}
	}

	po_new->po_path = strdup(src);
	po_new->po_len = srclen;
	closedir(dp);

	return 0;
}

int
main(int argc, char *argv[])
{
	struct puffs_usermount *pu;
	struct puffs_ops *pops;
	struct puffs_pathobj *po_root;
	struct puffs_node *pn_root;
	struct stat sb;
	mntoptparse_t mp;
	int mntflags, pflags;
	int detach, dpres;
	int ch;

	setprogname(argv[0]);

	if (argc < 3)
		usage();

	pflags = mntflags = dpres = 0;
	detach = 1;
	while ((ch = getopt(argc, argv, "o:sp")) != -1) {
		switch (ch) {
		case 'o':
			mp = getmntopts(optarg, puffsmopts, &mntflags, &pflags);
			if (mp == NULL)
				err(1, "getmntopts");
			freemntopts(mp);
			break;
		case 'p':
			dpres = 1;
			break;
		case 's':
			detach = 0;
			break;
		}
	}
	pflags |= PUFFS_FLAG_BUILDPATH;
	pflags |= PUFFS_KFLAG_NOCACHE_NAME;
	argv += optind;
	argc -= optind;

	if (pflags & PUFFS_FLAG_OPDUMP)
		detach = 0;

	if (argc != 2)
		usage();

	if (lstat(argv[0], &sb) == -1)
		err(1, "stat %s", argv[0]);
	if ((sb.st_mode & S_IFDIR) == 0)
		errx(1, "%s is not a directory", argv[0]);

	PUFFSOP_INIT(pops);
	puffs_null_setops(pops);

	if (dpres == 0)
		PUFFSOP_SET(pops, ic, node, readdir);

	if ((pu = puffs_init(pops, argv[0], "ic", NULL, pflags)) == NULL)
		err(1, "mount");

	pn_root = puffs_pn_new(pu, NULL);
	if (pn_root == NULL)
		err(1, "puffs_pn_new");
	puffs_setroot(pu, pn_root);

	po_root = puffs_getrootpathobj(pu);
	if (po_root == NULL)
		err(1, "getrootpathobj");
	po_root->po_path = argv[0];
	po_root->po_len = strlen(argv[0]);
	puffs_stat2vattr(&pn_root->pn_va, &sb);

	puffs_set_pathcmp(pu, icpathcmp);
	puffs_set_pathtransform(pu, icpathxform);

	if (detach)
		if (puffs_daemon(pu, 1, 1) == -1)
			err(1, "puffs_daemon");

	if (puffs_mount(pu, argv[1], mntflags, pn_root) == -1)
		err(1, "puffs_mount");
	if (puffs_mainloop(pu) == -1)
		err(1, "mainloop");

	return 0;
}

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)
{
	struct dirent *dp;
	size_t rl;
	int rv;

	dp = dent;
	rl = *reslen;

	rv = puffs_null_node_readdir(pu, opc, dent, readoff, reslen, pcr,
	    eofflag, cookies, ncookies);
	if (rv)
		return rv;

	while (rl > *reslen) {
		dotolower(dp->d_name, dp->d_namlen);
		rl -= _DIRENT_SIZE(dp);
		dp = _DIRENT_NEXT(dp);
	}

	return 0;
}