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