Home | History | Annotate | Line # | Download | only in rot13fs
rot13fs.c revision 1.4
      1 /*	$NetBSD: rot13fs.c,v 1.4 2007/04/12 15:09:02 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 puffs_node *pn_root;
     86 	struct statvfs svfsb;
     87 	struct stat sb;
     88 	mntoptparse_t mp;
     89 	int mntflags, pflags, lflags;
     90 	int ch;
     91 	int i;
     92 
     93 	setprogname(argv[0]);
     94 
     95 	if (argc < 3)
     96 		usage();
     97 
     98 	pflags = lflags = mntflags = 0;
     99 	while ((ch = getopt(argc, argv, "o:s")) != -1) {
    100 		switch (ch) {
    101 		case 'o':
    102 			mp = getmntopts(optarg, puffsmopts, &mntflags, &pflags);
    103 			if (mp == NULL)
    104 				err(1, "getmntopts");
    105 			freemntopts(mp);
    106 			break;
    107 		case 's':
    108 			lflags |= PUFFSLOOP_NODAEMON;
    109 			break;
    110 		}
    111 	}
    112 	pflags |= PUFFS_FLAG_BUILDPATH;
    113 	argv += optind;
    114 	argc -= optind;
    115 
    116 	if (pflags & PUFFS_FLAG_OPDUMP)
    117 		lflags = PUFFSLOOP_NODAEMON;
    118 
    119 	if (argc != 2)
    120 		usage();
    121 
    122 	PUFFSOP_INIT(pops);
    123 
    124 	PUFFSOP_SET(pops, puffs_null, fs, statvfs);
    125 	PUFFSOP_SETFSNOP(pops, unmount);
    126 	PUFFSOP_SETFSNOP(pops, sync);
    127 
    128 	PUFFSOP_SET(pops, rot13, node, readdir);
    129 	PUFFSOP_SET(pops, rot13, node, read);
    130 	PUFFSOP_SET(pops, rot13, node, write);
    131 
    132 	PUFFSOP_SET(pops, puffs_null, node, lookup);
    133 	PUFFSOP_SET(pops, puffs_null, node, create);
    134 	PUFFSOP_SET(pops, puffs_null, node, mknod);
    135 	PUFFSOP_SET(pops, puffs_null, node, getattr);
    136 	PUFFSOP_SET(pops, puffs_null, node, setattr);
    137 	PUFFSOP_SET(pops, puffs_null, node, fsync);
    138 	PUFFSOP_SET(pops, puffs_null, node, remove);
    139 	PUFFSOP_SET(pops, puffs_null, node, link);
    140 	PUFFSOP_SET(pops, puffs_null, node, rename);
    141 	PUFFSOP_SET(pops, puffs_null, node, mkdir);
    142 	PUFFSOP_SET(pops, puffs_null, node, rmdir);
    143 	PUFFSOP_SET(pops, puffs_null, node, symlink);
    144 	PUFFSOP_SET(pops, puffs_null, node, readlink);
    145 	PUFFSOP_SET(pops, puffs_null, node, reclaim);
    146 
    147 	if ((pu = puffs_mount(pops, argv[1], mntflags, "rot13", NULL,
    148 	    pflags, 0)) == NULL)
    149 		err(1, "mount");
    150 
    151 	if (statvfs(argv[0], &svfsb) == -1)
    152 		err(1, "statvfs %s", argv[0]);
    153 
    154 	pn_root = puffs_pn_new(pu, NULL);
    155 	if (pn_root == NULL)
    156 		err(1, "puffs_pn_new");
    157 	puffs_setroot(pu, pn_root);
    158 
    159 	po_root = puffs_getrootpathobj(pu);
    160 	if (po_root == NULL)
    161 		err(1, "getrootpathobj");
    162 	po_root->po_path = argv[0];
    163 	po_root->po_len = strlen(argv[0]);
    164 
    165 	puffs_set_namemod(pu, rot13path);
    166 
    167 	if (stat(argv[0], &sb) == -1)
    168 		err(1, "stat %s", argv[0]);
    169 	puffs_stat2vattr(&pn_root->pn_va, &sb);
    170 
    171 	/* initialize rot13 tables */
    172 	for (i = 0; i < 256; i++)
    173 		tbl[i] = (uint8_t)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);
    178 
    179 	if (puffs_start(pu, pn_root, &svfsb) == -1)
    180 		err(1, "puffs_start");
    181 
    182 	if (puffs_mainloop(pu, lflags) == -1)
    183 		err(1, "mainloop");
    184 
    185 	return 0;
    186 }
    187 
    188 int
    189 rot13_node_readdir(struct puffs_cc *pcc, void *opc, struct dirent *dent,
    190 	off_t *readoff, size_t *reslen, const struct puffs_cred *pcr,
    191 	int *eofflag, off_t *cookies, size_t *ncookies)
    192 {
    193 	struct dirent *dp;
    194 	size_t rl;
    195 	int rv;
    196 
    197 	dp = dent;
    198 	rl = *reslen;
    199 
    200 	rv = puffs_null_node_readdir(pcc, opc, dent, readoff, reslen, pcr,
    201 	    eofflag, cookies, ncookies);
    202 	if (rv)
    203 		return rv;
    204 
    205 	while (rl > *reslen) {
    206 		dorot13((uint8_t *)dp->d_name, dp->d_namlen);
    207 		rl -= _DIRENT_SIZE(dp);
    208 		dp = _DIRENT_NEXT(dp);
    209 	}
    210 
    211 	return 0;
    212 }
    213 
    214 int
    215 rot13_node_read(struct puffs_cc *pcc, void *opc, uint8_t *buf, off_t offset,
    216 	size_t *resid, const struct puffs_cred *pcr, int ioflag)
    217 {
    218 	uint8_t *prebuf = buf;
    219 	size_t preres = *resid;
    220 	int rv;
    221 
    222 	rv = puffs_null_node_read(pcc, opc, buf, offset, resid, pcr, ioflag);
    223 	if (rv)
    224 		return rv;
    225 
    226 	dorot13(prebuf, preres - *resid);
    227 
    228 	return rv;
    229 }
    230 
    231 int
    232 rot13_node_write(struct puffs_cc *pcc, void *opc, uint8_t *buf, off_t offset,
    233 	size_t *resid, const struct puffs_cred *pcr, int ioflag)
    234 {
    235 
    236 	dorot13(buf, *resid);
    237 	return puffs_null_node_write(pcc, opc, buf, offset, resid, pcr, ioflag);
    238 }
    239