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