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.
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
}
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
char *suf = suffix (u->local);
if (suf && (!strcasecmp (suf, "html") || !strcasecmp (suf, "htm")))
*dt |= TEXTHTML;
- FREE_MAYBE (suf);
}
#endif
}
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;
char *filename = NULL, *new_file;
int dt;
+ if (cur_url->ignore_when_downloading)
+ continue;
+
if (downloaded_exceeds_quota ())
{
status = QUOTEXC;
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;