Home | History | Annotate | Line # | Download | only in fanoutfs
fanoutfs.c revision 1.1
      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.1  agc 			return 1;
    122  1.1  agc 		}
    123  1.1  agc 	}
    124  1.1  agc 	return 0;
    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.1  agc /* file system operations start here */
    159  1.1  agc 
    160  1.1  agc /* perform the stat operation */
    161  1.1  agc static int
    162  1.1  agc fanoutfs_getattr(const char *path, struct stat *st)
    163  1.1  agc {
    164  1.1  agc 	char	name[MAXPATHLEN];
    165  1.1  agc 
    166  1.1  agc 	(void) memset(st, 0x0, sizeof(*st));
    167  1.1  agc 	if (strcmp(path, "/") == 0) {
    168  1.1  agc 		st->st_mode = S_IFDIR | 0755;
    169  1.1  agc 		st->st_nlink = 2;
    170  1.1  agc 		return 0;
    171  1.1  agc 	}
    172  1.1  agc 	if (!findentry(path, name, sizeof(name), st)) {
    173  1.1  agc 		return -ENOENT;
    174  1.1  agc 	}
    175  1.1  agc 	return 0;
    176  1.1  agc }
    177  1.1  agc 
    178  1.1  agc /* readdir operation */
    179  1.1  agc static int
    180  1.1  agc fanoutfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
    181  1.1  agc 	      off_t offset, struct fuse_file_info *fi)
    182  1.1  agc {
    183  1.1  agc 	struct dirent	*dp;
    184  1.1  agc 	strv_t		 names;
    185  1.1  agc 	char		 name[MAXPATHLEN];
    186  1.1  agc 	DIR		*dirp;
    187  1.1  agc 	int		 i;
    188  1.1  agc 
    189  1.1  agc 	(void) fi;
    190  1.1  agc 
    191  1.1  agc 	(void) memset(&names, 0x0, sizeof(names));
    192  1.1  agc 	for (i = 0 ; i < dirs.c ; i++) {
    193  1.1  agc 		(void) snprintf(name, sizeof(name), "%s%s", dirs.v[i], path);
    194  1.1  agc 		if ((dirp = opendir(name)) == NULL) {
    195  1.1  agc 			continue;
    196  1.1  agc 		}
    197  1.1  agc 		while ((dp = readdir(dirp)) != NULL) {
    198  1.1  agc 			if (!present(dp->d_name, &names)) {
    199  1.1  agc 				ALLOC(char *, names.v, names.size, names.c,
    200  1.1  agc 					10, 10, "readdir", exit(EXIT_FAILURE));
    201  1.1  agc 				names.v[names.c++] = strdup(dp->d_name);
    202  1.1  agc 			}
    203  1.1  agc 		}
    204  1.1  agc 		(void) closedir(dirp);
    205  1.1  agc 	}
    206  1.1  agc 	for (i = 0 ; i < names.c ; i++) {
    207  1.1  agc 		(void) filler(buf, names.v[i], NULL, 0);
    208  1.1  agc 		FREE(names.v[i]);
    209  1.1  agc 	}
    210  1.1  agc 	if (i > 0) {
    211  1.1  agc 		FREE(names.v);
    212  1.1  agc 	}
    213  1.1  agc 	return 0;
    214  1.1  agc }
    215  1.1  agc 
    216  1.1  agc /* open the file in the file system */
    217  1.1  agc static int
    218  1.1  agc fanoutfs_open(const char *path, struct fuse_file_info *fi)
    219  1.1  agc {
    220  1.1  agc 	char	name[MAXPATHLEN];
    221  1.1  agc 
    222  1.1  agc 	if (!findentry(path, name, sizeof(name), NULL)) {
    223  1.1  agc 		return -ENOENT;
    224  1.1  agc 	}
    225  1.1  agc 	return 0;
    226  1.1  agc }
    227  1.1  agc 
    228  1.1  agc /* read the file's contents in the file system */
    229  1.1  agc static int
    230  1.1  agc fanoutfs_read(const char *path, char *buf, size_t size, off_t offset,
    231  1.1  agc 	   struct fuse_file_info * fi)
    232  1.1  agc {
    233  1.1  agc 	char	name[MAXPATHLEN];
    234  1.1  agc 	int	fd;
    235  1.1  agc 	int	cc;
    236  1.1  agc 
    237  1.1  agc 	(void) fi;
    238  1.1  agc 
    239  1.1  agc 	if (!findentry(path, name, sizeof(name), NULL)) {
    240  1.1  agc 		return -ENOENT;
    241  1.1  agc 	}
    242  1.1  agc 	if ((fd = open(name, O_RDONLY, 0666)) < 0) {
    243  1.1  agc 		return -ENOENT;
    244  1.1  agc 	}
    245  1.1  agc 	if (lseek(fd, offset, SEEK_SET) < 0) {
    246  1.1  agc 		(void) close(fd);
    247  1.1  agc 		return -EBADF;
    248  1.1  agc 	}
    249  1.1  agc 	if ((cc = read(fd, buf, size)) < 0) {
    250  1.1  agc 		(void) close(fd);
    251  1.1  agc 		return -errno;
    252  1.1  agc 	}
    253  1.1  agc 	(void) close(fd);
    254  1.1  agc 	return cc;
    255  1.1  agc }
    256  1.1  agc 
    257  1.1  agc /* write the file's contents in the file system */
    258  1.1  agc static int
    259  1.1  agc fanoutfs_write(const char *path, const char *buf, size_t size, off_t offset,
    260  1.1  agc 	   struct fuse_file_info * fi)
    261  1.1  agc {
    262  1.1  agc 	char	name[MAXPATHLEN];
    263  1.1  agc 	int	fd;
    264  1.1  agc 	int	cc;
    265  1.1  agc 
    266  1.1  agc 	(void) fi;
    267  1.1  agc 
    268  1.1  agc 	if (!findentry(path, name, sizeof(name), NULL)) {
    269  1.1  agc 		return -ENOENT;
    270  1.1  agc 	}
    271  1.1  agc 	if ((fd = open(name, O_WRONLY, 0666)) < 0) {
    272  1.1  agc 		return -ENOENT;
    273  1.1  agc 	}
    274  1.1  agc 	if (lseek(fd, offset, SEEK_SET) < 0) {
    275  1.1  agc 		(void) close(fd);
    276  1.1  agc 		return -EBADF;
    277  1.1  agc 	}
    278  1.1  agc 	if ((cc = write(fd, buf, size)) < 0) {
    279  1.1  agc 		(void) close(fd);
    280  1.1  agc 		return -errno;
    281  1.1  agc 	}
    282  1.1  agc 	(void) close(fd);
    283  1.1  agc 	return cc;
    284  1.1  agc }
    285  1.1  agc 
    286  1.1  agc /* fill in the statvfs struct */
    287  1.1  agc static int
    288  1.1  agc fanoutfs_statfs(const char *path, struct statvfs *st)
    289  1.1  agc {
    290  1.1  agc 	(void) memset(st, 0x0, sizeof(*st));
    291  1.1  agc 	st->f_bsize = st->f_frsize = st->f_iosize = 512;
    292  1.1  agc 	st->f_owner = vfs.st_uid;
    293  1.1  agc 	st->f_files = 1;
    294  1.1  agc 	return 0;
    295  1.1  agc }
    296  1.1  agc 
    297  1.1  agc /* "remove" a file */
    298  1.1  agc static int
    299  1.1  agc fanoutfs_unlink(const char *path)
    300  1.1  agc {
    301  1.1  agc 	char	name[MAXPATHLEN];
    302  1.1  agc 
    303  1.1  agc 	if (!findentry(path, name, sizeof(name), NULL)) {
    304  1.1  agc 		return -ENOENT;
    305  1.1  agc 	}
    306  1.1  agc 	if (unlink(name) < 0) {
    307  1.1  agc 		return -errno;
    308  1.1  agc 	}
    309  1.1  agc 	return 0;
    310  1.1  agc }
    311  1.1  agc 
    312  1.1  agc /* check the access on a file */
    313  1.1  agc static int
    314  1.1  agc fanoutfs_access(const char *path, int acc)
    315  1.1  agc {
    316  1.1  agc 	char	name[MAXPATHLEN];
    317  1.1  agc 
    318  1.1  agc 	if (!findentry(path, name, sizeof(name), NULL)) {
    319  1.1  agc 		return -ENOENT;
    320  1.1  agc 	}
    321  1.1  agc 	if (access(name, acc) < 0) {
    322  1.1  agc 		return -errno;
    323  1.1  agc 	}
    324  1.1  agc 	return 0;
    325  1.1  agc }
    326  1.1  agc 
    327  1.1  agc /* change the mode of a file */
    328  1.1  agc static int
    329  1.1  agc fanoutfs_chmod(const char *path, mode_t mode)
    330  1.1  agc {
    331  1.1  agc 	char	name[MAXPATHLEN];
    332  1.1  agc 
    333  1.1  agc 	if (!findentry(path, name, sizeof(name), NULL)) {
    334  1.1  agc 		return -ENOENT;
    335  1.1  agc 	}
    336  1.1  agc 	if (chmod(name, mode) < 0) {
    337  1.1  agc 		return -errno;
    338  1.1  agc 	}
    339  1.1  agc 	return 0;
    340  1.1  agc }
    341  1.1  agc 
    342  1.1  agc /* change the owner and group of a file */
    343  1.1  agc static int
    344  1.1  agc fanoutfs_chown(const char *path, uid_t uid, gid_t gid)
    345  1.1  agc {
    346  1.1  agc 	char	name[MAXPATHLEN];
    347  1.1  agc 
    348  1.1  agc 	if (!findentry(path, name, sizeof(name), NULL)) {
    349  1.1  agc 		return -ENOENT;
    350  1.1  agc 	}
    351  1.1  agc 	if (lchown(name, uid, gid) < 0) {
    352  1.1  agc 		return -errno;
    353  1.1  agc 	}
    354  1.1  agc 	return 0;
    355  1.1  agc }
    356  1.1  agc 
    357  1.1  agc /* "rename" a file */
    358  1.1  agc static int
    359  1.1  agc fanoutfs_rename(const char *from, const char *to)
    360  1.1  agc {
    361  1.1  agc 	char	fromname[MAXPATHLEN];
    362  1.1  agc 	char	toname[MAXPATHLEN];
    363  1.1  agc 
    364  1.1  agc 	if (!findentry(from, fromname, sizeof(fromname), NULL)) {
    365  1.1  agc 		return -ENOENT;
    366  1.1  agc 	}
    367  1.1  agc 	(void) snprintf(toname, sizeof(toname), "%s%s", dirs.v[0], to);
    368  1.1  agc 	if (!mkdirs(toname)) {
    369  1.1  agc 		return -ENOENT;
    370  1.1  agc 	}
    371  1.1  agc 	if (rename(fromname, toname) < 0) {
    372  1.1  agc 		return -EPERM;
    373  1.1  agc 	}
    374  1.1  agc 	return 0;
    375  1.1  agc }
    376  1.1  agc 
    377  1.1  agc /* create a file */
    378  1.1  agc static int
    379  1.1  agc fanoutfs_create(const char *path, mode_t mode, struct fuse_file_info *fi)
    380  1.1  agc {
    381  1.1  agc 	struct stat	st;
    382  1.1  agc 	char		name[MAXPATHLEN];
    383  1.1  agc 	int		fd;
    384  1.1  agc 
    385  1.1  agc 	if (findentry(path, name, sizeof(name), &st)) {
    386  1.1  agc 		return -EEXIST;
    387  1.1  agc 	}
    388  1.1  agc 	if ((fd = open(name, O_RDWR | O_CREAT | O_EXCL, 0666)) < 0) {
    389  1.1  agc 		return -EPERM;
    390  1.1  agc 	}
    391  1.1  agc 	(void) close(fd);
    392  1.1  agc 	return 0;
    393  1.1  agc }
    394  1.1  agc 
    395  1.1  agc /* create a special node */
    396  1.1  agc static int
    397  1.1  agc fanoutfs_mknod(const char *path, mode_t mode, dev_t d)
    398  1.1  agc {
    399  1.1  agc 	struct stat	st;
    400  1.1  agc 	char		name[MAXPATHLEN];
    401  1.1  agc 
    402  1.1  agc 	if (findentry(path, name, sizeof(name), &st)) {
    403  1.1  agc 		return -EEXIST;
    404  1.1  agc 	}
    405  1.1  agc 	if (mknod(name, mode, d) < 0) {
    406  1.1  agc 		return -EPERM;
    407  1.1  agc 	}
    408  1.1  agc 	return 0;
    409  1.1  agc }
    410  1.1  agc 
    411  1.1  agc /* create a directory */
    412  1.1  agc static int
    413  1.1  agc fanoutfs_mkdir(const char *path, mode_t mode)
    414  1.1  agc {
    415  1.1  agc 	char	name[MAXPATHLEN];
    416  1.1  agc 
    417  1.1  agc 	(void) snprintf(name, sizeof(name), "%s%s", dirs.v[0], path);
    418  1.1  agc 	if (!mkdirs(name)) {
    419  1.1  agc 		return -ENOENT;
    420  1.1  agc 	}
    421  1.1  agc 	if (mkdir(name, mode) < 0) {
    422  1.1  agc 		return -EPERM;
    423  1.1  agc 	}
    424  1.1  agc 	return 0;
    425  1.1  agc }
    426  1.1  agc 
    427  1.1  agc /* create a symbolic link */
    428  1.1  agc static int
    429  1.1  agc fanoutfs_symlink(const char *path, const char *tgt)
    430  1.1  agc {
    431  1.1  agc 	char	name[MAXPATHLEN];
    432  1.1  agc 
    433  1.1  agc 	(void) snprintf(name, sizeof(name), "%s%s", dirs.v[0], path);
    434  1.1  agc 	if (!mkdirs(name)) {
    435  1.1  agc 		return -ENOENT;
    436  1.1  agc 	}
    437  1.1  agc 	if (symlink(name, tgt) < 0) {
    438  1.1  agc 		return -EPERM;
    439  1.1  agc 	}
    440  1.1  agc 	return 0;
    441  1.1  agc }
    442  1.1  agc 
    443  1.1  agc /* create a link */
    444  1.1  agc static int
    445  1.1  agc fanoutfs_link(const char *path, const char *tgt)
    446  1.1  agc {
    447  1.1  agc 	char	name[MAXPATHLEN];
    448  1.1  agc 
    449  1.1  agc 	(void) snprintf(name, sizeof(name), "%s%s", dirs.v[0], path);
    450  1.1  agc 	if (!mkdirs(name)) {
    451  1.1  agc 		return -ENOENT;
    452  1.1  agc 	}
    453  1.1  agc 	if (link(name, tgt) < 0) {
    454  1.1  agc 		return -errno;
    455  1.1  agc 	}
    456  1.1  agc 	return 0;
    457  1.1  agc }
    458  1.1  agc 
    459  1.1  agc /* read the contents of a symbolic link */
    460  1.1  agc static int
    461  1.1  agc fanoutfs_readlink(const char *path, char *buf, size_t size)
    462  1.1  agc {
    463  1.1  agc 	char	name[MAXPATHLEN];
    464  1.1  agc 
    465  1.1  agc 	if (!findentry(path, name, sizeof(name), NULL)) {
    466  1.1  agc 		return -ENOENT;
    467  1.1  agc 	}
    468  1.1  agc 	if (readlink(name, buf, size) < 0) {
    469  1.1  agc 		return -errno;
    470  1.1  agc 	}
    471  1.1  agc 	return 0;
    472  1.1  agc }
    473  1.1  agc 
    474  1.1  agc /* remove a directory */
    475  1.1  agc static int
    476  1.1  agc fanoutfs_rmdir(const char *path)
    477  1.1  agc {
    478  1.1  agc 	char	name[MAXPATHLEN];
    479  1.1  agc 
    480  1.1  agc 	if (!findentry(path, name, sizeof(name), NULL)) {
    481  1.1  agc 		return -ENOENT;
    482  1.1  agc 	}
    483  1.1  agc 	if (rmdir(name) < 0) {
    484  1.1  agc 		return -errno;
    485  1.1  agc 	}
    486  1.1  agc 	return 0;
    487  1.1  agc }
    488  1.1  agc 
    489  1.1  agc /* truncate a file */
    490  1.1  agc static int
    491  1.1  agc fanoutfs_truncate(const char *path, off_t size)
    492  1.1  agc {
    493  1.1  agc 	char	name[MAXPATHLEN];
    494  1.1  agc 
    495  1.1  agc 	if (!findentry(path, name, sizeof(name), NULL)) {
    496  1.1  agc 		return -ENOENT;
    497  1.1  agc 	}
    498  1.1  agc 	if (truncate(name, size) < 0) {
    499  1.1  agc 		return -errno;
    500  1.1  agc 	}
    501  1.1  agc 	return 0;
    502  1.1  agc }
    503  1.1  agc 
    504  1.1  agc /* set utimes on a file */
    505  1.1  agc static int
    506  1.1  agc fanoutfs_utime(const char *path, struct utimbuf *t)
    507  1.1  agc {
    508  1.1  agc 	char	name[MAXPATHLEN];
    509  1.1  agc 
    510  1.1  agc 	if (!findentry(path, name, sizeof(name), NULL)) {
    511  1.1  agc 		return -ENOENT;
    512  1.1  agc 	}
    513  1.1  agc 	if (utime(name, t) < 0) {
    514  1.1  agc 		return -errno;
    515  1.1  agc 	}
    516  1.1  agc 	return 0;
    517  1.1  agc }
    518  1.1  agc 
    519  1.1  agc /* operations struct */
    520  1.1  agc static struct fuse_operations fanoutfs_oper = {
    521  1.1  agc 	.getattr = fanoutfs_getattr,
    522  1.1  agc 	.readlink = fanoutfs_readlink,
    523  1.1  agc 	.mknod = fanoutfs_mknod,
    524  1.1  agc 	.mkdir = fanoutfs_mkdir,
    525  1.1  agc 	.unlink = fanoutfs_unlink,
    526  1.1  agc 	.rmdir = fanoutfs_rmdir,
    527  1.1  agc 	.symlink = fanoutfs_symlink,
    528  1.1  agc 	.rename = fanoutfs_rename,
    529  1.1  agc 	.link = fanoutfs_link,
    530  1.1  agc 	.chmod = fanoutfs_chmod,
    531  1.1  agc 	.chown = fanoutfs_chown,
    532  1.1  agc 	.truncate = fanoutfs_truncate,
    533  1.1  agc 	.utime = fanoutfs_utime,
    534  1.1  agc 	.open = fanoutfs_open,
    535  1.1  agc 	.read = fanoutfs_read,
    536  1.1  agc 	.write = fanoutfs_write,
    537  1.1  agc 	.statfs = fanoutfs_statfs,
    538  1.1  agc 	.readdir = fanoutfs_readdir,
    539  1.1  agc 	.access = fanoutfs_access,
    540  1.1  agc 	.create = fanoutfs_create
    541  1.1  agc };
    542  1.1  agc 
    543  1.1  agc int
    544  1.1  agc main(int argc, char **argv)
    545  1.1  agc {
    546  1.1  agc 	int	 i;
    547  1.1  agc 
    548  1.1  agc 	while ((i = getopt(argc, argv, "f:v")) != -1) {
    549  1.1  agc 		switch(i) {
    550  1.1  agc 		case 'f':
    551  1.1  agc 			conffile = optarg;
    552  1.1  agc 			break;
    553  1.1  agc 		case 'v':
    554  1.1  agc 			verbose = 1;
    555  1.1  agc 			break;
    556  1.1  agc 		}
    557  1.1  agc 	}
    558  1.1  agc 	(void) signal(SIGHUP, sighup);
    559  1.1  agc 	readconf(conffile);
    560  1.1  agc 	(void) daemon(1, 1);
    561  1.1  agc 	return fuse_main(argc, argv, &fanoutfs_oper);
    562  1.1  agc }
    563