rot13fs.c revision 1.13 1 /* $NetBSD: rot13fs.c,v 1.13 2007/11/05 17:48:18 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 * rot13fs: puffs layering experiment
30 * (unfinished, as is probably fairly easy to tell)
31 *
32 * This also demonstrates how namemod can be easily set to any
33 * function which reverses itself (argument -f provides a case-flipping
34 * file system).
35 */
36
37 #include <ctype.h>
38 #include <err.h>
39 #include <errno.h>
40 #include <puffs.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <unistd.h>
44
45 PUFFSOP_PROTOS(rot13)
46
47 static void usage(void);
48
49 static void
50 usage()
51 {
52
53 errx(1, "usage: %s [-s][-o mntopts]rot13path mountpath",
54 getprogname());
55 }
56
57 static void (*flipflop)(void *, size_t);
58
59 static uint8_t tbl[256];
60
61 static void
62 dorot13(void *buf, size_t buflen)
63 {
64 uint8_t *b = buf;
65
66 while (buflen--) {
67 *b = tbl[*b];
68 b++;
69 }
70 }
71
72 static void
73 docase(void *buf, size_t buflen)
74 {
75 unsigned char *b = buf;
76
77 while (buflen--) {
78 if (isalpha(*b))
79 *b ^= 0x20;
80 b++;
81 }
82 }
83
84 static int
85 rot13path(struct puffs_usermount *pu, struct puffs_pathobj *base,
86 struct puffs_cn *pcn)
87 {
88
89 flipflop(pcn->pcn_name, pcn->pcn_namelen);
90
91 return 0;
92 }
93
94 int
95 main(int argc, char *argv[])
96 {
97 struct puffs_usermount *pu;
98 struct puffs_ops *pops;
99 struct puffs_pathobj *po_root;
100 struct puffs_node *pn_root;
101 struct stat sb;
102 mntoptparse_t mp;
103 int mntflags, pflags;
104 int detach;
105 int ch;
106 int i;
107
108 setprogname(argv[0]);
109
110 if (argc < 3)
111 usage();
112
113 flipflop = dorot13;
114 pflags = mntflags = 0;
115 detach = 1;
116 while ((ch = getopt(argc, argv, "fo:s")) != -1) {
117 switch (ch) {
118 case 'f':
119 flipflop = docase;
120 break;
121 case 'o':
122 mp = getmntopts(optarg, puffsmopts, &mntflags, &pflags);
123 if (mp == NULL)
124 err(1, "getmntopts");
125 freemntopts(mp);
126 break;
127 case 's':
128 detach = 0;
129 break;
130 }
131 }
132 pflags |= PUFFS_FLAG_BUILDPATH;
133 argv += optind;
134 argc -= optind;
135
136 if (pflags & PUFFS_FLAG_OPDUMP)
137 detach = 0;
138
139 if (argc != 2)
140 usage();
141
142 if (lstat(argv[0], &sb) == -1)
143 err(1, "stat %s", argv[0]);
144 if ((sb.st_mode & S_IFDIR) == 0)
145 errx(1, "%s is not a directory", argv[0]);
146
147 PUFFSOP_INIT(pops);
148 puffs_null_setops(pops);
149
150 PUFFSOP_SET(pops, rot13, node, readdir);
151 PUFFSOP_SET(pops, rot13, node, read);
152 PUFFSOP_SET(pops, rot13, node, write);
153
154 if ((pu = puffs_init(pops, argv[0], "rot13", NULL, pflags)) == NULL)
155 err(1, "mount");
156
157 pn_root = puffs_pn_new(pu, NULL);
158 if (pn_root == NULL)
159 err(1, "puffs_pn_new");
160 puffs_setroot(pu, pn_root);
161
162 po_root = puffs_getrootpathobj(pu);
163 if (po_root == NULL)
164 err(1, "getrootpathobj");
165 po_root->po_path = argv[0];
166 po_root->po_len = strlen(argv[0]);
167 puffs_stat2vattr(&pn_root->pn_va, &sb);
168
169 puffs_set_namemod(pu, rot13path);
170
171 /* initialize rot13 tables */
172 for (i = 0; i < 256; i++)
173 tbl[i] = (uint8_t)i;
174 for (i = 0; i <= 26; i++)
175 tbl[i + 'a'] = 'a' + ((i + 13) % 26);
176 for (i = 0; i < 26; i++)
177 tbl[i + 'A'] = 'A' + ((i + 13) % 26);
178
179 if (puffs_mount(pu, argv[1], mntflags, pn_root) == -1)
180 err(1, "puffs_mount");
181
182 if (detach)
183 if (daemon(1, 1) == -1)
184 err(1, "daemon");
185
186 if (puffs_mainloop(pu) == -1)
187 return 1;
188
189 return 0;
190 }
191
192 int
193 rot13_node_readdir(struct puffs_cc *pcc, void *opc, struct dirent *dent,
194 off_t *readoff, size_t *reslen, const struct puffs_cred *pcr,
195 int *eofflag, off_t *cookies, size_t *ncookies)
196 {
197 struct dirent *dp;
198 size_t rl;
199 int rv;
200
201 dp = dent;
202 rl = *reslen;
203
204 rv = puffs_null_node_readdir(pcc, opc, dent, readoff, reslen, pcr,
205 eofflag, cookies, ncookies);
206 if (rv)
207 return rv;
208
209 while (rl > *reslen) {
210 flipflop((uint8_t *)dp->d_name, dp->d_namlen);
211 rl -= _DIRENT_SIZE(dp);
212 dp = _DIRENT_NEXT(dp);
213 }
214
215 return 0;
216 }
217
218 int
219 rot13_node_read(struct puffs_cc *pcc, void *opc, uint8_t *buf, off_t offset,
220 size_t *resid, const struct puffs_cred *pcr, int ioflag)
221 {
222 uint8_t *prebuf = buf;
223 size_t preres = *resid;
224 int rv;
225
226 rv = puffs_null_node_read(pcc, opc, buf, offset, resid, pcr, ioflag);
227 if (rv)
228 return rv;
229
230 flipflop(prebuf, preres - *resid);
231
232 return rv;
233 }
234
235 int
236 rot13_node_write(struct puffs_cc *pcc, void *opc, uint8_t *buf, off_t offset,
237 size_t *resid, const struct puffs_cred *pcr, int ioflag)
238 {
239
240 flipflop(buf, *resid);
241 return puffs_null_node_write(pcc, opc, buf, offset, resid, pcr, ioflag);
242 }
243