]> sjero.net Git - wget/blobdiff - src/retr.c
[svn] Allocate a timer directly in get_contents.
[wget] / src / retr.c
index d613da7c8037f0ab0fe0308cae3949778919629b..f4b80fdd50b609f8289653c7a8ee2b073403ca81 100644 (file)
@@ -77,32 +77,38 @@ int global_download_count;
    from fd immediately, flush or discard the buffer.  */
 int
 get_contents (int fd, FILE *fp, long *len, long restval, long expected,
-             struct rbuf *rbuf, int use_expected)
+             struct rbuf *rbuf, int use_expected, long *elapsed)
 {
   int res = 0;
   static char c[8192];
   void *progress = NULL;
+  struct wget_timer *timer = NULL;
 
   *len = restval;
+
   if (opt.verbose)
     progress = progress_create (restval, expected);
+  if (opt.verbose || elapsed != NULL)
+    timer = wtimer_new ();
 
   if (rbuf && RBUF_FD (rbuf) == fd)
     {
-      int need_flush = 0;
+      int sz = 0;
       while ((res = rbuf_flush (rbuf, c, sizeof (c))) != 0)
        {
-         if (fwrite (c, sizeof (char), res, fp) < res)
-           return -2;
-         if (opt.verbose)
-           progress_update (progress, res);
+         fwrite (c, sizeof (char), res, fp);
          *len += res;
-         need_flush = 1;
+         sz += res;
        }
-      if (need_flush)
+      if (sz)
        fflush (fp);
       if (ferror (fp))
-       return -2;
+       {
+         res = -2;
+         goto out;
+       }
+      if (opt.verbose)
+       progress_update (progress, sz, wtimer_elapsed (timer));
     }
   /* Read from fd while there is available data.
 
@@ -131,9 +137,12 @@ get_contents (int fd, FILE *fp, long *len, long restval, long expected,
             packets typically won't be too tiny anyway.  */
          fflush (fp);
          if (ferror (fp))
-           return -2;
+           {
+             res = -2;
+             goto out;
+           }
          if (opt.verbose)
-           progress_update (progress, res);
+           progress_update (progress, res, wtimer_elapsed (timer));
          *len += res;
        }
       else
@@ -141,8 +150,17 @@ get_contents (int fd, FILE *fp, long *len, long restval, long expected,
     }
   if (res < -1)
     res = -1;
-  if (opt.verbose)
-    progress_finish (progress);
+
+ out:
+  if (timer)
+    {
+      long dltime = wtimer_elapsed (timer);
+      if (opt.verbose)
+       progress_finish (progress, dltime);
+      if (elapsed)
+       *elapsed = dltime;
+      wtimer_delete (timer);
+    }
   return res;
 }
 \f
@@ -336,7 +354,6 @@ retrieve_url (const char *origurl, char **file, char **newloc,
          char *suf = suffix (u->local);
          if (suf && (!strcasecmp (suf, "html") || !strcasecmp (suf, "htm")))
            *dt |= TEXTHTML;
-         FREE_MAYBE (suf);
        }
 #endif
     }
@@ -428,13 +445,21 @@ retrieve_url (const char *origurl, char **file, char **newloc,
     FREE_MAYBE (local_file);
 
   url_free (u);
-  if (redirections)
-    string_set_free (redirections);
 
-  if (newloc)
-    *newloc = url;
+  if (redirections)
+    {
+      string_set_free (redirections);
+      if (newloc)
+       *newloc = url;
+      else
+       xfree (url);
+    }
   else
-    xfree (url);
+    {
+      if (newloc)
+       *newloc = NULL;
+      xfree (url);
+    }
 
   ++global_download_count;
 
@@ -462,6 +487,9 @@ retrieve_from_file (const char *file, int html, int *count)
       char *filename = NULL, *new_file;
       int dt;
 
+      if (cur_url->ignore_when_downloading)
+       continue;
+
       if (downloaded_exceeds_quota ())
        {
          status = QUOTEXC;
@@ -558,8 +586,18 @@ sleep_between_retrievals (int count)
            sleep (opt.waitretry);
        }
       else if (opt.wait)
-       /* Otherwise, check if opt.wait is specified.  If so, sleep.  */
-       sleep (opt.wait);
+       {
+         /* Otherwise, check if opt.wait is specified.  If so, sleep.  */
+         if (count > 1 || !opt.random_wait)
+           sleep (opt.wait);
+         else
+           {
+             int waitsecs = random() % (opt.wait * 2 + 1);
+             DEBUGP(("sleep_between_retrievals: norm=%ld,random=%ld,sleep=%d\n",
+                     opt.wait, waitsecs - opt.wait, waitsecs));
+             sleep(waitsecs);
+           }
+       }
     }
   if (first_retrieval)
     first_retrieval = 0;