2 * Copyright (c) 2000-2011 Dag-Erling Smørgrav
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer
10 * in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/socket.h>
50 #define MINBUFSIZE 4096
54 int A_flag; /* -A: do not follow 302 redirects */
55 int a_flag; /* -a: auto retry */
56 off_t B_size; /* -B: buffer size */
57 int b_flag; /*! -b: workaround TCP bug */
58 char *c_dirname; /* -c: remote directory */
59 int d_flag; /* -d: direct connection */
60 int F_flag; /* -F: restart without checking mtime */
61 char *f_filename; /* -f: file to fetch */
62 char *h_hostname; /* -h: host to fetch from */
63 int i_flag; /* -i: specify input file for mtime comparison */
64 char *i_filename; /* name of input file */
65 int l_flag; /* -l: link rather than copy file: URLs */
66 int m_flag; /* -[Mm]: mirror mode */
67 char *N_filename; /* -N: netrc file name */
68 int n_flag; /* -n: do not preserve modification time */
69 int o_flag; /* -o: specify output file */
70 int o_directory; /* output file is a directory */
71 char *o_filename; /* name of output file */
72 int o_stdout; /* output file is stdout */
73 int once_flag; /* -1: stop at first successful file */
74 int p_flag; /* -[Pp]: use passive FTP */
75 int R_flag; /* -R: don't delete partially transferred files */
76 int r_flag; /* -r: restart previously interrupted transfer */
77 off_t S_size; /* -S: require size to match */
78 int s_flag; /* -s: show size, don't fetch */
79 long T_secs; /* -T: transfer timeout in seconds */
80 int t_flag; /*! -t: workaround TCP bug */
81 int U_flag; /* -U: do not use high ports */
82 int v_level = 1; /* -v: verbosity level */
83 int v_tty; /* stdout is a tty */
84 pid_t pgrp; /* our process group */
85 long w_secs; /* -w: retry delay */
86 int family = PF_UNSPEC; /* -[46]: address family to use */
88 int sigalrm; /* SIGALRM received */
89 int siginfo; /* SIGINFO received */
90 int sigint; /* SIGINT received */
92 long ftp_timeout = TIMEOUT; /* default timeout for FTP transfers */
93 long http_timeout = TIMEOUT; /* default timeout for HTTP transfers */
94 char *buf; /* transfer buffer */
118 struct timeval start; /* start of transfer */
119 struct timeval last; /* time of last update */
120 struct timeval last2; /* time of previous last update */
121 off_t size; /* size of file per HTTP hdr */
122 off_t offset; /* starting offset in file */
123 off_t rcvd; /* bytes already received */
124 off_t lastrcvd; /* bytes received since last update */
128 * Compute and display ETA
131 stat_eta(struct xferstat *xs)
135 off_t received, expected;
137 elapsed = xs->last.tv_sec - xs->start.tv_sec;
138 received = xs->rcvd - xs->offset;
139 expected = xs->size - xs->rcvd;
140 eta = (long)((double)elapsed * expected / received);
142 snprintf(str, sizeof str, "%02ldh%02ldm",
143 eta / 3600, (eta % 3600) / 60);
145 snprintf(str, sizeof str, "%02ldm%02lds",
148 snprintf(str, sizeof str, "%02ldm%02lds",
149 elapsed / 60, elapsed % 60);
154 * Format a number as "xxxx YB" where Y is ' ', 'k', 'M'...
156 static const char *prefixes = " kMGTP";
158 stat_bytes(off_t bytes)
161 const char *prefix = prefixes;
163 while (bytes > 9999 && prefix[1] != '\0') {
167 snprintf(str, sizeof str, "%4jd %cB", (intmax_t)bytes, *prefix);
172 * Compute and display transfer rate
175 stat_bps(struct xferstat *xs)
180 delta = (xs->last.tv_sec + (xs->last.tv_usec / 1.e6))
181 - (xs->last2.tv_sec + (xs->last2.tv_usec / 1.e6));
184 snprintf(str, sizeof str, "?? Bps");
186 bps = (xs->rcvd - xs->lastrcvd) / delta;
187 snprintf(str, sizeof str, "%sps", stat_bytes((off_t)bps));
193 * Update the stats display
196 stat_display(struct xferstat *xs, int force)
201 /* check if we're the foreground process */
202 if (ioctl(STDERR_FILENO, TIOCGPGRP, &ctty_pgrp) == -1 ||
203 (pid_t)ctty_pgrp != pgrp)
206 gettimeofday(&now, NULL);
207 if (!force && now.tv_sec <= xs->last.tv_sec)
209 xs->last2 = xs->last;
212 fprintf(stderr, "\r%-46.46s", xs->name);
214 setproctitle("%s [%s]", xs->name, stat_bytes(xs->rcvd));
215 fprintf(stderr, " %s", stat_bytes(xs->rcvd));
217 setproctitle("%s [%d%% of %s]", xs->name,
218 (int)((100.0 * xs->rcvd) / xs->size),
219 stat_bytes(xs->size));
220 fprintf(stderr, "%3d%% of %s",
221 (int)((100.0 * xs->rcvd) / xs->size),
222 stat_bytes(xs->size));
225 xs->lastrcvd = xs->offset;
226 xs->last2 = xs->start;
228 fprintf(stderr, " %s", stat_bps(xs));
229 if ((xs->size > 0 && xs->rcvd > 0 &&
230 xs->last.tv_sec >= xs->start.tv_sec + 3) ||
232 fprintf(stderr, " %s", stat_eta(xs));
233 xs->lastrcvd = xs->rcvd;
237 * Initialize the transfer statistics
240 stat_start(struct xferstat *xs, const char *name, off_t size, off_t offset)
242 snprintf(xs->name, sizeof xs->name, "%s", name);
243 gettimeofday(&xs->start, NULL);
244 xs->last.tv_sec = xs->last.tv_usec = 0;
248 xs->lastrcvd = offset;
249 if (v_tty && v_level > 0)
251 else if (v_level > 0)
252 fprintf(stderr, "%-46s", xs->name);
256 * Update the transfer statistics
259 stat_update(struct xferstat *xs, off_t rcvd)
262 if (v_tty && v_level > 0)
267 * Finalize the transfer statistics
270 stat_end(struct xferstat *xs)
272 gettimeofday(&xs->last, NULL);
273 if (v_tty && v_level > 0) {
276 } else if (v_level > 0) {
277 fprintf(stderr, " %s %s\n",
278 stat_bytes(xs->size), stat_bps(xs));
283 * Ask the user for authentication details
286 query_auth(struct url *URL)
289 tcflag_t saved_flags;
292 fprintf(stderr, "Authentication required for <%s://%s:%d/>!\n",
293 URL->scheme, URL->host, URL->port);
295 fprintf(stderr, "Login: ");
296 if (fgets(URL->user, sizeof URL->user, stdin) == NULL)
298 for (i = strlen(URL->user); i >= 0; --i)
299 if (URL->user[i] == '\r' || URL->user[i] == '\n')
302 fprintf(stderr, "Password: ");
303 if (tcgetattr(STDIN_FILENO, &tios) == 0) {
304 saved_flags = tios.c_lflag;
305 tios.c_lflag &= ~ECHO;
306 tios.c_lflag |= ECHONL|ICANON;
307 tcsetattr(STDIN_FILENO, TCSAFLUSH|TCSASOFT, &tios);
308 nopwd = (fgets(URL->pwd, sizeof URL->pwd, stdin) == NULL);
309 tios.c_lflag = saved_flags;
310 tcsetattr(STDIN_FILENO, TCSANOW|TCSASOFT, &tios);
312 nopwd = (fgets(URL->pwd, sizeof URL->pwd, stdin) == NULL);
316 for (i = strlen(URL->pwd); i >= 0; --i)
317 if (URL->pwd[i] == '\r' || URL->pwd[i] == '\n')
327 fetch(char *URL, const char *path)
334 size_t size, readcnt, wr;
350 /* set verbosity level */
362 if ((url = fetchParseURL(URL)) == NULL) {
363 warnx("%s: parse error", URL);
367 /* if no scheme was specified, take a guess */
370 strcpy(url->scheme, SCHEME_FILE);
371 else if (strncasecmp(url->host, "ftp.", 4) == 0)
372 strcpy(url->scheme, SCHEME_FTP);
373 else if (strncasecmp(url->host, "www.", 4) == 0)
374 strcpy(url->scheme, SCHEME_HTTP);
387 /* FTP specific flags */
388 if (strcmp(url->scheme, SCHEME_FTP) == 0) {
395 timeout = T_secs ? T_secs : ftp_timeout;
398 /* HTTP specific flags */
399 if (strcmp(url->scheme, SCHEME_HTTP) == 0 ||
400 strcmp(url->scheme, SCHEME_HTTPS) == 0) {
405 timeout = T_secs ? T_secs : http_timeout;
407 if (stat(i_filename, &sb)) {
408 warn("%s: stat()", i_filename);
411 url->ims_time = sb.st_mtime;
416 /* set the protocol timeout. */
417 fetchTimeout = timeout;
419 /* just print size */
423 r = fetchStat(url, &us, flags);
426 if (sigalrm || sigint)
429 warnx("%s", fetchLastErrString);
435 printf("%jd\n", (intmax_t)us.size);
440 * If the -r flag was specified, we have to compare the local
441 * and remote files, so we should really do a fetchStat()
442 * first, but I know of at least one HTTP server that only
443 * sends the content size in response to GET requests, and
444 * leaves it out of replies to HEAD requests. Also, in the
445 * (frequent) case that the local and remote files match but
446 * the local file is truncated, we have sufficient information
447 * before the compare to issue a correct request. Therefore,
448 * we always issue a GET request as if we were sure the local
449 * file was a truncated copy of the remote file; we can drop
450 * the connection later if we change our minds.
455 if (r == 0 && r_flag && S_ISREG(sb.st_mode)) {
456 url->offset = sb.st_size;
457 } else if (r == -1 || !S_ISREG(sb.st_mode)) {
459 * Whatever value sb.st_size has now is either
460 * wrong (if stat(2) failed) or irrelevant (if the
461 * path does not refer to a regular file)
465 if (r == -1 && errno != ENOENT) {
466 warnx("%s: stat()", path);
471 /* start the transfer */
474 f = fetchXGet(url, &us, flags);
477 if (sigalrm || sigint)
480 warnx("%s: %s", URL, fetchLastErrString);
481 if (i_flag && strcmp(url->scheme, SCHEME_HTTP) == 0
482 && fetchLastErrCode == FETCH_OK
483 && strcmp(fetchLastErrString, "Not Modified") == 0) {
484 /* HTTP Not Modified Response, return OK. */
493 /* check that size is as expected */
496 warnx("%s: size unknown", URL);
497 } else if (us.size != S_size) {
498 warnx("%s: size mismatch: expected %jd, actual %jd",
499 URL, (intmax_t)S_size, (intmax_t)us.size);
504 /* symlink instead of copy */
505 if (l_flag && strcmp(url->scheme, "file") == 0 && !o_stdout) {
506 if (symlink(url->doc, path) == -1) {
507 warn("%s: symlink()", path);
513 if (us.size == -1 && !o_stdout && v_level > 0)
514 warnx("%s: size of remote file is not known", URL);
516 if (sb.st_size != -1)
517 fprintf(stderr, "local size / mtime: %jd / %ld\n",
518 (intmax_t)sb.st_size, (long)sb.st_mtime);
520 fprintf(stderr, "remote size / mtime: %jd / %ld\n",
521 (intmax_t)us.size, (long)us.mtime);
524 /* open output file */
526 /* output to stdout */
528 } else if (r_flag && sb.st_size != -1) {
529 /* resume mode, local file exists */
530 if (!F_flag && us.mtime && sb.st_mtime != us.mtime) {
531 /* no match! have to refetch */
533 /* if precious, warn the user and give up */
535 warnx("%s: local modification time "
536 "does not match remote", path);
539 } else if (url->offset > sb.st_size) {
540 /* gap between what we asked for and what we got */
541 warnx("%s: gap in resume mode", URL);
544 /* picked up again later */
545 } else if (us.size != -1) {
546 if (us.size == sb.st_size)
549 if (sb.st_size > us.size) {
550 /* local file too long! */
551 warnx("%s: local file (%jd bytes) is longer "
552 "than remote file (%jd bytes)", path,
553 (intmax_t)sb.st_size, (intmax_t)us.size);
556 /* we got it, open local file */
557 if ((of = fopen(path, "r+")) == NULL) {
558 warn("%s: fopen()", path);
561 /* check that it didn't move under our feet */
562 if (fstat(fileno(of), &nsb) == -1) {
564 warn("%s: fstat()", path);
567 if (nsb.st_dev != sb.st_dev ||
568 nsb.st_ino != nsb.st_ino ||
569 nsb.st_size != sb.st_size) {
570 warnx("%s: file has changed", URL);
574 /* picked up again later */
577 /* seek to where we left off */
578 if (of != NULL && fseeko(of, url->offset, SEEK_SET) != 0) {
579 warn("%s: fseeko()", path);
582 /* picked up again later */
584 } else if (m_flag && sb.st_size != -1) {
585 /* mirror mode, local file exists */
586 if (sb.st_size == us.size && sb.st_mtime == us.mtime)
592 * We don't yet have an output file; either this is a
593 * vanilla run with no special flags, or the local and
594 * remote files didn't match.
597 if (url->offset > 0) {
599 * We tried to restart a transfer, but for
600 * some reason gave up - so we have to restart
601 * from scratch if we want the whole file
604 if ((f = fetchXGet(url, &us, flags)) == NULL) {
605 warnx("%s: %s", URL, fetchLastErrString);
612 /* construct a temp file name */
613 if (sb.st_size != -1 && S_ISREG(sb.st_mode)) {
614 if ((slash = strrchr(path, '/')) == NULL)
618 asprintf(&tmppath, "%.*s.fetch.XXXXXX.%s",
619 (int)(slash - path), path, slash);
620 if (tmppath != NULL) {
621 if (mkstemps(tmppath, strlen(slash) + 1) == -1) {
622 warn("%s: mkstemps()", path);
625 of = fopen(tmppath, "w");
626 chown(tmppath, sb.st_uid, sb.st_gid);
627 chmod(tmppath, sb.st_mode & ALLPERMS);
631 of = fopen(path, "w");
633 warn("%s: open()", path);
639 /* start the counter */
640 stat_start(&xs, path, us.size, count);
642 sigalrm = siginfo = sigint = 0;
644 /* suck in the data */
645 signal(SIGINFO, sig_handler);
647 if (us.size != -1 && us.size - count < B_size &&
648 us.size - count >= 0)
649 size = us.size - count;
660 if ((readcnt = fread(buf, 1, size, f)) < size) {
661 if (ferror(f) && errno == EINTR && !sigint)
663 else if (readcnt == 0)
667 stat_update(&xs, count += readcnt);
668 for (ptr = buf; readcnt > 0; ptr += wr, readcnt -= wr)
669 if ((wr = fwrite(ptr, 1, readcnt, of)) < readcnt) {
670 if (ferror(of) && errno == EINTR && !sigint)
679 sigalrm = ferror(f) && errno == ETIMEDOUT;
680 signal(SIGINFO, SIG_DFL);
685 * If the transfer timed out or was interrupted, we still want to
686 * set the mtime in case the file is not removed (-r or -R) and
687 * the user later restarts the transfer.
690 /* set mtime of local file */
691 if (!n_flag && us.mtime && !o_stdout && of != NULL &&
692 (stat(path, &sb) != -1) && sb.st_mode & S_IFREG) {
693 struct timeval tv[2];
696 tv[0].tv_sec = (long)(us.atime ? us.atime : us.mtime);
697 tv[1].tv_sec = (long)us.mtime;
698 tv[0].tv_usec = tv[1].tv_usec = 0;
699 if (utimes(tmppath ? tmppath : path, tv))
700 warn("%s: utimes()", tmppath ? tmppath : path);
703 /* timed out or interrupted? */
705 warnx("transfer timed out");
707 warnx("transfer interrupted");
711 /* timeout / interrupt before connection completley established? */
716 /* check the status of our files */
721 if (ferror(f) || ferror(of))
725 /* did the transfer complete normally? */
726 if (us.size != -1 && count < us.size) {
727 warnx("%s appears to be truncated: %jd/%jd bytes",
728 path, (intmax_t)count, (intmax_t)us.size);
733 * If the transfer timed out and we didn't know how much to
734 * expect, assume the worst (i.e. we didn't get all of it)
736 if (sigalrm && us.size == -1) {
737 warnx("%s may be truncated", path);
743 if (tmppath != NULL && rename(tmppath, path) == -1) {
744 warn("%s: rename()", path);
749 if (of && of != stdout && !R_flag && !r_flag)
750 if (stat(path, &sb) != -1 && (sb.st_mode & S_IFREG))
751 unlink(tmppath ? tmppath : path);
752 if (R_flag && tmppath != NULL && sb.st_size == -1)
753 rename(tmppath, path); /* ignore errors here */
760 if (of && of != stdout)
772 fprintf(stderr, "%s\n%s\n%s\n%s\n",
773 "usage: fetch [-146AadFlMmnPpqRrsUv] [-B bytes] [-N file] [-o file] [-S bytes]",
774 " [-T seconds] [-w seconds] [-i file] URL ...",
775 " fetch [-146AadFlMmnPpqRrsUv] [-B bytes] [-N file] [-o file] [-S bytes]",
776 " [-T seconds] [-w seconds] [-i file] -h host -f file [-c dir]");
784 main(int argc, char *argv[])
792 while ((c = getopt(argc, argv,
793 "146AaB:bc:dFf:Hh:i:lMmN:nPpo:qRrS:sT:tUvw:")) != -1)
811 B_size = (off_t)strtol(optarg, &end, 10);
812 if (*optarg == '\0' || *end != '\0')
813 errx(1, "invalid buffer size (%s)", optarg);
816 warnx("warning: the -b option is deprecated");
832 warnx("the -H option is now implicit, "
833 "use -U to disable");
852 errx(1, "the -m and -r flags "
853 "are mutually exclusive");
874 errx(1, "the -m and -r flags "
875 "are mutually exclusive");
879 S_size = (off_t)strtol(optarg, &end, 10);
880 if (*optarg == '\0' || *end != '\0')
881 errx(1, "invalid size (%s)", optarg);
887 T_secs = strtol(optarg, &end, 10);
888 if (*optarg == '\0' || *end != '\0')
889 errx(1, "invalid timeout (%s)", optarg);
893 warnx("warning: the -t option is deprecated");
903 w_secs = strtol(optarg, &end, 10);
904 if (*optarg == '\0' || *end != '\0')
905 errx(1, "invalid delay (%s)", optarg);
915 if (h_hostname || f_filename || c_dirname) {
916 if (!h_hostname || !f_filename || argc) {
920 /* XXX this is a hack. */
921 if (strcspn(h_hostname, "@:/") != strlen(h_hostname))
922 errx(1, "invalid hostname");
923 if (asprintf(argv, "ftp://%s/%s/%s", h_hostname,
924 c_dirname ? c_dirname : "", f_filename) == -1)
925 errx(1, "%s", strerror(ENOMEM));
934 /* allocate buffer */
935 if (B_size < MINBUFSIZE)
937 if ((buf = malloc(B_size)) == NULL)
938 errx(1, "%s", strerror(ENOMEM));
941 if ((s = getenv("FTP_TIMEOUT")) != NULL) {
942 ftp_timeout = strtol(s, &end, 10);
943 if (*s == '\0' || *end != '\0' || ftp_timeout < 0) {
944 warnx("FTP_TIMEOUT (%s) is not a positive integer", s);
948 if ((s = getenv("HTTP_TIMEOUT")) != NULL) {
949 http_timeout = strtol(s, &end, 10);
950 if (*s == '\0' || *end != '\0' || http_timeout < 0) {
951 warnx("HTTP_TIMEOUT (%s) is not a positive integer", s);
956 /* signal handling */
958 sa.sa_handler = sig_handler;
959 sigemptyset(&sa.sa_mask);
960 sigaction(SIGALRM, &sa, NULL);
961 sa.sa_flags = SA_RESETHAND;
962 sigaction(SIGINT, &sa, NULL);
963 fetchRestartCalls = 0;
967 if (strcmp(o_filename, "-") == 0) {
969 } else if (stat(o_filename, &sb) == -1) {
970 if (errno == ENOENT) {
972 errx(1, "%s is not a directory",
975 err(1, "%s", o_filename);
978 if (sb.st_mode & S_IFDIR)
983 /* check if output is to a tty (for progress report) */
984 v_tty = isatty(STDERR_FILENO);
992 fetchAuthMethod = query_auth;
993 if (N_filename != NULL)
994 if (setenv("NETRC", N_filename, 1) == -1)
995 err(1, "setenv: cannot set NETRC=%s", N_filename);
998 if ((p = strrchr(*argv, '/')) == NULL)
1006 fetchLastErrCode = 0;
1010 e = fetch(*argv, "-");
1011 } else if (o_directory) {
1012 asprintf(&q, "%s/%s", o_filename, p);
1013 e = fetch(*argv, q);
1016 e = fetch(*argv, o_filename);
1019 e = fetch(*argv, p);
1023 kill(getpid(), SIGINT);
1025 if (e == 0 && once_flag)
1030 if ((fetchLastErrCode
1031 && fetchLastErrCode != FETCH_UNAVAIL
1032 && fetchLastErrCode != FETCH_MOVED
1033 && fetchLastErrCode != FETCH_URL
1034 && fetchLastErrCode != FETCH_RESOLV
1035 && fetchLastErrCode != FETCH_UNKNOWN)) {
1036 if (w_secs && v_level)
1037 fprintf(stderr, "Waiting %ld seconds "
1038 "before retrying\n", w_secs);