rot13fs.c revision 1.2 1 /* $NetBSD: rot13fs.c,v 1.2 2007/02/15 12:54:24 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 * 3. The name of the company nor the name of the author may be used to
15 * endorse or promote products derived from this software without specific
16 * prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
19 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31 /*
32 * rot13fs: puffs layering experiment
33 * (unfinished, as is probably fairly easy to tell)
34 */
35
36 #include <err.h>
37 #include <errno.h>
38 #include <puffs.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <unistd.h>
42
43 PUFFSOP_PROTOS(puffs_null) /* XXX */
44 PUFFSOP_PROTOS(rot13)
45
46 static void usage(void);
47
48 static void
49 usage()
50 {
51
52 errx(1, "usage: %s [-s][-o mntopts]rot13path mountpath",
53 getprogname());
54 }
55
56 static uint8_t tbl[256];
57
58 static void
59 dorot13(void *buf, size_t buflen)
60 {
61 uint8_t *b = buf;
62
63 while (buflen--) {
64 *b = tbl[*b];
65 b++;
66 }
67 }
68
69 static int
70 rot13path(struct puffs_usermount *pu, struct puffs_pathobj *base,
71 struct puffs_cn *pcn)
72 {
73
74 dorot13(pcn->pcn_name, pcn->pcn_namelen);
75
76 return 0;
77 }
78
79 int
80 main(int argc, char *argv[])
81 {
82 struct puffs_usermount *pu;
83 struct puffs_ops *pops;
84 struct puffs_pathobj *po_root;
85 struct statvfs svfsb;
86 struct stat sb;
87 mntoptparse_t mp;
88 int mntflags, pflags, lflags;
89 int ch;
90 int i;
91
92 setprogname(argv[0]);
93
94 if (argc < 3)
95 usage();
96
97 pflags = lflags = mntflags = 0;
98 while ((ch = getopt(argc, argv, "o:s")) != -1) {
99 switch (ch) {
100 case 'o':
101 mp = getmntopts(optarg, puffsmopts, &mntflags, &pflags);
102 if (mp == NULL)
103 err(1, "getmntopts");
104 freemntopts(mp);
105 break;
106 case 's':
107 lflags |= PUFFSLOOP_NODAEMON;
108 break;
109 }
110 }
111 pflags |= PUFFS_FLAG_BUILDPATH;
112 argv += optind;
113 argc -= optind;
114
115 if (pflags & PUFFS_FLAG_OPDUMP)
116 lflags = PUFFSLOOP_NODAEMON;
117
118 if (argc != 2)
119 usage();
120
121 PUFFSOP_INIT(pops);
122
123 PUFFSOP_SET(pops, puffs_null, fs, statvfs);
124 PUFFSOP_SETFSNOP(pops, unmount);
125 PUFFSOP_SETFSNOP(pops, sync);
126
127 PUFFSOP_SET(pops, rot13, node, readdir);
128 PUFFSOP_SET(pops, rot13, node, read);
129 PUFFSOP_SET(pops, rot13, node, write);
130
131 PUFFSOP_SET(pops, puffs_null, node, lookup);
132 PUFFSOP_SET(pops, puffs_null, node, create);
133 PUFFSOP_SET(pops, puffs_null, node, mknod);
134 PUFFSOP_SET(pops, puffs_null, node, getattr);
135 PUFFSOP_SET(pops, puffs_null, node, setattr);
136 PUFFSOP_SET(pops, puffs_null, node, fsync);
137 PUFFSOP_SET(pops, puffs_null, node, remove);
138 PUFFSOP_SET(pops, puffs_null, node, link);
139 PUFFSOP_SET(pops, puffs_null, node, rename);
140 PUFFSOP_SET(pops, puffs_null, node, mkdir);
141 PUFFSOP_SET(pops, puffs_null, node, rmdir);
142 PUFFSOP_SET(pops, puffs_null, node, symlink);
143 PUFFSOP_SET(pops, puffs_null, node, readlink);
144 PUFFSOP_SET(pops, puffs_null, node, reclaim);
145
146 if ((pu = puffs_mount(pops, argv[1], mntflags, "rot13", NULL,
147 pflags, 0)) == NULL)
148 err(1, "mount");
149
150 if (statvfs(argv[0], &svfsb) == -1)
151 err(1, "statvfs %s", argv[0]);
152
153 pu->pu_pn_root = puffs_pn_new(pu, NULL);
154 if (pu->pu_pn_root == NULL)
155 err(1, "puffs_pn_new");
156
157 po_root = puffs_getrootpathobj(pu);
158 if (po_root == NULL)
159 err(1, "getrootpathobj");
160 po_root->po_path = argv[0];
161 po_root->po_len = strlen(argv[0]);
162
163 puffs_set_namemod(pu, rot13path);
164
165 if (stat(argv[0], &sb) == -1)
166 err(1, "stat %s", argv[0]);
167 puffs_stat2vattr(&pu->pu_pn_root->pn_va, &sb);
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_start(pu, pu->pu_pn_root, &svfsb) == -1)
178 err(1, "puffs_start");
179
180 if (puffs_mainloop(pu, lflags) == -1)
181 err(1, "mainloop");
182
183 return 0;
184 }
185
186 int
187 rot13_node_readdir(struct puffs_cc *pcc, void *opc, struct dirent *dent,
188 const struct puffs_cred *pcr, off_t *readoff, size_t *reslen)
189 {
190 struct dirent *dp;
191 size_t rl;
192 int rv;
193
194 dp = dent;
195 rl = *reslen;
196
197 rv = puffs_null_node_readdir(pcc, opc, dent, pcr, readoff, reslen);
198 if (rv)
199 return rv;
200
201 while (rl > *reslen) {
202 dorot13((uint8_t *)dp->d_name, dp->d_namlen);
203 rl -= _DIRENT_SIZE(dp);
204 dp = _DIRENT_NEXT(dp);
205 }
206
207 return 0;
208 }
209
210 int
211 rot13_node_read(struct puffs_cc *pcc, void *opc, uint8_t *buf, off_t offset,
212 size_t *resid, const struct puffs_cred *pcr, int ioflag)
213 {
214 uint8_t *prebuf = buf;
215 size_t preres = *resid;
216 int rv;
217
218 rv = puffs_null_node_read(pcc, opc, buf, offset, resid, pcr, ioflag);
219 if (rv)
220 return rv;
221
222 dorot13(prebuf, preres - *resid);
223
224 return rv;
225 }
226
227 int
228 rot13_node_write(struct puffs_cc *pcc, void *opc, uint8_t *buf, off_t offset,
229 size_t *resid, const struct puffs_cred *pcr, int ioflag)
230 {
231
232 dorot13(buf, *resid);
233 return puffs_null_node_write(pcc, opc, buf, offset, resid, pcr, ioflag);
234 }
235