Home | History | Annotate | Line # | Download | only in rot13fs
rot13fs.c revision 1.9
      1  1.9  pooka /*	$NetBSD: rot13fs.c,v 1.9 2007/06/20 21:11:26 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  *
     15  1.1  pooka  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     16  1.1  pooka  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     17  1.1  pooka  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     18  1.1  pooka  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     19  1.1  pooka  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     20  1.1  pooka  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     21  1.1  pooka  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22  1.1  pooka  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     23  1.1  pooka  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     24  1.1  pooka  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     25  1.1  pooka  * SUCH DAMAGE.
     26  1.1  pooka  */
     27  1.1  pooka 
     28  1.1  pooka /*
     29  1.1  pooka  * rot13fs: puffs layering experiment
     30  1.1  pooka  * (unfinished, as is probably fairly easy to tell)
     31  1.1  pooka  */
     32  1.1  pooka 
     33  1.1  pooka #include <err.h>
     34  1.1  pooka #include <errno.h>
     35  1.1  pooka #include <puffs.h>
     36  1.1  pooka #include <stdio.h>
     37  1.1  pooka #include <stdlib.h>
     38  1.1  pooka #include <unistd.h>
     39  1.1  pooka 
     40  1.1  pooka PUFFSOP_PROTOS(puffs_null) /* XXX */
     41  1.1  pooka PUFFSOP_PROTOS(rot13)
     42  1.1  pooka 
     43  1.1  pooka static void usage(void);
     44  1.1  pooka 
     45  1.1  pooka static void
     46  1.1  pooka usage()
     47  1.1  pooka {
     48  1.1  pooka 
     49  1.1  pooka 	errx(1, "usage: %s [-s][-o mntopts]rot13path mountpath",
     50  1.1  pooka 	    getprogname());
     51  1.1  pooka }
     52  1.1  pooka 
     53  1.1  pooka static uint8_t tbl[256];
     54  1.1  pooka 
     55  1.1  pooka static void
     56  1.1  pooka dorot13(void *buf, size_t buflen)
     57  1.1  pooka {
     58  1.1  pooka 	uint8_t *b = buf;
     59  1.1  pooka 
     60  1.1  pooka 	while (buflen--) {
     61  1.1  pooka 		*b = tbl[*b];
     62  1.1  pooka 		b++;
     63  1.1  pooka 	}
     64  1.1  pooka }
     65  1.1  pooka 
     66  1.1  pooka static int
     67  1.1  pooka rot13path(struct puffs_usermount *pu, struct puffs_pathobj *base,
     68  1.1  pooka 	struct puffs_cn *pcn)
     69  1.1  pooka {
     70  1.1  pooka 
     71  1.1  pooka 	dorot13(pcn->pcn_name, pcn->pcn_namelen);
     72  1.1  pooka 
     73  1.1  pooka 	return 0;
     74  1.1  pooka }
     75  1.1  pooka 
     76  1.1  pooka int
     77  1.1  pooka main(int argc, char *argv[])
     78  1.1  pooka {
     79  1.1  pooka 	struct puffs_usermount *pu;
     80  1.1  pooka 	struct puffs_ops *pops;
     81  1.1  pooka 	struct puffs_pathobj *po_root;
     82  1.4  pooka 	struct puffs_node *pn_root;
     83  1.1  pooka 	struct stat sb;
     84  1.1  pooka 	mntoptparse_t mp;
     85  1.1  pooka 	int mntflags, pflags, lflags;
     86  1.1  pooka 	int ch;
     87  1.1  pooka 	int i;
     88  1.1  pooka 
     89  1.1  pooka 	setprogname(argv[0]);
     90  1.1  pooka 
     91  1.1  pooka 	if (argc < 3)
     92  1.1  pooka 		usage();
     93  1.1  pooka 
     94  1.1  pooka 	pflags = lflags = mntflags = 0;
     95  1.1  pooka 	while ((ch = getopt(argc, argv, "o:s")) != -1) {
     96  1.1  pooka 		switch (ch) {
     97  1.1  pooka 		case 'o':
     98  1.1  pooka 			mp = getmntopts(optarg, puffsmopts, &mntflags, &pflags);
     99  1.1  pooka 			if (mp == NULL)
    100  1.1  pooka 				err(1, "getmntopts");
    101  1.1  pooka 			freemntopts(mp);
    102  1.1  pooka 			break;
    103  1.1  pooka 		case 's':
    104  1.1  pooka 			lflags |= PUFFSLOOP_NODAEMON;
    105  1.1  pooka 			break;
    106  1.1  pooka 		}
    107  1.1  pooka 	}
    108  1.1  pooka 	pflags |= PUFFS_FLAG_BUILDPATH;
    109  1.1  pooka 	argv += optind;
    110  1.1  pooka 	argc -= optind;
    111  1.1  pooka 
    112  1.1  pooka 	if (pflags & PUFFS_FLAG_OPDUMP)
    113  1.1  pooka 		lflags = PUFFSLOOP_NODAEMON;
    114  1.1  pooka 
    115  1.1  pooka 	if (argc != 2)
    116  1.1  pooka 		usage();
    117  1.1  pooka 
    118  1.7  pooka 	if (lstat(argv[0], &sb) == -1)
    119  1.7  pooka 		err(1, "stat %s", argv[0]);
    120  1.7  pooka 	if ((sb.st_mode & S_IFDIR) == 0)
    121  1.7  pooka 		errx(1, "%s is not a directory", argv[0]);
    122  1.7  pooka 
    123  1.1  pooka 	PUFFSOP_INIT(pops);
    124  1.1  pooka 
    125  1.1  pooka 	PUFFSOP_SET(pops, puffs_null, fs, statvfs);
    126  1.1  pooka 	PUFFSOP_SETFSNOP(pops, unmount);
    127  1.1  pooka 	PUFFSOP_SETFSNOP(pops, sync);
    128  1.1  pooka 
    129  1.1  pooka 	PUFFSOP_SET(pops, rot13, node, readdir);
    130  1.1  pooka 	PUFFSOP_SET(pops, rot13, node, read);
    131  1.1  pooka 	PUFFSOP_SET(pops, rot13, node, write);
    132  1.1  pooka 
    133  1.1  pooka 	PUFFSOP_SET(pops, puffs_null, node, lookup);
    134  1.1  pooka 	PUFFSOP_SET(pops, puffs_null, node, create);
    135  1.1  pooka 	PUFFSOP_SET(pops, puffs_null, node, mknod);
    136  1.1  pooka 	PUFFSOP_SET(pops, puffs_null, node, getattr);
    137  1.1  pooka 	PUFFSOP_SET(pops, puffs_null, node, setattr);
    138  1.1  pooka 	PUFFSOP_SET(pops, puffs_null, node, fsync);
    139  1.1  pooka 	PUFFSOP_SET(pops, puffs_null, node, remove);
    140  1.1  pooka 	PUFFSOP_SET(pops, puffs_null, node, link);
    141  1.1  pooka 	PUFFSOP_SET(pops, puffs_null, node, rename);
    142  1.1  pooka 	PUFFSOP_SET(pops, puffs_null, node, mkdir);
    143  1.1  pooka 	PUFFSOP_SET(pops, puffs_null, node, rmdir);
    144  1.1  pooka 	PUFFSOP_SET(pops, puffs_null, node, symlink);
    145  1.1  pooka 	PUFFSOP_SET(pops, puffs_null, node, readlink);
    146  1.2  pooka 	PUFFSOP_SET(pops, puffs_null, node, reclaim);
    147  1.1  pooka 
    148  1.7  pooka 	if ((pu = puffs_init(pops, "rot13", NULL, pflags)) == NULL)
    149  1.1  pooka 		err(1, "mount");
    150  1.1  pooka 
    151  1.4  pooka 	pn_root = puffs_pn_new(pu, NULL);
    152  1.4  pooka 	if (pn_root == NULL)
    153  1.1  pooka 		err(1, "puffs_pn_new");
    154  1.4  pooka 	puffs_setroot(pu, pn_root);
    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.7  pooka 	puffs_stat2vattr(&pn_root->pn_va, &sb);
    162  1.1  pooka 
    163  1.1  pooka 	puffs_set_namemod(pu, rot13path);
    164  1.1  pooka 
    165  1.1  pooka 	/* initialize rot13 tables */
    166  1.1  pooka 	for (i = 0; i < 256; i++)
    167  1.1  pooka 		tbl[i] = (uint8_t)i;
    168  1.1  pooka 	for (i = 0; i <= 26; i++)
    169  1.1  pooka 		tbl[i + 'a'] = 'a' + ((i + 13) % 26);
    170  1.1  pooka 	for (i = 0; i < 26; i++)
    171  1.1  pooka 		tbl[i + 'A'] = 'A' + ((i + 13) % 26);
    172  1.1  pooka 
    173  1.9  pooka 	if (puffs_mount(pu, argv[1], mntflags, pn_root) == -1)
    174  1.7  pooka 		err(1, "puffs_mount");
    175  1.1  pooka 
    176  1.7  pooka 	return puffs_mainloop(pu, lflags);
    177  1.1  pooka }
    178  1.1  pooka 
    179  1.1  pooka int
    180  1.1  pooka rot13_node_readdir(struct puffs_cc *pcc, void *opc, struct dirent *dent,
    181  1.3  pooka 	off_t *readoff, size_t *reslen, const struct puffs_cred *pcr,
    182  1.3  pooka 	int *eofflag, off_t *cookies, size_t *ncookies)
    183  1.1  pooka {
    184  1.1  pooka 	struct dirent *dp;
    185  1.1  pooka 	size_t rl;
    186  1.1  pooka 	int rv;
    187  1.1  pooka 
    188  1.1  pooka 	dp = dent;
    189  1.1  pooka 	rl = *reslen;
    190  1.1  pooka 
    191  1.3  pooka 	rv = puffs_null_node_readdir(pcc, opc, dent, readoff, reslen, pcr,
    192  1.3  pooka 	    eofflag, cookies, ncookies);
    193  1.1  pooka 	if (rv)
    194  1.1  pooka 		return rv;
    195  1.1  pooka 
    196  1.1  pooka 	while (rl > *reslen) {
    197  1.1  pooka 		dorot13((uint8_t *)dp->d_name, dp->d_namlen);
    198  1.1  pooka 		rl -= _DIRENT_SIZE(dp);
    199  1.1  pooka 		dp = _DIRENT_NEXT(dp);
    200  1.1  pooka 	}
    201  1.1  pooka 
    202  1.1  pooka 	return 0;
    203  1.1  pooka }
    204  1.1  pooka 
    205  1.1  pooka int
    206  1.1  pooka rot13_node_read(struct puffs_cc *pcc, void *opc, uint8_t *buf, off_t offset,
    207  1.1  pooka 	size_t *resid, const struct puffs_cred *pcr, int ioflag)
    208  1.1  pooka {
    209  1.1  pooka 	uint8_t *prebuf = buf;
    210  1.1  pooka 	size_t preres = *resid;
    211  1.1  pooka 	int rv;
    212  1.1  pooka 
    213  1.1  pooka 	rv = puffs_null_node_read(pcc, opc, buf, offset, resid, pcr, ioflag);
    214  1.1  pooka 	if (rv)
    215  1.1  pooka 		return rv;
    216  1.1  pooka 
    217  1.1  pooka 	dorot13(prebuf, preres - *resid);
    218  1.1  pooka 
    219  1.1  pooka 	return rv;
    220  1.1  pooka }
    221  1.1  pooka 
    222  1.1  pooka int
    223  1.1  pooka rot13_node_write(struct puffs_cc *pcc, void *opc, uint8_t *buf, off_t offset,
    224  1.1  pooka 	size_t *resid, const struct puffs_cred *pcr, int ioflag)
    225  1.1  pooka {
    226  1.1  pooka 
    227  1.1  pooka 	dorot13(buf, *resid);
    228  1.1  pooka 	return puffs_null_node_write(pcc, opc, buf, offset, resid, pcr, ioflag);
    229  1.1  pooka }
    230