Patch from Denis Vlasenko:
* Rename a var: statbytes -> transferred * cursize == transferred, always. Nuke cursize. * Make progressmeter() a nop if !CONFIG_FEATURE_WGET_STATUSBAR (reduces #ifdef forest) * double elapsed -> int elapsed * Do not sprintf to buf first and then write(STDERR) it, just fprintf directly to stderr * Progress bar printing code made smaller * Style fixes
This commit is contained in:
parent
76ef08c5e3
commit
19a3940f15
@ -42,15 +42,19 @@ static char *gethdr(char *buf, size_t bufsiz, FILE *fp, int *istrunc);
|
|||||||
static int ftpcmd(char *s1, char *s2, FILE *fp, char *buf);
|
static int ftpcmd(char *s1, char *s2, FILE *fp, char *buf);
|
||||||
|
|
||||||
/* Globals (can be accessed from signal handlers */
|
/* Globals (can be accessed from signal handlers */
|
||||||
static off_t filesize = 0; /* content-length of the file */
|
static off_t filesize; /* content-length of the file */
|
||||||
static int chunked = 0; /* chunked transfer encoding */
|
static int chunked; /* chunked transfer encoding */
|
||||||
#ifdef CONFIG_FEATURE_WGET_STATUSBAR
|
#ifdef CONFIG_FEATURE_WGET_STATUSBAR
|
||||||
static void progressmeter(int flag);
|
static void progressmeter(int flag);
|
||||||
static char *curfile; /* Name of current file being transferred. */
|
static char *curfile; /* Name of current file being transferred. */
|
||||||
static struct timeval start; /* Time a transfer started. */
|
static struct timeval start; /* Time a transfer started. */
|
||||||
static volatile unsigned long statbytes = 0; /* Number of bytes transferred so far. */
|
static off_t transferred; /* Number of bytes transferred so far. */
|
||||||
/* For progressmeter() -- number of seconds before xfer considered "stalled" */
|
/* For progressmeter() -- number of seconds before xfer considered "stalled" */
|
||||||
static const int STALLTIME = 5;
|
enum {
|
||||||
|
STALLTIME = 5
|
||||||
|
};
|
||||||
|
#else
|
||||||
|
static inline void progressmeter(int flag) {}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
static void close_and_delete_outfile(FILE* output, char *fname_out, int do_continue)
|
static void close_and_delete_outfile(FILE* output, char *fname_out, int do_continue)
|
||||||
@ -321,7 +325,7 @@ int wget_main(int argc, char **argv)
|
|||||||
if (use_proxy) {
|
if (use_proxy) {
|
||||||
const char *format = "GET %stp://%s:%d/%s HTTP/1.1\r\n";
|
const char *format = "GET %stp://%s:%d/%s HTTP/1.1\r\n";
|
||||||
#ifdef CONFIG_FEATURE_WGET_IP6_LITERAL
|
#ifdef CONFIG_FEATURE_WGET_IP6_LITERAL
|
||||||
if (strchr (target.host, ':'))
|
if (strchr(target.host, ':'))
|
||||||
format = "GET %stp://[%s]:%d/%s HTTP/1.1\r\n";
|
format = "GET %stp://[%s]:%d/%s HTTP/1.1\r\n";
|
||||||
#endif
|
#endif
|
||||||
fprintf(sfp, format,
|
fprintf(sfp, format,
|
||||||
@ -504,17 +508,17 @@ read_response:
|
|||||||
fgets(buf, sizeof(buf), dfp);
|
fgets(buf, sizeof(buf), dfp);
|
||||||
filesize = strtol(buf, (char **) NULL, 16);
|
filesize = strtol(buf, (char **) NULL, 16);
|
||||||
}
|
}
|
||||||
#ifdef CONFIG_FEATURE_WGET_STATUSBAR
|
|
||||||
if (quiet_flag==FALSE)
|
if (quiet_flag==FALSE)
|
||||||
progressmeter(-1);
|
progressmeter(-1);
|
||||||
#endif
|
|
||||||
do {
|
do {
|
||||||
while ((filesize > 0 || !got_clen) && (n = safe_fread(buf, 1, ((chunked || got_clen) && (filesize < sizeof(buf)) ? filesize : sizeof(buf)), dfp)) > 0) {
|
while ((filesize > 0 || !got_clen) && (n = safe_fread(buf, 1, ((chunked || got_clen) && (filesize < sizeof(buf)) ? filesize : sizeof(buf)), dfp)) > 0) {
|
||||||
if (safe_fwrite(buf, 1, n, output) != n) {
|
if (safe_fwrite(buf, 1, n, output) != n) {
|
||||||
bb_perror_msg_and_die(bb_msg_write_error);
|
bb_perror_msg_and_die(bb_msg_write_error);
|
||||||
}
|
}
|
||||||
#ifdef CONFIG_FEATURE_WGET_STATUSBAR
|
#ifdef CONFIG_FEATURE_WGET_STATUSBAR
|
||||||
statbytes+=n;
|
transferred += n;
|
||||||
#endif
|
#endif
|
||||||
if (got_clen) {
|
if (got_clen) {
|
||||||
filesize -= n;
|
filesize -= n;
|
||||||
@ -534,10 +538,10 @@ read_response:
|
|||||||
bb_perror_msg_and_die(bb_msg_read_error);
|
bb_perror_msg_and_die(bb_msg_read_error);
|
||||||
}
|
}
|
||||||
} while (chunked);
|
} while (chunked);
|
||||||
#ifdef CONFIG_FEATURE_WGET_STATUSBAR
|
|
||||||
if (quiet_flag==FALSE)
|
if (quiet_flag==FALSE)
|
||||||
progressmeter(1);
|
progressmeter(1);
|
||||||
#endif
|
|
||||||
if ((use_proxy == 0) && target.is_ftp) {
|
if ((use_proxy == 0) && target.is_ftp) {
|
||||||
fclose(dfp);
|
fclose(dfp);
|
||||||
if (ftpcmd(NULL, NULL, sfp, buf) != 226)
|
if (ftpcmd(NULL, NULL, sfp, buf) != 226)
|
||||||
@ -724,13 +728,12 @@ alarmtimer(int wait)
|
|||||||
static void
|
static void
|
||||||
progressmeter(int flag)
|
progressmeter(int flag)
|
||||||
{
|
{
|
||||||
static const char prefixes[] = " KMGTP";
|
|
||||||
static struct timeval lastupdate;
|
static struct timeval lastupdate;
|
||||||
static off_t lastsize, totalsize;
|
static off_t lastsize, totalsize;
|
||||||
|
|
||||||
struct timeval now, td, wait;
|
struct timeval now, td, wait;
|
||||||
off_t cursize, abbrevsize;
|
off_t abbrevsize;
|
||||||
double elapsed;
|
int elapsed, ratio, barlength, i;
|
||||||
int ratio, barlength, i, remaining;
|
|
||||||
char buf[256];
|
char buf[256];
|
||||||
|
|
||||||
if (flag == -1) {
|
if (flag == -1) {
|
||||||
@ -741,68 +744,52 @@ progressmeter(int flag)
|
|||||||
}
|
}
|
||||||
|
|
||||||
(void) gettimeofday(&now, (struct timezone *) 0);
|
(void) gettimeofday(&now, (struct timezone *) 0);
|
||||||
cursize = statbytes;
|
ratio = 100;
|
||||||
if (totalsize != 0 && !chunked) {
|
if (totalsize != 0 && !chunked) {
|
||||||
ratio = 100.0 * cursize / totalsize;
|
ratio = (int) (100 * transferred / totalsize);
|
||||||
ratio = MAX(ratio, 0);
|
|
||||||
ratio = MIN(ratio, 100);
|
ratio = MIN(ratio, 100);
|
||||||
} else
|
}
|
||||||
ratio = 100;
|
|
||||||
|
|
||||||
snprintf(buf, sizeof(buf), "\r%-20.20s %3d%% ", curfile, ratio);
|
fprintf(stderr, "\r%-20.20s%4d%% ", curfile, ratio);
|
||||||
|
|
||||||
barlength = getttywidth() - 51;
|
barlength = getttywidth() - 51;
|
||||||
if (barlength > 0) {
|
if (barlength > 0 && barlength < sizeof(buf)) {
|
||||||
i = barlength * ratio / 100;
|
i = barlength * ratio / 100;
|
||||||
snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
|
memset(buf, '*', i);
|
||||||
"|%.*s%*s|", i,
|
memset(buf + i, ' ', barlength - i);
|
||||||
"*****************************************************************************"
|
buf[barlength] = '\0';
|
||||||
"*****************************************************************************",
|
fprintf(stderr, "|%s|", buf);
|
||||||
barlength - i, "");
|
|
||||||
}
|
}
|
||||||
i = 0;
|
i = 0;
|
||||||
abbrevsize = cursize;
|
abbrevsize = transferred;
|
||||||
while (abbrevsize >= 100000 && i < sizeof(prefixes)) {
|
while (abbrevsize >= 100000) {
|
||||||
i++;
|
i++;
|
||||||
abbrevsize >>= 10;
|
abbrevsize >>= 10;
|
||||||
}
|
}
|
||||||
snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " %5d %c%c ",
|
/* See http://en.wikipedia.org/wiki/Tera */
|
||||||
(int) abbrevsize, prefixes[i], prefixes[i] == ' ' ? ' ' :
|
fprintf(stderr, "%6d %c%c ", (int)abbrevsize, " KMGTPEZY"[i], i?'B':' ');
|
||||||
'B');
|
|
||||||
|
|
||||||
timersub(&now, &lastupdate, &wait);
|
timersub(&now, &lastupdate, &wait);
|
||||||
if (cursize > lastsize) {
|
if (transferred > lastsize) {
|
||||||
lastupdate = now;
|
lastupdate = now;
|
||||||
lastsize = cursize;
|
lastsize = transferred;
|
||||||
if (wait.tv_sec >= STALLTIME) {
|
if (wait.tv_sec >= STALLTIME)
|
||||||
start.tv_sec += wait.tv_sec;
|
timeradd(&start, &wait, &start);
|
||||||
start.tv_usec += wait.tv_usec;
|
|
||||||
}
|
|
||||||
wait.tv_sec = 0;
|
wait.tv_sec = 0;
|
||||||
}
|
}
|
||||||
timersub(&now, &start, &td);
|
timersub(&now, &start, &td);
|
||||||
elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
|
elapsed = td.tv_sec;
|
||||||
|
|
||||||
if (wait.tv_sec >= STALLTIME) {
|
if (wait.tv_sec >= STALLTIME) {
|
||||||
snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
|
fprintf(stderr, " - stalled -");
|
||||||
" - stalled -");
|
} else if (transferred <= 0 || elapsed <= 0 || transferred > totalsize || chunked) {
|
||||||
} else if (statbytes <= 0 || elapsed <= 0.0 || cursize > totalsize || chunked) {
|
fprintf(stderr, "--:--:-- ETA");
|
||||||
snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
|
|
||||||
" --:-- ETA");
|
|
||||||
} else {
|
} else {
|
||||||
remaining = (int) (totalsize / (statbytes / elapsed) - elapsed);
|
/* totalsize / (transferred/elapsed) - elapsed: */
|
||||||
i = remaining / 3600;
|
int eta = (int) (totalsize*elapsed/transferred - elapsed);
|
||||||
if (i)
|
i = eta % 3600;
|
||||||
snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
|
fprintf(stderr, "%02d:%02d:%02d ETA", eta / 3600, i / 60, i % 60);
|
||||||
"%2d:", i);
|
|
||||||
else
|
|
||||||
snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
|
|
||||||
" ");
|
|
||||||
i = remaining % 3600;
|
|
||||||
snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
|
|
||||||
"%02d:%02d ETA", i / 60, i % 60);
|
|
||||||
}
|
}
|
||||||
write(STDERR_FILENO, buf, strlen(buf));
|
|
||||||
|
|
||||||
if (flag == -1) {
|
if (flag == -1) {
|
||||||
struct sigaction sa;
|
struct sigaction sa;
|
||||||
@ -813,7 +800,7 @@ progressmeter(int flag)
|
|||||||
alarmtimer(1);
|
alarmtimer(1);
|
||||||
} else if (flag == 1) {
|
} else if (flag == 1) {
|
||||||
alarmtimer(0);
|
alarmtimer(0);
|
||||||
statbytes = 0;
|
transferred = 0;
|
||||||
putc('\n', stderr);
|
putc('\n', stderr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user