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