rot13fs.c revision 1.12 1 /* $NetBSD: rot13fs.c,v 1.12 2007/07/17 11:34:54 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, lflags;
104 int ch;
105 int i;
106
107 setprogname(argv[0]);
108
109 if (argc < 3)
110 usage();
111
112 flipflop = dorot13;
113 pflags = lflags = mntflags = 0;
114 while ((ch = getopt(argc, argv, "fo:s")) != -1) {
115 switch (ch) {
116 case 'f':
117 flipflop = docase;
118 break;
119 case 'o':
120 mp = getmntopts(optarg, puffsmopts, &mntflags, &pflags);
121 if (mp == NULL)
122 err(1, "getmntopts");
123 freemntopts(mp);
124 break;
125 case 's':
126 lflags |= PUFFSLOOP_NODAEMON;
127 break;
128 }
129 }
130 pflags |= PUFFS_FLAG_BUILDPATH;
131 argv += optind;
132 argc -= optind;
133
134 if (pflags & PUFFS_FLAG_OPDUMP)
135 lflags = PUFFSLOOP_NODAEMON;
136
137 if (argc != 2)
138 usage();
139
140 if (lstat(argv[0], &sb) == -1)
141 err(1, "stat %s", argv[0]);
142 if ((sb.st_mode & S_IFDIR) == 0)
143 errx(1, "%s is not a directory", argv[0]);
144
145 PUFFSOP_INIT(pops);
146 puffs_null_setops(pops);
147
148 PUFFSOP_SET(pops, rot13, node, readdir);
149 PUFFSOP_SET(pops, rot13, node, read);
150 PUFFSOP_SET(pops, rot13, node, write);
151
152 if ((pu = puffs_init(pops, argv[0], "rot13", NULL, pflags)) == NULL)
153 err(1, "mount");
154
155 pn_root = puffs_pn_new(pu, NULL);
156 if (pn_root == NULL)
157 err(1, "puffs_pn_new");
158 puffs_setroot(pu, pn_root);
159
160 po_root = puffs_getrootpathobj(pu);
161 if (po_root == NULL)
162 err(1, "getrootpathobj");
163 po_root->po_path = argv[0];
164 po_root->po_len = strlen(argv[0]);
165 puffs_stat2vattr(&pn_root->pn_va, &sb);
166
167 puffs_set_namemod(pu, rot13path);
168
169 /* initialize rot13 tables */
170 for (i = 0; i < 256; i++)
171 tbl[i] = (uint8_t)i;
172 for (i = 0; i <= 26; i++)
173 tbl[i + 'a'] = 'a' + ((i + 13) % 26);
174 for (i = 0; i < 26; i++)
175 tbl[i + 'A'] = 'A' + ((i + 13) % 26);
176
177 if (puffs_mount(pu, argv[1], mntflags, pn_root) == -1)
178 err(1, "puffs_mount");
179
180 return puffs_mainloop(pu, lflags);
181 }
182
183 int
184 rot13_node_readdir(struct puffs_cc *pcc, void *opc, struct dirent *dent,
185 off_t *readoff, size_t *reslen, const struct puffs_cred *pcr,
186 int *eofflag, off_t *cookies, size_t *ncookies)
187 {
188 struct dirent *dp;
189 size_t rl;
190 int rv;
191
192 dp = dent;
193 rl = *reslen;
194
195 rv = puffs_null_node_readdir(pcc, opc, dent, readoff, reslen, pcr,
196 eofflag, cookies, ncookies);
197 if (rv)
198 return rv;
199
200 while (rl > *reslen) {
201 flipflop((uint8_t *)dp->d_name, dp->d_namlen);
202 rl -= _DIRENT_SIZE(dp);
203 dp = _DIRENT_NEXT(dp);
204 }
205
206 return 0;
207 }
208
209 int
210 rot13_node_read(struct puffs_cc *pcc, void *opc, uint8_t *buf, off_t offset,
211 size_t *resid, const struct puffs_cred *pcr, int ioflag)
212 {
213 uint8_t *prebuf = buf;
214 size_t preres = *resid;
215 int rv;
216
217 rv = puffs_null_node_read(pcc, opc, buf, offset, resid, pcr, ioflag);
218 if (rv)
219 return rv;
220
221 flipflop(prebuf, preres - *resid);
222
223 return rv;
224 }
225
226 int
227 rot13_node_write(struct puffs_cc *pcc, void *opc, uint8_t *buf, off_t offset,
228 size_t *resid, const struct puffs_cred *pcr, int ioflag)
229 {
230
231 flipflop(buf, *resid);
232 return puffs_null_node_write(pcc, opc, buf, offset, resid, pcr, ioflag);
233 }
234