Home | History | Annotate | Line # | Download | only in fanoutfs
      1  1.1  agc /*
      2  1.1  agc  * Copyright  2007 Alistair Crooks.  All rights reserved.
      3  1.1  agc  *
      4  1.1  agc  * Redistribution and use in source and binary forms, with or without
      5  1.1  agc  * modification, are permitted provided that the following conditions
      6  1.1  agc  * are met:
      7  1.1  agc  * 1. Redistributions of source code must retain the above copyright
      8  1.1  agc  *    notice, this list of conditions and the following disclaimer.
      9  1.1  agc  * 2. Redistributions in binary form must reproduce the above copyright
     10  1.1  agc  *    notice, this list of conditions and the following disclaimer in the
     11  1.1  agc  *    documentation and/or other materials provided with the distribution.
     12  1.1  agc  * 3. The name of the author may not be used to endorse or promote
     13  1.1  agc  *    products derived from this software without specific prior written
     14  1.1  agc  *    permission.
     15  1.1  agc  *
     16  1.1  agc  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     17  1.1  agc  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     18  1.1  agc  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  1.1  agc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
     20  1.1  agc  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  1.1  agc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
     22  1.1  agc  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.1  agc  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     24  1.1  agc  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     25  1.1  agc  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     26  1.1  agc  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  1.1  agc  */
     28  1.1  agc #include <sys/types.h>
     29  1.1  agc #include <sys/stat.h>
     30  1.1  agc 
     31  1.1  agc #include <ctype.h>
     32  1.1  agc #include <dirent.h>
     33  1.1  agc #include <err.h>
     34  1.1  agc #include <errno.h>
     35  1.1  agc #include <fcntl.h>
     36  1.1  agc #include <fuse.h>
     37  1.1  agc #include <signal.h>
     38  1.1  agc #include <stdio.h>
     39  1.1  agc #include <stdlib.h>
     40  1.1  agc #include <string.h>
     41  1.1  agc #include <time.h>
     42  1.1  agc #include <unistd.h>
     43  1.1  agc 
     44  1.1  agc #include "defs.h"
     45  1.1  agc 
     46  1.1  agc #ifndef PREFIX
     47  1.1  agc #define PREFIX		""
     48  1.1  agc #endif
     49  1.1  agc 
     50  1.1  agc #ifndef DEF_CONF_FILE
     51  1.1  agc #define DEF_CONF_FILE	"/etc/fanoutfs.conf"
     52  1.1  agc #endif
     53  1.1  agc 
     54  1.1  agc DEFINE_ARRAY(strv_t, char *);
     55  1.1  agc 
     56  1.1  agc static struct stat	 vfs;		/* stat info of directory */
     57  1.1  agc static strv_t		 dirs;		/* the directories, in order */
     58  1.1  agc static char		*conffile;	/* configuration file name */
     59  1.1  agc static int		 verbose; 	/* how chatty are we? */
     60  1.1  agc 
     61  1.1  agc 
     62  1.1  agc 
     63  1.1  agc 
     64  1.1  agc 
     65  1.1  agc /********************************************************************/
     66  1.1  agc 
     67  1.1  agc static int
     68  1.1  agc readconf(const char *f)
     69  1.1  agc {
     70  1.1  agc 	char	 buf[BUFSIZ];
     71  1.1  agc 	char	*cp;
     72  1.1  agc 	FILE	*fp;
     73  1.1  agc 	int	 line;
     74  1.1  agc 
     75  1.1  agc 	if ((fp = fopen((f) ? f : PREFIX DEF_CONF_FILE, "r")) == NULL) {
     76  1.1  agc 		warn("can't read configuration file `%s'\n", f);
     77  1.1  agc 		return 0;
     78  1.1  agc 	}
     79  1.1  agc 	for (line = 1 ;  fgets(buf, sizeof(buf), fp) != NULL ; line += 1) {
     80  1.1  agc 		buf[strlen(buf) - 1] = 0x0;
     81  1.1  agc 		for (cp = buf ; *cp && isspace((unsigned)*cp) ; cp++) {
     82  1.1  agc 		}
     83  1.1  agc 		if (*cp == '\n' || *cp == 0x0 || *cp == '#') {
     84  1.1  agc 			continue;
     85  1.1  agc 		}
     86  1.1  agc 		ALLOC(char *, dirs.v, dirs.size, dirs.c, 10, 10,
     87  1.1  agc 			"readconf", exit(EXIT_FAILURE));
     88  1.1  agc 		dirs.v[dirs.c++] = strdup(cp);
     89  1.1  agc 	}
     90  1.1  agc 	(void) fclose(fp);
     91  1.1  agc 	return 1;
     92  1.1  agc }
     93  1.1  agc 
     94  1.1  agc /* yes, this does too much work */
     95  1.1  agc static void
     96  1.1  agc sighup(int n)
     97  1.1  agc {
     98  1.1  agc 	int	i;
     99  1.1  agc 
    100  1.1  agc 	printf("Reading configuration file `%s'\n", conffile);
    101  1.1  agc 	for (i = 0 ; i < dirs.c ; i++) {
    102  1.1  agc 		FREE(dirs.v[i]);
    103  1.1  agc 	}
    104  1.1  agc 	dirs.c = 0;
    105  1.1  agc 	readconf(conffile);
    106  1.1  agc }
    107  1.1  agc 
    108  1.1  agc /* find the correct entry in the list of directories */
    109  1.1  agc static int
    110  1.1  agc findentry(const char *path, char *name, size_t namesize, struct stat *sp)
    111  1.1  agc {
    112  1.1  agc 	struct stat	st;
    113  1.1  agc 	int		i;
    114  1.1  agc 
    115  1.1  agc 	if (sp == NULL) {
    116  1.1  agc 		sp = &st;
    117  1.1  agc 	}
    118  1.1  agc 	for (i = 0 ; i < dirs.c ; i++) {
    119  1.1  agc 		(void) snprintf(name, namesize, "%s%s", dirs.v[i], path);
    120  1.1  agc 		if (stat(name, sp) == 0) {
    121  1.2  agc 			return i;
    122  1.1  agc 		}
    123  1.1  agc 	}
    124  1.2  agc 	return -1;
    125  1.1  agc }
    126  1.1  agc 
    127  1.1  agc /* return 1 if the string `s' is present in the array */
    128  1.1  agc static int
    129  1.1  agc present(char *s, strv_t *sp)
    130  1.1  agc {
    131  1.1  agc 	int	i;
    132  1.1  agc 
    133  1.1  agc 	for (i = 0 ; i < sp->c && strcmp(s, sp->v[i]) != 0 ; i++) {
    134  1.1  agc 	}
    135  1.1  agc 	return (i < sp->c);
    136  1.1  agc }
    137  1.1  agc 
    138  1.1  agc /* make sure the directory hierarchy exists */
    139  1.1  agc static int
    140  1.1  agc mkdirs(char *path)
    141  1.1  agc {
    142  1.1  agc 	char	 name[MAXPATHLEN];
    143  1.1  agc 	char	*slash;
    144  1.1  agc 
    145  1.1  agc 	(void) snprintf(name, sizeof(name), "%s%s", dirs.v[0], path);
    146  1.1  agc 	slash = &name[strlen(path) + 1];
    147  1.1  agc 	while ((slash = strchr(slash, '/')) != NULL) {
    148  1.1  agc 		*slash = 0x0;
    149  1.1  agc printf("mkdirs: dir `%s'\n", name);
    150  1.1  agc 		if (mkdir(name, 0777) < 0) {
    151  1.1  agc 			return 0;
    152  1.1  agc 		}
    153  1.1  agc 		*slash = '/';
    154  1.1  agc 	}
    155  1.1  agc 	return 1;
    156  1.1  agc }
    157  1.1  agc 
    158  1.2  agc /* copy a file, preserving mode, to make it writable */
    159  1.2  agc static int
    160  1.2  agc copyfile(char *from, char *to)
    161  1.2  agc {
    162  1.2  agc 	struct stat	st;
    163  1.2  agc 	char		buf[BUFSIZ * 10];
    164  1.2  agc 	int		fdfrom;
    165  1.2  agc 	int		fdto;
    166  1.2  agc 	int		ret;
    167  1.2  agc 	int		cc;
    168  1.2  agc 
    169  1.2  agc 	if ((fdfrom = open(from, O_RDONLY, 0666)) < 0) {
    170  1.2  agc 		warn("can't open file `%s' for reading", from);
    171  1.2  agc 		return 0;
    172  1.2  agc 	}
    173  1.2  agc 	(void) fstat(fdfrom, &st);
    174  1.2  agc 	if ((fdto = open(to, O_WRONLY | O_CREAT | O_EXCL, st.st_mode & 07777)) < 0) {
    175  1.2  agc 		warn("can't open file `%s' for reading", from);
    176  1.2  agc 		close(fdfrom);
    177  1.2  agc 		return 0;
    178  1.2  agc 	}
    179  1.2  agc 	for (ret = 1 ; ret && (cc = read(fdfrom, buf, sizeof(buf))) > 0 ; ) {
    180  1.2  agc 		if (write(fdto, buf, cc) != cc) {
    181  1.2  agc 			warn("short write");
    182  1.2  agc 			ret = 0;
    183  1.2  agc 		}
    184  1.2  agc 	}
    185  1.2  agc 	if (fchown(fdto, st.st_uid, st.st_gid) < 0) {
    186  1.2  agc 		warn("bad fchown");
    187  1.2  agc 		ret = 0;
    188  1.2  agc 	}
    189  1.2  agc 	(void) close(fdfrom);
    190  1.2  agc 	(void) close(fdto);
    191  1.2  agc 	return ret;
    192  1.2  agc }
    193  1.2  agc 
    194  1.1  agc /* file system operations start here */
    195  1.1  agc 
    196  1.1  agc /* perform the stat operation */
    197  1.1  agc static int
    198  1.1  agc fanoutfs_getattr(const char *path, struct stat *st)
    199  1.1  agc {
    200  1.1  agc 	char	name[MAXPATHLEN];
    201  1.1  agc 
    202  1.1  agc 	(void) memset(st, 0x0, sizeof(*st));
    203  1.1  agc 	if (strcmp(path, "/") == 0) {
    204  1.1  agc 		st->st_mode = S_IFDIR | 0755;
    205  1.1  agc 		st->st_nlink = 2;
    206  1.1  agc 		return 0;
    207  1.1  agc 	}
    208  1.2  agc 	if (findentry(path, name, sizeof(name), st) < 0) {
    209  1.1  agc 		return -ENOENT;
    210  1.1  agc 	}
    211  1.1  agc 	return 0;
    212  1.1  agc }
    213  1.1  agc 
    214  1.1  agc /* readdir operation */
    215  1.1  agc static int
    216  1.1  agc fanoutfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
    217  1.1  agc 	      off_t offset, struct fuse_file_info *fi)
    218  1.1  agc {
    219  1.1  agc 	struct dirent	*dp;
    220  1.1  agc 	strv_t		 names;
    221  1.1  agc 	char		 name[MAXPATHLEN];
    222  1.1  agc 	DIR		*dirp;
    223  1.1  agc 	int		 i;
    224  1.1  agc 
    225  1.1  agc 	(void) fi;
    226  1.1  agc 
    227  1.1  agc 	(void) memset(&names, 0x0, sizeof(names));
    228  1.1  agc 	for (i = 0 ; i < dirs.c ; i++) {
    229  1.1  agc 		(void) snprintf(name, sizeof(name), "%s%s", dirs.v[i], path);
    230  1.1  agc 		if ((dirp = opendir(name)) == NULL) {
    231  1.1  agc 			continue;
    232  1.1  agc 		}
    233  1.1  agc 		while ((dp = readdir(dirp)) != NULL) {
    234  1.1  agc 			if (!present(dp->d_name, &names)) {
    235  1.1  agc 				ALLOC(char *, names.v, names.size, names.c,
    236  1.1  agc 					10, 10, "readdir", exit(EXIT_FAILURE));
    237  1.1  agc 				names.v[names.c++] = strdup(dp->d_name);
    238  1.1  agc 			}
    239  1.1  agc 		}
    240  1.1  agc 		(void) closedir(dirp);
    241  1.1  agc 	}
    242  1.1  agc 	for (i = 0 ; i < names.c ; i++) {
    243  1.1  agc 		(void) filler(buf, names.v[i], NULL, 0);
    244  1.1  agc 		FREE(names.v[i]);
    245  1.1  agc 	}
    246  1.1  agc 	if (i > 0) {
    247  1.1  agc 		FREE(names.v);
    248  1.1  agc 	}
    249  1.1  agc 	return 0;
    250  1.1  agc }
    251  1.1  agc 
    252  1.1  agc /* open the file in the file system */
    253  1.1  agc static int
    254  1.1  agc fanoutfs_open(const char *path, struct fuse_file_info *fi)
    255  1.1  agc {
    256  1.2  agc 	char	newname[MAXPATHLEN];
    257  1.1  agc 	char	name[MAXPATHLEN];
    258  1.2  agc 	int	d;
    259  1.1  agc 
    260  1.2  agc 	if ((d = findentry(path, name, sizeof(name), NULL)) < 0) {
    261  1.1  agc 		return -ENOENT;
    262  1.1  agc 	}
    263  1.2  agc 	if (d > 0 && (fi->flags & 0x3) != O_RDONLY) {
    264  1.2  agc 		/* need to copy file to writable dir */
    265  1.2  agc 		(void) snprintf(newname, sizeof(newname), "%s%s", dirs.v[0], path);
    266  1.2  agc 		if (!mkdirs(newname)) {
    267  1.2  agc 			return -ENOENT;
    268  1.2  agc 		}
    269  1.2  agc 		if (!copyfile(name, newname)) {
    270  1.2  agc 			return -EPERM;
    271  1.2  agc 		}
    272  1.2  agc 	}
    273  1.1  agc 	return 0;
    274  1.1  agc }
    275  1.1  agc 
    276  1.1  agc /* read the file's contents in the file system */
    277  1.1  agc static int
    278  1.1  agc fanoutfs_read(const char *path, char *buf, size_t size, off_t offset,
    279  1.1  agc 	   struct fuse_file_info * fi)
    280  1.1  agc {
    281  1.1  agc 	char	name[MAXPATHLEN];
    282  1.1  agc 	int	fd;
    283  1.1  agc 	int	cc;
    284  1.1  agc 
    285  1.1  agc 	(void) fi;
    286  1.1  agc 
    287  1.2  agc 	if (findentry(path, name, sizeof(name), NULL) < 0) {
    288  1.1  agc 		return -ENOENT;
    289  1.1  agc 	}
    290  1.1  agc 	if ((fd = open(name, O_RDONLY, 0666)) < 0) {
    291  1.1  agc 		return -ENOENT;
    292  1.1  agc 	}
    293  1.1  agc 	if (lseek(fd, offset, SEEK_SET) < 0) {
    294  1.1  agc 		(void) close(fd);
    295  1.1  agc 		return -EBADF;
    296  1.1  agc 	}
    297  1.1  agc 	if ((cc = read(fd, buf, size)) < 0) {
    298  1.1  agc 		(void) close(fd);
    299  1.1  agc 		return -errno;
    300  1.1  agc 	}
    301  1.1  agc 	(void) close(fd);
    302  1.1  agc 	return cc;
    303  1.1  agc }
    304  1.1  agc 
    305  1.1  agc /* write the file's contents in the file system */
    306  1.1  agc static int
    307  1.1  agc fanoutfs_write(const char *path, const char *buf, size_t size, off_t offset,
    308  1.1  agc 	   struct fuse_file_info * fi)
    309  1.1  agc {
    310  1.1  agc 	char	name[MAXPATHLEN];
    311  1.1  agc 	int	fd;
    312  1.1  agc 	int	cc;
    313  1.1  agc 
    314  1.1  agc 	(void) fi;
    315  1.1  agc 
    316  1.2  agc 	if (findentry(path, name, sizeof(name), NULL) < 0) {
    317  1.1  agc 		return -ENOENT;
    318  1.1  agc 	}
    319  1.1  agc 	if ((fd = open(name, O_WRONLY, 0666)) < 0) {
    320  1.1  agc 		return -ENOENT;
    321  1.1  agc 	}
    322  1.1  agc 	if (lseek(fd, offset, SEEK_SET) < 0) {
    323  1.1  agc 		(void) close(fd);
    324  1.1  agc 		return -EBADF;
    325  1.1  agc 	}
    326  1.1  agc 	if ((cc = write(fd, buf, size)) < 0) {
    327  1.1  agc 		(void) close(fd);
    328  1.1  agc 		return -errno;
    329  1.1  agc 	}
    330  1.1  agc 	(void) close(fd);
    331  1.1  agc 	return cc;
    332  1.1  agc }
    333  1.1  agc 
    334  1.1  agc /* fill in the statvfs struct */
    335  1.1  agc static int
    336  1.1  agc fanoutfs_statfs(const char *path, struct statvfs *st)
    337  1.1  agc {
    338  1.1  agc 	(void) memset(st, 0x0, sizeof(*st));
    339  1.1  agc 	st->f_bsize = st->f_frsize = st->f_iosize = 512;
    340  1.1  agc 	st->f_owner = vfs.st_uid;
    341  1.1  agc 	st->f_files = 1;
    342  1.1  agc 	return 0;
    343  1.1  agc }
    344  1.1  agc 
    345  1.1  agc /* "remove" a file */
    346  1.1  agc static int
    347  1.1  agc fanoutfs_unlink(const char *path)
    348  1.1  agc {
    349  1.1  agc 	char	name[MAXPATHLEN];
    350  1.1  agc 
    351  1.2  agc 	if (findentry(path, name, sizeof(name), NULL) < 0) {
    352  1.1  agc 		return -ENOENT;
    353  1.1  agc 	}
    354  1.1  agc 	if (unlink(name) < 0) {
    355  1.1  agc 		return -errno;
    356  1.1  agc 	}
    357  1.1  agc 	return 0;
    358  1.1  agc }
    359  1.1  agc 
    360  1.1  agc /* check the access on a file */
    361  1.1  agc static int
    362  1.1  agc fanoutfs_access(const char *path, int acc)
    363  1.1  agc {
    364  1.1  agc 	char	name[MAXPATHLEN];
    365  1.1  agc 
    366  1.2  agc 	if (findentry(path, name, sizeof(name), NULL) < 0) {
    367  1.1  agc 		return -ENOENT;
    368  1.1  agc 	}
    369  1.1  agc 	if (access(name, acc) < 0) {
    370  1.1  agc 		return -errno;
    371  1.1  agc 	}
    372  1.1  agc 	return 0;
    373  1.1  agc }
    374  1.1  agc 
    375  1.1  agc /* change the mode of a file */
    376  1.1  agc static int
    377  1.1  agc fanoutfs_chmod(const char *path, mode_t mode)
    378  1.1  agc {
    379  1.1  agc 	char	name[MAXPATHLEN];
    380  1.1  agc 
    381  1.2  agc 	if (findentry(path, name, sizeof(name), NULL) < 0) {
    382  1.1  agc 		return -ENOENT;
    383  1.1  agc 	}
    384  1.1  agc 	if (chmod(name, mode) < 0) {
    385  1.1  agc 		return -errno;
    386  1.1  agc 	}
    387  1.1  agc 	return 0;
    388  1.1  agc }
    389  1.1  agc 
    390  1.1  agc /* change the owner and group of a file */
    391  1.1  agc static int
    392  1.1  agc fanoutfs_chown(const char *path, uid_t uid, gid_t gid)
    393  1.1  agc {
    394  1.1  agc 	char	name[MAXPATHLEN];
    395  1.1  agc 
    396  1.2  agc 	if (findentry(path, name, sizeof(name), NULL) < 0) {
    397  1.1  agc 		return -ENOENT;
    398  1.1  agc 	}
    399  1.1  agc 	if (lchown(name, uid, gid) < 0) {
    400  1.1  agc 		return -errno;
    401  1.1  agc 	}
    402  1.1  agc 	return 0;
    403  1.1  agc }
    404  1.1  agc 
    405  1.1  agc /* "rename" a file */
    406  1.1  agc static int
    407  1.1  agc fanoutfs_rename(const char *from, const char *to)
    408  1.1  agc {
    409  1.1  agc 	char	fromname[MAXPATHLEN];
    410  1.1  agc 	char	toname[MAXPATHLEN];
    411  1.1  agc 
    412  1.2  agc 	if (findentry(from, fromname, sizeof(fromname), NULL) < 0) {
    413  1.1  agc 		return -ENOENT;
    414  1.1  agc 	}
    415  1.1  agc 	(void) snprintf(toname, sizeof(toname), "%s%s", dirs.v[0], to);
    416  1.1  agc 	if (!mkdirs(toname)) {
    417  1.1  agc 		return -ENOENT;
    418  1.1  agc 	}
    419  1.1  agc 	if (rename(fromname, toname) < 0) {
    420  1.1  agc 		return -EPERM;
    421  1.1  agc 	}
    422  1.1  agc 	return 0;
    423  1.1  agc }
    424  1.1  agc 
    425  1.1  agc /* create a file */
    426  1.1  agc static int
    427  1.1  agc fanoutfs_create(const char *path, mode_t mode, struct fuse_file_info *fi)
    428  1.1  agc {
    429  1.1  agc 	struct stat	st;
    430  1.1  agc 	char		name[MAXPATHLEN];
    431  1.1  agc 	int		fd;
    432  1.1  agc 
    433  1.2  agc 	if (findentry(path, name, sizeof(name), &st) >= 0) {
    434  1.1  agc 		return -EEXIST;
    435  1.1  agc 	}
    436  1.1  agc 	if ((fd = open(name, O_RDWR | O_CREAT | O_EXCL, 0666)) < 0) {
    437  1.1  agc 		return -EPERM;
    438  1.1  agc 	}
    439  1.1  agc 	(void) close(fd);
    440  1.1  agc 	return 0;
    441  1.1  agc }
    442  1.1  agc 
    443  1.1  agc /* create a special node */
    444  1.1  agc static int
    445  1.1  agc fanoutfs_mknod(const char *path, mode_t mode, dev_t d)
    446  1.1  agc {
    447  1.1  agc 	struct stat	st;
    448  1.1  agc 	char		name[MAXPATHLEN];
    449  1.1  agc 
    450  1.2  agc 	if (findentry(path, name, sizeof(name), &st) >= 0) {
    451  1.1  agc 		return -EEXIST;
    452  1.1  agc 	}
    453  1.1  agc 	if (mknod(name, mode, d) < 0) {
    454  1.1  agc 		return -EPERM;
    455  1.1  agc 	}
    456  1.1  agc 	return 0;
    457  1.1  agc }
    458  1.1  agc 
    459  1.1  agc /* create a directory */
    460  1.1  agc static int
    461  1.1  agc fanoutfs_mkdir(const char *path, mode_t mode)
    462  1.1  agc {
    463  1.1  agc 	char	name[MAXPATHLEN];
    464  1.1  agc 
    465  1.1  agc 	(void) snprintf(name, sizeof(name), "%s%s", dirs.v[0], path);
    466  1.1  agc 	if (!mkdirs(name)) {
    467  1.1  agc 		return -ENOENT;
    468  1.1  agc 	}
    469  1.1  agc 	if (mkdir(name, mode) < 0) {
    470  1.1  agc 		return -EPERM;
    471  1.1  agc 	}
    472  1.1  agc 	return 0;
    473  1.1  agc }
    474  1.1  agc 
    475  1.1  agc /* create a symbolic link */
    476  1.1  agc static int
    477  1.1  agc fanoutfs_symlink(const char *path, const char *tgt)
    478  1.1  agc {
    479  1.1  agc 	char	name[MAXPATHLEN];
    480  1.1  agc 
    481  1.1  agc 	(void) snprintf(name, sizeof(name), "%s%s", dirs.v[0], path);
    482  1.1  agc 	if (!mkdirs(name)) {
    483  1.1  agc 		return -ENOENT;
    484  1.1  agc 	}
    485  1.1  agc 	if (symlink(name, tgt) < 0) {
    486  1.1  agc 		return -EPERM;
    487  1.1  agc 	}
    488  1.1  agc 	return 0;
    489  1.1  agc }
    490  1.1  agc 
    491  1.1  agc /* create a link */
    492  1.1  agc static int
    493  1.1  agc fanoutfs_link(const char *path, const char *tgt)
    494  1.1  agc {
    495  1.1  agc 	char	name[MAXPATHLEN];
    496  1.1  agc 
    497  1.1  agc 	(void) snprintf(name, sizeof(name), "%s%s", dirs.v[0], path);
    498  1.1  agc 	if (!mkdirs(name)) {
    499  1.1  agc 		return -ENOENT;
    500  1.1  agc 	}
    501  1.1  agc 	if (link(name, tgt) < 0) {
    502  1.1  agc 		return -errno;
    503  1.1  agc 	}
    504  1.1  agc 	return 0;
    505  1.1  agc }
    506  1.1  agc 
    507  1.1  agc /* read the contents of a symbolic link */
    508  1.1  agc static int
    509  1.1  agc fanoutfs_readlink(const char *path, char *buf, size_t size)
    510  1.1  agc {
    511  1.1  agc 	char	name[MAXPATHLEN];
    512  1.1  agc 
    513  1.2  agc 	if (findentry(path, name, sizeof(name), NULL) < 0) {
    514  1.1  agc 		return -ENOENT;
    515  1.1  agc 	}
    516  1.1  agc 	if (readlink(name, buf, size) < 0) {
    517  1.1  agc 		return -errno;
    518  1.1  agc 	}
    519  1.1  agc 	return 0;
    520  1.1  agc }
    521  1.1  agc 
    522  1.1  agc /* remove a directory */
    523  1.1  agc static int
    524  1.1  agc fanoutfs_rmdir(const char *path)
    525  1.1  agc {
    526  1.1  agc 	char	name[MAXPATHLEN];
    527  1.1  agc 
    528  1.2  agc 	if (findentry(path, name, sizeof(name), NULL) < 0) {
    529  1.1  agc 		return -ENOENT;
    530  1.1  agc 	}
    531  1.1  agc 	if (rmdir(name) < 0) {
    532  1.1  agc 		return -errno;
    533  1.1  agc 	}
    534  1.1  agc 	return 0;
    535  1.1  agc }
    536  1.1  agc 
    537  1.1  agc /* truncate a file */
    538  1.1  agc static int
    539  1.1  agc fanoutfs_truncate(const char *path, off_t size)
    540  1.1  agc {
    541  1.1  agc 	char	name[MAXPATHLEN];
    542  1.1  agc 
    543  1.2  agc 	if (findentry(path, name, sizeof(name), NULL) < 0) {
    544  1.1  agc 		return -ENOENT;
    545  1.1  agc 	}
    546  1.1  agc 	if (truncate(name, size) < 0) {
    547  1.1  agc 		return -errno;
    548  1.1  agc 	}
    549  1.1  agc 	return 0;
    550  1.1  agc }
    551  1.1  agc 
    552  1.1  agc /* set utimes on a file */
    553  1.1  agc static int
    554  1.1  agc fanoutfs_utime(const char *path, struct utimbuf *t)
    555  1.1  agc {
    556  1.1  agc 	char	name[MAXPATHLEN];
    557  1.1  agc 
    558  1.2  agc 	if (findentry(path, name, sizeof(name), NULL) < 0) {
    559  1.1  agc 		return -ENOENT;
    560  1.1  agc 	}
    561  1.1  agc 	if (utime(name, t) < 0) {
    562  1.1  agc 		return -errno;
    563  1.1  agc 	}
    564  1.1  agc 	return 0;
    565  1.1  agc }
    566  1.1  agc 
    567  1.1  agc /* operations struct */
    568  1.1  agc static struct fuse_operations fanoutfs_oper = {
    569  1.1  agc 	.getattr = fanoutfs_getattr,
    570  1.1  agc 	.readlink = fanoutfs_readlink,
    571  1.1  agc 	.mknod = fanoutfs_mknod,
    572  1.1  agc 	.mkdir = fanoutfs_mkdir,
    573  1.1  agc 	.unlink = fanoutfs_unlink,
    574  1.1  agc 	.rmdir = fanoutfs_rmdir,
    575  1.1  agc 	.symlink = fanoutfs_symlink,
    576  1.1  agc 	.rename = fanoutfs_rename,
    577  1.1  agc 	.link = fanoutfs_link,
    578  1.1  agc 	.chmod = fanoutfs_chmod,
    579  1.1  agc 	.chown = fanoutfs_chown,
    580  1.1  agc 	.truncate = fanoutfs_truncate,
    581  1.1  agc 	.utime = fanoutfs_utime,
    582  1.1  agc 	.open = fanoutfs_open,
    583  1.1  agc 	.read = fanoutfs_read,
    584  1.1  agc 	.write = fanoutfs_write,
    585  1.1  agc 	.statfs = fanoutfs_statfs,
    586  1.1  agc 	.readdir = fanoutfs_readdir,
    587  1.1  agc 	.access = fanoutfs_access,
    588  1.1  agc 	.create = fanoutfs_create
    589  1.1  agc };
    590  1.1  agc 
    591  1.1  agc int
    592  1.1  agc main(int argc, char **argv)
    593  1.1  agc {
    594  1.1  agc 	int	 i;
    595  1.1  agc 
    596  1.1  agc 	while ((i = getopt(argc, argv, "f:v")) != -1) {
    597  1.1  agc 		switch(i) {
    598  1.1  agc 		case 'f':
    599  1.1  agc 			conffile = optarg;
    600  1.1  agc 			break;
    601  1.1  agc 		case 'v':
    602  1.1  agc 			verbose = 1;
    603  1.1  agc 			break;
    604  1.1  agc 		}
    605  1.1  agc 	}
    606  1.1  agc 	(void) signal(SIGHUP, sighup);
    607  1.1  agc 	readconf(conffile);
    608  1.1  agc 	(void) daemon(1, 1);
    609  1.2  agc 	return fuse_main(argc, argv, &fanoutfs_oper, NULL);
    610  1.1  agc }
    611