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 | * |
| 28 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 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 | |
| 48 | static unsigned int get_tty2_width(void) |
| 49 | { |
| 50 | unsigned width; |
| 51 | get_terminal_width_height(2, &width, NULL); |
| 52 | return width; |
| 53 | } |
| 54 | |
| 55 | void FAST_FUNC bb_progress_init(bb_progress_t *p) |
| 56 | { |
| 57 | p->start_sec = monotonic_sec(); |
| 58 | p->lastupdate_sec = p->start_sec; |
| 59 | p->lastsize = 0; |
Denys Vlasenko | cbcc123 | 2010-03-05 23:38:54 +0100 | [diff] [blame] | 60 | p->inited = 1; |
Magnus Damm | f591499 | 2009-11-08 16:34:43 +0100 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | void FAST_FUNC bb_progress_update(bb_progress_t *p, |
| 64 | const char *curfile, |
| 65 | off_t beg_range, |
| 66 | off_t transferred, |
| 67 | off_t totalsize) |
| 68 | { |
Bradley M. Kuhn | c97131c | 2010-08-08 02:51:20 +0200 | [diff] [blame] | 69 | uoff_t beg_and_transferred; |
Magnus Damm | f591499 | 2009-11-08 16:34:43 +0100 | [diff] [blame] | 70 | unsigned since_last_update, elapsed; |
| 71 | unsigned ratio; |
| 72 | int barlength, i; |
| 73 | |
Bradley M. Kuhn | c97131c | 2010-08-08 02:51:20 +0200 | [diff] [blame] | 74 | /* totalsize == 0 if it is unknown */ |
| 75 | |
| 76 | elapsed = monotonic_sec(); |
| 77 | since_last_update = elapsed - p->lastupdate_sec; |
| 78 | /* Do not update on every call |
Denys Vlasenko | da0df47 | 2010-08-08 04:21:50 +0200 | [diff] [blame] | 79 | * (we can be called on every network read!) */ |
Bradley M. Kuhn | c97131c | 2010-08-08 02:51:20 +0200 | [diff] [blame] | 80 | if (since_last_update == 0 && !totalsize) |
| 81 | return; |
| 82 | |
| 83 | beg_and_transferred = beg_range + transferred; |
Magnus Damm | f591499 | 2009-11-08 16:34:43 +0100 | [diff] [blame] | 84 | ratio = 100; |
Bradley M. Kuhn | c97131c | 2010-08-08 02:51:20 +0200 | [diff] [blame] | 85 | if (beg_and_transferred < totalsize) { |
| 86 | /* Do not update on every call |
Denys Vlasenko | da0df47 | 2010-08-08 04:21:50 +0200 | [diff] [blame] | 87 | * (we can be called on every network read!) */ |
Bradley M. Kuhn | c97131c | 2010-08-08 02:51:20 +0200 | [diff] [blame] | 88 | if (since_last_update == 0) |
| 89 | return; |
Magnus Damm | f591499 | 2009-11-08 16:34:43 +0100 | [diff] [blame] | 90 | /* long long helps to have it working even if !LFS */ |
Bradley M. Kuhn | c97131c | 2010-08-08 02:51:20 +0200 | [diff] [blame] | 91 | ratio = 100ULL * beg_and_transferred / (uoff_t)totalsize; |
Magnus Damm | f591499 | 2009-11-08 16:34:43 +0100 | [diff] [blame] | 92 | } |
| 93 | |
Denys Vlasenko | 19158a8 | 2010-03-26 14:06:56 +0100 | [diff] [blame] | 94 | #if ENABLE_UNICODE_SUPPORT |
Denys Vlasenko | 2805502 | 2010-01-04 20:49:58 +0100 | [diff] [blame] | 95 | init_unicode(); |
Magnus Damm | f591499 | 2009-11-08 16:34:43 +0100 | [diff] [blame] | 96 | /* libbb candidate? */ |
| 97 | { |
| 98 | wchar_t wbuf21[21]; |
| 99 | char *buf = xstrdup(curfile); |
| 100 | unsigned len; |
| 101 | |
| 102 | /* trim to 20 wide chars max (sets wbuf21[20] to 0) |
| 103 | * also, in case mbstowcs fails, we at least |
| 104 | * dont get garbage */ |
| 105 | memset(wbuf21, 0, sizeof(wbuf21)); |
| 106 | /* convert to wide chars, no more than 20 */ |
| 107 | len = mbstowcs(wbuf21, curfile, 20); /* NB: may return -1 */ |
| 108 | /* back to multibyte; cant overflow */ |
| 109 | wcstombs(buf, wbuf21, INT_MAX); |
| 110 | len = (len > 20) ? 0 : 20 - len; |
Bradley M. Kuhn | c97131c | 2010-08-08 02:51:20 +0200 | [diff] [blame] | 111 | fprintf(stderr, "\r%s%*s%4u%% ", buf, len, "", ratio); |
Magnus Damm | f591499 | 2009-11-08 16:34:43 +0100 | [diff] [blame] | 112 | free(buf); |
| 113 | } |
| 114 | #else |
Bradley M. Kuhn | c97131c | 2010-08-08 02:51:20 +0200 | [diff] [blame] | 115 | fprintf(stderr, "\r%-20.20s%4u%% ", curfile, ratio); |
Magnus Damm | f591499 | 2009-11-08 16:34:43 +0100 | [diff] [blame] | 116 | #endif |
| 117 | |
| 118 | barlength = get_tty2_width() - 49; |
| 119 | if (barlength > 0) { |
| 120 | /* god bless gcc for variable arrays :) */ |
Denys Vlasenko | da0df47 | 2010-08-08 04:21:50 +0200 | [diff] [blame] | 121 | char buf[barlength + 1]; |
| 122 | unsigned stars = (unsigned)barlength * ratio / (unsigned)100; |
| 123 | memset(buf, ' ', barlength); |
| 124 | buf[barlength] = '\0'; |
| 125 | memset(buf, '*', stars); |
| 126 | fprintf(stderr, "|%s|", buf); |
Magnus Damm | f591499 | 2009-11-08 16:34:43 +0100 | [diff] [blame] | 127 | } |
Denys Vlasenko | da0df47 | 2010-08-08 04:21:50 +0200 | [diff] [blame] | 128 | |
Magnus Damm | f591499 | 2009-11-08 16:34:43 +0100 | [diff] [blame] | 129 | i = 0; |
Bradley M. Kuhn | c97131c | 2010-08-08 02:51:20 +0200 | [diff] [blame] | 130 | while (beg_and_transferred >= 100000) { |
Magnus Damm | f591499 | 2009-11-08 16:34:43 +0100 | [diff] [blame] | 131 | i++; |
Bradley M. Kuhn | c97131c | 2010-08-08 02:51:20 +0200 | [diff] [blame] | 132 | beg_and_transferred >>= 10; |
Magnus Damm | f591499 | 2009-11-08 16:34:43 +0100 | [diff] [blame] | 133 | } |
| 134 | /* see http://en.wikipedia.org/wiki/Tera */ |
Bradley M. Kuhn | c97131c | 2010-08-08 02:51:20 +0200 | [diff] [blame] | 135 | fprintf(stderr, "%6u%c ", (unsigned)beg_and_transferred, " kMGTPEZY"[i]); |
| 136 | #define beg_and_transferred dont_use_beg_and_transferred_below |
Magnus Damm | f591499 | 2009-11-08 16:34:43 +0100 | [diff] [blame] | 137 | |
Magnus Damm | f591499 | 2009-11-08 16:34:43 +0100 | [diff] [blame] | 138 | if (transferred > p->lastsize) { |
| 139 | p->lastupdate_sec = elapsed; |
| 140 | p->lastsize = transferred; |
| 141 | if (since_last_update >= STALLTIME) { |
| 142 | /* We "cut off" these seconds from elapsed time |
| 143 | * by adjusting start time */ |
| 144 | p->start_sec += since_last_update; |
| 145 | } |
| 146 | since_last_update = 0; /* we are un-stalled now */ |
| 147 | } |
| 148 | elapsed -= p->start_sec; /* now it's "elapsed since start" */ |
| 149 | |
| 150 | if (since_last_update >= STALLTIME) { |
| 151 | fprintf(stderr, " - stalled -"); |
| 152 | } else { |
| 153 | off_t to_download = totalsize - beg_range; |
| 154 | if (!totalsize || transferred <= 0 || (int)elapsed <= 0 || transferred > to_download) { |
| 155 | fprintf(stderr, "--:--:-- ETA"); |
| 156 | } else { |
| 157 | /* to_download / (transferred/elapsed) - elapsed: */ |
Magnus Damm | f591499 | 2009-11-08 16:34:43 +0100 | [diff] [blame] | 158 | /* (long long helps to have working ETA even if !LFS) */ |
Denys Vlasenko | da0df47 | 2010-08-08 04:21:50 +0200 | [diff] [blame] | 159 | unsigned eta = (unsigned long long)to_download*elapsed/(uoff_t)transferred - elapsed; |
| 160 | unsigned secs = eta % 3600; |
| 161 | fprintf(stderr, "%02u:%02u:%02u ETA", eta / 3600, secs / 60, secs % 60); |
Magnus Damm | f591499 | 2009-11-08 16:34:43 +0100 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | } |