From b7a8c6d3f5d6555f323523c3b87507eebc1df3eb Mon Sep 17 00:00:00 2001 From: hniksic Date: Wed, 1 Nov 2000 15:17:31 -0800 Subject: [PATCH] [svn] Gracefully handle opt.downloaded overflowing. Published in . --- src/ChangeLog | 31 +++++++++++++++++++++++++++++++ src/ftp.c | 10 +++++----- src/http.c | 6 +++--- src/main.c | 6 ++++-- src/options.h | 1 + src/recur.c | 6 +++--- src/retr.c | 36 +++++++++++++++++++++++++++++++++++- src/retr.h | 3 +++ src/wget.h | 2 +- 9 files changed, 86 insertions(+), 15 deletions(-) diff --git a/src/ChangeLog b/src/ChangeLog index d62a181f..18f28cc4 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,34 @@ +2000-11-01 Hrvoje Niksic + + * main.c (main): In case of opt.downloaded overflowing, print + instead of a totally bogus random value. + + * retr.c (retrieve_from_file): Ditto. + + * recur.c (recursive_retrieve): Ditto. + + * main.c (main): Ditto. + + * http.c (http_loop): Ditto. + + * ftp.c (ftp_loop_internal): Use downloaded_increase() instead of + `+=', and downloaded_exceeds_quota() instead of the simple-minded + check. + (ftp_retrieve_list): Ditto. + (ftp_retrieve_dirs): Ditto. + (ftp_retrieve_glob): Ditto. + + * retr.c (downloaded_increase): New function. Notice overflows of + opt.downloaded. + (downloaded_exceeds_quota): Make sure that opt.downloaded is not + used if it overflowed. + + * options.h (struct options): New member downloaded_overflow. + +2000-11-01 Hrvoje Niksic + + * wget.h (enum): Remove extra space after last enumeration. + 2000-11-01 Hrvoje Niksic * main.c (main): Use legible_very_long() for printing diff --git a/src/ftp.c b/src/ftp.c index 7496a5ae..d2399311 100644 --- a/src/ftp.c +++ b/src/ftp.c @@ -1006,7 +1006,7 @@ ftp_loop_internal (struct urlinfo *u, struct fileinfo *f, ccon *con) /* --dont-remove-listing was specified, so do count this towards the number of bytes and files downloaded. */ { - opt.downloaded += len; + downloaded_increase (len); opt.numurls++; } @@ -1021,7 +1021,7 @@ ftp_loop_internal (struct urlinfo *u, struct fileinfo *f, ccon *con) downloaded if they're going to be deleted. People seeding proxies, for instance, may want to know how many bytes and files they've downloaded through it. */ - opt.downloaded += len; + downloaded_increase (len); opt.numurls++; if (opt.delete_after) @@ -1142,7 +1142,7 @@ ftp_retrieve_list (struct urlinfo *u, struct fileinfo *f, ccon *con) while (f) { - if (opt.quota && opt.downloaded > opt.quota) + if (downloaded_exceeds_quota ()) { --depth; return QUOTEXC; @@ -1308,7 +1308,7 @@ ftp_retrieve_dirs (struct urlinfo *u, struct fileinfo *f, ccon *con) { int len; - if (opt.quota && opt.downloaded > opt.quota) + if (downloaded_exceeds_quota ()) break; if (f->type != FT_DIRECTORY) continue; @@ -1429,7 +1429,7 @@ ftp_retrieve_glob (struct urlinfo *u, ccon *con, int action) } } freefileinfo (start); - if (opt.quota && opt.downloaded > opt.quota) + if (downloaded_exceeds_quota ()) return QUOTEXC; else /* #### Should we return `res' here? */ diff --git a/src/http.c b/src/http.c index 24de9bb8..37b98724 100644 --- a/src/http.c +++ b/src/http.c @@ -1223,7 +1223,7 @@ The sizes do not match (local %ld) -- retrieving.\n"), local_size); tms, u->url, hstat.len, hstat.contlen, locf, count); } ++opt.numurls; - opt.downloaded += hstat.len; + downloaded_increase (hstat.len); /* Remember that we downloaded the file for later ".orig" code. */ if (*dt & ADDED_HTML_EXTENSION) @@ -1249,7 +1249,7 @@ The sizes do not match (local %ld) -- retrieving.\n"), local_size); tms, u->url, hstat.len, locf, count); } ++opt.numurls; - opt.downloaded += hstat.len; + downloaded_increase (hstat.len); /* Remember that we downloaded the file for later ".orig" code. */ if (*dt & ADDED_HTML_EXTENSION) @@ -1278,7 +1278,7 @@ The sizes do not match (local %ld) -- retrieving.\n"), local_size); "%s URL:%s [%ld/%ld] -> \"%s\" [%d]\n", tms, u->url, hstat.len, hstat.contlen, locf, count); ++opt.numurls; - opt.downloaded += hstat.len; + downloaded_increase (hstat.len); /* Remember that we downloaded the file for later ".orig" code. */ if (*dt & ADDED_HTML_EXTENSION) diff --git a/src/main.c b/src/main.c index 3bb6b14f..956b9485 100644 --- a/src/main.c +++ b/src/main.c @@ -771,10 +771,12 @@ Can't timestamp and not clobber old files at the same time.\n")); { logprintf (LOG_NOTQUIET, _("\nFINISHED --%s--\nDownloaded: %s bytes in %d files\n"), - time_str (NULL), legible_very_long (opt.downloaded), + time_str (NULL), + (opt.downloaded_overflow ? + "" : legible_very_long (opt.downloaded)), opt.numurls); /* Print quota warning, if exceeded. */ - if (opt.quota && opt.downloaded > opt.quota) + if (downloaded_exceeds_quota ()) logprintf (LOG_NOTQUIET, _("Download quota (%s bytes) EXCEEDED!\n"), legible (opt.quota)); diff --git a/src/options.h b/src/options.h index a11de0e4..742c7547 100644 --- a/src/options.h +++ b/src/options.h @@ -105,6 +105,7 @@ struct options long quota; /* Maximum number of bytes to retrieve. */ VERY_LONG_TYPE downloaded; /* How much we downloaded already. */ + int downloaded_overflow; /* Whether the above overflowed. */ int numurls; /* Number of successfully downloaded URLs */ diff --git a/src/recur.c b/src/recur.c index 91a8fed0..0e7a7f0d 100644 --- a/src/recur.c +++ b/src/recur.c @@ -127,7 +127,7 @@ recursive_retrieve (const char *file, const char *this_url) assert (this_url != NULL); assert (file != NULL); /* If quota was exceeded earlier, bail out. */ - if (opt.quota && (opt.downloaded > opt.quota)) + if (downloaded_exceeds_quota ()) return QUOTEXC; /* Cache the current URL in the list. */ if (first_time) @@ -198,7 +198,7 @@ recursive_retrieve (const char *file, const char *this_url) for (cur_url = url_list; cur_url; cur_url = cur_url->next) { /* If quota was exceeded earlier, bail out. */ - if (opt.quota && (opt.downloaded > opt.quota)) + if (downloaded_exceeds_quota ()) break; /* Parse the URL for convenient use in other functions, as well as to get the optimized form. It also checks URL integrity. */ @@ -496,7 +496,7 @@ recursive_retrieve (const char *file, const char *this_url) FREE_MAYBE (canon_this_url); /* Decrement the recursion depth. */ --depth; - if (opt.quota && (opt.downloaded > opt.quota)) + if (downloaded_exceeds_quota ()) return QUOTEXC; else return RETROK; diff --git a/src/retr.c b/src/retr.c index 71017761..5f9327a3 100644 --- a/src/retr.c +++ b/src/retr.c @@ -534,7 +534,7 @@ retrieve_from_file (const char *file, int html, int *count) char *filename, *new_file; int dt; - if (opt.quota && opt.downloaded > opt.quota) + if (downloaded_exceeds_quota ()) { status = QUOTEXC; break; @@ -571,3 +571,37 @@ printwhat (int n1, int n2) { logputs (LOG_VERBOSE, (n1 == n2) ? _("Giving up.\n\n") : _("Retrying.\n\n")); } + +/* Increment opt.downloaded by BY_HOW_MUCH. If an overflow occurs, + set opt.downloaded_overflow to 1. */ +void +downloaded_increase (unsigned long by_how_much) +{ + VERY_LONG_TYPE old; + if (opt.downloaded_overflow) + return; + old = opt.downloaded; + opt.downloaded += by_how_much; + if (opt.downloaded < old) /* carry flag, where are you when I + need you? */ + { + /* Overflow. */ + opt.downloaded_overflow = 1; + opt.downloaded = ~((VERY_LONG_TYPE)0); + } +} + +/* Return non-zero if the downloaded amount of bytes exceeds the + desired quota. If quota is not set or if the amount overflowed, 0 + is returned. */ +int +downloaded_exceeds_quota (void) +{ + if (!opt.quota) + return 0; + if (opt.downloaded_overflow) + /* We don't really no. (Wildly) assume not. */ + return 0; + + return opt.downloaded > opt.quota; +} diff --git a/src/retr.h b/src/retr.h index 5c96ad82..308eeedf 100644 --- a/src/retr.h +++ b/src/retr.h @@ -34,4 +34,7 @@ char *rate PARAMS ((long, long)); void printwhat PARAMS ((int, int)); +void downloaded_increase PARAMS ((unsigned long)); +int downloaded_exceeds_quota PARAMS ((void)); + #endif /* RETR_H */ diff --git a/src/wget.h b/src/wget.h index 5dd71d21..aa27bc41 100644 --- a/src/wget.h +++ b/src/wget.h @@ -210,7 +210,7 @@ enum HEAD_ONLY = 0x0004, /* only send the HEAD request */ SEND_NOCACHE = 0x0008, /* send Pragma: no-cache directive */ ACCEPTRANGES = 0x0010, /* Accept-ranges header was found */ - ADDED_HTML_EXTENSION = 0x0020, /* added ".html" extension due to -E */ + ADDED_HTML_EXTENSION = 0x0020 /* added ".html" extension due to -E */ }; /* Universal error type -- used almost everywhere.