[PATCH] wget: add timeout parameter

Bernhard Reutner-Fischer rep.dot.nop at gmail.com
Tue Aug 19 11:57:13 UTC 2008


On Tue, Aug 19, 2008 at 10:58:39AM +0200, Matthias Kaehlcke wrote:
>wget: Add support for timeout parameter
>
>Signed-off-by: Matthias Kaehlcke <matthias at kaehlcke.net>
>
>---
>
>--- busybox-1.11.1.org/networking/wget.c	2008-06-25 14:51:14.000000000 +0200
>+++ busybox-1.11.1/networking/wget.c	2008-08-19 10:41:38.000000000 +0200
>@@ -29,9 +29,11 @@
> 	off_t transferred;        /* Number of bytes transferred so far */
> 	const char *curfile;      /* Name of current file being transferred */
> 	unsigned lastupdate_sec;
>-	unsigned start_sec;
> #endif
>+	unsigned start_sec;
> 	smallint chunked;             /* chunked transfer encoding */
>+	unsigned timeout;         /* Operation timeout */
>+	smallint quiet;

perhaps unify quiet and chunked into one "flags" for size-benefit.

> };
> #define G (*(struct globals*)&bb_common_bufsiz1)
> struct BUG_G_too_big {
>@@ -46,6 +48,8 @@
> #define lastupdate_sec  (G.lastupdate_sec )
> #define start_sec       (G.start_sec      )
> #define chunked         (G.chunked        )
>+#define timeout         (G.timeout        )
>+#define quiet           (G.quiet          )
> #define INIT_G() do { } while (0)
> 
> 
>@@ -63,15 +67,12 @@
> 
> static void progressmeter(int flag)
> {
>-	/* We can be called from signal handler */
>-	int save_errno = errno;
> 	off_t abbrevsize;
> 	unsigned since_last_update, elapsed;
> 	unsigned ratio;
> 	int barlength, i;
> 
>-	if (flag == -1) { /* first call to progressmeter */
>-		start_sec = monotonic_sec();
>+	if (flag == -1) { /* first call to progressmeter */		

superfluous whitespace at EOL.

> 		lastupdate_sec = start_sec;
> 		lastsize = 0;
> 		totalsize = content_len + beg_range; /* as content_len changes.. */
>@@ -139,18 +140,12 @@
> 
> 	if (flag == 0) {
> 		/* last call to progressmeter */
>-		alarm(0);
> 		transferred = 0;
> 		fputc('\n', stderr);
>-	} else {
>-		if (flag == -1) { /* first call to progressmeter */
>-			signal_SA_RESTART_empty_mask(SIGALRM, progressmeter);
>-		}
>-		alarm(1);
>-	}
>-
>-	errno = save_errno;
>+	} 	
> }
>+
>+
> /* Original copyright notice which applies to the CONFIG_FEATURE_WGET_STATUSBAR stuff,
>  * much of which was blatantly stolen from openssh.  */
> /*-
>@@ -193,6 +188,29 @@
> #endif
> 
> 
>+/* Perform periodic tasks during a download */   

superfluous whitespace at EOL.

>+static void timer(int flag ATTRIBUTE_UNUSED)
>+{
>+	/* we are called from a signal handler */
>+	int save_errno = errno;
>+
>+	if (timeout != 0) {
>+		if ((start_sec + timeout) < monotonic_sec()) {
>+			bb_perror_msg_and_die("download timed out");
>+		}
>+	}
>+	

superfluous whitespace.

>+	if (!quiet) {
>+		progressmeter(1);
>+	}
>+	

ditto.

>+	/* re-trigger timer */
>+	alarm(1);
>+		

likewise.

>+	errno = save_errno;
>+}
>+
>+
> /* Read NMEMB bytes into PTR from STREAM.  Returns the number of bytes read,
>  * and a short count if an eof or non-interrupt error is encountered.  */
> static size_t safe_fread(void *ptr, size_t nmemb, FILE *stream)
>@@ -410,6 +428,7 @@
> 	bool use_proxy = 1;             /* Use proxies if env vars are set  */
> 	const char *proxy_flag = "on";  /* Use proxies if env vars are set  */
> 	const char *user_agent = "Wget";/* "User-Agent" header field        */
>+	const char *timeout_param = "0";/* Operation timeout parameter      */
> 
> 	static const char keywords[] ALIGN1 =
> 		"content-length\0""transfer-encoding\0""chunked\0""location\0";
>@@ -426,6 +445,7 @@
> 		WGET_OPT_USER_AGENT = 0x40,
> 		WGET_OPT_PASSIVE    = 0x80,
> 		WGET_OPT_HEADER     = 0x100,
>+		WGET_OPT_TIMEOUT    = 0x200,
> 	};
> #if ENABLE_FEATURE_WGET_LONG_OPTIONS
> 	static const char wget_longopts[] ALIGN1 =
>@@ -437,6 +457,7 @@
> 		"directory-prefix\0" Required_argument "P"
> 		"proxy\0"            Required_argument "Y"
> 		"user-agent\0"       Required_argument "U"
>+		"timeout\0"          Required_argument "T"
> 		"passive-ftp\0"      No_argument       "\xff"
> 		"header\0"           Required_argument "\xfe"
> 		;
>@@ -449,15 +470,19 @@
> #endif
> 	/* server.allocated = target.allocated = NULL; */
> 	opt_complementary = "-1" USE_FEATURE_WGET_LONG_OPTIONS(":\xfe::");
>-	opt = getopt32(argv, "csqO:P:Y:U:",
>+	opt = getopt32(argv, "csqO:P:Y:U:T:",
> 				&fname_out, &dir_prefix,
>-				&proxy_flag, &user_agent
>+				&proxy_flag, &user_agent,
>+				&timeout_param
> 				USE_FEATURE_WGET_LONG_OPTIONS(, &headers_llist)
> 				);
> 	if (strcmp(proxy_flag, "off") == 0) {
> 		/* Use the proxy if necessary */
> 		use_proxy = 0;
> 	}
>+
>+	timeout = atoi(timeout_param);
>+	

same here.
atoi() is discouraged, use one of the existing number converters that do
input validation and range-checking (see xatonum.c and other users).

> #if ENABLE_FEATURE_WGET_LONG_OPTIONS
> 	if (headers_llist) {
> 		int size = 1;
>@@ -504,10 +529,18 @@
> 			opt &= ~WGET_OPT_CONTINUE;
> 		}
> 	}
>+

unrelated edit...

> #if ENABLE_FEATURE_WGET_STATUSBAR
> 	curfile = bb_get_last_path_component_nostrip(fname_out);
> #endif
> 
>+	if (opt & WGET_OPT_QUIET) {
>+		quiet = 1;
>+	}
>+	else {
>+		quiet = 0;
>+	}

quiet = opt & WGET_OPT_QUIET; smaller?

>+
> 	/* Impossible?
> 	if ((opt & WGET_OPT_CONTINUE) && !fname_out)
> 		bb_error_msg_and_die("cannot specify continue (-c) without a filename (-O)"); */
>@@ -772,9 +805,17 @@
> 		output_fd = xopen(fname_out, o_flags);
> 	}
> 
>-	if (!(opt & WGET_OPT_QUIET))
>+	start_sec = monotonic_sec();
>+
>+	if (!(opt & WGET_OPT_QUIET)) {

Why don't you use quiet here and below?

>+		/* first call to progressmeter */
> 		progressmeter(-1);
>+	}
> 
>+	/* setup the timer */
>+	signal_SA_RESTART_empty_mask(SIGALRM, timer);	
>+	alarm(1);
>+	
> 	if (chunked)
> 		goto get_clen;
> 
>@@ -814,8 +855,13 @@
> 			break; /* all done! */
> 	}
> 
>-	if (!(opt & WGET_OPT_QUIET))
>+	/* stop timer */
>+	alarm(0);
>+
>+	if (!(opt & WGET_OPT_QUIET)) {
>+		/* last call to progressmeter */
> 		progressmeter(0);
>+	}	
> 
> 	if ((use_proxy == 0) && target.is_ftp) {
> 		fclose(dfp);
>--- busybox-1.11.1.org/include/usage.h	2008-07-11 22:22:20.000000000 +0200
>+++ busybox-1.11.1/include/usage.h	2008-08-19 10:37:24.000000000 +0200
>@@ -4514,10 +4514,10 @@
> 	USE_GETOPT_LONG( \
>        "[-c|--continue] [-s|--spider] [-q|--quiet] [-O|--output-document file]\n" \
>        "	[--header 'header: value'] [-Y|--proxy on/off] [-P DIR]\n" \
>-       "	[-U|--user-agent agent] url" \
>+       "	[-U|--user-agent agent] url [-T|--timeout SECONDS]" \
> 	) \
> 	SKIP_GETOPT_LONG( \
>-       "[-csq] [-O file] [-Y on/off] [-P DIR] [-U agent] url" \
>+       "[-csq] [-O file] [-Y on/off] [-P DIR] [-U agent] url [-T SECONDS]" \
> 	)
> #define wget_full_usage "\n\n" \
>        "Retrieve files via HTTP or FTP\n" \
>@@ -4529,6 +4529,7 @@
>      "\n	-O	Save to filename ('-' for stdout)" \
>      "\n	-U	Adjust 'User-Agent' field" \
>      "\n	-Y	Use proxy ('on' or 'off')" \
>+     "\n	-T	Set timeout value to SECONDS" \

      "\n	-T	timeout in SECONDS" \
should be clear enough (and is shorter).

Care to resend?

PS: a size(1) would have been nice
TIA,
Bernhard



More information about the busybox mailing list