wget: Add support for timeout parameter

Signed-off-by: Matthias Kaehlcke <[EMAIL PROTECTED]>

---

--- 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;
 };
 #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 */             
                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 */   
+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 +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);
+       
 #if ENABLE_FEATURE_WGET_LONG_OPTIONS
        if (headers_llist) {
                int size = 1;
@@ -504,10 +529,18 @@
                        opt &= ~WGET_OPT_CONTINUE;
                }
        }
+
 #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;
+       }
+
        /* 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)) {
+               /* 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" \
 
 #define which_trivial_usage \
        "[COMMAND...]"

-- 
Matthias Kaehlcke
Embedded Linux Engineer
Barcelona

     La libertad es como la maƱana. Hay quienes esperan dormidos a que
   llegue, pero hay quienes desvelan y caminan la noche para alcanzarla
                        (Subcomandante Marcos)
                                                                 .''`.
    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