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