1 /* $NetBSD: rot13fs.c,v 1.16 2007/11/30 19:02:38 pooka Exp $ */
4 * Copyright (c) 2007 Antti Kantee. All Rights Reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
29 * rot13fs: puffs layering experiment
30 * (unfinished, as is probably fairly easy to tell)
32 * This also demonstrates how namemod can be easily set to any
33 * function which reverses itself (argument -f provides a case-flipping
47 static void usage(void);
53 errx(1, "usage: %s [-s] [-o mntopts] rot13path mountpath",
57 static void (*flipflop
)(void *, size_t);
59 static uint8_t tbl
[256];
62 dorot13(void *buf
, size_t buflen
)
73 docase(void *buf
, size_t buflen
)
75 unsigned char *b
= buf
;
85 rot13path(struct puffs_usermount
*pu
, struct puffs_pathobj
*base
,
89 flipflop(pcn
->pcn_name
, pcn
->pcn_namelen
);
95 main(int argc
, char *argv
[])
97 struct puffs_usermount
*pu
;
98 struct puffs_ops
*pops
;
99 struct puffs_pathobj
*po_root
;
100 struct puffs_node
*pn_root
;
103 int mntflags
, pflags
;
108 setprogname(argv
[0]);
114 pflags
= mntflags
= 0;
116 while ((ch
= getopt(argc
, argv
, "fo:s")) != -1) {
122 mp
= getmntopts(optarg
, puffsmopts
, &mntflags
, &pflags
);
124 err(1, "getmntopts");
132 pflags
|= PUFFS_FLAG_BUILDPATH
;
136 if (pflags
& PUFFS_FLAG_OPDUMP
)
142 if (lstat(argv
[0], &sb
) == -1)
143 err(1, "stat %s", argv
[0]);
144 if ((sb
.st_mode
& S_IFDIR
) == 0)
145 errx(1, "%s is not a directory", argv
[0]);
148 puffs_null_setops(pops
);
150 PUFFSOP_SET(pops
, rot13
, node
, readdir
);
151 PUFFSOP_SET(pops
, rot13
, node
, read
);
152 PUFFSOP_SET(pops
, rot13
, node
, write
);
154 if ((pu
= puffs_init(pops
, argv
[0], "rot13", NULL
, pflags
)) == NULL
)
157 pn_root
= puffs_pn_new(pu
, NULL
);
159 err(1, "puffs_pn_new");
160 puffs_setroot(pu
, pn_root
);
162 po_root
= puffs_getrootpathobj(pu
);
164 err(1, "getrootpathobj");
165 po_root
->po_path
= argv
[0];
166 po_root
->po_len
= strlen(argv
[0]);
167 puffs_stat2vattr(&pn_root
->pn_va
, &sb
);
169 puffs_set_namemod(pu
, rot13path
);
171 /* initialize rot13 tables */
172 for (i
= 0; i
< 256; i
++)
174 for (i
= 0; i
<= 26; i
++)
175 tbl
[i
+ 'a'] = 'a' + ((i
+ 13) % 26);
176 for (i
= 0; i
< 26; i
++)
177 tbl
[i
+ 'A'] = 'A' + ((i
+ 13) % 26);
180 if (puffs_daemon(pu
, 1, 1) == -1)
181 err(1, "puffs_daemon");
183 if (puffs_mount(pu
, argv
[1], mntflags
, pn_root
) == -1)
184 err(1, "puffs_mount");
185 if (puffs_mainloop(pu
) == -1)
192 rot13_node_readdir(struct puffs_usermount
*pu
, void *opc
, struct dirent
*dent
,
193 off_t
*readoff
, size_t *reslen
, const struct puffs_cred
*pcr
,
194 int *eofflag
, off_t
*cookies
, size_t *ncookies
)
203 rv
= puffs_null_node_readdir(pu
, opc
, dent
, readoff
, reslen
, pcr
,
204 eofflag
, cookies
, ncookies
);
208 while (rl
> *reslen
) {
209 flipflop((uint8_t *)dp
->d_name
, dp
->d_namlen
);
210 rl
-= _DIRENT_SIZE(dp
);
211 dp
= _DIRENT_NEXT(dp
);
218 rot13_node_read(struct puffs_usermount
*pu
, void *opc
,
219 uint8_t *buf
, off_t offset
, size_t *resid
,
220 const struct puffs_cred
*pcr
, int ioflag
)
222 uint8_t *prebuf
= buf
;
223 size_t preres
= *resid
;
226 rv
= puffs_null_node_read(pu
, opc
, buf
, offset
, resid
, pcr
, ioflag
);
230 flipflop(prebuf
, preres
- *resid
);
236 rot13_node_write(struct puffs_usermount
*pu
, void *opc
,
237 uint8_t *buf
, off_t offset
, size_t *resid
,
238 const struct puffs_cred
*pcr
, int ioflag
)
241 flipflop(buf
, *resid
);
242 return puffs_null_node_write(pu
, opc
, buf
, offset
, resid
, pcr
, ioflag
);