2000-06-28 16:55:15 +00:00
|
|
|
/*-
|
2017-11-27 15:37:16 +00:00
|
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
*
|
2014-01-28 14:29:24 +00:00
|
|
|
* Copyright (c) 2000-2014 Dag-Erling Smørgrav
|
2013-07-26 15:53:43 +00:00
|
|
|
* Copyright (c) 2013 Michael Gmelin <freebsd@grem.de>
|
2000-06-28 16:55:15 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer
|
|
|
|
* in this position and unchanged.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. The name of the author may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR 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.
|
|
|
|
*/
|
|
|
|
|
2002-03-26 12:09:49 +00:00
|
|
|
#include <sys/cdefs.h>
|
2000-06-28 16:55:15 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/socket.h>
|
2002-02-25 05:00:39 +00:00
|
|
|
#include <sys/stat.h>
|
2002-03-27 02:23:18 +00:00
|
|
|
#include <sys/time.h>
|
2000-06-28 16:55:15 +00:00
|
|
|
|
2009-12-13 03:14:06 +00:00
|
|
|
#include <ctype.h>
|
2000-06-28 16:55:15 +00:00
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
2013-07-26 15:53:43 +00:00
|
|
|
#include <getopt.h>
|
2000-07-16 00:47:00 +00:00
|
|
|
#include <signal.h>
|
2004-02-18 21:37:36 +00:00
|
|
|
#include <stdint.h>
|
2000-06-28 16:55:15 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2001-05-26 19:51:05 +00:00
|
|
|
#include <termios.h>
|
2000-06-28 16:55:15 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <fetch.h>
|
|
|
|
|
2014-01-28 14:32:04 +00:00
|
|
|
#define MINBUFSIZE 16384
|
2009-01-17 13:34:56 +00:00
|
|
|
#define TIMEOUT 120
|
2000-06-28 16:55:15 +00:00
|
|
|
|
|
|
|
/* Option flags */
|
2012-10-19 14:49:42 +00:00
|
|
|
static int A_flag; /* -A: do not follow 302 redirects */
|
|
|
|
static int a_flag; /* -a: auto retry */
|
|
|
|
static off_t B_size; /* -B: buffer size */
|
|
|
|
static int b_flag; /*! -b: workaround TCP bug */
|
|
|
|
static char *c_dirname; /* -c: remote directory */
|
|
|
|
static int d_flag; /* -d: direct connection */
|
|
|
|
static int F_flag; /* -F: restart without checking mtime */
|
|
|
|
static char *f_filename; /* -f: file to fetch */
|
|
|
|
static char *h_hostname; /* -h: host to fetch from */
|
|
|
|
static int i_flag; /* -i: specify file for mtime comparison */
|
|
|
|
static char *i_filename; /* name of input file */
|
|
|
|
static int l_flag; /* -l: link rather than copy file: URLs */
|
|
|
|
static int m_flag; /* -[Mm]: mirror mode */
|
|
|
|
static char *N_filename; /* -N: netrc file name */
|
|
|
|
static int n_flag; /* -n: do not preserve modification time */
|
|
|
|
static int o_flag; /* -o: specify output file */
|
|
|
|
static int o_directory; /* output file is a directory */
|
|
|
|
static char *o_filename; /* name of output file */
|
|
|
|
static int o_stdout; /* output file is stdout */
|
|
|
|
static int once_flag; /* -1: stop at first successful file */
|
|
|
|
static int p_flag; /* -[Pp]: use passive FTP */
|
|
|
|
static int R_flag; /* -R: don't delete partial files */
|
|
|
|
static int r_flag; /* -r: restart previous transfer */
|
|
|
|
static off_t S_size; /* -S: require size to match */
|
|
|
|
static int s_flag; /* -s: show size, don't fetch */
|
|
|
|
static long T_secs; /* -T: transfer timeout in seconds */
|
|
|
|
static int t_flag; /*! -t: workaround TCP bug */
|
|
|
|
static int U_flag; /* -U: do not use high ports */
|
|
|
|
static int v_level = 1; /* -v: verbosity level */
|
|
|
|
static int v_tty; /* stdout is a tty */
|
2018-09-10 19:39:20 +00:00
|
|
|
static int v_progress; /* whether to display progress */
|
2012-10-19 14:49:42 +00:00
|
|
|
static pid_t pgrp; /* our process group */
|
|
|
|
static long w_secs; /* -w: retry delay */
|
|
|
|
static int family = PF_UNSPEC; /* -[46]: address family to use */
|
2000-06-28 16:55:15 +00:00
|
|
|
|
2012-10-19 14:49:42 +00:00
|
|
|
static int sigalrm; /* SIGALRM received */
|
|
|
|
static int siginfo; /* SIGINFO received */
|
|
|
|
static int sigint; /* SIGINT received */
|
2000-06-28 16:55:15 +00:00
|
|
|
|
2012-10-19 14:49:42 +00:00
|
|
|
static long ftp_timeout = TIMEOUT; /* default timeout for FTP transfers */
|
|
|
|
static long http_timeout = TIMEOUT;/* default timeout for HTTP transfers */
|
|
|
|
static char *buf; /* transfer buffer */
|
2000-06-28 16:55:15 +00:00
|
|
|
|
2013-07-26 15:53:43 +00:00
|
|
|
enum options
|
|
|
|
{
|
|
|
|
OPTION_BIND_ADDRESS,
|
|
|
|
OPTION_NO_FTP_PASSIVE_MODE,
|
|
|
|
OPTION_HTTP_REFERER,
|
|
|
|
OPTION_HTTP_USER_AGENT,
|
|
|
|
OPTION_NO_PROXY,
|
|
|
|
OPTION_SSL_CA_CERT_FILE,
|
|
|
|
OPTION_SSL_CA_CERT_PATH,
|
|
|
|
OPTION_SSL_CLIENT_CERT_FILE,
|
|
|
|
OPTION_SSL_CLIENT_KEY_FILE,
|
|
|
|
OPTION_SSL_CRL_FILE,
|
|
|
|
OPTION_SSL_NO_SSL3,
|
2014-01-28 14:29:24 +00:00
|
|
|
OPTION_SSL_NO_TLS1,
|
2013-07-26 15:53:43 +00:00
|
|
|
OPTION_SSL_NO_VERIFY_HOSTNAME,
|
|
|
|
OPTION_SSL_NO_VERIFY_PEER
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static struct option longopts[] =
|
|
|
|
{
|
|
|
|
/* mapping to single character argument */
|
|
|
|
{ "one-file", no_argument, NULL, '1' },
|
|
|
|
{ "ipv4-only", no_argument, NULL, '4' },
|
|
|
|
{ "ipv6-only", no_argument, NULL, '6' },
|
|
|
|
{ "no-redirect", no_argument, NULL, 'A' },
|
|
|
|
{ "retry", no_argument, NULL, 'a' },
|
|
|
|
{ "buffer-size", required_argument, NULL, 'B' },
|
|
|
|
/* -c not mapped, since it's deprecated */
|
|
|
|
{ "direct", no_argument, NULL, 'd' },
|
|
|
|
{ "force-restart", no_argument, NULL, 'F' },
|
|
|
|
/* -f not mapped, since it's deprecated */
|
|
|
|
/* -h not mapped, since it's deprecated */
|
|
|
|
{ "if-modified-since", required_argument, NULL, 'i' },
|
|
|
|
{ "symlink", no_argument, NULL, 'l' },
|
|
|
|
/* -M not mapped since it's the same as -m */
|
|
|
|
{ "mirror", no_argument, NULL, 'm' },
|
|
|
|
{ "netrc", required_argument, NULL, 'N' },
|
|
|
|
{ "no-mtime", no_argument, NULL, 'n' },
|
|
|
|
{ "output", required_argument, NULL, 'o' },
|
|
|
|
/* -P not mapped since it's the same as -p */
|
|
|
|
{ "passive", no_argument, NULL, 'p' },
|
|
|
|
{ "quiet", no_argument, NULL, 'q' },
|
|
|
|
{ "keep-output", no_argument, NULL, 'R' },
|
|
|
|
{ "restart", no_argument, NULL, 'r' },
|
|
|
|
{ "require-size", required_argument, NULL, 'S' },
|
|
|
|
{ "print-size", no_argument, NULL, 's' },
|
|
|
|
{ "timeout", required_argument, NULL, 'T' },
|
|
|
|
{ "passive-portrange-default", no_argument, NULL, 'T' },
|
|
|
|
{ "verbose", no_argument, NULL, 'v' },
|
|
|
|
{ "retry-delay", required_argument, NULL, 'w' },
|
2014-01-28 14:29:24 +00:00
|
|
|
|
2013-07-26 15:53:43 +00:00
|
|
|
/* options without a single character equivalent */
|
|
|
|
{ "bind-address", required_argument, NULL, OPTION_BIND_ADDRESS },
|
|
|
|
{ "no-passive", no_argument, NULL, OPTION_NO_FTP_PASSIVE_MODE },
|
|
|
|
{ "referer", required_argument, NULL, OPTION_HTTP_REFERER },
|
|
|
|
{ "user-agent", required_argument, NULL, OPTION_HTTP_USER_AGENT },
|
|
|
|
{ "no-proxy", required_argument, NULL, OPTION_NO_PROXY },
|
|
|
|
{ "ca-cert", required_argument, NULL, OPTION_SSL_CA_CERT_FILE },
|
|
|
|
{ "ca-path", required_argument, NULL, OPTION_SSL_CA_CERT_PATH },
|
|
|
|
{ "cert", required_argument, NULL, OPTION_SSL_CLIENT_CERT_FILE },
|
|
|
|
{ "key", required_argument, NULL, OPTION_SSL_CLIENT_KEY_FILE },
|
|
|
|
{ "crl", required_argument, NULL, OPTION_SSL_CRL_FILE },
|
|
|
|
{ "no-sslv3", no_argument, NULL, OPTION_SSL_NO_SSL3 },
|
|
|
|
{ "no-tlsv1", no_argument, NULL, OPTION_SSL_NO_TLS1 },
|
|
|
|
{ "no-verify-hostname", no_argument, NULL, OPTION_SSL_NO_VERIFY_HOSTNAME },
|
|
|
|
{ "no-verify-peer", no_argument, NULL, OPTION_SSL_NO_VERIFY_PEER },
|
|
|
|
|
|
|
|
{ NULL, 0, NULL, 0 }
|
|
|
|
};
|
2000-06-28 16:55:15 +00:00
|
|
|
|
2001-08-17 22:22:45 +00:00
|
|
|
/*
|
|
|
|
* Signal handler
|
|
|
|
*/
|
2001-07-17 21:22:32 +00:00
|
|
|
static void
|
2000-06-28 16:55:15 +00:00
|
|
|
sig_handler(int sig)
|
|
|
|
{
|
2001-07-17 21:22:32 +00:00
|
|
|
switch (sig) {
|
|
|
|
case SIGALRM:
|
|
|
|
sigalrm = 1;
|
|
|
|
break;
|
|
|
|
case SIGINFO:
|
|
|
|
siginfo = 1;
|
|
|
|
break;
|
|
|
|
case SIGINT:
|
|
|
|
sigint = 1;
|
|
|
|
break;
|
|
|
|
}
|
2000-06-28 16:55:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct xferstat {
|
2004-02-18 15:12:00 +00:00
|
|
|
char name[64];
|
2012-11-16 12:05:10 +00:00
|
|
|
struct timeval start; /* start of transfer */
|
|
|
|
struct timeval last; /* time of last update */
|
|
|
|
struct timeval last2; /* time of previous last update */
|
|
|
|
off_t size; /* size of file per HTTP hdr */
|
|
|
|
off_t offset; /* starting offset in file */
|
|
|
|
off_t rcvd; /* bytes already received */
|
|
|
|
off_t lastrcvd; /* bytes received since last update */
|
2000-06-28 16:55:15 +00:00
|
|
|
};
|
|
|
|
|
2018-09-10 19:39:20 +00:00
|
|
|
/*
|
|
|
|
* Format a number of seconds as either XXdYYh, XXhYYm, XXmYYs, or XXs
|
|
|
|
* depending on its magnitude
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
stat_seconds(char *str, size_t strsz, long seconds)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (seconds > 86400)
|
|
|
|
snprintf(str, strsz, "%02ldd%02ldh",
|
|
|
|
seconds / 86400, (seconds % 86400) / 3600);
|
|
|
|
else if (seconds > 3600)
|
|
|
|
snprintf(str, strsz, "%02ldh%02ldm",
|
|
|
|
seconds / 3600, (seconds % 3600) / 60);
|
|
|
|
else if (seconds > 60)
|
|
|
|
snprintf(str, strsz, "%02ldm%02lds",
|
|
|
|
seconds / 60, seconds % 60);
|
|
|
|
else
|
|
|
|
snprintf(str, strsz, " %02lds",
|
|
|
|
seconds);
|
|
|
|
}
|
|
|
|
|
2003-01-22 18:33:39 +00:00
|
|
|
/*
|
|
|
|
* Compute and display ETA
|
|
|
|
*/
|
2018-09-10 19:39:20 +00:00
|
|
|
static void
|
|
|
|
stat_eta(char *str, size_t strsz, const struct xferstat *xs)
|
2003-01-22 18:33:39 +00:00
|
|
|
{
|
2004-02-18 21:37:36 +00:00
|
|
|
long elapsed, eta;
|
|
|
|
off_t received, expected;
|
2003-01-22 18:33:39 +00:00
|
|
|
|
|
|
|
elapsed = xs->last.tv_sec - xs->start.tv_sec;
|
2003-03-11 08:21:51 +00:00
|
|
|
received = xs->rcvd - xs->offset;
|
|
|
|
expected = xs->size - xs->rcvd;
|
2003-03-11 21:33:43 +00:00
|
|
|
eta = (long)((double)elapsed * expected / received);
|
2018-09-10 19:39:20 +00:00
|
|
|
if (eta > 0)
|
|
|
|
stat_seconds(str, strsz, eta);
|
2012-11-16 12:05:10 +00:00
|
|
|
else
|
2018-09-10 19:39:20 +00:00
|
|
|
stat_seconds(str, strsz, elapsed);
|
2004-02-18 15:12:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Format a number as "xxxx YB" where Y is ' ', 'k', 'M'...
|
|
|
|
*/
|
|
|
|
static const char *prefixes = " kMGTP";
|
2018-09-10 19:39:20 +00:00
|
|
|
static void
|
|
|
|
stat_bytes(char *str, size_t strsz, off_t bytes)
|
2004-02-18 15:12:00 +00:00
|
|
|
{
|
|
|
|
const char *prefix = prefixes;
|
|
|
|
|
|
|
|
while (bytes > 9999 && prefix[1] != '\0') {
|
|
|
|
bytes /= 1024;
|
|
|
|
prefix++;
|
2003-01-22 18:33:39 +00:00
|
|
|
}
|
2018-09-10 19:39:20 +00:00
|
|
|
snprintf(str, strsz, "%4ju %cB", (uintmax_t)bytes, *prefix);
|
2003-01-22 18:33:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Compute and display transfer rate
|
|
|
|
*/
|
2018-09-10 19:39:20 +00:00
|
|
|
static void
|
|
|
|
stat_bps(char *str, size_t strsz, struct xferstat *xs)
|
2003-01-22 18:33:39 +00:00
|
|
|
{
|
2018-09-10 19:39:20 +00:00
|
|
|
char bytes[16];
|
2003-01-23 13:06:13 +00:00
|
|
|
double delta, bps;
|
2003-01-22 18:33:39 +00:00
|
|
|
|
2018-09-10 19:39:20 +00:00
|
|
|
delta = ((double)xs->last.tv_sec + (xs->last.tv_usec / 1.e6))
|
|
|
|
- ((double)xs->last2.tv_sec + (xs->last2.tv_usec / 1.e6));
|
2012-11-16 12:05:10 +00:00
|
|
|
|
2003-01-23 13:06:13 +00:00
|
|
|
if (delta == 0.0) {
|
2018-09-10 19:39:20 +00:00
|
|
|
snprintf(str, strsz, "?? Bps");
|
2004-02-18 15:12:00 +00:00
|
|
|
} else {
|
2012-12-09 22:54:03 +00:00
|
|
|
bps = (xs->rcvd - xs->lastrcvd) / delta;
|
2018-09-10 19:39:20 +00:00
|
|
|
stat_bytes(bytes, sizeof bytes, (off_t)bps);
|
|
|
|
snprintf(str, strsz, "%sps", bytes);
|
2003-01-23 13:06:13 +00:00
|
|
|
}
|
2003-01-22 18:33:39 +00:00
|
|
|
}
|
|
|
|
|
2001-08-17 22:22:45 +00:00
|
|
|
/*
|
|
|
|
* Update the stats display
|
|
|
|
*/
|
2001-07-17 21:22:32 +00:00
|
|
|
static void
|
2000-07-12 20:53:07 +00:00
|
|
|
stat_display(struct xferstat *xs, int force)
|
2000-06-28 16:55:15 +00:00
|
|
|
{
|
2018-09-10 19:39:20 +00:00
|
|
|
char bytes[16], bps[16], eta[16];
|
2001-07-17 21:22:32 +00:00
|
|
|
struct timeval now;
|
2001-09-23 22:03:39 +00:00
|
|
|
int ctty_pgrp;
|
2001-07-17 21:22:32 +00:00
|
|
|
|
2001-09-23 22:03:39 +00:00
|
|
|
/* check if we're the foreground process */
|
2018-09-10 19:39:20 +00:00
|
|
|
if (ioctl(STDERR_FILENO, TIOCGPGRP, &ctty_pgrp) != 0 ||
|
2001-09-23 22:03:39 +00:00
|
|
|
(pid_t)ctty_pgrp != pgrp)
|
|
|
|
return;
|
2002-10-27 15:32:51 +00:00
|
|
|
|
2001-07-17 21:22:32 +00:00
|
|
|
gettimeofday(&now, NULL);
|
|
|
|
if (!force && now.tv_sec <= xs->last.tv_sec)
|
|
|
|
return;
|
2012-11-16 12:05:10 +00:00
|
|
|
xs->last2 = xs->last;
|
2001-07-17 21:22:32 +00:00
|
|
|
xs->last = now;
|
|
|
|
|
2004-07-05 11:21:50 +00:00
|
|
|
fprintf(stderr, "\r%-46.46s", xs->name);
|
2018-09-10 19:39:20 +00:00
|
|
|
if (xs->rcvd >= xs->size) {
|
|
|
|
stat_bytes(bytes, sizeof bytes, xs->rcvd);
|
|
|
|
setproctitle("%s [%s]", xs->name, bytes);
|
|
|
|
fprintf(stderr, " %s", bytes);
|
2002-10-27 15:15:13 +00:00
|
|
|
} else {
|
2018-09-10 19:39:20 +00:00
|
|
|
stat_bytes(bytes, sizeof bytes, xs->size);
|
2005-12-30 18:17:11 +00:00
|
|
|
setproctitle("%s [%d%% of %s]", xs->name,
|
|
|
|
(int)((100.0 * xs->rcvd) / xs->size),
|
2018-09-10 19:39:20 +00:00
|
|
|
bytes);
|
2004-02-18 15:12:00 +00:00
|
|
|
fprintf(stderr, "%3d%% of %s",
|
|
|
|
(int)((100.0 * xs->rcvd) / xs->size),
|
2018-09-10 19:39:20 +00:00
|
|
|
bytes);
|
2002-10-27 15:15:13 +00:00
|
|
|
}
|
2012-11-16 12:05:10 +00:00
|
|
|
if (force == 2) {
|
|
|
|
xs->lastrcvd = xs->offset;
|
|
|
|
xs->last2 = xs->start;
|
|
|
|
}
|
2018-09-10 19:39:20 +00:00
|
|
|
stat_bps(bps, sizeof bps, xs);
|
|
|
|
fprintf(stderr, " %s", bps);
|
2012-11-16 12:05:10 +00:00
|
|
|
if ((xs->size > 0 && xs->rcvd > 0 &&
|
|
|
|
xs->last.tv_sec >= xs->start.tv_sec + 3) ||
|
2018-09-10 19:39:20 +00:00
|
|
|
force == 2) {
|
|
|
|
stat_eta(eta, sizeof eta, xs);
|
|
|
|
fprintf(stderr, " %s", eta);
|
|
|
|
}
|
2012-11-16 12:05:10 +00:00
|
|
|
xs->lastrcvd = xs->rcvd;
|
2000-06-28 16:55:15 +00:00
|
|
|
}
|
|
|
|
|
2001-08-17 22:22:45 +00:00
|
|
|
/*
|
|
|
|
* Initialize the transfer statistics
|
|
|
|
*/
|
2001-07-17 21:22:32 +00:00
|
|
|
static void
|
|
|
|
stat_start(struct xferstat *xs, const char *name, off_t size, off_t offset)
|
2000-07-12 20:53:07 +00:00
|
|
|
{
|
2018-09-10 19:39:20 +00:00
|
|
|
|
|
|
|
memset(xs, 0, sizeof *xs);
|
2001-07-17 21:22:32 +00:00
|
|
|
snprintf(xs->name, sizeof xs->name, "%s", name);
|
|
|
|
gettimeofday(&xs->start, NULL);
|
2018-09-10 19:39:20 +00:00
|
|
|
xs->last2 = xs->last = xs->start;
|
2001-07-17 21:22:32 +00:00
|
|
|
xs->size = size;
|
|
|
|
xs->offset = offset;
|
|
|
|
xs->rcvd = offset;
|
2012-11-16 12:05:10 +00:00
|
|
|
xs->lastrcvd = offset;
|
2018-09-10 19:39:20 +00:00
|
|
|
if (v_progress)
|
2004-02-18 15:12:00 +00:00
|
|
|
stat_display(xs, 1);
|
|
|
|
else if (v_level > 0)
|
|
|
|
fprintf(stderr, "%-46s", xs->name);
|
2000-07-12 20:53:07 +00:00
|
|
|
}
|
|
|
|
|
2001-08-17 22:22:45 +00:00
|
|
|
/*
|
|
|
|
* Update the transfer statistics
|
|
|
|
*/
|
2001-07-17 21:22:32 +00:00
|
|
|
static void
|
|
|
|
stat_update(struct xferstat *xs, off_t rcvd)
|
2000-07-12 20:53:07 +00:00
|
|
|
{
|
2018-09-10 19:39:20 +00:00
|
|
|
|
2001-07-17 21:22:32 +00:00
|
|
|
xs->rcvd = rcvd;
|
2018-09-10 19:39:20 +00:00
|
|
|
if (v_progress)
|
2004-02-18 15:12:00 +00:00
|
|
|
stat_display(xs, 0);
|
2000-07-12 20:53:07 +00:00
|
|
|
}
|
|
|
|
|
2001-08-17 22:22:45 +00:00
|
|
|
/*
|
|
|
|
* Finalize the transfer statistics
|
|
|
|
*/
|
2001-07-17 21:22:32 +00:00
|
|
|
static void
|
2000-06-28 16:55:15 +00:00
|
|
|
stat_end(struct xferstat *xs)
|
|
|
|
{
|
2018-09-10 19:39:20 +00:00
|
|
|
char bytes[16], bps[16], eta[16];
|
|
|
|
|
2003-01-23 13:06:13 +00:00
|
|
|
gettimeofday(&xs->last, NULL);
|
2018-09-10 19:39:20 +00:00
|
|
|
if (v_progress) {
|
2012-11-16 12:05:10 +00:00
|
|
|
stat_display(xs, 2);
|
2004-02-18 15:12:00 +00:00
|
|
|
putc('\n', stderr);
|
|
|
|
} else if (v_level > 0) {
|
2018-09-10 19:39:20 +00:00
|
|
|
stat_bytes(bytes, sizeof bytes, xs->rcvd);
|
|
|
|
stat_bps(bps, sizeof bps, xs);
|
|
|
|
stat_eta(eta, sizeof eta, xs);
|
|
|
|
fprintf(stderr, " %s %s %s\n", bytes, bps, eta);
|
2004-02-18 15:12:00 +00:00
|
|
|
}
|
2000-06-28 16:55:15 +00:00
|
|
|
}
|
|
|
|
|
2001-08-17 22:22:45 +00:00
|
|
|
/*
|
|
|
|
* Ask the user for authentication details
|
|
|
|
*/
|
2001-07-17 21:22:32 +00:00
|
|
|
static int
|
2001-05-26 19:51:05 +00:00
|
|
|
query_auth(struct url *URL)
|
|
|
|
{
|
2001-07-17 21:22:32 +00:00
|
|
|
struct termios tios;
|
|
|
|
tcflag_t saved_flags;
|
|
|
|
int i, nopwd;
|
|
|
|
|
|
|
|
fprintf(stderr, "Authentication required for <%s://%s:%d/>!\n",
|
2001-11-10 18:39:54 +00:00
|
|
|
URL->scheme, URL->host, URL->port);
|
2001-07-17 21:22:32 +00:00
|
|
|
|
|
|
|
fprintf(stderr, "Login: ");
|
|
|
|
if (fgets(URL->user, sizeof URL->user, stdin) == NULL)
|
2004-07-27 11:30:35 +00:00
|
|
|
return (-1);
|
2004-07-27 11:34:25 +00:00
|
|
|
for (i = strlen(URL->user); i >= 0; --i)
|
|
|
|
if (URL->user[i] == '\r' || URL->user[i] == '\n')
|
2001-07-17 21:22:32 +00:00
|
|
|
URL->user[i] = '\0';
|
|
|
|
|
|
|
|
fprintf(stderr, "Password: ");
|
|
|
|
if (tcgetattr(STDIN_FILENO, &tios) == 0) {
|
|
|
|
saved_flags = tios.c_lflag;
|
|
|
|
tios.c_lflag &= ~ECHO;
|
|
|
|
tios.c_lflag |= ECHONL|ICANON;
|
|
|
|
tcsetattr(STDIN_FILENO, TCSAFLUSH|TCSASOFT, &tios);
|
|
|
|
nopwd = (fgets(URL->pwd, sizeof URL->pwd, stdin) == NULL);
|
|
|
|
tios.c_lflag = saved_flags;
|
|
|
|
tcsetattr(STDIN_FILENO, TCSANOW|TCSASOFT, &tios);
|
|
|
|
} else {
|
|
|
|
nopwd = (fgets(URL->pwd, sizeof URL->pwd, stdin) == NULL);
|
|
|
|
}
|
|
|
|
if (nopwd)
|
2004-07-27 11:30:35 +00:00
|
|
|
return (-1);
|
2004-07-27 11:34:25 +00:00
|
|
|
for (i = strlen(URL->pwd); i >= 0; --i)
|
|
|
|
if (URL->pwd[i] == '\r' || URL->pwd[i] == '\n')
|
2001-07-17 21:22:32 +00:00
|
|
|
URL->pwd[i] = '\0';
|
2004-07-27 11:34:25 +00:00
|
|
|
|
2004-07-27 11:30:35 +00:00
|
|
|
return (0);
|
2001-05-26 19:51:05 +00:00
|
|
|
}
|
|
|
|
|
2001-08-17 22:22:45 +00:00
|
|
|
/*
|
|
|
|
* Fetch a file
|
|
|
|
*/
|
2001-07-17 21:22:32 +00:00
|
|
|
static int
|
2022-01-24 07:35:49 +00:00
|
|
|
fetch(char *URL, const char *path, int *is_http)
|
2000-06-28 16:55:15 +00:00
|
|
|
{
|
2001-07-17 21:22:32 +00:00
|
|
|
struct url *url;
|
|
|
|
struct url_stat us;
|
2001-09-08 15:17:15 +00:00
|
|
|
struct stat sb, nsb;
|
2001-07-17 21:22:32 +00:00
|
|
|
struct xferstat xs;
|
|
|
|
FILE *f, *of;
|
2012-01-18 15:13:21 +00:00
|
|
|
size_t size, readcnt, wr;
|
2022-01-24 04:03:42 +00:00
|
|
|
off_t count, size_prev;
|
2001-07-17 21:22:32 +00:00
|
|
|
char flags[8];
|
2001-09-08 15:17:15 +00:00
|
|
|
const char *slash;
|
|
|
|
char *tmppath;
|
2022-01-24 04:03:42 +00:00
|
|
|
int r, tries;
|
2004-02-18 21:37:36 +00:00
|
|
|
unsigned timeout;
|
|
|
|
char *ptr;
|
2001-07-17 21:22:32 +00:00
|
|
|
|
|
|
|
f = of = NULL;
|
2001-09-08 15:17:15 +00:00
|
|
|
tmppath = NULL;
|
2001-07-17 21:22:32 +00:00
|
|
|
|
2003-01-22 18:33:39 +00:00
|
|
|
timeout = 0;
|
|
|
|
*flags = 0;
|
|
|
|
count = 0;
|
|
|
|
|
|
|
|
/* set verbosity level */
|
|
|
|
if (v_level > 1)
|
|
|
|
strcat(flags, "v");
|
|
|
|
if (v_level > 2)
|
|
|
|
fetchDebug = 1;
|
|
|
|
|
2001-07-17 21:22:32 +00:00
|
|
|
/* parse URL */
|
2009-12-30 23:01:49 +00:00
|
|
|
url = NULL;
|
|
|
|
if (*URL == '\0') {
|
|
|
|
warnx("empty URL");
|
|
|
|
goto failure;
|
|
|
|
}
|
2001-07-17 21:22:32 +00:00
|
|
|
if ((url = fetchParseURL(URL)) == NULL) {
|
|
|
|
warnx("%s: parse error", URL);
|
|
|
|
goto failure;
|
2000-07-17 21:49:01 +00:00
|
|
|
}
|
2001-07-17 21:22:32 +00:00
|
|
|
|
|
|
|
/* if no scheme was specified, take a guess */
|
|
|
|
if (!*url->scheme) {
|
|
|
|
if (!*url->host)
|
|
|
|
strcpy(url->scheme, SCHEME_FILE);
|
|
|
|
else if (strncasecmp(url->host, "ftp.", 4) == 0)
|
|
|
|
strcpy(url->scheme, SCHEME_FTP);
|
|
|
|
else if (strncasecmp(url->host, "www.", 4) == 0)
|
|
|
|
strcpy(url->scheme, SCHEME_HTTP);
|
2000-06-28 16:55:15 +00:00
|
|
|
}
|
2001-07-17 21:22:32 +00:00
|
|
|
|
2022-01-24 07:35:49 +00:00
|
|
|
/* for both of http and https */
|
2022-01-24 08:07:18 +00:00
|
|
|
*is_http = strncmp(url->scheme, "http", 4) == 0;
|
2022-01-24 07:35:49 +00:00
|
|
|
|
2001-07-17 21:22:32 +00:00
|
|
|
/* common flags */
|
|
|
|
switch (family) {
|
|
|
|
case PF_INET:
|
|
|
|
strcat(flags, "4");
|
|
|
|
break;
|
|
|
|
case PF_INET6:
|
|
|
|
strcat(flags, "6");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* FTP specific flags */
|
2008-08-21 14:12:34 +00:00
|
|
|
if (strcmp(url->scheme, SCHEME_FTP) == 0) {
|
2001-07-17 21:22:32 +00:00
|
|
|
if (p_flag)
|
|
|
|
strcat(flags, "p");
|
|
|
|
if (d_flag)
|
|
|
|
strcat(flags, "d");
|
|
|
|
if (U_flag)
|
|
|
|
strcat(flags, "l");
|
|
|
|
timeout = T_secs ? T_secs : ftp_timeout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* HTTP specific flags */
|
2008-12-11 06:54:36 +00:00
|
|
|
if (strcmp(url->scheme, SCHEME_HTTP) == 0 ||
|
|
|
|
strcmp(url->scheme, SCHEME_HTTPS) == 0) {
|
2001-07-17 21:22:32 +00:00
|
|
|
if (d_flag)
|
|
|
|
strcat(flags, "d");
|
|
|
|
if (A_flag)
|
|
|
|
strcat(flags, "A");
|
|
|
|
timeout = T_secs ? T_secs : http_timeout;
|
2008-12-15 08:27:44 +00:00
|
|
|
if (i_flag) {
|
|
|
|
if (stat(i_filename, &sb)) {
|
|
|
|
warn("%s: stat()", i_filename);
|
|
|
|
goto failure;
|
|
|
|
}
|
|
|
|
url->ims_time = sb.st_mtime;
|
|
|
|
strcat(flags, "i");
|
|
|
|
}
|
2001-07-17 21:22:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* set the protocol timeout. */
|
|
|
|
fetchTimeout = timeout;
|
|
|
|
|
|
|
|
/* just print size */
|
|
|
|
if (s_flag) {
|
2002-10-27 15:15:13 +00:00
|
|
|
if (timeout)
|
|
|
|
alarm(timeout);
|
|
|
|
r = fetchStat(url, &us, flags);
|
|
|
|
if (timeout)
|
2002-10-27 15:32:51 +00:00
|
|
|
alarm(0);
|
2002-10-27 15:15:13 +00:00
|
|
|
if (sigalrm || sigint)
|
|
|
|
goto signal;
|
|
|
|
if (r == -1) {
|
|
|
|
warnx("%s", fetchLastErrString);
|
2001-07-17 21:22:32 +00:00
|
|
|
goto failure;
|
2002-10-27 15:15:13 +00:00
|
|
|
}
|
2001-07-17 21:22:32 +00:00
|
|
|
if (us.size == -1)
|
|
|
|
printf("Unknown\n");
|
|
|
|
else
|
2004-02-18 21:37:36 +00:00
|
|
|
printf("%jd\n", (intmax_t)us.size);
|
2000-07-19 23:56:45 +00:00
|
|
|
goto success;
|
2000-06-28 16:55:15 +00:00
|
|
|
}
|
2001-07-17 21:22:32 +00:00
|
|
|
|
2022-01-24 04:03:42 +00:00
|
|
|
tries = 1;
|
|
|
|
again:
|
|
|
|
r = 0;
|
2000-07-17 21:49:01 +00:00
|
|
|
/*
|
2001-07-17 21:22:32 +00:00
|
|
|
* If the -r flag was specified, we have to compare the local
|
|
|
|
* and remote files, so we should really do a fetchStat()
|
|
|
|
* first, but I know of at least one HTTP server that only
|
|
|
|
* sends the content size in response to GET requests, and
|
|
|
|
* leaves it out of replies to HEAD requests. Also, in the
|
|
|
|
* (frequent) case that the local and remote files match but
|
|
|
|
* the local file is truncated, we have sufficient information
|
|
|
|
* before the compare to issue a correct request. Therefore,
|
|
|
|
* we always issue a GET request as if we were sure the local
|
|
|
|
* file was a truncated copy of the remote file; we can drop
|
|
|
|
* the connection later if we change our minds.
|
2000-07-17 21:49:01 +00:00
|
|
|
*/
|
2001-09-08 15:17:15 +00:00
|
|
|
sb.st_size = -1;
|
2004-08-15 22:22:35 +00:00
|
|
|
if (!o_stdout) {
|
|
|
|
r = stat(path, &sb);
|
2022-01-24 04:03:42 +00:00
|
|
|
if (r == 0 && (r_flag || tries > 1) && S_ISREG(sb.st_mode)) {
|
2004-08-15 22:22:35 +00:00
|
|
|
url->offset = sb.st_size;
|
2005-12-30 23:36:26 +00:00
|
|
|
} else if (r == -1 || !S_ISREG(sb.st_mode)) {
|
2004-08-15 22:22:35 +00:00
|
|
|
/*
|
|
|
|
* Whatever value sb.st_size has now is either
|
|
|
|
* wrong (if stat(2) failed) or irrelevant (if the
|
|
|
|
* path does not refer to a regular file)
|
|
|
|
*/
|
|
|
|
sb.st_size = -1;
|
2005-12-30 23:36:26 +00:00
|
|
|
}
|
|
|
|
if (r == -1 && errno != ENOENT) {
|
|
|
|
warnx("%s: stat()", path);
|
|
|
|
goto failure;
|
2004-08-15 22:22:35 +00:00
|
|
|
}
|
2000-06-28 16:55:15 +00:00
|
|
|
}
|
2022-01-24 04:03:42 +00:00
|
|
|
size_prev = sb.st_size;
|
2000-06-28 16:55:15 +00:00
|
|
|
|
2001-07-17 21:22:32 +00:00
|
|
|
/* start the transfer */
|
2002-10-27 15:15:13 +00:00
|
|
|
if (timeout)
|
|
|
|
alarm(timeout);
|
|
|
|
f = fetchXGet(url, &us, flags);
|
2002-10-27 15:32:06 +00:00
|
|
|
if (timeout)
|
|
|
|
alarm(0);
|
2002-10-27 15:15:13 +00:00
|
|
|
if (sigalrm || sigint)
|
|
|
|
goto signal;
|
|
|
|
if (f == NULL) {
|
2002-11-27 20:52:07 +00:00
|
|
|
warnx("%s: %s", URL, fetchLastErrString);
|
2018-05-12 17:04:40 +00:00
|
|
|
if (i_flag && (strcmp(url->scheme, SCHEME_HTTP) == 0 ||
|
|
|
|
strcmp(url->scheme, SCHEME_HTTPS) == 0) &&
|
|
|
|
fetchLastErrCode == FETCH_OK &&
|
|
|
|
strcmp(fetchLastErrString, "Not Modified") == 0) {
|
2008-12-15 08:27:44 +00:00
|
|
|
/* HTTP Not Modified Response, return OK. */
|
|
|
|
r = 0;
|
|
|
|
goto done;
|
|
|
|
} else
|
|
|
|
goto failure;
|
2001-07-17 21:22:32 +00:00
|
|
|
}
|
|
|
|
if (sigint)
|
|
|
|
goto signal;
|
2000-06-28 16:55:15 +00:00
|
|
|
|
2001-07-17 21:22:32 +00:00
|
|
|
/* check that size is as expected */
|
|
|
|
if (S_size) {
|
|
|
|
if (us.size == -1) {
|
2002-11-27 20:52:07 +00:00
|
|
|
warnx("%s: size unknown", URL);
|
2001-07-17 21:22:32 +00:00
|
|
|
} else if (us.size != S_size) {
|
2004-02-18 21:37:36 +00:00
|
|
|
warnx("%s: size mismatch: expected %jd, actual %jd",
|
|
|
|
URL, (intmax_t)S_size, (intmax_t)us.size);
|
2001-07-17 21:22:32 +00:00
|
|
|
goto failure;
|
|
|
|
}
|
|
|
|
}
|
2000-07-12 20:53:07 +00:00
|
|
|
|
2001-07-17 21:22:32 +00:00
|
|
|
/* symlink instead of copy */
|
|
|
|
if (l_flag && strcmp(url->scheme, "file") == 0 && !o_stdout) {
|
|
|
|
if (symlink(url->doc, path) == -1) {
|
|
|
|
warn("%s: symlink()", path);
|
|
|
|
goto failure;
|
|
|
|
}
|
|
|
|
goto success;
|
2001-05-26 19:51:05 +00:00
|
|
|
}
|
2001-07-17 21:22:32 +00:00
|
|
|
|
2002-10-27 17:33:08 +00:00
|
|
|
if (us.size == -1 && !o_stdout && v_level > 0)
|
2002-11-27 20:52:07 +00:00
|
|
|
warnx("%s: size of remote file is not known", URL);
|
2001-07-17 21:22:32 +00:00
|
|
|
if (v_level > 1) {
|
|
|
|
if (sb.st_size != -1)
|
2004-02-18 21:37:36 +00:00
|
|
|
fprintf(stderr, "local size / mtime: %jd / %ld\n",
|
|
|
|
(intmax_t)sb.st_size, (long)sb.st_mtime);
|
2001-07-17 21:22:32 +00:00
|
|
|
if (us.size != -1)
|
2004-02-18 21:37:36 +00:00
|
|
|
fprintf(stderr, "remote size / mtime: %jd / %ld\n",
|
|
|
|
(intmax_t)us.size, (long)us.mtime);
|
2001-03-07 05:33:50 +00:00
|
|
|
}
|
2001-07-17 21:22:32 +00:00
|
|
|
|
|
|
|
/* open output file */
|
|
|
|
if (o_stdout) {
|
|
|
|
/* output to stdout */
|
|
|
|
of = stdout;
|
2001-09-08 15:17:15 +00:00
|
|
|
} else if (r_flag && sb.st_size != -1) {
|
2001-07-17 21:22:32 +00:00
|
|
|
/* resume mode, local file exists */
|
2022-01-24 04:03:42 +00:00
|
|
|
if (!F_flag && us.mtime && sb.st_mtime != us.mtime && tries == 1) {
|
2001-07-17 21:22:32 +00:00
|
|
|
/* no match! have to refetch */
|
|
|
|
fclose(f);
|
|
|
|
/* if precious, warn the user and give up */
|
|
|
|
if (R_flag) {
|
|
|
|
warnx("%s: local modification time "
|
|
|
|
"does not match remote", path);
|
|
|
|
goto failure_keep;
|
|
|
|
}
|
2011-09-15 22:50:31 +00:00
|
|
|
} else if (url->offset > sb.st_size) {
|
|
|
|
/* gap between what we asked for and what we got */
|
|
|
|
warnx("%s: gap in resume mode", URL);
|
|
|
|
fclose(of);
|
|
|
|
of = NULL;
|
|
|
|
/* picked up again later */
|
2004-04-06 12:24:26 +00:00
|
|
|
} else if (us.size != -1) {
|
2001-07-17 21:22:32 +00:00
|
|
|
if (us.size == sb.st_size)
|
|
|
|
/* nothing to do */
|
|
|
|
goto success;
|
|
|
|
if (sb.st_size > us.size) {
|
|
|
|
/* local file too long! */
|
2004-02-18 21:37:36 +00:00
|
|
|
warnx("%s: local file (%jd bytes) is longer "
|
|
|
|
"than remote file (%jd bytes)", path,
|
|
|
|
(intmax_t)sb.st_size, (intmax_t)us.size);
|
2001-07-17 21:22:32 +00:00
|
|
|
goto failure;
|
|
|
|
}
|
2001-09-08 15:17:15 +00:00
|
|
|
/* we got it, open local file */
|
2011-09-27 15:57:13 +00:00
|
|
|
if ((of = fopen(path, "r+")) == NULL) {
|
2001-07-17 21:22:32 +00:00
|
|
|
warn("%s: fopen()", path);
|
|
|
|
goto failure;
|
|
|
|
}
|
2001-09-08 15:17:15 +00:00
|
|
|
/* check that it didn't move under our feet */
|
|
|
|
if (fstat(fileno(of), &nsb) == -1) {
|
|
|
|
/* can't happen! */
|
|
|
|
warn("%s: fstat()", path);
|
2001-07-17 21:22:32 +00:00
|
|
|
goto failure;
|
|
|
|
}
|
2001-09-08 15:17:15 +00:00
|
|
|
if (nsb.st_dev != sb.st_dev ||
|
2013-06-02 18:10:07 +00:00
|
|
|
nsb.st_ino != sb.st_ino ||
|
2001-09-08 15:17:15 +00:00
|
|
|
nsb.st_size != sb.st_size) {
|
2002-11-27 20:52:07 +00:00
|
|
|
warnx("%s: file has changed", URL);
|
2001-09-08 15:17:15 +00:00
|
|
|
fclose(of);
|
|
|
|
of = NULL;
|
|
|
|
sb = nsb;
|
2011-09-15 22:50:31 +00:00
|
|
|
/* picked up again later */
|
|
|
|
}
|
2011-09-27 15:57:13 +00:00
|
|
|
}
|
|
|
|
/* seek to where we left off */
|
2011-09-27 17:11:31 +00:00
|
|
|
if (of != NULL && fseeko(of, url->offset, SEEK_SET) != 0) {
|
|
|
|
warn("%s: fseeko()", path);
|
2011-09-27 15:57:13 +00:00
|
|
|
fclose(of);
|
|
|
|
of = NULL;
|
|
|
|
/* picked up again later */
|
2001-07-17 21:22:32 +00:00
|
|
|
}
|
2001-09-08 15:17:15 +00:00
|
|
|
} else if (m_flag && sb.st_size != -1) {
|
2001-07-17 21:22:32 +00:00
|
|
|
/* mirror mode, local file exists */
|
|
|
|
if (sb.st_size == us.size && sb.st_mtime == us.mtime)
|
|
|
|
goto success;
|
|
|
|
}
|
2002-10-27 15:32:51 +00:00
|
|
|
|
2001-09-08 15:17:15 +00:00
|
|
|
if (of == NULL) {
|
2001-07-17 21:22:32 +00:00
|
|
|
/*
|
|
|
|
* We don't yet have an output file; either this is a
|
|
|
|
* vanilla run with no special flags, or the local and
|
|
|
|
* remote files didn't match.
|
|
|
|
*/
|
2002-10-27 15:32:51 +00:00
|
|
|
|
2003-01-22 18:33:39 +00:00
|
|
|
if (url->offset > 0) {
|
2001-09-08 15:17:15 +00:00
|
|
|
/*
|
|
|
|
* We tried to restart a transfer, but for
|
|
|
|
* some reason gave up - so we have to restart
|
|
|
|
* from scratch if we want the whole file
|
|
|
|
*/
|
|
|
|
url->offset = 0;
|
|
|
|
if ((f = fetchXGet(url, &us, flags)) == NULL) {
|
2002-11-27 20:52:07 +00:00
|
|
|
warnx("%s: %s", URL, fetchLastErrString);
|
2001-09-08 15:17:15 +00:00
|
|
|
goto failure;
|
|
|
|
}
|
|
|
|
if (sigint)
|
|
|
|
goto signal;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* construct a temp file name */
|
|
|
|
if (sb.st_size != -1 && S_ISREG(sb.st_mode)) {
|
|
|
|
if ((slash = strrchr(path, '/')) == NULL)
|
|
|
|
slash = path;
|
|
|
|
else
|
|
|
|
++slash;
|
2022-01-24 04:03:42 +00:00
|
|
|
if(tmppath != NULL)
|
|
|
|
free(tmppath);
|
2001-09-08 15:17:15 +00:00
|
|
|
asprintf(&tmppath, "%.*s.fetch.XXXXXX.%s",
|
2001-09-10 17:23:57 +00:00
|
|
|
(int)(slash - path), path, slash);
|
2002-07-28 21:09:25 +00:00
|
|
|
if (tmppath != NULL) {
|
2012-12-08 18:41:16 +00:00
|
|
|
if (mkstemps(tmppath, strlen(slash) + 1) == -1) {
|
|
|
|
warn("%s: mkstemps()", path);
|
|
|
|
goto failure;
|
|
|
|
}
|
2002-07-28 21:09:25 +00:00
|
|
|
of = fopen(tmppath, "w");
|
2006-11-10 22:05:41 +00:00
|
|
|
chown(tmppath, sb.st_uid, sb.st_gid);
|
|
|
|
chmod(tmppath, sb.st_mode & ALLPERMS);
|
2002-07-28 21:09:25 +00:00
|
|
|
}
|
2001-09-08 15:17:15 +00:00
|
|
|
}
|
2002-07-28 21:09:25 +00:00
|
|
|
if (of == NULL)
|
2001-09-08 15:17:15 +00:00
|
|
|
of = fopen(path, "w");
|
|
|
|
if (of == NULL) {
|
2001-07-17 21:22:32 +00:00
|
|
|
warn("%s: open()", path);
|
|
|
|
goto failure;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
count = url->offset;
|
|
|
|
|
|
|
|
/* start the counter */
|
|
|
|
stat_start(&xs, path, us.size, count);
|
|
|
|
|
|
|
|
sigalrm = siginfo = sigint = 0;
|
|
|
|
|
|
|
|
/* suck in the data */
|
2014-01-28 14:32:04 +00:00
|
|
|
setvbuf(f, NULL, _IOFBF, B_size);
|
2001-07-17 21:22:32 +00:00
|
|
|
signal(SIGINFO, sig_handler);
|
2002-10-27 15:15:13 +00:00
|
|
|
while (!sigint) {
|
2004-11-18 12:01:30 +00:00
|
|
|
if (us.size != -1 && us.size - count < B_size &&
|
|
|
|
us.size - count >= 0)
|
2001-07-17 21:22:32 +00:00
|
|
|
size = us.size - count;
|
2001-03-07 05:33:50 +00:00
|
|
|
else
|
2001-07-17 21:22:32 +00:00
|
|
|
size = B_size;
|
|
|
|
if (siginfo) {
|
|
|
|
stat_end(&xs);
|
|
|
|
siginfo = 0;
|
|
|
|
}
|
2012-01-18 15:13:21 +00:00
|
|
|
|
|
|
|
if (size == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if ((readcnt = fread(buf, 1, size, f)) < size) {
|
2002-10-27 15:15:13 +00:00
|
|
|
if (ferror(f) && errno == EINTR && !sigint)
|
2001-07-17 21:22:32 +00:00
|
|
|
clearerr(f);
|
2012-01-18 15:13:21 +00:00
|
|
|
else if (readcnt == 0)
|
2001-07-17 21:22:32 +00:00
|
|
|
break;
|
|
|
|
}
|
2012-01-18 15:13:21 +00:00
|
|
|
|
|
|
|
stat_update(&xs, count += readcnt);
|
|
|
|
for (ptr = buf; readcnt > 0; ptr += wr, readcnt -= wr)
|
|
|
|
if ((wr = fwrite(ptr, 1, readcnt, of)) < readcnt) {
|
2002-10-27 15:15:13 +00:00
|
|
|
if (ferror(of) && errno == EINTR && !sigint)
|
2001-07-17 21:22:32 +00:00
|
|
|
clearerr(of);
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
2012-01-18 15:13:21 +00:00
|
|
|
if (readcnt != 0)
|
2001-07-17 21:22:32 +00:00
|
|
|
break;
|
|
|
|
}
|
2002-10-27 15:15:13 +00:00
|
|
|
if (!sigalrm)
|
|
|
|
sigalrm = ferror(f) && errno == ETIMEDOUT;
|
2001-07-17 21:22:32 +00:00
|
|
|
signal(SIGINFO, SIG_DFL);
|
|
|
|
|
|
|
|
stat_end(&xs);
|
|
|
|
|
2002-10-27 15:15:13 +00:00
|
|
|
/*
|
|
|
|
* If the transfer timed out or was interrupted, we still want to
|
|
|
|
* set the mtime in case the file is not removed (-r or -R) and
|
|
|
|
* the user later restarts the transfer.
|
|
|
|
*/
|
|
|
|
signal:
|
2001-07-17 21:22:32 +00:00
|
|
|
/* set mtime of local file */
|
2002-11-13 16:04:20 +00:00
|
|
|
if (!n_flag && us.mtime && !o_stdout && of != NULL &&
|
|
|
|
(stat(path, &sb) != -1) && sb.st_mode & S_IFREG) {
|
2001-07-17 21:22:32 +00:00
|
|
|
struct timeval tv[2];
|
2002-10-27 15:32:51 +00:00
|
|
|
|
2001-07-17 21:22:32 +00:00
|
|
|
fflush(of);
|
|
|
|
tv[0].tv_sec = (long)(us.atime ? us.atime : us.mtime);
|
|
|
|
tv[1].tv_sec = (long)us.mtime;
|
|
|
|
tv[0].tv_usec = tv[1].tv_usec = 0;
|
2002-02-16 14:02:57 +00:00
|
|
|
if (utimes(tmppath ? tmppath : path, tv))
|
|
|
|
warn("%s: utimes()", tmppath ? tmppath : path);
|
2001-07-17 21:22:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* timed out or interrupted? */
|
|
|
|
if (sigalrm)
|
|
|
|
warnx("transfer timed out");
|
|
|
|
if (sigint) {
|
|
|
|
warnx("transfer interrupted");
|
|
|
|
goto failure;
|
|
|
|
}
|
|
|
|
|
2002-11-07 22:16:45 +00:00
|
|
|
/* timeout / interrupt before connection completley established? */
|
|
|
|
if (f == NULL)
|
|
|
|
goto failure;
|
|
|
|
|
2001-07-17 21:22:32 +00:00
|
|
|
if (!sigalrm) {
|
|
|
|
/* check the status of our files */
|
|
|
|
if (ferror(f))
|
|
|
|
warn("%s", URL);
|
|
|
|
if (ferror(of))
|
|
|
|
warn("%s", path);
|
|
|
|
if (ferror(f) || ferror(of))
|
|
|
|
goto failure;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* did the transfer complete normally? */
|
|
|
|
if (us.size != -1 && count < us.size) {
|
2004-02-18 21:37:36 +00:00
|
|
|
warnx("%s appears to be truncated: %jd/%jd bytes",
|
|
|
|
path, (intmax_t)count, (intmax_t)us.size);
|
2022-01-24 04:17:24 +00:00
|
|
|
if(!o_stdout && a_flag && count > size_prev) {
|
2022-01-24 04:03:42 +00:00
|
|
|
fclose(f);
|
|
|
|
if (w_secs)
|
|
|
|
sleep(w_secs);
|
|
|
|
tries++;
|
|
|
|
goto again;
|
|
|
|
}
|
2001-07-17 21:22:32 +00:00
|
|
|
goto failure_keep;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the transfer timed out and we didn't know how much to
|
|
|
|
* expect, assume the worst (i.e. we didn't get all of it)
|
|
|
|
*/
|
|
|
|
if (sigalrm && us.size == -1) {
|
|
|
|
warnx("%s may be truncated", path);
|
|
|
|
goto failure_keep;
|
|
|
|
}
|
|
|
|
|
2000-06-28 16:55:15 +00:00
|
|
|
success:
|
2001-07-17 21:22:32 +00:00
|
|
|
r = 0;
|
2001-09-08 15:17:15 +00:00
|
|
|
if (tmppath != NULL && rename(tmppath, path) == -1) {
|
|
|
|
warn("%s: rename()", path);
|
|
|
|
goto failure_keep;
|
|
|
|
}
|
2001-07-17 21:22:32 +00:00
|
|
|
goto done;
|
2000-06-28 16:55:15 +00:00
|
|
|
failure:
|
2001-07-17 21:22:32 +00:00
|
|
|
if (of && of != stdout && !R_flag && !r_flag)
|
|
|
|
if (stat(path, &sb) != -1 && (sb.st_mode & S_IFREG))
|
2001-09-08 15:17:15 +00:00
|
|
|
unlink(tmppath ? tmppath : path);
|
|
|
|
if (R_flag && tmppath != NULL && sb.st_size == -1)
|
|
|
|
rename(tmppath, path); /* ignore errors here */
|
2000-07-12 20:53:07 +00:00
|
|
|
failure_keep:
|
2001-07-17 21:22:32 +00:00
|
|
|
r = -1;
|
|
|
|
goto done;
|
2000-06-28 16:55:15 +00:00
|
|
|
done:
|
2001-07-17 21:22:32 +00:00
|
|
|
if (f)
|
|
|
|
fclose(f);
|
|
|
|
if (of && of != stdout)
|
|
|
|
fclose(of);
|
|
|
|
if (url)
|
|
|
|
fetchFreeURL(url);
|
2001-09-08 15:17:15 +00:00
|
|
|
if (tmppath != NULL)
|
|
|
|
free(tmppath);
|
2004-07-27 11:30:35 +00:00
|
|
|
return (r);
|
2000-06-28 16:55:15 +00:00
|
|
|
}
|
|
|
|
|
2001-07-17 21:22:32 +00:00
|
|
|
static void
|
2000-06-28 16:55:15 +00:00
|
|
|
usage(void)
|
|
|
|
{
|
2013-07-26 15:53:43 +00:00
|
|
|
fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
|
2015-03-25 18:56:36 +00:00
|
|
|
"usage: fetch [-146AadFlMmnPpqRrsUv] [-B bytes] [--bind-address=host]",
|
|
|
|
" [--ca-cert=file] [--ca-path=dir] [--cert=file] [--crl=file]",
|
|
|
|
" [-i file] [--key=file] [-N file] [--no-passive] [--no-proxy=list]",
|
|
|
|
" [--no-sslv3] [--no-tlsv1] [--no-verify-hostname] [--no-verify-peer]",
|
|
|
|
" [-o file] [--referer=URL] [-S bytes] [-T seconds]",
|
2013-07-26 15:53:43 +00:00
|
|
|
" [--user-agent=agent-string] [-w seconds] URL ...",
|
2015-03-25 18:56:36 +00:00
|
|
|
" fetch [-146AadFlMmnPpqRrsUv] [-B bytes] [--bind-address=host]",
|
|
|
|
" [--ca-cert=file] [--ca-path=dir] [--cert=file] [--crl=file]",
|
|
|
|
" [-i file] [--key=file] [-N file] [--no-passive] [--no-proxy=list]",
|
|
|
|
" [--no-sslv3] [--no-tlsv1] [--no-verify-hostname] [--no-verify-peer]",
|
|
|
|
" [-o file] [--referer=URL] [-S bytes] [-T seconds]",
|
2013-07-26 15:53:43 +00:00
|
|
|
" [--user-agent=agent-string] [-w seconds] -h host -f file [-c dir]");
|
2000-06-28 16:55:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-08-17 22:22:45 +00:00
|
|
|
/*
|
|
|
|
* Entry point
|
|
|
|
*/
|
2000-06-28 16:55:15 +00:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
2001-07-17 21:22:32 +00:00
|
|
|
struct stat sb;
|
|
|
|
struct sigaction sa;
|
|
|
|
const char *p, *s;
|
2002-07-28 21:09:25 +00:00
|
|
|
char *end, *q;
|
2022-01-24 07:35:49 +00:00
|
|
|
int c, e, is_http, r;
|
2001-07-17 21:22:32 +00:00
|
|
|
|
2013-07-26 15:53:43 +00:00
|
|
|
|
|
|
|
while ((c = getopt_long(argc, argv,
|
|
|
|
"146AaB:bc:dFf:Hh:i:lMmN:nPpo:qRrS:sT:tUvw:",
|
|
|
|
longopts, NULL)) != -1)
|
2001-07-17 21:22:32 +00:00
|
|
|
switch (c) {
|
|
|
|
case '1':
|
|
|
|
once_flag = 1;
|
|
|
|
break;
|
|
|
|
case '4':
|
|
|
|
family = PF_INET;
|
|
|
|
break;
|
|
|
|
case '6':
|
|
|
|
family = PF_INET6;
|
|
|
|
break;
|
|
|
|
case 'A':
|
|
|
|
A_flag = 1;
|
|
|
|
break;
|
|
|
|
case 'a':
|
|
|
|
a_flag = 1;
|
|
|
|
break;
|
|
|
|
case 'B':
|
2002-07-28 21:09:25 +00:00
|
|
|
B_size = (off_t)strtol(optarg, &end, 10);
|
|
|
|
if (*optarg == '\0' || *end != '\0')
|
2001-07-28 21:34:56 +00:00
|
|
|
errx(1, "invalid buffer size (%s)", optarg);
|
2001-07-17 21:22:32 +00:00
|
|
|
break;
|
|
|
|
case 'b':
|
|
|
|
warnx("warning: the -b option is deprecated");
|
|
|
|
b_flag = 1;
|
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
c_dirname = optarg;
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
d_flag = 1;
|
|
|
|
break;
|
|
|
|
case 'F':
|
|
|
|
F_flag = 1;
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
f_filename = optarg;
|
|
|
|
break;
|
|
|
|
case 'H':
|
2002-03-26 12:09:49 +00:00
|
|
|
warnx("the -H option is now implicit, "
|
2001-07-17 21:22:32 +00:00
|
|
|
"use -U to disable");
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
h_hostname = optarg;
|
|
|
|
break;
|
2008-12-15 08:27:44 +00:00
|
|
|
case 'i':
|
|
|
|
i_flag = 1;
|
|
|
|
i_filename = optarg;
|
|
|
|
break;
|
2001-07-17 21:22:32 +00:00
|
|
|
case 'l':
|
|
|
|
l_flag = 1;
|
|
|
|
break;
|
|
|
|
case 'o':
|
|
|
|
o_flag = 1;
|
|
|
|
o_filename = optarg;
|
|
|
|
break;
|
|
|
|
case 'M':
|
|
|
|
case 'm':
|
|
|
|
if (r_flag)
|
|
|
|
errx(1, "the -m and -r flags "
|
|
|
|
"are mutually exclusive");
|
|
|
|
m_flag = 1;
|
|
|
|
break;
|
2003-01-22 18:33:39 +00:00
|
|
|
case 'N':
|
|
|
|
N_filename = optarg;
|
|
|
|
break;
|
2001-07-17 21:22:32 +00:00
|
|
|
case 'n':
|
|
|
|
n_flag = 1;
|
|
|
|
break;
|
|
|
|
case 'P':
|
|
|
|
case 'p':
|
|
|
|
p_flag = 1;
|
|
|
|
break;
|
|
|
|
case 'q':
|
|
|
|
v_level = 0;
|
|
|
|
break;
|
|
|
|
case 'R':
|
|
|
|
R_flag = 1;
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
if (m_flag)
|
|
|
|
errx(1, "the -m and -r flags "
|
|
|
|
"are mutually exclusive");
|
|
|
|
r_flag = 1;
|
|
|
|
break;
|
|
|
|
case 'S':
|
2022-02-20 14:24:43 +00:00
|
|
|
S_size = strtoll(optarg, &end, 10);
|
2002-07-28 21:09:25 +00:00
|
|
|
if (*optarg == '\0' || *end != '\0')
|
2001-07-28 21:34:56 +00:00
|
|
|
errx(1, "invalid size (%s)", optarg);
|
2001-07-17 21:22:32 +00:00
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
s_flag = 1;
|
|
|
|
break;
|
2002-10-27 15:32:51 +00:00
|
|
|
case 'T':
|
2002-07-28 21:09:25 +00:00
|
|
|
T_secs = strtol(optarg, &end, 10);
|
|
|
|
if (*optarg == '\0' || *end != '\0')
|
2001-07-28 21:34:56 +00:00
|
|
|
errx(1, "invalid timeout (%s)", optarg);
|
2001-07-17 21:22:32 +00:00
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
t_flag = 1;
|
|
|
|
warnx("warning: the -t option is deprecated");
|
|
|
|
break;
|
|
|
|
case 'U':
|
|
|
|
U_flag = 1;
|
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
v_level++;
|
|
|
|
break;
|
|
|
|
case 'w':
|
|
|
|
a_flag = 1;
|
2002-07-28 21:09:25 +00:00
|
|
|
w_secs = strtol(optarg, &end, 10);
|
|
|
|
if (*optarg == '\0' || *end != '\0')
|
2001-07-28 21:34:56 +00:00
|
|
|
errx(1, "invalid delay (%s)", optarg);
|
2001-07-17 21:22:32 +00:00
|
|
|
break;
|
2013-07-26 15:53:43 +00:00
|
|
|
case OPTION_BIND_ADDRESS:
|
|
|
|
setenv("FETCH_BIND_ADDRESS", optarg, 1);
|
|
|
|
break;
|
|
|
|
case OPTION_NO_FTP_PASSIVE_MODE:
|
|
|
|
setenv("FTP_PASSIVE_MODE", "no", 1);
|
|
|
|
break;
|
|
|
|
case OPTION_HTTP_REFERER:
|
|
|
|
setenv("HTTP_REFERER", optarg, 1);
|
|
|
|
break;
|
|
|
|
case OPTION_HTTP_USER_AGENT:
|
|
|
|
setenv("HTTP_USER_AGENT", optarg, 1);
|
|
|
|
break;
|
|
|
|
case OPTION_NO_PROXY:
|
|
|
|
setenv("NO_PROXY", optarg, 1);
|
|
|
|
break;
|
|
|
|
case OPTION_SSL_CA_CERT_FILE:
|
|
|
|
setenv("SSL_CA_CERT_FILE", optarg, 1);
|
|
|
|
break;
|
|
|
|
case OPTION_SSL_CA_CERT_PATH:
|
|
|
|
setenv("SSL_CA_CERT_PATH", optarg, 1);
|
|
|
|
break;
|
|
|
|
case OPTION_SSL_CLIENT_CERT_FILE:
|
|
|
|
setenv("SSL_CLIENT_CERT_FILE", optarg, 1);
|
|
|
|
break;
|
|
|
|
case OPTION_SSL_CLIENT_KEY_FILE:
|
|
|
|
setenv("SSL_CLIENT_KEY_FILE", optarg, 1);
|
|
|
|
break;
|
|
|
|
case OPTION_SSL_CRL_FILE:
|
|
|
|
setenv("SSL_CLIENT_CRL_FILE", optarg, 1);
|
|
|
|
break;
|
|
|
|
case OPTION_SSL_NO_SSL3:
|
|
|
|
setenv("SSL_NO_SSL3", "", 1);
|
|
|
|
break;
|
|
|
|
case OPTION_SSL_NO_TLS1:
|
|
|
|
setenv("SSL_NO_TLS1", "", 1);
|
|
|
|
break;
|
|
|
|
case OPTION_SSL_NO_VERIFY_HOSTNAME:
|
|
|
|
setenv("SSL_NO_VERIFY_HOSTNAME", "", 1);
|
|
|
|
break;
|
|
|
|
case OPTION_SSL_NO_VERIFY_PEER:
|
|
|
|
setenv("SSL_NO_VERIFY_PEER", "", 1);
|
|
|
|
break;
|
2001-07-17 21:22:32 +00:00
|
|
|
default:
|
|
|
|
usage();
|
2008-12-17 18:00:18 +00:00
|
|
|
exit(1);
|
2001-07-17 21:22:32 +00:00
|
|
|
}
|
2000-06-28 16:55:15 +00:00
|
|
|
|
2001-07-17 21:22:32 +00:00
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
2000-06-28 16:55:15 +00:00
|
|
|
|
2001-07-17 21:22:32 +00:00
|
|
|
if (h_hostname || f_filename || c_dirname) {
|
|
|
|
if (!h_hostname || !f_filename || argc) {
|
|
|
|
usage();
|
2008-12-17 18:00:18 +00:00
|
|
|
exit(1);
|
2001-07-17 21:22:32 +00:00
|
|
|
}
|
|
|
|
/* XXX this is a hack. */
|
|
|
|
if (strcspn(h_hostname, "@:/") != strlen(h_hostname))
|
|
|
|
errx(1, "invalid hostname");
|
|
|
|
if (asprintf(argv, "ftp://%s/%s/%s", h_hostname,
|
|
|
|
c_dirname ? c_dirname : "", f_filename) == -1)
|
|
|
|
errx(1, "%s", strerror(ENOMEM));
|
|
|
|
argc++;
|
2000-06-28 16:55:15 +00:00
|
|
|
}
|
2001-07-17 21:22:32 +00:00
|
|
|
|
|
|
|
if (!argc) {
|
|
|
|
usage();
|
2008-12-17 18:00:18 +00:00
|
|
|
exit(1);
|
2000-06-28 16:55:15 +00:00
|
|
|
}
|
2001-07-17 21:22:32 +00:00
|
|
|
|
|
|
|
/* allocate buffer */
|
|
|
|
if (B_size < MINBUFSIZE)
|
|
|
|
B_size = MINBUFSIZE;
|
|
|
|
if ((buf = malloc(B_size)) == NULL)
|
|
|
|
errx(1, "%s", strerror(ENOMEM));
|
|
|
|
|
|
|
|
/* timeouts */
|
|
|
|
if ((s = getenv("FTP_TIMEOUT")) != NULL) {
|
2002-07-28 21:09:25 +00:00
|
|
|
ftp_timeout = strtol(s, &end, 10);
|
2002-08-27 10:19:24 +00:00
|
|
|
if (*s == '\0' || *end != '\0' || ftp_timeout < 0) {
|
|
|
|
warnx("FTP_TIMEOUT (%s) is not a positive integer", s);
|
2001-07-17 21:22:32 +00:00
|
|
|
ftp_timeout = 0;
|
|
|
|
}
|
2000-06-28 16:55:15 +00:00
|
|
|
}
|
2001-07-17 21:22:32 +00:00
|
|
|
if ((s = getenv("HTTP_TIMEOUT")) != NULL) {
|
2002-07-28 21:09:25 +00:00
|
|
|
http_timeout = strtol(s, &end, 10);
|
2002-08-27 10:19:24 +00:00
|
|
|
if (*s == '\0' || *end != '\0' || http_timeout < 0) {
|
|
|
|
warnx("HTTP_TIMEOUT (%s) is not a positive integer", s);
|
2001-07-17 21:22:32 +00:00
|
|
|
http_timeout = 0;
|
|
|
|
}
|
2000-06-28 16:55:15 +00:00
|
|
|
}
|
|
|
|
|
2001-07-17 21:22:32 +00:00
|
|
|
/* signal handling */
|
|
|
|
sa.sa_flags = 0;
|
|
|
|
sa.sa_handler = sig_handler;
|
|
|
|
sigemptyset(&sa.sa_mask);
|
|
|
|
sigaction(SIGALRM, &sa, NULL);
|
|
|
|
sa.sa_flags = SA_RESETHAND;
|
|
|
|
sigaction(SIGINT, &sa, NULL);
|
|
|
|
fetchRestartCalls = 0;
|
|
|
|
|
|
|
|
/* output file */
|
2000-06-28 16:55:15 +00:00
|
|
|
if (o_flag) {
|
2001-07-17 21:22:32 +00:00
|
|
|
if (strcmp(o_filename, "-") == 0) {
|
|
|
|
o_stdout = 1;
|
|
|
|
} else if (stat(o_filename, &sb) == -1) {
|
|
|
|
if (errno == ENOENT) {
|
|
|
|
if (argc > 1)
|
2008-12-17 18:00:18 +00:00
|
|
|
errx(1, "%s is not a directory",
|
2001-07-17 21:22:32 +00:00
|
|
|
o_filename);
|
|
|
|
} else {
|
2008-12-17 18:00:18 +00:00
|
|
|
err(1, "%s", o_filename);
|
2001-07-17 21:22:32 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (sb.st_mode & S_IFDIR)
|
|
|
|
o_directory = 1;
|
|
|
|
}
|
2000-06-28 16:55:15 +00:00
|
|
|
}
|
|
|
|
|
2001-07-17 21:22:32 +00:00
|
|
|
/* check if output is to a tty (for progress report) */
|
|
|
|
v_tty = isatty(STDERR_FILENO);
|
2018-09-10 19:39:20 +00:00
|
|
|
v_progress = v_tty && v_level > 0;
|
|
|
|
if (v_progress)
|
2001-09-23 22:03:39 +00:00
|
|
|
pgrp = getpgrp();
|
2002-10-27 15:32:51 +00:00
|
|
|
|
2001-07-17 21:22:32 +00:00
|
|
|
r = 0;
|
2002-10-27 15:32:51 +00:00
|
|
|
|
2001-07-17 21:22:32 +00:00
|
|
|
/* authentication */
|
|
|
|
if (v_tty)
|
|
|
|
fetchAuthMethod = query_auth;
|
2003-01-22 18:33:39 +00:00
|
|
|
if (N_filename != NULL)
|
2012-12-08 18:41:16 +00:00
|
|
|
if (setenv("NETRC", N_filename, 1) == -1)
|
|
|
|
err(1, "setenv: cannot set NETRC=%s", N_filename);
|
2001-07-17 21:22:32 +00:00
|
|
|
|
|
|
|
while (argc) {
|
|
|
|
if ((p = strrchr(*argv, '/')) == NULL)
|
|
|
|
p = *argv;
|
|
|
|
else
|
|
|
|
p++;
|
|
|
|
|
|
|
|
if (!*p)
|
|
|
|
p = "fetch.out";
|
2002-10-27 15:32:51 +00:00
|
|
|
|
2001-07-17 21:22:32 +00:00
|
|
|
fetchLastErrCode = 0;
|
2002-10-27 15:32:51 +00:00
|
|
|
|
2001-07-17 21:22:32 +00:00
|
|
|
if (o_flag) {
|
|
|
|
if (o_stdout) {
|
2022-01-24 07:35:49 +00:00
|
|
|
e = fetch(*argv, "-", &is_http);
|
2001-07-17 21:22:32 +00:00
|
|
|
} else if (o_directory) {
|
|
|
|
asprintf(&q, "%s/%s", o_filename, p);
|
2022-01-24 07:35:49 +00:00
|
|
|
e = fetch(*argv, q, &is_http);
|
2001-07-17 21:22:32 +00:00
|
|
|
free(q);
|
|
|
|
} else {
|
2022-01-24 07:35:49 +00:00
|
|
|
e = fetch(*argv, o_filename, &is_http);
|
2001-07-17 21:22:32 +00:00
|
|
|
}
|
|
|
|
} else {
|
2022-01-24 07:35:49 +00:00
|
|
|
e = fetch(*argv, p, &is_http);
|
2000-06-28 16:55:15 +00:00
|
|
|
}
|
2001-07-17 21:22:32 +00:00
|
|
|
|
|
|
|
if (sigint)
|
|
|
|
kill(getpid(), SIGINT);
|
2002-10-27 15:32:51 +00:00
|
|
|
|
2001-07-17 21:22:32 +00:00
|
|
|
if (e == 0 && once_flag)
|
|
|
|
exit(0);
|
2002-10-27 15:32:51 +00:00
|
|
|
|
2001-07-17 21:22:32 +00:00
|
|
|
if (e) {
|
|
|
|
r = 1;
|
|
|
|
if ((fetchLastErrCode
|
2022-01-25 05:46:07 +00:00
|
|
|
&& fetchLastErrCode != FETCH_AUTH
|
2001-07-17 21:22:32 +00:00
|
|
|
&& fetchLastErrCode != FETCH_UNAVAIL
|
|
|
|
&& fetchLastErrCode != FETCH_MOVED
|
|
|
|
&& fetchLastErrCode != FETCH_URL
|
|
|
|
&& fetchLastErrCode != FETCH_RESOLV
|
2022-01-24 07:35:49 +00:00
|
|
|
&& fetchLastErrCode != FETCH_UNKNOWN
|
2022-01-25 05:46:07 +00:00
|
|
|
&& (!is_http || (
|
|
|
|
fetchLastErrCode != FETCH_PROTO
|
2022-01-24 07:35:49 +00:00
|
|
|
&& fetchLastErrCode != FETCH_SERVER
|
|
|
|
&& fetchLastErrCode != FETCH_TEMP
|
|
|
|
&& fetchLastErrCode != FETCH_TIMEOUT
|
2022-01-25 05:46:07 +00:00
|
|
|
)))) {
|
2001-07-17 21:22:32 +00:00
|
|
|
if (w_secs && v_level)
|
2002-07-28 21:09:25 +00:00
|
|
|
fprintf(stderr, "Waiting %ld seconds "
|
2001-07-17 21:22:32 +00:00
|
|
|
"before retrying\n", w_secs);
|
|
|
|
if (w_secs)
|
|
|
|
sleep(w_secs);
|
|
|
|
if (a_flag)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
argc--, argv++;
|
2000-06-28 16:55:15 +00:00
|
|
|
}
|
|
|
|
|
2001-07-17 21:22:32 +00:00
|
|
|
exit(r);
|
2000-06-28 16:55:15 +00:00
|
|
|
}
|