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/param.h> 30 1.1 agc #include <sys/stat.h> 31 1.1 agc 32 1.1 agc #include <err.h> 33 1.1 agc #include <errno.h> 34 1.1 agc #include <fcntl.h> 35 1.1 agc #include <fuse.h> 36 1.1 agc #include <stdio.h> 37 1.1 agc #include <stdlib.h> 38 1.1 agc #include <string.h> 39 1.1 agc #include <time.h> 40 1.1 agc #include <unistd.h> 41 1.1 agc 42 1.1 agc #include "fetch.h" 43 1.1 agc 44 1.1 agc #include "virtdir.h" 45 1.1 agc #include "defs.h" 46 1.1 agc 47 1.1 agc #ifndef DEFAULT_CACHE_DIR 48 1.1 agc #define DEFAULT_CACHE_DIR "/tmp" 49 1.1 agc #endif 50 1.1 agc 51 1.1 agc static char *cachedir; /* location for cached files */ 52 1.1 agc 53 1.1 agc static int verbose; /* how chatty are we? */ 54 1.1 agc 55 1.1 agc static virtdir_t ian; 56 1.1 agc 57 1.1 agc /* retrieve a file from the remote location, and cache it in cachedir */ 58 1.1 agc static int 59 1.1 agc getfile(const char *url) 60 1.1 agc { 61 1.1 agc struct url_stat urlstat; 62 1.1 agc struct url *newurlp; 63 1.1 agc int64_t cc; 64 1.1 agc char buf[BUFSIZ * 8]; 65 1.1 agc char tmpname[BUFSIZ]; 66 1.1 agc char newurl[BUFSIZ]; 67 1.1 agc char *cp; 68 1.1 agc FILE *out; 69 1.1 agc FILE *fp; 70 1.1 agc int got; 71 1.1 agc int ret; 72 1.1 agc int fd; 73 1.1 agc 74 1.1 agc if ((cp = strchr(url, ':')) == NULL) { 75 1.1 agc warn("malformed URL `%s'", url + 1); 76 1.1 agc return 0; 77 1.1 agc } 78 1.1 agc (void) snprintf(newurl, sizeof(newurl), "%.*s/%s", 79 1.1 agc (int)(cp - url), url + 1, cp + 1); 80 1.1 agc if ((newurlp = fetchParseURL(newurl)) == NULL) { 81 1.1 agc /* add as virtdir */ 82 1.1 agc virtdir_add(&ian, newurl, strlen(newurl), 'd', "", strlen("")); 83 1.1 agc return 1; 84 1.1 agc } 85 1.1 agc if (newurlp->host[0] == 0x0 || strcmp(newurlp->doc, "/") == 0) { 86 1.1 agc virtdir_add(&ian, newurl, strlen(newurl), 'd', "", strlen("")); 87 1.1 agc return 1; 88 1.1 agc } 89 1.1 agc if ((fp = fetchXGetURL(newurl, &urlstat, "p")) == NULL) { 90 1.1 agc if (verbose) { 91 1.1 agc warn("Error [%d] \"%s\" \"%s\"\n", fetchLastErrCode, newurl, fetchLastErrString); 92 1.1 agc } 93 1.1 agc /* XXX - before adding as a directory, we should check it does actually exist */ 94 1.1 agc virtdir_add(&ian, newurl, strlen(newurl), 'd', "", strlen("")); 95 1.1 agc return 1; 96 1.1 agc } 97 1.1 agc if (verbose) { 98 1.1 agc printf("::::::::::\n%s\nSize: %lld\nmtime: %s::::::::::\n", 99 1.1 agc newurl, 100 1.1 agc (long long) urlstat.size, 101 1.1 agc ctime(&urlstat.mtime)); 102 1.1 agc } 103 1.1 agc (void) snprintf(tmpname, sizeof(tmpname), "%s/fetch.XXXXXX", cachedir); 104 1.1 agc fd = mkstemp(tmpname); 105 1.1 agc if ((out = fdopen(fd, "w")) == NULL) { 106 1.1 agc warnx("can't create output file `%s'", tmpname); 107 1.1 agc return 0; 108 1.1 agc } 109 1.1 agc for (ret = 1, cc = 0 ; (got = fread(buf, 1, sizeof(buf), fp)) > 0 ; cc += got) { 110 1.1 agc if (fwrite(buf, 1, got, out) != got) { 111 1.1 agc warnx("short write"); 112 1.1 agc ret = 0; 113 1.1 agc break; 114 1.1 agc } 115 1.1 agc } 116 1.1 agc (void) fclose(out); 117 1.1 agc (void) fclose(fp); 118 1.1 agc if (ret) { 119 1.1 agc virtdir_add(&ian, newurl, strlen(newurl), 'f', tmpname, strlen(tmpname)); 120 1.1 agc } 121 1.1 agc return ret; 122 1.1 agc } 123 1.1 agc 124 1.1 agc 125 1.1 agc /* perform the stat operation */ 126 1.1 agc /* if this is the root, then just synthesise the data */ 127 1.1 agc /* otherwise, retrieve the data, and be sure to fill in the size */ 128 1.1 agc static int 129 1.1 agc ian_getattr(const char *path, struct stat *st) 130 1.1 agc { 131 1.1 agc virt_dirent_t *ep; 132 1.1 agc 133 1.1 agc if (strcmp(path, "/") == 0) { 134 1.1 agc (void) memset(st, 0x0, sizeof(*st)); 135 1.1 agc st->st_mode = S_IFDIR | 0755; 136 1.1 agc st->st_nlink = 2; 137 1.1 agc return 0; 138 1.1 agc } 139 1.1 agc if ((ep = virtdir_find(&ian, path + 1, strlen(path) - 1)) == NULL) { 140 1.1 agc if (!getfile(path)) { 141 1.1 agc return -ENOENT; 142 1.1 agc } 143 1.1 agc ep = virtdir_find(&ian, path + 1, strlen(path) - 1); 144 1.1 agc } 145 1.1 agc switch(ep->type) { 146 1.1 agc case 'f': 147 1.1 agc (void) memcpy(st, &ian.file, sizeof(*st)); 148 1.1 agc if (stat(ep->tgt, st) < 0) { 149 1.1 agc warn("`%s' has gone", ep->tgt); 150 1.1 agc } 151 1.1 agc break; 152 1.1 agc case 'd': 153 1.1 agc (void) memcpy(st, &ian.dir, sizeof(*st)); 154 1.1 agc break; 155 1.1 agc case 'l': 156 1.1 agc (void) memcpy(st, &ian.lnk, sizeof(*st)); 157 1.1 agc st->st_size = ep->tgtlen; 158 1.1 agc break; 159 1.1 agc } 160 1.1 agc return 0; 161 1.1 agc } 162 1.1 agc 163 1.1 agc /* readdir operation */ 164 1.1 agc static int 165 1.1 agc ian_readdir(const char *path, void *buf, fuse_fill_dir_t filler, 166 1.1 agc off_t offset, struct fuse_file_info * fi) 167 1.1 agc { 168 1.1 agc virt_dirent_t *dp; 169 1.1 agc VIRTDIR *dirp; 170 1.1 agc 171 1.1 agc if ((dirp = openvirtdir(&ian, path)) == NULL) { 172 1.1 agc return 0; 173 1.1 agc } 174 1.1 agc filler(buf, ".", NULL, 0); 175 1.1 agc filler(buf, "..", NULL, 0); 176 1.1 agc while ((dp = readvirtdir(dirp)) != NULL) { 177 1.1 agc filler(buf, dp->d_name, NULL, 0); 178 1.1 agc } 179 1.1 agc closevirtdir(dirp); 180 1.1 agc return 0; 181 1.1 agc } 182 1.1 agc 183 1.1 agc /* open the file in the file system */ 184 1.1 agc static int 185 1.1 agc ian_open(const char *path, struct fuse_file_info * fi) 186 1.1 agc { 187 1.1 agc return 0; 188 1.1 agc } 189 1.1 agc 190 1.1 agc /* read the file's contents in the file system */ 191 1.1 agc static int 192 1.1 agc ian_read(const char *path, char *buf, size_t size, off_t offset, 193 1.1 agc struct fuse_file_info * fi) 194 1.1 agc { 195 1.1 agc virt_dirent_t *ep; 196 1.1 agc int cc; 197 1.1 agc int fd; 198 1.1 agc 199 1.1 agc if ((ep = virtdir_find(&ian, path + 1, strlen(path) - 1)) == NULL) { 200 1.1 agc return -ENOENT; 201 1.1 agc } 202 1.1 agc if ((fd = open(ep->tgt, O_RDONLY, 0666)) < 0) { 203 1.1 agc return -EACCES; 204 1.1 agc } 205 1.1 agc if (lseek(fd, offset, SEEK_SET) < 0) { 206 1.1 agc (void) close(fd); 207 1.1 agc return -EBADF; 208 1.1 agc } 209 1.1 agc if ((cc = read(fd, buf, size)) < 0) { 210 1.1 agc (void) close(fd); 211 1.1 agc return -EBADF; 212 1.1 agc } 213 1.1 agc (void) close(fd); 214 1.1 agc return cc; 215 1.1 agc } 216 1.1 agc 217 1.1 agc /* fill in the statvfs struct */ 218 1.1 agc static int 219 1.1 agc ian_statfs(const char *path, struct statvfs *st) 220 1.1 agc { 221 1.1 agc (void) memset(st, 0x0, sizeof(*st)); 222 1.1 agc return 0; 223 1.1 agc } 224 1.1 agc 225 1.1 agc /* read the symbolic link */ 226 1.1 agc static int 227 1.1 agc ian_readlink(const char *path, char *buf, size_t size) 228 1.1 agc { 229 1.1 agc virt_dirent_t *ep; 230 1.1 agc 231 1.1 agc if ((ep = virtdir_find(&ian, path, strlen(path))) == NULL) { 232 1.1 agc return -ENOENT; 233 1.1 agc } 234 1.1 agc if (ep->tgt == NULL) { 235 1.1 agc return -ENOENT; 236 1.1 agc } 237 1.1 agc (void) strlcpy(buf, ep->tgt, size); 238 1.1 agc return 0; 239 1.1 agc } 240 1.1 agc 241 1.1 agc /* operations struct */ 242 1.1 agc static struct fuse_operations ian_oper = { 243 1.1 agc .getattr = ian_getattr, 244 1.1 agc .readlink = ian_readlink, 245 1.1 agc .readdir = ian_readdir, 246 1.1 agc .open = ian_open, 247 1.1 agc .read = ian_read, 248 1.1 agc .statfs = ian_statfs 249 1.1 agc }; 250 1.1 agc 251 1.1 agc int 252 1.1 agc main(int argc, char **argv) 253 1.1 agc { 254 1.1 agc int i; 255 1.1 agc 256 1.1 agc cachedir = strdup(DEFAULT_CACHE_DIR); 257 1.1 agc while ((i = getopt(argc, argv, "v")) != -1) { 258 1.1 agc switch(i) { 259 1.1 agc case 'd': 260 1.1 agc (void) free(cachedir); 261 1.1 agc cachedir = strdup(optarg); 262 1.1 agc break; 263 1.1 agc case 'v': 264 1.1 agc verbose += 1; 265 1.1 agc break; 266 1.1 agc default: 267 1.1 agc warn("unrecognised option `%c'", i); 268 1.1 agc exit(EXIT_FAILURE); 269 1.1 agc } 270 1.1 agc } 271 1.1 agc (void) daemon(1, 1); 272 1.1 agc virtdir_add(&ian, "ftp:", strlen("ftp:"), 'd', "", 0); 273 1.1 agc virtdir_add(&ian, "http:", strlen("http:"), 'd', "", 0); 274 1.1 agc virtdir_add(&ian, "https:", strlen("https:"), 'd', "", 0); 275 1.1 agc return fuse_main(argc, argv, &ian_oper, NULL); 276 1.1 agc } 277