rot13fs.c revision 1.1 1 /* $NetBSD: rot13fs.c,v 1.1 2007/01/15 00:46:29 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
145 if ((pu = puffs_mount(pops, argv[1], mntflags, "rot13", NULL,
146 pflags, 0)) == NULL)
147 err(1, "mount");
148
149 if (statvfs(argv[0], &svfsb) == -1)
150 err(1, "statvfs %s", argv[0]);
151
152 pu->pu_pn_root = puffs_pn_new(pu, NULL);
153 if (pu->pu_pn_root == NULL)
154 err(1, "puffs_pn_new");
155
156 po_root = puffs_getrootpathobj(pu);
157 if (po_root == NULL)
158 err(1, "getrootpathobj");
159 po_root->po_path = argv[0];
160 po_root->po_len = strlen(argv[0]);
161
162 puffs_set_namemod(pu, rot13path);
163
164 if (stat(argv[0], &sb) == -1)
165 err(1, "stat %s", argv[0]);
166 puffs_stat2vattr(&pu->pu_pn_root->pn_va, &sb);
167
168 /* initialize rot13 tables */
169 for (i = 0; i < 256; i++)
170 tbl[i] = (uint8_t)i;
171 for (i = 0; i <= 26; i++)
172 tbl[i + 'a'] = 'a' + ((i + 13) % 26);
173 for (i = 0; i < 26; i++)
174 tbl[i + 'A'] = 'A' + ((i + 13) % 26);
175
176 if (puffs_start(pu, pu->pu_pn_root, &svfsb) == -1)
177 err(1, "puffs_start");
178
179 if (puffs_mainloop(pu, lflags) == -1)
180 err(1, "mainloop");
181
182 return 0;
183 }
184
185 int
186 rot13_node_readdir(struct puffs_cc *pcc, void *opc, struct dirent *dent,
187 const struct puffs_cred *pcr, off_t *readoff, size_t *reslen)
188 {
189 struct dirent *dp;
190 size_t rl;
191 int rv;
192
193 dp = dent;
194 rl = *reslen;
195
196 rv = puffs_null_node_readdir(pcc, opc, dent, pcr, readoff, reslen);
197 if (rv)
198 return rv;
199
200 while (rl > *reslen) {
201 dorot13((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 dorot13(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 dorot13(buf, *resid);
232 return puffs_null_node_write(pcc, opc, buf, offset, resid, pcr, ioflag);
233 }
234