aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBradley M. Kuhn <bkuhn@ebb.org>2010-08-08 02:51:20 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2010-08-08 02:51:20 +0200
commitc97131c2af832f03e769a12b2a95e4de86c5858f (patch)
tree6bb49cd582b75953c822431162a94809a9539918
parent33bbb27e45c7c6a0fecb40b3a5aa36aef69825f9 (diff)
downloadbusybox-w32-c97131c2af832f03e769a12b2a95e4de86c5858f.tar.gz
busybox-w32-c97131c2af832f03e769a12b2a95e4de86c5858f.tar.bz2
busybox-w32-c97131c2af832f03e769a12b2a95e4de86c5858f.zip
wget: implement -T SEC; rework progress meter to not use signals (it was unsafe)
function old new delta retrieve_file_data 364 450 +86 bb_progress_update 615 682 +67 packed_usage 27406 27422 +16 wget_main 2440 2453 +13 static.wget_longopts 145 155 +10 progress_meter 199 159 -40 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 5/1 up/down: 192/-40) Total: 152 bytes Signed-off-by: Bradley M. Kuhn <bkuhn@ebb.org> Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--TEST_config_rh91
-rw-r--r--include/usage.src.h11
-rw-r--r--libbb/progress.c35
-rw-r--r--networking/Config.src12
-rw-r--r--networking/wget.c121
5 files changed, 117 insertions, 63 deletions
diff --git a/TEST_config_rh9 b/TEST_config_rh9
index f376cd439..3ffb1c6f3 100644
--- a/TEST_config_rh9
+++ b/TEST_config_rh9
@@ -803,6 +803,7 @@ CONFIG_WGET=y
803CONFIG_FEATURE_WGET_STATUSBAR=y 803CONFIG_FEATURE_WGET_STATUSBAR=y
804CONFIG_FEATURE_WGET_AUTHENTICATION=y 804CONFIG_FEATURE_WGET_AUTHENTICATION=y
805CONFIG_FEATURE_WGET_LONG_OPTIONS=y 805CONFIG_FEATURE_WGET_LONG_OPTIONS=y
806CONFIG_FEATURE_WGET_TIMEOUT=y
806CONFIG_ZCIP=y 807CONFIG_ZCIP=y
807 808
808# 809#
diff --git a/include/usage.src.h b/include/usage.src.h
index 57c25a0f8..ebd8c2e6a 100644
--- a/include/usage.src.h
+++ b/include/usage.src.h
@@ -4779,10 +4779,12 @@ INSERT
4779 IF_FEATURE_WGET_LONG_OPTIONS( \ 4779 IF_FEATURE_WGET_LONG_OPTIONS( \
4780 "[-c|--continue] [-s|--spider] [-q|--quiet] [-O|--output-document FILE]\n" \ 4780 "[-c|--continue] [-s|--spider] [-q|--quiet] [-O|--output-document FILE]\n" \
4781 " [--header 'header: value'] [-Y|--proxy on/off] [-P DIR]\n" \ 4781 " [--header 'header: value'] [-Y|--proxy on/off] [-P DIR]\n" \
4782 " [--no-check-certificate] [-U|--user-agent AGENT] URL" \ 4782 " [--no-check-certificate] [-U|--user-agent AGENT]" \
4783 IF_FEATURE_WGET_TIMEOUT("[-T SEC] ") " URL" \
4783 ) \ 4784 ) \
4784 IF_NOT_FEATURE_WGET_LONG_OPTIONS( \ 4785 IF_NOT_FEATURE_WGET_LONG_OPTIONS( \
4785 "[-csq] [-O FILE] [-Y on/off] [-P DIR] [-U AGENT] URL" \ 4786 "[-csq] [-O FILE] [-Y on/off] [-P DIR] [-U AGENT]" \
4787 IF_FEATURE_WGET_TIMEOUT("[-T SEC] ") " URL" \
4786 ) 4788 )
4787#define wget_full_usage "\n\n" \ 4789#define wget_full_usage "\n\n" \
4788 "Retrieve files via HTTP or FTP\n" \ 4790 "Retrieve files via HTTP or FTP\n" \
@@ -4790,7 +4792,10 @@ INSERT
4790 "\n -s Spider mode - only check file existence" \ 4792 "\n -s Spider mode - only check file existence" \
4791 "\n -c Continue retrieval of aborted transfer" \ 4793 "\n -c Continue retrieval of aborted transfer" \
4792 "\n -q Quiet" \ 4794 "\n -q Quiet" \
4793 "\n -P Set directory prefix to DIR" \ 4795 "\n -P DIR Save to DIR (default .)" \
4796 IF_FEATURE_WGET_TIMEOUT( \
4797 "\n -T SEC Network read timeout is SEC seconds" \
4798 ) \
4794 "\n -O FILE Save to FILE ('-' for stdout)" \ 4799 "\n -O FILE Save to FILE ('-' for stdout)" \
4795 "\n -U STR Use STR for User-Agent header" \ 4800 "\n -U STR Use STR for User-Agent header" \
4796 "\n -Y Use proxy ('on' or 'off')" \ 4801 "\n -Y Use proxy ('on' or 'off')" \
diff --git a/libbb/progress.c b/libbb/progress.c
index e96039042..7fb8536d2 100644
--- a/libbb/progress.c
+++ b/libbb/progress.c
@@ -66,16 +66,29 @@ void FAST_FUNC bb_progress_update(bb_progress_t *p,
66 off_t transferred, 66 off_t transferred,
67 off_t totalsize) 67 off_t totalsize)
68{ 68{
69 off_t abbrevsize; 69 uoff_t beg_and_transferred;
70 unsigned since_last_update, elapsed; 70 unsigned since_last_update, elapsed;
71 unsigned ratio; 71 unsigned ratio;
72 int barlength, i; 72 int barlength, i;
73 73
74 /* totalsize == 0 if it is unknown */
75
76 elapsed = monotonic_sec();
77 since_last_update = elapsed - p->lastupdate_sec;
78 /* Do not update on every call
79 * (might be: on every network read!) */
80 if (since_last_update == 0 && !totalsize)
81 return;
82
83 beg_and_transferred = beg_range + transferred;
74 ratio = 100; 84 ratio = 100;
75 if (totalsize) { 85 if (beg_and_transferred < totalsize) {
86 /* Do not update on every call
87 * (might be: on every network read!) */
88 if (since_last_update == 0)
89 return;
76 /* long long helps to have it working even if !LFS */ 90 /* long long helps to have it working even if !LFS */
77 ratio = (unsigned) (100ULL * (transferred+beg_range) / totalsize); 91 ratio = 100ULL * beg_and_transferred / (uoff_t)totalsize;
78 if (ratio > 100) ratio = 100;
79 } 92 }
80 93
81#if ENABLE_UNICODE_SUPPORT 94#if ENABLE_UNICODE_SUPPORT
@@ -95,11 +108,11 @@ void FAST_FUNC bb_progress_update(bb_progress_t *p,
95 /* back to multibyte; cant overflow */ 108 /* back to multibyte; cant overflow */
96 wcstombs(buf, wbuf21, INT_MAX); 109 wcstombs(buf, wbuf21, INT_MAX);
97 len = (len > 20) ? 0 : 20 - len; 110 len = (len > 20) ? 0 : 20 - len;
98 fprintf(stderr, "\r%s%*s%4d%% ", buf, len, "", ratio); 111 fprintf(stderr, "\r%s%*s%4u%% ", buf, len, "", ratio);
99 free(buf); 112 free(buf);
100 } 113 }
101#else 114#else
102 fprintf(stderr, "\r%-20.20s%4d%% ", curfile, ratio); 115 fprintf(stderr, "\r%-20.20s%4u%% ", curfile, ratio);
103#endif 116#endif
104 117
105 barlength = get_tty2_width() - 49; 118 barlength = get_tty2_width() - 49;
@@ -114,16 +127,14 @@ void FAST_FUNC bb_progress_update(bb_progress_t *p,
114 } 127 }
115 } 128 }
116 i = 0; 129 i = 0;
117 abbrevsize = transferred + beg_range; 130 while (beg_and_transferred >= 100000) {
118 while (abbrevsize >= 100000) {
119 i++; 131 i++;
120 abbrevsize >>= 10; 132 beg_and_transferred >>= 10;
121 } 133 }
122 /* see http://en.wikipedia.org/wiki/Tera */ 134 /* see http://en.wikipedia.org/wiki/Tera */
123 fprintf(stderr, "%6d%c ", (int)abbrevsize, " kMGTPEZY"[i]); 135 fprintf(stderr, "%6u%c ", (unsigned)beg_and_transferred, " kMGTPEZY"[i]);
136#define beg_and_transferred dont_use_beg_and_transferred_below
124 137
125 elapsed = monotonic_sec();
126 since_last_update = elapsed - p->lastupdate_sec;
127 if (transferred > p->lastsize) { 138 if (transferred > p->lastsize) {
128 p->lastupdate_sec = elapsed; 139 p->lastupdate_sec = elapsed;
129 p->lastsize = transferred; 140 p->lastsize = transferred;
diff --git a/networking/Config.src b/networking/Config.src
index 8604c53e9..1d9a33f2b 100644
--- a/networking/Config.src
+++ b/networking/Config.src
@@ -1012,6 +1012,18 @@ config FEATURE_WGET_LONG_OPTIONS
1012 help 1012 help
1013 Support long options for the wget applet. 1013 Support long options for the wget applet.
1014 1014
1015config FEATURE_WGET_TIMEOUT
1016 bool "Enable timeout options"
1017 default n
1018 depends on WGET
1019 help
1020 Supports network read timeout for wget, so that wget will give
1021 up and timeout when reading network data, through the -T command
1022 line option. Currently only network data read timeout is
1023 supported (i.e., timeout is not applied to the DNS nor TCP
1024 connection initialization). When FEATURE_WGET_LONG_OPTIONS is
1025 also enabled, the --timeout option will work in addition to -T.
1026
1015config ZCIP 1027config ZCIP
1016 bool "zcip" 1028 bool "zcip"
1017 default y 1029 default y
diff --git a/networking/wget.c b/networking/wget.c
index 1f35f8b03..f62339071 100644
--- a/networking/wget.c
+++ b/networking/wget.c
@@ -3,8 +3,10 @@
3 * wget - retrieve a file using HTTP or FTP 3 * wget - retrieve a file using HTTP or FTP
4 * 4 *
5 * Chip Rosenthal Covad Communications <chip@laserlink.net> 5 * Chip Rosenthal Covad Communications <chip@laserlink.net>
6 *
7 * Licensed under GPLv2, see file LICENSE in this tarball for details. 6 * Licensed under GPLv2, see file LICENSE in this tarball for details.
7 *
8 * Copyright (C) 2010 Bradley M. Kuhn <bkuhn@ebb.org>
9 * Kuhn's copyrights are licensed GPLv2-or-later. File as a whole remains GPLv2.
8 */ 10 */
9#include "libbb.h" 11#include "libbb.h"
10 12
@@ -19,7 +21,7 @@ struct host_info {
19}; 21};
20 22
21 23
22/* Globals (can be accessed from signal handlers) */ 24/* Globals */
23struct globals { 25struct globals {
24 off_t content_len; /* Content-length of the file */ 26 off_t content_len; /* Content-length of the file */
25 off_t beg_range; /* Range at which continue begins */ 27 off_t beg_range; /* Range at which continue begins */
@@ -28,6 +30,9 @@ struct globals {
28 const char *curfile; /* Name of current file being transferred */ 30 const char *curfile; /* Name of current file being transferred */
29 bb_progress_t pmt; 31 bb_progress_t pmt;
30#endif 32#endif
33#if ENABLE_FEATURE_WGET_TIMEOUT
34 unsigned timeout_seconds;
35#endif
31 smallint chunked; /* chunked transfer encoding */ 36 smallint chunked; /* chunked transfer encoding */
32 smallint got_clen; /* got content-length: from server */ 37 smallint got_clen; /* got content-length: from server */
33} FIX_ALIASING; 38} FIX_ALIASING;
@@ -35,42 +40,51 @@ struct globals {
35struct BUG_G_too_big { 40struct BUG_G_too_big {
36 char BUG_G_too_big[sizeof(G) <= COMMON_BUFSIZE ? 1 : -1]; 41 char BUG_G_too_big[sizeof(G) <= COMMON_BUFSIZE ? 1 : -1];
37}; 42};
38#define INIT_G() do { } while (0) 43#define INIT_G() do { \
44 IF_FEATURE_WGET_TIMEOUT(G.timeout_seconds = 900;) \
45} while (0)
39 46
40 47
41#if ENABLE_FEATURE_WGET_STATUSBAR 48/* Must match option string! */
49enum {
50 WGET_OPT_CONTINUE = (1 << 0),
51 WGET_OPT_SPIDER = (1 << 1),
52 WGET_OPT_QUIET = (1 << 2),
53 WGET_OPT_OUTNAME = (1 << 3),
54 WGET_OPT_PREFIX = (1 << 4),
55 WGET_OPT_PROXY = (1 << 5),
56 WGET_OPT_USER_AGENT = (1 << 6),
57 WGET_OPT_NETWORK_READ_TIMEOUT = (1 << 7),
58 WGET_OPT_RETRIES = (1 << 8),
59 WGET_OPT_PASSIVE = (1 << 9),
60 WGET_OPT_HEADER = (1 << 10) * ENABLE_FEATURE_WGET_LONG_OPTIONS,
61 WGET_OPT_POST_DATA = (1 << 11) * ENABLE_FEATURE_WGET_LONG_OPTIONS,
62};
42 63
64enum {
65 PROGRESS_START = -1,
66 PROGRESS_END = 0,
67 PROGRESS_BUMP = 1,
68};
69#if ENABLE_FEATURE_WGET_STATUSBAR
43static void progress_meter(int flag) 70static void progress_meter(int flag)
44{ 71{
45 /* We can be called from signal handler */ 72 if (option_mask32 & WGET_OPT_QUIET)
46 int save_errno = errno; 73 return;
47 74
48 if (flag == -1) { /* first call to progress_meter */ 75 if (flag == PROGRESS_START)
49 bb_progress_init(&G.pmt); 76 bb_progress_init(&G.pmt);
50 }
51 77
52 bb_progress_update(&G.pmt, G.curfile, G.beg_range, G.transferred, 78 bb_progress_update(&G.pmt, G.curfile, G.beg_range, G.transferred,
53 G.chunked ? 0 : G.beg_range + G.transferred + G.content_len); 79 G.chunked ? 0 : G.beg_range + G.transferred + G.content_len);
54 80
55 if (flag == 0) { 81 if (flag == PROGRESS_END) {
56 /* last call to progress_meter */
57 alarm(0);
58 bb_putchar_stderr('\n'); 82 bb_putchar_stderr('\n');
59 G.transferred = 0; 83 G.transferred = 0;
60 } else {
61 if (flag == -1) { /* first call to progress_meter */
62 signal_SA_RESTART_empty_mask(SIGALRM, progress_meter);
63 }
64 alarm(1);
65 } 84 }
66
67 errno = save_errno;
68} 85}
69 86#else
70#else /* FEATURE_WGET_STATUSBAR */
71
72static ALWAYS_INLINE void progress_meter(int flag UNUSED_PARAM) { } 87static ALWAYS_INLINE void progress_meter(int flag UNUSED_PARAM) { }
73
74#endif 88#endif
75 89
76 90
@@ -430,28 +444,20 @@ static FILE* prepare_ftp_session(FILE **dfpp, struct host_info *target, len_and_
430 return sfp; 444 return sfp;
431} 445}
432 446
433/* Must match option string! */
434enum {
435 WGET_OPT_CONTINUE = (1 << 0),
436 WGET_OPT_SPIDER = (1 << 1),
437 WGET_OPT_QUIET = (1 << 2),
438 WGET_OPT_OUTNAME = (1 << 3),
439 WGET_OPT_PREFIX = (1 << 4),
440 WGET_OPT_PROXY = (1 << 5),
441 WGET_OPT_USER_AGENT = (1 << 6),
442 WGET_OPT_RETRIES = (1 << 7),
443 WGET_OPT_NETWORK_READ_TIMEOUT = (1 << 8),
444 WGET_OPT_PASSIVE = (1 << 9),
445 WGET_OPT_HEADER = (1 << 10) * ENABLE_FEATURE_WGET_LONG_OPTIONS,
446 WGET_OPT_POST_DATA = (1 << 11) * ENABLE_FEATURE_WGET_LONG_OPTIONS,
447};
448
449static void NOINLINE retrieve_file_data(FILE *dfp, int output_fd) 447static void NOINLINE retrieve_file_data(FILE *dfp, int output_fd)
450{ 448{
451 char buf[512]; 449 char buf[512];
452 450#if ENABLE_FEATURE_WGET_STATUSBAR || ENABLE_FEATURE_WGET_TIMEOUT
453 if (!(option_mask32 & WGET_OPT_QUIET)) 451# if ENABLE_FEATURE_WGET_TIMEOUT
454 progress_meter(-1); 452 unsigned second_cnt;
453# endif
454 struct pollfd polldata;
455
456 polldata.fd = fileno(dfp);
457 polldata.events = POLLIN | POLLPRI;
458 ndelay(polldata.fd);
459#endif
460 progress_meter(PROGRESS_START);
455 461
456 if (G.chunked) 462 if (G.chunked)
457 goto get_clen; 463 goto get_clen;
@@ -470,6 +476,23 @@ static void NOINLINE retrieve_file_data(FILE *dfp, int output_fd)
470 rdsz = (unsigned)G.content_len; 476 rdsz = (unsigned)G.content_len;
471 } 477 }
472 } 478 }
479#if ENABLE_FEATURE_WGET_STATUSBAR || ENABLE_FEATURE_WGET_TIMEOUT
480# if ENABLE_FEATURE_WGET_TIMEOUT
481 second_cnt = G.timeout_seconds;
482# endif
483 while (1) {
484 if (safe_poll(&polldata, 1, 1000) != 0)
485 break; /* error, EOF, or data is available */
486# if ENABLE_FEATURE_WGET_TIMEOUT
487 if (second_cnt != 0 && --second_cnt == 0) {
488 progress_meter(PROGRESS_END);
489 bb_perror_msg_and_die("download timed out");
490 }
491# endif
492 /* Needed for "stalled" indicator */
493 progress_meter(PROGRESS_BUMP);
494 }
495#endif
473 n = safe_fread(buf, rdsz, dfp); 496 n = safe_fread(buf, rdsz, dfp);
474 if (n <= 0) { 497 if (n <= 0) {
475 if (ferror(dfp)) { 498 if (ferror(dfp)) {
@@ -481,6 +504,7 @@ static void NOINLINE retrieve_file_data(FILE *dfp, int output_fd)
481 xwrite(output_fd, buf, n); 504 xwrite(output_fd, buf, n);
482#if ENABLE_FEATURE_WGET_STATUSBAR 505#if ENABLE_FEATURE_WGET_STATUSBAR
483 G.transferred += n; 506 G.transferred += n;
507 progress_meter(PROGRESS_BUMP);
484#endif 508#endif
485 if (G.got_clen) 509 if (G.got_clen)
486 G.content_len -= n; 510 G.content_len -= n;
@@ -499,8 +523,7 @@ static void NOINLINE retrieve_file_data(FILE *dfp, int output_fd)
499 G.got_clen = 1; 523 G.got_clen = 1;
500 } 524 }
501 525
502 if (!(option_mask32 & WGET_OPT_QUIET)) 526 progress_meter(PROGRESS_END);
503 progress_meter(0);
504} 527}
505 528
506int wget_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; 529int wget_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
@@ -541,9 +564,11 @@ int wget_main(int argc UNUSED_PARAM, char **argv)
541 "directory-prefix\0" Required_argument "P" 564 "directory-prefix\0" Required_argument "P"
542 "proxy\0" Required_argument "Y" 565 "proxy\0" Required_argument "Y"
543 "user-agent\0" Required_argument "U" 566 "user-agent\0" Required_argument "U"
567#if ENABLE_FEATURE_WGET_TIMEOUT
568 "timeout\0" Required_argument "T"
569#endif
544 /* Ignored: */ 570 /* Ignored: */
545 // "tries\0" Required_argument "t" 571 // "tries\0" Required_argument "t"
546 // "timeout\0" Required_argument "T"
547 /* Ignored (we always use PASV): */ 572 /* Ignored (we always use PASV): */
548 "passive-ftp\0" No_argument "\xff" 573 "passive-ftp\0" No_argument "\xff"
549 "header\0" Required_argument "\xfe" 574 "header\0" Required_argument "\xfe"
@@ -559,12 +584,12 @@ int wget_main(int argc UNUSED_PARAM, char **argv)
559 applet_long_options = wget_longopts; 584 applet_long_options = wget_longopts;
560#endif 585#endif
561 /* server.allocated = target.allocated = NULL; */ 586 /* server.allocated = target.allocated = NULL; */
562 opt_complementary = "-1" IF_FEATURE_WGET_LONG_OPTIONS(":\xfe::"); 587 opt_complementary = "-1" IF_FEATURE_WGET_TIMEOUT(":T+") IF_FEATURE_WGET_LONG_OPTIONS(":\xfe::");
563 opt = getopt32(argv, "csqO:P:Y:U:" /*ignored:*/ "t:T:", 588 opt = getopt32(argv, "csqO:P:Y:U:T:" /*ignored:*/ "t:",
564 &fname_out, &dir_prefix, 589 &fname_out, &dir_prefix,
565 &proxy_flag, &user_agent, 590 &proxy_flag, &user_agent,
566 NULL, /* -t RETRIES */ 591 IF_FEATURE_WGET_TIMEOUT(&G.timeout_seconds) IF_NOT_FEATURE_WGET_TIMEOUT(NULL),
567 NULL /* -T NETWORK_READ_TIMEOUT */ 592 NULL /* -t RETRIES */
568 IF_FEATURE_WGET_LONG_OPTIONS(, &headers_llist) 593 IF_FEATURE_WGET_LONG_OPTIONS(, &headers_llist)
569 IF_FEATURE_WGET_LONG_OPTIONS(, &post_data) 594 IF_FEATURE_WGET_LONG_OPTIONS(, &post_data)
570 ); 595 );