FreeBSD Bugzilla – Attachment 196911 Details for
Bug 202424
[regression] fetch(1) reports "0 B 0 Bps" in the logs
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
Remember
[x]
|
Forgot Password
Login:
[x]
[patch]
Fix end-of-transfer statistics when not on a tty
bz202424-fetch-stats.diff (text/plain), 6.91 KB, created by
Dag-Erling Smørgrav
on 2018-09-06 12:52:27 UTC
(
hide
)
Description:
Fix end-of-transfer statistics when not on a tty
Filename:
MIME Type:
Creator:
Dag-Erling Smørgrav
Created:
2018-09-06 12:52:27 UTC
Size:
6.91 KB
patch
obsolete
>Index: usr.bin/fetch/fetch.c >=================================================================== >--- usr.bin/fetch/fetch.c (revision 338493) >+++ usr.bin/fetch/fetch.c (working copy) >@@ -85,6 +85,7 @@ static int t_flag; /*! -t: workaround TCP bug * > static int U_flag; /* -U: do not use high ports */ > static int v_level = 1; /* -v: verbosity level */ > static int v_tty; /* stdout is a tty */ >+static int v_progress; /* whether to display progress */ > static pid_t pgrp; /* our process group */ > static long w_secs; /* -w: retry delay */ > static int family = PF_UNSPEC; /* -[46]: address family to use */ >@@ -199,29 +200,44 @@ struct xferstat { > }; > > /* >+ * Format a number of seconds as either XXdYYh, XXhYYm, XXmYYs, or XXs >+ * depending on its magnitude >+ */ >+static void >+stat_seconds(char *str, size_t strsz, time_t seconds) >+{ >+ >+ if (seconds > 86400) >+ snprintf(str, strsz, "%02ldd%02ldh", >+ seconds / 86400, (seconds % 86400) / 3600); >+ else if (seconds > 3600) >+ snprintf(str, strsz, "%02ldh%02ldm", >+ seconds / 3600, (seconds % 3600) / 60); >+ else if (seconds > 60) >+ snprintf(str, strsz, "%02ldm%02lds", >+ seconds / 60, seconds % 60); >+ else >+ snprintf(str, strsz, " %02lds", >+ seconds); >+} >+ >+/* > * Compute and display ETA > */ >-static const char * >-stat_eta(struct xferstat *xs) >+static void >+stat_eta(char *str, size_t strsz, const struct xferstat *xs) > { >- static char str[16]; >- long elapsed, eta; >+ time_t elapsed, eta; > off_t received, expected; > > elapsed = xs->last.tv_sec - xs->start.tv_sec; > received = xs->rcvd - xs->offset; > expected = xs->size - xs->rcvd; >- eta = (long)((double)elapsed * expected / received); >- if (eta > 3600) >- snprintf(str, sizeof str, "%02ldh%02ldm", >- eta / 3600, (eta % 3600) / 60); >- else if (eta > 0) >- snprintf(str, sizeof str, "%02ldm%02lds", >- eta / 60, eta % 60); >+ eta = (time_t)((double)elapsed * expected / received); >+ if (eta > 0) >+ stat_seconds(str, strsz, eta); > else >- snprintf(str, sizeof str, "%02ldm%02lds", >- elapsed / 60, elapsed % 60); >- return (str); >+ stat_seconds(str, strsz, elapsed); > } > > /* >@@ -228,10 +244,9 @@ struct xferstat { > * Format a number as "xxxx YB" where Y is ' ', 'k', 'M'... > */ > static const char *prefixes = " kMGTP"; >-static const char * >-stat_bytes(off_t bytes) >+static void >+stat_bytes(char *str, size_t strsz, off_t bytes) > { >- static char str[16]; > const char *prefix = prefixes; > > while (bytes > 9999 && prefix[1] != '\0') { >@@ -238,29 +253,28 @@ static const char *prefixes = " kMGTP"; > bytes /= 1024; > prefix++; > } >- snprintf(str, sizeof str, "%4jd %cB", (intmax_t)bytes, *prefix); >- return (str); >+ snprintf(str, strsz, "%4ju %cB", (uintmax_t)bytes, *prefix); > } > > /* > * Compute and display transfer rate > */ >-static const char * >-stat_bps(struct xferstat *xs) >+static void >+stat_bps(char *str, size_t strsz, struct xferstat *xs) > { >- static char str[16]; >+ char bytes[16]; > double delta, bps; > >- delta = (xs->last.tv_sec + (xs->last.tv_usec / 1.e6)) >- - (xs->last2.tv_sec + (xs->last2.tv_usec / 1.e6)); >+ delta = ((double)xs->last.tv_sec + (xs->last.tv_usec / 1.e6)) >+ - ((double)xs->last2.tv_sec + (xs->last2.tv_usec / 1.e6)); > > if (delta == 0.0) { >- snprintf(str, sizeof str, "?? Bps"); >+ snprintf(str, strsz, "?? Bps"); > } else { > bps = (xs->rcvd - xs->lastrcvd) / delta; >- snprintf(str, sizeof str, "%sps", stat_bytes((off_t)bps)); >+ stat_bytes(bytes, sizeof bytes, (off_t)bps); >+ snprintf(str, strsz, "%sps", bytes); > } >- return (str); > } > > /* >@@ -269,11 +283,12 @@ static const char *prefixes = " kMGTP"; > static void > stat_display(struct xferstat *xs, int force) > { >+ char bytes[16], bps[16], eta[16]; > struct timeval now; > int ctty_pgrp; > > /* check if we're the foreground process */ >- if (ioctl(STDERR_FILENO, TIOCGPGRP, &ctty_pgrp) == -1 || >+ if (ioctl(STDERR_FILENO, TIOCGPGRP, &ctty_pgrp) != 0 || > (pid_t)ctty_pgrp != pgrp) > return; > >@@ -284,26 +299,31 @@ stat_display(struct xferstat *xs, int force) > xs->last = now; > > fprintf(stderr, "\r%-46.46s", xs->name); >- if (xs->size <= 0) { >- setproctitle("%s [%s]", xs->name, stat_bytes(xs->rcvd)); >- fprintf(stderr, " %s", stat_bytes(xs->rcvd)); >+ if (xs->rcvd >= xs->size) { >+ stat_bytes(bytes, sizeof bytes, xs->rcvd); >+ setproctitle("%s [%s]", xs->name, bytes); >+ fprintf(stderr, " %s", bytes); > } else { >+ stat_bytes(bytes, sizeof bytes, xs->size); > setproctitle("%s [%d%% of %s]", xs->name, > (int)((100.0 * xs->rcvd) / xs->size), >- stat_bytes(xs->size)); >+ bytes); > fprintf(stderr, "%3d%% of %s", > (int)((100.0 * xs->rcvd) / xs->size), >- stat_bytes(xs->size)); >+ bytes); > } > if (force == 2) { > xs->lastrcvd = xs->offset; > xs->last2 = xs->start; > } >- fprintf(stderr, " %s", stat_bps(xs)); >+ stat_bps(bps, sizeof bps, xs); >+ fprintf(stderr, " %s", bps); > if ((xs->size > 0 && xs->rcvd > 0 && > xs->last.tv_sec >= xs->start.tv_sec + 3) || >- force == 2) >- fprintf(stderr, " %s", stat_eta(xs)); >+ force == 2) { >+ stat_eta(eta, sizeof eta, xs); >+ fprintf(stderr, " %s", eta); >+ } > xs->lastrcvd = xs->rcvd; > } > >@@ -313,14 +333,16 @@ stat_display(struct xferstat *xs, int force) > static void > stat_start(struct xferstat *xs, const char *name, off_t size, off_t offset) > { >+ >+ memset(xs, 0, sizeof *xs); > snprintf(xs->name, sizeof xs->name, "%s", name); > gettimeofday(&xs->start, NULL); >- xs->last.tv_sec = xs->last.tv_usec = 0; >+ xs->last2 = xs->last = xs->start; > xs->size = size; > xs->offset = offset; > xs->rcvd = offset; > xs->lastrcvd = offset; >- if (v_tty && v_level > 0) >+ if (v_progress) > stat_display(xs, 1); > else if (v_level > 0) > fprintf(stderr, "%-46s", xs->name); >@@ -332,8 +354,9 @@ stat_start(struct xferstat *xs, const char *name, > static void > stat_update(struct xferstat *xs, off_t rcvd) > { >+ > xs->rcvd = rcvd; >- if (v_tty && v_level > 0) >+ if (v_progress) > stat_display(xs, 0); > } > >@@ -343,13 +366,17 @@ stat_update(struct xferstat *xs, off_t rcvd) > static void > stat_end(struct xferstat *xs) > { >+ char bytes[16], bps[16], eta[16]; >+ > gettimeofday(&xs->last, NULL); >- if (v_tty && v_level > 0) { >+ if (v_progress) { > stat_display(xs, 2); > putc('\n', stderr); > } else if (v_level > 0) { >- fprintf(stderr, " %s %s\n", >- stat_bytes(xs->size), stat_bps(xs)); >+ stat_bytes(bytes, sizeof bytes, xs->size); >+ stat_bps(bps, sizeof bps, xs); >+ stat_eta(eta, sizeof eta, xs); >+ fprintf(stderr, " %s %s %s\n", bytes, bps, eta); > } > } > >@@ -557,7 +584,6 @@ fetch(char *URL, const char *path) > fetchLastErrCode == FETCH_OK && > strcmp(fetchLastErrString, "Not Modified") == 0) { > /* HTTP Not Modified Response, return OK. */ >- r = 0; > goto done; > } else > goto failure; >@@ -1110,7 +1136,8 @@ main(int argc, char *argv[]) > > /* check if output is to a tty (for progress report) */ > v_tty = isatty(STDERR_FILENO); >- if (v_tty) >+ v_progress = v_tty && v_level > 0; >+ if (v_progress) > pgrp = getpgrp(); > > r = 0;
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
Attachments on
bug 202424
:
196911
|
196912
|
197011