Hi Bernhard,

El Tue, Aug 19, 2008 at 01:57:13PM +0200 Bernhard Reutner-Fischer ha dit:

> ...
>
> Care to resend?

thanks for your review and your constructive comments. here is a new
version of the patch that adresses the issues you brought up.

> PS: a size(1) would have been nice

what does size(1) mean? a patch with zero impact on the size of the
binary? if desired i can add a config option for the timeout
parameter

---

--- 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 15:43:40.000000000 +0200
@@ -29,9 +29,10 @@
        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
-       smallint chunked;             /* chunked transfer encoding */
+       unsigned start_sec;
+       smallint flags;
+       unsigned timeout;         /* Operation timeout */
 };
 #define G (*(struct globals*)&bb_common_bufsiz1)
 struct BUG_G_too_big {
@@ -45,9 +46,16 @@
 #define curfile         (G.curfile        )
 #define lastupdate_sec  (G.lastupdate_sec )
 #define start_sec       (G.start_sec      )
-#define chunked         (G.chunked        )
+#define flags           (G.flags          )
+#define timeout         (G.timeout        )
 #define INIT_G() do { } while (0)
 
+#define WGET_CHUNKED  1
+#define WGET_QUIET    2
+
+#define chunked (flags & WGET_CHUNKED)
+#define quiet   (flags & WGET_QUIET)
+
 
 #if ENABLE_FEATURE_WGET_STATUSBAR
 enum {
@@ -63,15 +71,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();
                lastupdate_sec = start_sec;
                lastsize = 0;
                totalsize = content_len + beg_range; /* as content_len 
changes.. */
@@ -139,18 +144,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 +192,29 @@
 #endif
 
 
+/* Perform periodic tasks during a download */
+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");
+               }
+       }
+
+       if (!quiet) {
+               progressmeter(1);
+       }
+
+       /* re-trigger timer */
+       alarm(1);
+
+       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 +432,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 +449,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 +461,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"
                ;
@@ -444,20 +469,26 @@
 
        INIT_G();
 
+       flags = 0;
+
 #if ENABLE_FEATURE_WGET_LONG_OPTIONS
        applet_long_options = wget_longopts;
 #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 = xatou(timeout_param);
+
 #if ENABLE_FEATURE_WGET_LONG_OPTIONS
        if (headers_llist) {
                int size = 1;
@@ -504,10 +535,15 @@
                        opt &= ~WGET_OPT_CONTINUE;
                }
        }
+
 #if ENABLE_FEATURE_WGET_STATUSBAR
        curfile = bb_get_last_path_component_nostrip(fname_out);
 #endif
 
+       if (opt & WGET_OPT_QUIET) {
+               flags |= WGET_QUIET;
+       }
+
        /* Impossible?
        if ((opt & WGET_OPT_CONTINUE) && !fname_out)
                bb_error_msg_and_die("cannot specify continue (-c) without a 
filename (-O)"); */
@@ -526,7 +562,7 @@
         * sites (i.e. ftp.us.debian.org) use round-robin DNS
         * and we want to connect to only one IP... */
        lsa = xhost2sockaddr(server.host, server.port);
-       if (!(opt & WGET_OPT_QUIET)) {
+       if (!quiet) {
                fprintf(stderr, "Connecting to %s (%s)\n", server.host,
                                xmalloc_sockaddr2dotted(&lsa->u.sa));
                /* We leak result of xmalloc_sockaddr2dotted */
@@ -538,7 +574,7 @@
                 */
                do {
                        got_clen = 0;
-                       chunked = 0;
+                       flags &= ~WGET_CHUNKED;
 
                        if (!--try)
                                bb_error_msg_and_die("too many redirections");
@@ -656,7 +692,8 @@
                                if (key == KEY_transfer_encoding) {
                                        if (index_in_strings(keywords, 
str_tolower(str)) + 1 != KEY_chunked)
                                                bb_error_msg_and_die("transfer 
encoding '%s' is not supported", str);
-                                       chunked = got_clen = 1;
+                                       flags |= WGET_CHUNKED;
+                                       got_clen = 1;
                                }
                                if (key == KEY_location) {
                                        if (str[0] == '/')
@@ -772,8 +809,16 @@
                output_fd = xopen(fname_out, o_flags);
        }
 
-       if (!(opt & WGET_OPT_QUIET))
+       start_sec = monotonic_sec();
+
+       if (!quiet) {
+               /* 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 +859,13 @@
                        break; /* all done! */
        }
 
-       if (!(opt & WGET_OPT_QUIET))
+       /* stop timer */
+       alarm(0);
+
+       if (!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 14:41:25.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 to SECONDS" \
 
 #define which_trivial_usage \
        "[COMMAND...]"

-- 
Matthias Kaehlcke
Embedded Linux Engineer
Barcelona

     Nationalism is an infantile disease. It is the measles of mankind
                         (Albert Einstein)
                                                                 .''`.
    using free software / Debian GNU/Linux | http://debian.org  : :'  :
                                                                `. `'`
gpg --keyserver pgp.mit.edu --recv-keys 47D8E5D4                  `-
_______________________________________________
busybox mailing list
[email protected]
http://busybox.net/cgi-bin/mailman/listinfo/busybox

Reply via email to