Magnus Damm | f591499 | 2009-11-08 16:34:43 +0100 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Progress bar code. |
| 4 | */ |
| 5 | /* Original copyright notice which applies to the CONFIG_FEATURE_WGET_STATUSBAR stuff, |
| 6 | * much of which was blatantly stolen from openssh. |
| 7 | */ |
| 8 | /*- |
| 9 | * Copyright (c) 1992, 1993 |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 10 | * The Regents of the University of California. All rights reserved. |
Magnus Damm | f591499 | 2009-11-08 16:34:43 +0100 | [diff] [blame] | 11 | * |
| 12 | * Redistribution and use in source and binary forms, with or without |
| 13 | * modification, are permitted provided that the following conditions |
| 14 | * are met: |
| 15 | * 1. Redistributions of source code must retain the above copyright |
| 16 | * notice, this list of conditions and the following disclaimer. |
| 17 | * 2. Redistributions in binary form must reproduce the above copyright |
| 18 | * notice, this list of conditions and the following disclaimer in the |
| 19 | * documentation and/or other materials provided with the distribution. |
| 20 | * |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 21 | * 3. BSD Advertising Clause omitted per the July 22, 1999 licensing change |
| 22 | * ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change |
Magnus Damm | f591499 | 2009-11-08 16:34:43 +0100 | [diff] [blame] | 23 | * |
| 24 | * 4. Neither the name of the University nor the names of its contributors |
| 25 | * may be used to endorse or promote products derived from this software |
| 26 | * without specific prior written permission. |
| 27 | * |
Denys Vlasenko | 95f7953 | 2017-08-02 14:26:33 +0200 | [diff] [blame] | 28 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND |
Magnus Damm | f591499 | 2009-11-08 16:34:43 +0100 | [diff] [blame] | 29 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 30 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 31 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 32 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 33 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 34 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 35 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 36 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 37 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 38 | * SUCH DAMAGE. |
| 39 | */ |
| 40 | #include "libbb.h" |
| 41 | #include "unicode.h" |
| 42 | |
| 43 | enum { |
| 44 | /* Seconds when xfer considered "stalled" */ |
| 45 | STALLTIME = 5 |
| 46 | }; |
| 47 | |
Denys Vlasenko | d55e139 | 2011-02-11 18:56:13 +0100 | [diff] [blame] | 48 | void FAST_FUNC bb_progress_init(bb_progress_t *p, const char *curfile) |
Magnus Damm | f591499 | 2009-11-08 16:34:43 +0100 | [diff] [blame] | 49 | { |
Denys Vlasenko | d55e139 | 2011-02-11 18:56:13 +0100 | [diff] [blame] | 50 | #if ENABLE_UNICODE_SUPPORT |
| 51 | init_unicode(); |
| 52 | p->curfile = unicode_conv_to_printable_fixedwidth(/*NULL,*/ curfile, 20); |
| 53 | #else |
| 54 | p->curfile = curfile; |
| 55 | #endif |
Magnus Damm | f591499 | 2009-11-08 16:34:43 +0100 | [diff] [blame] | 56 | p->start_sec = monotonic_sec(); |
Denys Vlasenko | ab8d00d | 2011-02-11 19:09:30 +0100 | [diff] [blame] | 57 | p->last_update_sec = p->start_sec; |
| 58 | p->last_change_sec = p->start_sec; |
| 59 | p->last_size = 0; |
Denys Vlasenko | edccc98 | 2018-02-13 16:48:52 +0100 | [diff] [blame] | 60 | #if 0 |
| 61 | p->last_eta = INT_MAX; |
| 62 | #endif |
Magnus Damm | f591499 | 2009-11-08 16:34:43 +0100 | [diff] [blame] | 63 | } |
| 64 | |
Denys Vlasenko | f836f01 | 2011-02-10 23:02:28 +0100 | [diff] [blame] | 65 | /* File already had beg_size bytes. |
| 66 | * Then we started downloading. |
| 67 | * We downloaded "transferred" bytes so far. |
| 68 | * Download is expected to stop when total size (beg_size + transferred) |
| 69 | * will be "totalsize" bytes. |
| 70 | * If totalsize == 0, then it is unknown. |
| 71 | */ |
Denys Vlasenko | 26602b8 | 2018-11-23 19:14:52 +0100 | [diff] [blame] | 72 | int FAST_FUNC bb_progress_update(bb_progress_t *p, |
Denys Vlasenko | f836f01 | 2011-02-10 23:02:28 +0100 | [diff] [blame] | 73 | uoff_t beg_size, |
Denys Vlasenko | 805aa9f | 2011-02-10 14:25:51 +0100 | [diff] [blame] | 74 | uoff_t transferred, |
| 75 | uoff_t totalsize) |
Magnus Damm | f591499 | 2009-11-08 16:34:43 +0100 | [diff] [blame] | 76 | { |
Denys Vlasenko | 47529d3 | 2018-02-07 23:48:34 +0100 | [diff] [blame] | 77 | char numbuf5[6]; /* 5 + 1 for NUL */ |
Magnus Damm | f591499 | 2009-11-08 16:34:43 +0100 | [diff] [blame] | 78 | unsigned since_last_update, elapsed; |
Denys Vlasenko | d3d6534 | 2015-10-23 02:01:38 +0200 | [diff] [blame] | 79 | int notty; |
Magnus Damm | f591499 | 2009-11-08 16:34:43 +0100 | [diff] [blame] | 80 | |
Denys Vlasenko | ab8d00d | 2011-02-11 19:09:30 +0100 | [diff] [blame] | 81 | //transferred = 1234; /* use for stall detection testing */ |
| 82 | //totalsize = 0; /* use for unknown size download testing */ |
| 83 | |
Bradley M. Kuhn | c97131c | 2010-08-08 02:51:20 +0200 | [diff] [blame] | 84 | elapsed = monotonic_sec(); |
Denys Vlasenko | ab8d00d | 2011-02-11 19:09:30 +0100 | [diff] [blame] | 85 | since_last_update = elapsed - p->last_update_sec; |
| 86 | p->last_update_sec = elapsed; |
Denys Vlasenko | e52e67c | 2011-02-11 12:59:11 +0100 | [diff] [blame] | 87 | |
| 88 | if (totalsize != 0 && transferred >= totalsize - beg_size) { |
| 89 | /* Last call. Do not skip this update */ |
| 90 | transferred = totalsize - beg_size; /* sanitize just in case */ |
| 91 | } |
| 92 | else if (since_last_update == 0) { |
| 93 | /* |
| 94 | * Do not update on every call |
| 95 | * (we can be called on every network read!) |
| 96 | */ |
Denys Vlasenko | 26602b8 | 2018-11-23 19:14:52 +0100 | [diff] [blame] | 97 | return -1; |
Denys Vlasenko | e52e67c | 2011-02-11 12:59:11 +0100 | [diff] [blame] | 98 | } |
Bradley M. Kuhn | c97131c | 2010-08-08 02:51:20 +0200 | [diff] [blame] | 99 | |
Denys Vlasenko | 47529d3 | 2018-02-07 23:48:34 +0100 | [diff] [blame] | 100 | /* Before we lose real, unscaled sizes, produce human-readable size string */ |
| 101 | smart_ulltoa5(beg_size + transferred, numbuf5, " kMGTPEZY")[0] = '\0'; |
| 102 | |
Denys Vlasenko | f836f01 | 2011-02-10 23:02:28 +0100 | [diff] [blame] | 103 | /* |
| 104 | * Scale sizes down if they are close to overflowing. |
| 105 | * This allows calculations like (100 * transferred / totalsize) |
| 106 | * without risking overflow: we guarantee 10 highest bits to be 0. |
| 107 | * Introduced error is less than 1 / 2^12 ~= 0.025% |
| 108 | */ |
Denys Vlasenko | 47529d3 | 2018-02-07 23:48:34 +0100 | [diff] [blame] | 109 | while (totalsize >= (1 << 20)) { |
| 110 | totalsize >>= 8; |
| 111 | beg_size >>= 8; |
| 112 | transferred >>= 8; |
Magnus Damm | f591499 | 2009-11-08 16:34:43 +0100 | [diff] [blame] | 113 | } |
Denys Vlasenko | 47529d3 | 2018-02-07 23:48:34 +0100 | [diff] [blame] | 114 | /* If they were huge, now they are scaled down to [1048575,4096] range. |
| 115 | * (N * totalsize) won't overflow 32 bits for N up to 4096. |
Denys Vlasenko | ab843e3 | 2018-02-07 17:47:39 +0100 | [diff] [blame] | 116 | */ |
| 117 | #if ULONG_MAX == 0xffffffff |
| 118 | /* 32-bit CPU, uoff_t arithmetic is complex on it, cast variables to narrower types */ |
| 119 | # define totalsize ((unsigned)totalsize) |
| 120 | # define beg_size ((unsigned)beg_size) |
| 121 | # define transferred ((unsigned)transferred) |
| 122 | #endif |
Magnus Damm | f591499 | 2009-11-08 16:34:43 +0100 | [diff] [blame] | 123 | |
Denys Vlasenko | d3d6534 | 2015-10-23 02:01:38 +0200 | [diff] [blame] | 124 | notty = !isatty(STDERR_FILENO); |
| 125 | |
Denys Vlasenko | ab8d00d | 2011-02-11 19:09:30 +0100 | [diff] [blame] | 126 | if (ENABLE_UNICODE_SUPPORT) |
Denys Vlasenko | 47529d3 | 2018-02-07 23:48:34 +0100 | [diff] [blame] | 127 | fprintf(stderr, "\r%s " + notty, p->curfile); |
Denys Vlasenko | ab8d00d | 2011-02-11 19:09:30 +0100 | [diff] [blame] | 128 | else |
Denys Vlasenko | 47529d3 | 2018-02-07 23:48:34 +0100 | [diff] [blame] | 129 | fprintf(stderr, "\r%-20.20s " + notty, p->curfile); |
Denys Vlasenko | 805aa9f | 2011-02-10 14:25:51 +0100 | [diff] [blame] | 130 | |
Denys Vlasenko | ab8d00d | 2011-02-11 19:09:30 +0100 | [diff] [blame] | 131 | if (totalsize != 0) { |
Denys Vlasenko | d3d6534 | 2015-10-23 02:01:38 +0200 | [diff] [blame] | 132 | int barlength; |
Denys Vlasenko | 47529d3 | 2018-02-07 23:48:34 +0100 | [diff] [blame] | 133 | unsigned beg_and_transferred; /* does not need uoff_t, see scaling code */ |
| 134 | unsigned ratio; |
Magnus Damm | f591499 | 2009-11-08 16:34:43 +0100 | [diff] [blame] | 135 | |
Denys Vlasenko | 47529d3 | 2018-02-07 23:48:34 +0100 | [diff] [blame] | 136 | beg_and_transferred = beg_size + transferred; |
| 137 | ratio = 100 * beg_and_transferred / totalsize; |
| 138 | /* can't overflow ^^^^^^^^^^^^^^^ */ |
| 139 | fprintf(stderr, "%3u%% ", ratio); |
| 140 | |
| 141 | barlength = get_terminal_width(2) - 48; |
| 142 | /* |
| 143 | * Must reject barlength <= 0 (terminal too narrow). While at it, |
| 144 | * also reject: 1-char bar (useless), 2-char bar (ridiculous). |
| 145 | */ |
| 146 | if (barlength > 2) { |
Denys Vlasenko | ab843e3 | 2018-02-07 17:47:39 +0100 | [diff] [blame] | 147 | if (barlength > 999) |
| 148 | barlength = 999; |
| 149 | { |
| 150 | /* god bless gcc for variable arrays :) */ |
| 151 | char buf[barlength + 1]; |
| 152 | unsigned stars = (unsigned)barlength * beg_and_transferred / totalsize; |
| 153 | /* can't overflow ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ */ |
| 154 | memset(buf, ' ', barlength); |
| 155 | buf[barlength] = '\0'; |
| 156 | memset(buf, '*', stars); |
Denys Vlasenko | 47529d3 | 2018-02-07 23:48:34 +0100 | [diff] [blame] | 157 | fprintf(stderr, "|%s| ", buf); |
Denys Vlasenko | ab843e3 | 2018-02-07 17:47:39 +0100 | [diff] [blame] | 158 | } |
Denys Vlasenko | ab8d00d | 2011-02-11 19:09:30 +0100 | [diff] [blame] | 159 | } |
Magnus Damm | f591499 | 2009-11-08 16:34:43 +0100 | [diff] [blame] | 160 | } |
Denys Vlasenko | da0df47 | 2010-08-08 04:21:50 +0200 | [diff] [blame] | 161 | |
Denys Vlasenko | 47529d3 | 2018-02-07 23:48:34 +0100 | [diff] [blame] | 162 | fputs(numbuf5, stderr); /* "NNNNk" */ |
Magnus Damm | f591499 | 2009-11-08 16:34:43 +0100 | [diff] [blame] | 163 | |
Denys Vlasenko | ab8d00d | 2011-02-11 19:09:30 +0100 | [diff] [blame] | 164 | since_last_update = elapsed - p->last_change_sec; |
| 165 | if ((unsigned)transferred != p->last_size) { |
| 166 | p->last_change_sec = elapsed; |
| 167 | p->last_size = (unsigned)transferred; |
Magnus Damm | f591499 | 2009-11-08 16:34:43 +0100 | [diff] [blame] | 168 | if (since_last_update >= STALLTIME) { |
Denys Vlasenko | e52e67c | 2011-02-11 12:59:11 +0100 | [diff] [blame] | 169 | /* We "cut out" these seconds from elapsed time |
Magnus Damm | f591499 | 2009-11-08 16:34:43 +0100 | [diff] [blame] | 170 | * by adjusting start time */ |
| 171 | p->start_sec += since_last_update; |
| 172 | } |
| 173 | since_last_update = 0; /* we are un-stalled now */ |
| 174 | } |
Denys Vlasenko | f836f01 | 2011-02-10 23:02:28 +0100 | [diff] [blame] | 175 | |
Magnus Damm | f591499 | 2009-11-08 16:34:43 +0100 | [diff] [blame] | 176 | elapsed -= p->start_sec; /* now it's "elapsed since start" */ |
| 177 | |
| 178 | if (since_last_update >= STALLTIME) { |
Denys Vlasenko | 838d4bb | 2011-02-10 23:35:52 +0100 | [diff] [blame] | 179 | fprintf(stderr, " - stalled -"); |
Denys Vlasenko | ab8d00d | 2011-02-11 19:09:30 +0100 | [diff] [blame] | 180 | } else if (!totalsize || !transferred || (int)elapsed < 0) { |
Denys Vlasenko | 838d4bb | 2011-02-10 23:35:52 +0100 | [diff] [blame] | 181 | fprintf(stderr, " --:--:-- ETA"); |
Magnus Damm | f591499 | 2009-11-08 16:34:43 +0100 | [diff] [blame] | 182 | } else { |
Denys Vlasenko | f836f01 | 2011-02-10 23:02:28 +0100 | [diff] [blame] | 183 | unsigned eta, secs, hours; |
Denys Vlasenko | ab843e3 | 2018-02-07 17:47:39 +0100 | [diff] [blame] | 184 | unsigned bytes; |
Denys Vlasenko | f836f01 | 2011-02-10 23:02:28 +0100 | [diff] [blame] | 185 | |
Denys Vlasenko | ab843e3 | 2018-02-07 17:47:39 +0100 | [diff] [blame] | 186 | bytes = totalsize - beg_size; |
Denys Vlasenko | f836f01 | 2011-02-10 23:02:28 +0100 | [diff] [blame] | 187 | |
| 188 | /* Estimated remaining time = |
Denys Vlasenko | ab843e3 | 2018-02-07 17:47:39 +0100 | [diff] [blame] | 189 | * estimated_sec_to_dl_bytes - elapsed_sec = |
| 190 | * bytes / average_bytes_sec_so_far - elapsed = |
| 191 | * bytes / (transferred/elapsed) - elapsed = |
| 192 | * bytes * elapsed / transferred - elapsed |
Denys Vlasenko | f836f01 | 2011-02-10 23:02:28 +0100 | [diff] [blame] | 193 | */ |
Denys Vlasenko | ab843e3 | 2018-02-07 17:47:39 +0100 | [diff] [blame] | 194 | eta = (unsigned long)bytes * elapsed / transferred - elapsed; |
| 195 | /* if 32bit, can overflow ^^^^^^^^^^, but this would only show bad ETA */ |
Denys Vlasenko | 838d4bb | 2011-02-10 23:35:52 +0100 | [diff] [blame] | 196 | if (eta >= 1000*60*60) |
| 197 | eta = 1000*60*60 - 1; |
Denys Vlasenko | edccc98 | 2018-02-13 16:48:52 +0100 | [diff] [blame] | 198 | #if 0 |
| 199 | /* To prevent annoying "back-and-forth" estimation jitter, |
| 200 | * if new ETA is larger than the last just by a few seconds, |
| 201 | * disregard it, and show last one. The end result is that |
| 202 | * ETA usually only decreases, unless download slows down a lot. |
| 203 | */ |
| 204 | if ((unsigned)(eta - p->last_eta) < 10) |
| 205 | eta = p->last_eta; |
| 206 | p->last_eta = eta; |
| 207 | #endif |
Denys Vlasenko | f836f01 | 2011-02-10 23:02:28 +0100 | [diff] [blame] | 208 | secs = eta % 3600; |
| 209 | hours = eta / 3600; |
Denys Vlasenko | 838d4bb | 2011-02-10 23:35:52 +0100 | [diff] [blame] | 210 | fprintf(stderr, "%3u:%02u:%02u ETA", hours, secs / 60, secs % 60); |
Magnus Damm | f591499 | 2009-11-08 16:34:43 +0100 | [diff] [blame] | 211 | } |
Denys Vlasenko | d3d6534 | 2015-10-23 02:01:38 +0200 | [diff] [blame] | 212 | if (notty) |
| 213 | fputc('\n', stderr); |
Denys Vlasenko | 26602b8 | 2018-11-23 19:14:52 +0100 | [diff] [blame] | 214 | return notty; |
Magnus Damm | f591499 | 2009-11-08 16:34:43 +0100 | [diff] [blame] | 215 | } |