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