1 /*- 2 * Copyright (c) 2000 Dag-Erling Co�dan Sm�rgrav 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 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 16 * 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. 27 * 28 * $FreeBSD$ 29 */ 30 31 #include <sys/param.h> 32 #include <sys/stat.h> 33 #include <sys/socket.h> 34 35 #include <ctype.h> 36 #include <err.h> 37 #include <errno.h> 38 #include <signal.h> 39 #include <stdio.h> 40 #include <stdlib.h> 41 #include <string.h> 42 #include <sysexits.h> 43 #include <unistd.h> 44 45 #include <fetch.h> 46 47 #define MINBUFSIZE 4096 48 49 /* Option flags */ 50 int A_flag; /* -A: do not follow 302 redirects */ 51 int a_flag; /* -a: auto retry */ 52 size_t B_size; /* -B: buffer size */ 53 int b_flag; /*! -b: workaround TCP bug */ 54 char *c_dirname; /* -c: remote directory */ 55 int d_flag; /* -d: direct connection */ 56 int F_flag; /* -F: restart without checking mtime */ 57 char *f_filename; /* -f: file to fetch */ 58 int H_flag; /* -H: use high port */ 59 char *h_hostname; /* -h: host to fetch from */ 60 int l_flag; /* -l: link rather than copy file: URLs */ 61 int m_flag; /* -[Mm]: mirror mode */ 62 int n_flag; /* -n: do not preserve modification time */ 63 int o_flag; /* -o: specify output file */ 64 int o_directory; /* output file is a directory */ 65 char *o_filename; /* name of output file */ 66 int o_stdout; /* output file is stdout */ 67 int once_flag; /* -1: stop at first successful file */ 68 int p_flag; /* -[Pp]: use passive FTP */ 69 int R_flag; /* -R: don't delete partially transferred files */ 70 int r_flag; /* -r: restart previously interrupted transfer */ 71 u_int T_secs = 0; /* -T: transfer timeout in seconds */ 72 int s_flag; /* -s: show size, don't fetch */ 73 off_t S_size; /* -S: require size to match */ 74 int t_flag; /*! -t: workaround TCP bug */ 75 int v_level = 1; /* -v: verbosity level */ 76 int v_tty; /* stdout is a tty */ 77 u_int w_secs; /* -w: retry delay */ 78 int family = PF_UNSPEC; /* -[46]: address family to use */ 79 80 int sigalrm; /* SIGALRM received */ 81 int sigint; /* SIGINT received */ 82 83 u_int ftp_timeout; /* default timeout for FTP transfers */ 84 u_int http_timeout; /* default timeout for HTTP transfers */ 85 u_char *buf; /* transfer buffer */ 86 87 88 void 89 sig_handler(int sig) 90 { 91 switch (sig) { 92 case SIGALRM: 93 sigalrm = 1; 94 break; 95 case SIGINT: 96 sigint = 1; 97 break; 98 } 99 } 100 101 struct xferstat { 102 char name[40]; 103 struct timeval start; 104 struct timeval end; 105 struct timeval last; 106 off_t size; 107 off_t offset; 108 off_t rcvd; 109 }; 110 111 void 112 stat_display(struct xferstat *xs, int force) 113 { 114 struct timeval now; 115 116 if (!v_tty) 117 return; 118 119 gettimeofday(&now, NULL); 120 if (!force && now.tv_sec <= xs->last.tv_sec) 121 return; 122 xs->last = now; 123 124 fprintf(stderr, "\rReceiving %s", xs->name); 125 if (xs->size == -1) 126 fprintf(stderr, ": %lld bytes", xs->rcvd); 127 else 128 fprintf(stderr, " (%lld bytes): %d%%", xs->size, 129 (int)((100.0 * xs->rcvd) / xs->size)); 130 } 131 132 void 133 stat_start(struct xferstat *xs, char *name, off_t size, off_t offset) 134 { 135 snprintf(xs->name, sizeof xs->name, "%s", name); 136 gettimeofday(&xs->start, NULL); 137 xs->last.tv_sec = xs->last.tv_usec = 0; 138 xs->end = xs->last; 139 xs->size = size; 140 xs->offset = offset; 141 xs->rcvd = offset; 142 stat_display(xs, 1); 143 } 144 145 void 146 stat_update(struct xferstat *xs, off_t rcvd, int force) 147 { 148 xs->rcvd = rcvd; 149 stat_display(xs, 0); 150 } 151 152 void 153 stat_end(struct xferstat *xs) 154 { 155 double delta; 156 double bps; 157 158 gettimeofday(&xs->end, NULL); 159 160 stat_display(xs, 1); 161 fputc('\n', stderr); 162 delta = (xs->end.tv_sec + (xs->end.tv_usec / 1.e6)) 163 - (xs->start.tv_sec + (xs->start.tv_usec / 1.e6)); 164 fprintf(stderr, "%lld bytes transferred in %.1f seconds ", 165 xs->rcvd - xs->offset, delta); 166 bps = (xs->rcvd - xs->offset) / delta; 167 if (bps > 1024*1024) 168 fprintf(stderr, "(%.2f MBps)\n", bps / (1024*1024)); 169 else if (bps > 1024) 170 fprintf(stderr, "(%.2f kBps)\n", bps / 1024); 171 else 172 fprintf(stderr, "(%.2f Bps)\n", bps); 173 } 174 175 int 176 fetch(char *URL, char *path) 177 { 178 struct url *url; 179 struct url_stat us; 180 struct stat sb; 181 struct xferstat xs; 182 FILE *f, *of; 183 size_t size; 184 off_t count; 185 char flags[8]; 186 int n, r; 187 u_int timeout; 188 189 f = of = NULL; 190 191 /* parse URL */ 192 if ((url = fetchParseURL(URL)) == NULL) { 193 warnx("%s: parse error", URL); 194 goto failure; 195 } 196 197 timeout = 0; 198 *flags = 0; 199 count = 0; 200 201 /* common flags */ 202 if (v_level > 1) 203 strcat(flags, "v"); 204 switch (family) { 205 case PF_INET: 206 strcat(flags, "4"); 207 break; 208 case PF_INET6: 209 strcat(flags, "6"); 210 break; 211 } 212 213 /* FTP specific flags */ 214 if (strcmp(url->scheme, "ftp") == 0) { 215 if (p_flag) 216 strcat(flags, "p"); 217 if (d_flag) 218 strcat(flags, "d"); 219 if (H_flag) 220 strcat(flags, "h"); 221 timeout = T_secs ? T_secs : ftp_timeout; 222 } 223 224 /* HTTP specific flags */ 225 if (strcmp(url->scheme, "http") == 0) { 226 if (d_flag) 227 strcat(flags, "d"); 228 if (A_flag) 229 strcat(flags, "A"); 230 timeout = T_secs ? T_secs : http_timeout; 231 } 232 233 /* set the protocol timeout. */ 234 fetchTimeout = timeout; 235 236 /* just print size */ 237 if (s_flag) { 238 if (fetchStat(url, &us, flags) == -1) 239 goto failure; 240 if (us.size == -1) 241 printf("Unknown\n"); 242 else 243 printf("%lld\n", us.size); 244 goto success; 245 } 246 247 /* 248 * If the -r flag was specified, we have to compare the local and 249 * remote files, so we should really do a fetchStat() first, but I 250 * know of at least one HTTP server that only sends the content 251 * size in response to GET requests, and leaves it out of replies 252 * to HEAD requests. Also, in the (frequent) case that the local 253 * and remote files match but the local file is truncated, we have 254 * sufficient information *before* the compare to issue a correct 255 * request. Therefore, we always issue a GET request as if we were 256 * sure the local file was a truncated copy of the remote file; we 257 * can drop the connection later if we change our minds. 258 */ 259 if (r_flag && !o_stdout && stat(path, &sb) != -1) 260 url->offset = sb.st_size; 261 else 262 sb.st_size = 0; 263 264 /* start the transfer */ 265 if ((f = fetchXGet(url, &us, flags)) == NULL) { 266 warnx("%s: %s", path, fetchLastErrString); 267 goto failure; 268 } 269 if (sigint) 270 goto signal; 271 272 /* check that size is as expected */ 273 if (S_size) { 274 if (us.size == -1) { 275 warnx("%s: size unknown", path); 276 goto failure; 277 } else if (us.size != S_size) { 278 warnx("%s: size mismatch: expected %lld, actual %lld", 279 path, S_size, us.size); 280 goto failure; 281 } 282 } 283 284 /* symlink instead of copy */ 285 if (l_flag && strcmp(url->scheme, "file") == 0 && !o_stdout) { 286 if (symlink(url->doc, path) == -1) { 287 warn("%s: symlink()", path); 288 goto failure; 289 } 290 goto success; 291 } 292 293 /* open output file */ 294 if (o_stdout) { 295 /* output to stdout */ 296 of = stdout; 297 } else if (sb.st_size) { 298 /* resume mode, local file exists */ 299 if (!F_flag && us.mtime && sb.st_mtime != us.mtime) { 300 /* no match! have to refetch */ 301 fclose(f); 302 url->offset = 0; 303 if ((f = fetchXGet(url, &us, flags)) == NULL) { 304 warnx("%s: %s", path, fetchLastErrString); 305 goto failure; 306 } 307 if (sigint) 308 goto signal; 309 } else { 310 us.size += url->offset; 311 if (us.size == sb.st_size) 312 /* nothing to do */ 313 goto success; 314 if (sb.st_size > us.size) { 315 /* local file too long! */ 316 warnx("%s: local file (%lld bytes) is longer " 317 "than remote file (%lld bytes)", 318 path, sb.st_size, us.size); 319 goto failure; 320 } 321 /* we got through, open local file and seek to offset */ 322 /* 323 * XXX there's a race condition here - the file we open is not 324 * necessarily the same as the one we stat()'ed earlier... 325 */ 326 if ((of = fopen(path, "a")) == NULL) { 327 warn("%s: fopen()", path); 328 goto failure; 329 } 330 if (fseek(of, url->offset, SEEK_SET) == -1) { 331 warn("%s: fseek()", path); 332 goto failure; 333 } 334 } 335 } 336 if (m_flag && stat(path, &sb) != -1) { 337 /* mirror mode, local file exists */ 338 if (sb.st_size == us.size && sb.st_mtime == us.mtime) 339 goto success; 340 } 341 if (!of) { 342 /* 343 * We don't yet have an output file; either this is a vanilla 344 * run with no special flags, or the local and remote files 345 * didn't match. 346 */ 347 if ((of = fopen(path, "w")) == NULL) { 348 warn("%s: open()", path); 349 goto failure; 350 } 351 } 352 count = url->offset; 353 354 /* start the counter */ 355 stat_start(&xs, path, us.size, count); 356 357 sigint = sigalrm = 0; 358 359 /* suck in the data */ 360 for (n = 0; !sigint && !sigalrm; ++n) { 361 if (us.size != -1 && us.size - count < B_size) 362 size = us.size - count; 363 else 364 size = B_size; 365 if (timeout) 366 alarm(timeout); 367 if ((size = fread(buf, 1, size, f)) <= 0) 368 break; 369 stat_update(&xs, count += size, 0); 370 if (fwrite(buf, size, 1, of) != 1) 371 break; 372 } 373 374 if (timeout) 375 alarm(0); 376 377 stat_end(&xs); 378 379 /* Set mtime of local file */ 380 if (!n_flag && us.mtime && !o_stdout) { 381 struct timeval tv[2]; 382 383 fflush(of); 384 tv[0].tv_sec = (long)(us.atime ? us.atime : us.mtime); 385 tv[1].tv_sec = (long)us.mtime; 386 tv[0].tv_usec = tv[1].tv_usec = 0; 387 if (utimes(path, tv)) 388 warn("%s: utimes()", path); 389 } 390 391 /* timed out or interrupted? */ 392 signal: 393 if (sigalrm) 394 warnx("transfer timed out"); 395 if (sigint) 396 warnx("transfer interrupted"); 397 398 if (!sigalrm && !sigint) { 399 /* check the status of our files */ 400 if (ferror(f)) 401 warn("%s", URL); 402 if (ferror(of)) 403 warn("%s", path); 404 if (ferror(f) || ferror(of)) 405 goto failure; 406 } 407 408 /* did the transfer complete normally? */ 409 if (us.size != -1 && count < us.size) { 410 warnx("%s appears to be truncated: %lld/%lld bytes", 411 path, count, us.size); 412 goto failure_keep; 413 } 414 415 success: 416 r = 0; 417 goto done; 418 failure: 419 if (of && of != stdout && !R_flag && !r_flag) 420 unlink(path); 421 failure_keep: 422 r = -1; 423 goto done; 424 done: 425 if (f) 426 fclose(f); 427 if (of && of != stdout) 428 fclose(of); 429 if (url) 430 fetchFreeURL(url); 431 return r; 432 } 433 434 void 435 usage(void) 436 { 437 /* XXX badly out of synch */ 438 fprintf(stderr, 439 "Usage: fetch [-1AFHMPRabdlmnpqrstv] [-o outputfile] [-S bytes]\n" 440 " [-B bytes] [-T seconds] [-w seconds]\n" 441 " [-f file -h host [-c dir] | URL ...]\n" 442 ); 443 } 444 445 446 #define PARSENUM(NAME, TYPE) \ 447 int \ 448 NAME(char *s, TYPE *v) \ 449 { \ 450 *v = 0; \ 451 for (*v = 0; *s; s++) \ 452 if (isdigit(*s)) \ 453 *v = *v * 10 + *s - '0'; \ 454 else \ 455 return -1; \ 456 return 0; \ 457 } 458 459 PARSENUM(parseint, u_int) 460 PARSENUM(parsesize, size_t) 461 PARSENUM(parseoff, off_t) 462 463 int 464 main(int argc, char *argv[]) 465 { 466 struct stat sb; 467 struct sigaction sa; 468 char *p, *q, *s; 469 int c, e, r; 470 471 while ((c = getopt(argc, argv, 472 "146AaB:bc:dFf:h:lHMmnPpo:qRrS:sT:tvw:")) != EOF) 473 switch (c) { 474 case '1': 475 once_flag = 1; 476 break; 477 case '4': 478 family = PF_INET; 479 break; 480 case '6': 481 family = PF_INET6; 482 break; 483 case 'A': 484 A_flag = 1; 485 break; 486 case 'a': 487 a_flag = 1; 488 break; 489 case 'B': 490 if (parsesize(optarg, &B_size) == -1) 491 errx(1, "invalid buffer size"); 492 break; 493 case 'b': 494 warnx("warning: the -b option is deprecated"); 495 b_flag = 1; 496 break; 497 case 'c': 498 c_dirname = optarg; 499 break; 500 case 'd': 501 d_flag = 1; 502 break; 503 case 'F': 504 F_flag = 1; 505 break; 506 case 'f': 507 f_filename = optarg; 508 break; 509 case 'H': 510 H_flag = 1; 511 break; 512 case 'h': 513 h_hostname = optarg; 514 break; 515 case 'l': 516 l_flag = 1; 517 break; 518 case 'o': 519 o_flag = 1; 520 o_filename = optarg; 521 break; 522 case 'M': 523 case 'm': 524 if (r_flag) 525 errx(1, "the -m and -r flags are mutually exclusive"); 526 m_flag = 1; 527 break; 528 case 'n': 529 n_flag = 1; 530 break; 531 case 'P': 532 case 'p': 533 p_flag = 1; 534 break; 535 case 'q': 536 v_level = 0; 537 break; 538 case 'R': 539 R_flag = 1; 540 break; 541 case 'r': 542 if (m_flag) 543 errx(1, "the -m and -r flags are mutually exclusive"); 544 r_flag = 1; 545 break; 546 case 'S': 547 if (parseoff(optarg, &S_size) == -1) 548 errx(1, "invalid size"); 549 break; 550 case 's': 551 s_flag = 1; 552 break; 553 case 'T': 554 if (parseint(optarg, &T_secs) == -1) 555 errx(1, "invalid timeout"); 556 break; 557 case 't': 558 t_flag = 1; 559 warnx("warning: the -t option is deprecated"); 560 break; 561 case 'v': 562 v_level++; 563 break; 564 case 'w': 565 a_flag = 1; 566 if (parseint(optarg, &w_secs) == -1) 567 errx(1, "invalid delay"); 568 break; 569 default: 570 usage(); 571 exit(EX_USAGE); 572 } 573 574 argc -= optind; 575 argv += optind; 576 577 if (h_hostname || f_filename || c_dirname) { 578 if (!h_hostname || !f_filename || argc) { 579 usage(); 580 exit(EX_USAGE); 581 } 582 /* XXX this is a hack. */ 583 if (strcspn(h_hostname, "@:/") != strlen(h_hostname)) 584 errx(1, "invalid hostname"); 585 if (asprintf(argv, "ftp://%s/%s/%s", h_hostname, 586 c_dirname ? c_dirname : "", f_filename) == -1) 587 errx(1, strerror(ENOMEM)); 588 argc++; 589 } 590 591 if (!argc) { 592 usage(); 593 exit(EX_USAGE); 594 } 595 596 /* allocate buffer */ 597 if (B_size < MINBUFSIZE) 598 B_size = MINBUFSIZE; 599 if ((buf = malloc(B_size)) == NULL) 600 errx(1, strerror(ENOMEM)); 601 602 /* timeouts */ 603 if ((s = getenv("FTP_TIMEOUT")) != NULL) { 604 if (parseint(s, &ftp_timeout) == -1) { 605 warnx("FTP_TIMEOUT is not a positive integer"); 606 ftp_timeout = 0; 607 } 608 } 609 if ((s = getenv("HTTP_TIMEOUT")) != NULL) { 610 if (parseint(s, &http_timeout) == -1) { 611 warnx("HTTP_TIMEOUT is not a positive integer"); 612 http_timeout = 0; 613 } 614 } 615 616 /* signal handling */ 617 sa.sa_flags = 0; 618 sa.sa_handler = sig_handler; 619 sigemptyset(&sa.sa_mask); 620 sigaction(SIGALRM, &sa, NULL); 621 sa.sa_flags = SA_RESETHAND; 622 sigaction(SIGINT, &sa, NULL); 623 fetchRestartCalls = 0; 624 625 /* output file */ 626 if (o_flag) { 627 if (strcmp(o_filename, "-") == 0) { 628 o_stdout = 1; 629 } else if (stat(o_filename, &sb) == -1) { 630 if (errno == ENOENT) { 631 if (argc > 1) 632 errx(EX_USAGE, "%s is not a directory", o_filename); 633 } else { 634 err(EX_IOERR, "%s", o_filename); 635 } 636 } else { 637 if (sb.st_mode & S_IFDIR) 638 o_directory = 1; 639 } 640 } 641 642 /* check if output is to a tty (for progress report) */ 643 v_tty = isatty(STDERR_FILENO); 644 r = 0; 645 646 while (argc) { 647 if ((p = strrchr(*argv, '/')) == NULL) 648 p = *argv; 649 else 650 p++; 651 652 if (!*p) 653 p = "fetch.out"; 654 655 fetchLastErrCode = 0; 656 657 if (o_flag) { 658 if (o_stdout) { 659 e = fetch(*argv, "-"); 660 } else if (o_directory) { 661 asprintf(&q, "%s/%s", o_filename, p); 662 e = fetch(*argv, q); 663 free(q); 664 } else { 665 e = fetch(*argv, o_filename); 666 } 667 } else { 668 e = fetch(*argv, p); 669 } 670 671 if (sigint) 672 kill(getpid(), SIGINT); 673 674 if (e == 0 && once_flag) 675 exit(0); 676 677 if (e) { 678 r = 1; 679 if ((fetchLastErrCode 680 && fetchLastErrCode != FETCH_UNAVAIL 681 && fetchLastErrCode != FETCH_MOVED 682 && fetchLastErrCode != FETCH_URL 683 && fetchLastErrCode != FETCH_RESOLV 684 && fetchLastErrCode != FETCH_UNKNOWN)) { 685 if (w_secs) { 686 if (v_level) 687 fprintf(stderr, "Waiting %d seconds before retrying\n", 688 w_secs); 689 sleep(w_secs); 690 } 691 if (a_flag) 692 continue; 693 } 694 } 695 696 argc--, argv++; 697 } 698 699 exit(r); 700 } 701