Home | History | Annotate | Line # | Download | only in ian
      1 /*
      2  * Copyright  2007 Alistair Crooks.  All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions
      6  * are met:
      7  * 1. Redistributions of source code must retain the above copyright
      8  *    notice, this list of conditions and the following disclaimer.
      9  * 2. Redistributions in binary form must reproduce the above copyright
     10  *    notice, this list of conditions and the following disclaimer in the
     11  *    documentation and/or other materials provided with the distribution.
     12  * 3. The name of the author may not be used to endorse or promote
     13  *    products derived from this software without specific prior written
     14  *    permission.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     17  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
     20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
     22  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     24  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     25  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 #include <sys/types.h>
     29 #include <sys/param.h>
     30 #include <sys/stat.h>
     31 
     32 #include <err.h>
     33 #include <errno.h>
     34 #include <fcntl.h>
     35 #include <fuse.h>
     36 #include <stdio.h>
     37 #include <stdlib.h>
     38 #include <string.h>
     39 #include <time.h>
     40 #include <unistd.h>
     41 
     42 #include "fetch.h"
     43 
     44 #include "virtdir.h"
     45 #include "defs.h"
     46 
     47 #ifndef DEFAULT_CACHE_DIR
     48 #define DEFAULT_CACHE_DIR	"/tmp"
     49 #endif
     50 
     51 static char		*cachedir;	/* location for cached files */
     52 
     53 static int		 verbose; /* how chatty are we? */
     54 
     55 static virtdir_t	 ian;
     56 
     57 /* retrieve a file from the remote location, and cache it in cachedir */
     58 static int
     59 getfile(const char *url)
     60 {
     61 	struct url_stat	 urlstat;
     62 	struct url	*newurlp;
     63 	int64_t		 cc;
     64 	char		 buf[BUFSIZ * 8];
     65 	char		 tmpname[BUFSIZ];
     66 	char		 newurl[BUFSIZ];
     67 	char		*cp;
     68 	FILE		*out;
     69 	FILE		*fp;
     70 	int		 got;
     71 	int		 ret;
     72 	int		 fd;
     73 
     74 	if ((cp = strchr(url, ':')) == NULL) {
     75 		warn("malformed URL `%s'", url + 1);
     76 		return 0;
     77 	}
     78 	(void) snprintf(newurl, sizeof(newurl), "%.*s/%s",
     79 		(int)(cp - url), url + 1, cp + 1);
     80 	if ((newurlp = fetchParseURL(newurl)) == NULL) {
     81 		/* add as virtdir */
     82 		virtdir_add(&ian, newurl, strlen(newurl), 'd', "", strlen(""));
     83 		return 1;
     84 	}
     85 	if (newurlp->host[0] == 0x0 || strcmp(newurlp->doc, "/") == 0) {
     86 		virtdir_add(&ian, newurl, strlen(newurl), 'd', "", strlen(""));
     87 		return 1;
     88 	}
     89 	if ((fp = fetchXGetURL(newurl, &urlstat, "p")) == NULL) {
     90 		if (verbose) {
     91 			warn("Error [%d] \"%s\" \"%s\"\n", fetchLastErrCode, newurl, fetchLastErrString);
     92 		}
     93 		/* XXX - before adding as a directory, we should check it does actually exist */
     94 		virtdir_add(&ian, newurl, strlen(newurl), 'd', "", strlen(""));
     95 		return 1;
     96 	}
     97 	if (verbose) {
     98 		printf("::::::::::\n%s\nSize: %lld\nmtime: %s::::::::::\n",
     99 			newurl,
    100 			(long long) urlstat.size,
    101 			ctime(&urlstat.mtime));
    102 	}
    103 	(void) snprintf(tmpname, sizeof(tmpname), "%s/fetch.XXXXXX", cachedir);
    104 	fd = mkstemp(tmpname);
    105 	if ((out = fdopen(fd, "w")) == NULL) {
    106 		warnx("can't create output file `%s'", tmpname);
    107 		return 0;
    108 	}
    109 	for (ret = 1, cc = 0 ; (got = fread(buf, 1, sizeof(buf), fp)) > 0 ; cc += got) {
    110 		if (fwrite(buf, 1, got, out) != got) {
    111 			warnx("short write");
    112 			ret = 0;
    113 			break;
    114 		}
    115 	}
    116 	(void) fclose(out);
    117 	(void) fclose(fp);
    118 	if (ret) {
    119 		virtdir_add(&ian, newurl, strlen(newurl), 'f', tmpname, strlen(tmpname));
    120 	}
    121 	return ret;
    122 }
    123 
    124 
    125 /* perform the stat operation */
    126 /* if this is the root, then just synthesise the data */
    127 /* otherwise, retrieve the data, and be sure to fill in the size */
    128 static int
    129 ian_getattr(const char *path, struct stat *st)
    130 {
    131 	virt_dirent_t	*ep;
    132 
    133 	if (strcmp(path, "/") == 0) {
    134 		(void) memset(st, 0x0, sizeof(*st));
    135 		st->st_mode = S_IFDIR | 0755;
    136 		st->st_nlink = 2;
    137 		return 0;
    138 	}
    139 	if ((ep = virtdir_find(&ian, path + 1, strlen(path) - 1)) == NULL) {
    140 		if (!getfile(path)) {
    141 			return -ENOENT;
    142 		}
    143 		ep = virtdir_find(&ian, path + 1, strlen(path) - 1);
    144 	}
    145 	switch(ep->type) {
    146 	case 'f':
    147 		(void) memcpy(st, &ian.file, sizeof(*st));
    148 		if (stat(ep->tgt, st) < 0) {
    149 			warn("`%s' has gone", ep->tgt);
    150 		}
    151 		break;
    152 	case 'd':
    153 		(void) memcpy(st, &ian.dir, sizeof(*st));
    154 		break;
    155 	case 'l':
    156 		(void) memcpy(st, &ian.lnk, sizeof(*st));
    157 		st->st_size = ep->tgtlen;
    158 		break;
    159 	}
    160 	return 0;
    161 }
    162 
    163 /* readdir operation */
    164 static int
    165 ian_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
    166 	      off_t offset, struct fuse_file_info * fi)
    167 {
    168 	virt_dirent_t	*dp;
    169 	VIRTDIR		*dirp;
    170 
    171 	if ((dirp = openvirtdir(&ian, path)) == NULL) {
    172 		return 0;
    173 	}
    174 	filler(buf, ".", NULL, 0);
    175 	filler(buf, "..", NULL, 0);
    176 	while ((dp = readvirtdir(dirp)) != NULL) {
    177 		filler(buf, dp->d_name, NULL, 0);
    178 	}
    179 	closevirtdir(dirp);
    180 	return 0;
    181 }
    182 
    183 /* open the file in the file system */
    184 static int
    185 ian_open(const char *path, struct fuse_file_info * fi)
    186 {
    187 	return 0;
    188 }
    189 
    190 /* read the file's contents in the file system */
    191 static int
    192 ian_read(const char *path, char *buf, size_t size, off_t offset,
    193 	   struct fuse_file_info * fi)
    194 {
    195 	virt_dirent_t	*ep;
    196 	int		 cc;
    197 	int		 fd;
    198 
    199 	if ((ep = virtdir_find(&ian, path + 1, strlen(path) - 1)) == NULL) {
    200 		return -ENOENT;
    201 	}
    202 	if ((fd = open(ep->tgt, O_RDONLY, 0666)) < 0) {
    203 		return -EACCES;
    204 	}
    205 	if (lseek(fd, offset, SEEK_SET) < 0) {
    206 		(void) close(fd);
    207 		return -EBADF;
    208 	}
    209 	if ((cc = read(fd, buf, size)) < 0) {
    210 		(void) close(fd);
    211 		return -EBADF;
    212 	}
    213 	(void) close(fd);
    214 	return cc;
    215 }
    216 
    217 /* fill in the statvfs struct */
    218 static int
    219 ian_statfs(const char *path, struct statvfs *st)
    220 {
    221 	(void) memset(st, 0x0, sizeof(*st));
    222 	return 0;
    223 }
    224 
    225 /* read the symbolic link */
    226 static int
    227 ian_readlink(const char *path, char *buf, size_t size)
    228 {
    229 	virt_dirent_t	*ep;
    230 
    231 	if ((ep = virtdir_find(&ian, path, strlen(path))) == NULL) {
    232 		return -ENOENT;
    233 	}
    234 	if (ep->tgt == NULL) {
    235 		return -ENOENT;
    236 	}
    237 	(void) strlcpy(buf, ep->tgt, size);
    238 	return 0;
    239 }
    240 
    241 /* operations struct */
    242 static struct fuse_operations ian_oper = {
    243 	.getattr = ian_getattr,
    244 	.readlink = ian_readlink,
    245 	.readdir = ian_readdir,
    246 	.open = ian_open,
    247 	.read = ian_read,
    248 	.statfs = ian_statfs
    249 };
    250 
    251 int
    252 main(int argc, char **argv)
    253 {
    254 	int	i;
    255 
    256 	cachedir = strdup(DEFAULT_CACHE_DIR);
    257 	while ((i = getopt(argc, argv, "v")) != -1) {
    258 		switch(i) {
    259 		case 'd':
    260 			(void) free(cachedir);
    261 			cachedir = strdup(optarg);
    262 			break;
    263 		case 'v':
    264 			verbose += 1;
    265 			break;
    266 		default:
    267 			warn("unrecognised option `%c'", i);
    268 			exit(EXIT_FAILURE);
    269 		}
    270 	}
    271 	(void) daemon(1, 1);
    272 	virtdir_add(&ian, "ftp:", strlen("ftp:"), 'd', "", 0);
    273 	virtdir_add(&ian, "http:", strlen("http:"), 'd', "", 0);
    274 	virtdir_add(&ian, "https:", strlen("https:"), 'd', "", 0);
    275 	return fuse_main(argc, argv, &ian_oper, NULL);
    276 }
    277