2 * Copyright 1997 Massachusetts Institute of Technology
4 * Permission to use, copy, modify, and distribute this software and
5 * its documentation for any purpose and without fee is hereby
6 * granted, provided that both the above copyright notice and this
7 * permission notice appear in all copies, that both the above
8 * copyright notice and this permission notice appear in all
9 * supporting documentation, and that the name of M.I.T. not be used
10 * in advertising or publicity pertaining to distribution of the
11 * software without specific, written prior permission. M.I.T. makes
12 * no representations about the suitability of this software for any
13 * purpose. It is provided "as is" without express or implied
16 * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS
17 * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
18 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
20 * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/types.h>
48 static int file_retrieve(struct fetch_state *fs);
49 static int file_close(struct fetch_state *fs);
50 static int file_parse(struct fetch_state *fs, const char *uri);
52 struct uri_scheme file_scheme =
53 { "file", file_parse, 0, 0, 0 };
56 * Again, we slightly misinterpret the slash after the hostname as
57 * being the start of the pathname rather than merely a separator.
60 file_parse(struct fetch_state *fs, const char *uri)
64 p = uri + 5; /* skip past `file:' */
65 if (p[0] == '/' && p[1] == '/') {
66 /* skip past `//localhost', if any */
68 while (*p && *p != '/')
73 warnx("`%s': expected absolute pathname in `file' URL", uri);
77 fs->fs_proto = percent_decode(p);
78 /* guaranteed to succeed because of above test */
79 p = strrchr(fs->fs_proto, '/');
80 if (fs->fs_outputfile == 0) /* only set if not overridden by user */
81 fs->fs_outputfile = p + 1;
82 fs->fs_retrieve = file_retrieve;
83 fs->fs_close = file_close;
88 file_close(struct fetch_state *fs)
92 fs->fs_outputfile = 0;
93 fs->fs_status = "free";
98 file_retrieve(struct fetch_state *fs)
102 /* XXX - this seems bogus to me! */
103 if (access(fs->fs_outputfile, F_OK) == 0) {
105 warn("%s", fs->fs_outputfile);
109 if (fs->fs_linkfile) {
110 fs->fs_status = "checking path";
111 if (stat(fs->fs_proto, &sb) == -1) {
112 warn("%s", (char *)fs->fs_proto);
115 fs->fs_status = "symlink";
116 if (symlink(fs->fs_proto, fs->fs_outputfile) == -1) {
120 fs->fs_status = "done";
121 } else if (strcmp(fs->fs_outputfile, "-") == 0) {
125 if ((f = fopen(fs->fs_proto, "r")) == NULL) {
129 while ((ch = fgetc(f)) != EOF)
146 } else if (pid == 0) {
147 execl(PATH_CP, "cp", "-p", fs->fs_proto,
148 fs->fs_outputfile, (char *)0);
149 warn("execl: " PATH_CP);
153 fs->fs_status = "copying";
154 if (waitpid(pid, &status, 0) < 0) {
155 warn("waitpid(%ld)", (long)pid);
158 if (WIFEXITED(status))
159 return WEXITSTATUS(status);
160 if (WIFSIGNALED(status))
161 warn(PATH_CP " exited on signal: %s",
162 sys_signame[WTERMSIG(status)]);