1
0
mirror of https://git.FreeBSD.org/src.git synced 2024-12-25 11:37:56 +00:00
freebsd/usr.bin/fetch/file.c
Garrett Wollman 78be319939 Here is my long-threatened revamping of fetch. Jean-Marc probably won't
recognize it any more.  This makes the following significant changes:

- The main body of the program doesn't know a thing about URIs,
  HTTP, or FTP.  This makes it possible to easily plug in other
  protocols.  (The next revision will probably be able to dynamically
  add new recognizers.)

- There are no longer arbitrary timeouts for the protocols.  If you want
  to set one for yourself, use the environment variables.

- FTP proxies are now supported (if I implemented it right).

- The HTTP implementation is much more complete, and can now do restarts,
  preserve modtimes, and mrun in mirror mode.  It's not yet up to 1.1,
  but it's getting there.

- Transaction TCP is now used for sending HTTP requests.  The HTTP/1.1 syntax
  for requesting that the connection be closed after one request is
  implemented.

In all of this, I have doubtless broken somebody.  Please test it and tell me
about the bugs.
1997-01-30 21:43:44 +00:00

145 lines
3.9 KiB
C

/*-
* Copyright 1997 Massachusetts Institute of Technology
*
* Permission to use, copy, modify, and distribute this software and
* its documentation for any purpose and without fee is hereby
* granted, provided that both the above copyright notice and this
* permission notice appear in all copies, that both the above
* copyright notice and this permission notice appear in all
* supporting documentation, and that the name of M.I.T. not be used
* in advertising or publicity pertaining to distribution of the
* software without specific, written prior permission. M.I.T. makes
* no representations about the suitability of this software for any
* purpose. It is provided "as is" without express or implied
* warranty.
*
* THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS
* ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
* SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id$
*/
#include <sys/types.h>
#include <err.h>
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sysexits.h>
#include <unistd.h>
#include <sys/wait.h>
#include "fetch.h"
static int file_retrieve(struct fetch_state *fs);
static int file_close(struct fetch_state *fs);
static int file_parse(struct fetch_state *fs, const char *uri);
struct uri_scheme file_scheme =
{ "file", file_parse, 0, 0, 0 };
/*
* Again, we slightly misinterpret the slash after the hostname as
* being the start of the pathname rather than merely a separator.
*/
static int
file_parse(struct fetch_state *fs, const char *uri)
{
const char *p;
p = uri + 5; /* skip past `file:' */
if (p[0] == '/' && p[1] == '/') {
/* skip past `//localhost', if any */
p += 2;
while (*p && *p != '/')
p++;
}
if (p[0] != '/') {
warnx("`%s': expected absolute pathname in `file' URL", uri);
return EX_USAGE;
}
fs->fs_proto = percent_decode(p);
/* guaranteed to succeed because of above test */
p = strrchr(fs->fs_proto, '/');
if (fs->fs_outputfile == 0) /* only set if not overridden by user */
fs->fs_outputfile = p + 1;
fs->fs_retrieve = file_retrieve;
fs->fs_close = file_close;
return 0;
}
static int
file_close(struct fetch_state *fs)
{
free(fs->fs_proto);
fs->fs_proto = 0;
fs->fs_outputfile = 0;
fs->fs_status = "free";
return 0;
}
static int
file_retrieve(struct fetch_state *fs)
{
/* XXX - this seems bogus to me! */
if (access(fs->fs_outputfile, F_OK) == 0) {
errno = EEXIST;
warn("%s", fs->fs_outputfile);
return EX_USAGE;
}
if (fs->fs_linkfile) {
fs->fs_status = "symlink";
if (symlink(fs->fs_proto, fs->fs_outputfile) == -1) {
warn("symlink");
return EX_OSERR;
}
fs->fs_status = "done";
} else {
pid_t pid;
int status;
fflush(stderr);
pid = fork();
if (pid < 0) {
warn("fork");
return EX_TEMPFAIL;
} else if (pid == 0) {
execl(PATH_CP, "cp", "-p", fs->fs_proto,
fs->fs_outputfile, (char *)0);
warn("execl: " PATH_CP);
fflush(stderr);
_exit(EX_OSERR);
} else {
fs->fs_status = "copying";
if (waitpid(pid, &status, 0) < 0) {
warn("waitpid(%ld)", (long)pid);
return EX_OSERR;
}
if (WIFEXITED(status))
return WEXITSTATUS(status);
if (WIFSIGNALED(status))
warn(PATH_CP " exited on signal: %s",
sys_signame[WTERMSIG(status)]);
return EX_OSERR;
}
}
return 0;
}