blob: 1062e9a0d02e163e0b53c90b2b6cc6a85f77607d [file] [log] [blame]
Magnus Dammf5914992009-11-08 16:34:43 +01001/* 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 Vlasenkoe4dcba12010-10-28 18:57:19 +020010 * The Regents of the University of California. All rights reserved.
Magnus Dammf5914992009-11-08 16:34:43 +010011 *
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 Vlasenkoe4dcba12010-10-28 18:57:19 +020021 * 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 Dammf5914992009-11-08 16:34:43 +010023 *
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
43enum {
44 /* Seconds when xfer considered "stalled" */
45 STALLTIME = 5
46};
47
48static unsigned int get_tty2_width(void)
49{
50 unsigned width;
51 get_terminal_width_height(2, &width, NULL);
52 return width;
53}
54
55void 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 Vlasenkocbcc1232010-03-05 23:38:54 +010060 p->inited = 1;
Magnus Dammf5914992009-11-08 16:34:43 +010061}
62
Denys Vlasenkof836f012011-02-10 23:02:28 +010063/* File already had beg_size bytes.
64 * Then we started downloading.
65 * We downloaded "transferred" bytes so far.
66 * Download is expected to stop when total size (beg_size + transferred)
67 * will be "totalsize" bytes.
68 * If totalsize == 0, then it is unknown.
69 */
Magnus Dammf5914992009-11-08 16:34:43 +010070void FAST_FUNC bb_progress_update(bb_progress_t *p,
71 const char *curfile,
Denys Vlasenkof836f012011-02-10 23:02:28 +010072 uoff_t beg_size,
Denys Vlasenko805aa9f2011-02-10 14:25:51 +010073 uoff_t transferred,
74 uoff_t totalsize)
Magnus Dammf5914992009-11-08 16:34:43 +010075{
Bradley M. Kuhnc97131c2010-08-08 02:51:20 +020076 uoff_t beg_and_transferred;
Magnus Dammf5914992009-11-08 16:34:43 +010077 unsigned since_last_update, elapsed;
78 unsigned ratio;
Denys Vlasenko805aa9f2011-02-10 14:25:51 +010079 int barlength;
80 int kiloscale;
Magnus Dammf5914992009-11-08 16:34:43 +010081
Bradley M. Kuhnc97131c2010-08-08 02:51:20 +020082 elapsed = monotonic_sec();
83 since_last_update = elapsed - p->lastupdate_sec;
Denys Vlasenkoe52e67c2011-02-11 12:59:11 +010084
85 if (totalsize != 0 && transferred >= totalsize - beg_size) {
86 /* Last call. Do not skip this update */
87 transferred = totalsize - beg_size; /* sanitize just in case */
88 }
89 else if (since_last_update == 0) {
90 /*
91 * Do not update on every call
92 * (we can be called on every network read!)
93 */
Denys Vlasenko84dba9c2011-01-10 12:51:44 +010094 return;
Denys Vlasenkoe52e67c2011-02-11 12:59:11 +010095 }
Bradley M. Kuhnc97131c2010-08-08 02:51:20 +020096
Denys Vlasenko805aa9f2011-02-10 14:25:51 +010097 kiloscale = 0;
Denys Vlasenkof836f012011-02-10 23:02:28 +010098 /*
99 * Scale sizes down if they are close to overflowing.
100 * This allows calculations like (100 * transferred / totalsize)
101 * without risking overflow: we guarantee 10 highest bits to be 0.
102 * Introduced error is less than 1 / 2^12 ~= 0.025%
103 */
104 if (ULONG_MAX > 0xffffffff || sizeof(off_t) == 4 || sizeof(off_t) != 8) {
105 /*
106 * 64-bit CPU || small off_t: in either case,
107 * >> is cheap, single-word operation.
Denys Vlasenkoe52e67c2011-02-11 12:59:11 +0100108 * ... || strange off_t: also use this code
109 * (it is safe, just suboptimal wrt code size),
110 * because 32/64 optimized one works only for 64-bit off_t.
Denys Vlasenkof836f012011-02-10 23:02:28 +0100111 */
112 if (totalsize >= (1 << 22)) {
113 totalsize >>= 10;
114 beg_size >>= 10;
115 transferred >>= 10;
Denys Vlasenkof836f012011-02-10 23:02:28 +0100116 kiloscale = 1;
117 }
118 } else {
119 /* 32-bit CPU and 64-bit off_t.
Denys Vlasenkoe52e67c2011-02-11 12:59:11 +0100120 * Use a 40-bit shift, it is easier to do on 32-bit CPU.
Denys Vlasenkof836f012011-02-10 23:02:28 +0100121 */
122 if (totalsize >= (uoff_t)(1ULL << 54)) {
123 totalsize = (uint32_t)(totalsize >> 32) >> 8;
124 beg_size = (uint32_t)(beg_size >> 32) >> 8;
125 transferred = (uint32_t)(transferred >> 32) >> 8;
Denys Vlasenkof836f012011-02-10 23:02:28 +0100126 kiloscale = 4;
127 }
Magnus Dammf5914992009-11-08 16:34:43 +0100128 }
129
Denys Vlasenkoe52e67c2011-02-11 12:59:11 +0100130 beg_and_transferred = beg_size + transferred;
Denys Vlasenko805aa9f2011-02-10 14:25:51 +0100131
132 ratio = 100 * beg_and_transferred / totalsize;
Denys Vlasenko19158a82010-03-26 14:06:56 +0100133#if ENABLE_UNICODE_SUPPORT
Denys Vlasenko28055022010-01-04 20:49:58 +0100134 init_unicode();
Magnus Dammf5914992009-11-08 16:34:43 +0100135 {
Denys Vlasenkodc7e5c42011-01-11 13:08:28 +0100136 char *buf = unicode_conv_to_printable_fixedwidth(/*NULL,*/ curfile, 20);
Denys Vlasenko0f44c082011-01-10 16:10:29 +0100137 fprintf(stderr, "\r%s%4u%% ", buf, ratio);
Magnus Dammf5914992009-11-08 16:34:43 +0100138 free(buf);
139 }
140#else
Bradley M. Kuhnc97131c2010-08-08 02:51:20 +0200141 fprintf(stderr, "\r%-20.20s%4u%% ", curfile, ratio);
Magnus Dammf5914992009-11-08 16:34:43 +0100142#endif
143
144 barlength = get_tty2_width() - 49;
145 if (barlength > 0) {
146 /* god bless gcc for variable arrays :) */
Denys Vlasenkoda0df472010-08-08 04:21:50 +0200147 char buf[barlength + 1];
Denys Vlasenko805aa9f2011-02-10 14:25:51 +0100148 unsigned stars = (unsigned)barlength * beg_and_transferred / totalsize;
Denys Vlasenkoda0df472010-08-08 04:21:50 +0200149 memset(buf, ' ', barlength);
150 buf[barlength] = '\0';
151 memset(buf, '*', stars);
152 fprintf(stderr, "|%s|", buf);
Magnus Dammf5914992009-11-08 16:34:43 +0100153 }
Denys Vlasenkoda0df472010-08-08 04:21:50 +0200154
Bradley M. Kuhnc97131c2010-08-08 02:51:20 +0200155 while (beg_and_transferred >= 100000) {
Bradley M. Kuhnc97131c2010-08-08 02:51:20 +0200156 beg_and_transferred >>= 10;
Denys Vlasenkof836f012011-02-10 23:02:28 +0100157 kiloscale++;
Magnus Dammf5914992009-11-08 16:34:43 +0100158 }
159 /* see http://en.wikipedia.org/wiki/Tera */
Denys Vlasenko838d4bb2011-02-10 23:35:52 +0100160 fprintf(stderr, "%6u%c", (unsigned)beg_and_transferred, " kMGTPEZY"[kiloscale]);
Denys Vlasenko805aa9f2011-02-10 14:25:51 +0100161#define beg_and_transferred dont_use_beg_and_transferred_below()
Magnus Dammf5914992009-11-08 16:34:43 +0100162
Denys Vlasenkof836f012011-02-10 23:02:28 +0100163 if (transferred != p->lastsize) {
Magnus Dammf5914992009-11-08 16:34:43 +0100164 p->lastupdate_sec = elapsed;
165 p->lastsize = transferred;
166 if (since_last_update >= STALLTIME) {
Denys Vlasenkoe52e67c2011-02-11 12:59:11 +0100167 /* We "cut out" these seconds from elapsed time
Magnus Dammf5914992009-11-08 16:34:43 +0100168 * by adjusting start time */
169 p->start_sec += since_last_update;
170 }
171 since_last_update = 0; /* we are un-stalled now */
172 }
Denys Vlasenkof836f012011-02-10 23:02:28 +0100173
Magnus Dammf5914992009-11-08 16:34:43 +0100174 elapsed -= p->start_sec; /* now it's "elapsed since start" */
175
176 if (since_last_update >= STALLTIME) {
Denys Vlasenko838d4bb2011-02-10 23:35:52 +0100177 fprintf(stderr, " - stalled -");
Denys Vlasenkof836f012011-02-10 23:02:28 +0100178 } else if (!totalsize || !transferred || (int)elapsed <= 0) {
Denys Vlasenko838d4bb2011-02-10 23:35:52 +0100179 fprintf(stderr, " --:--:-- ETA");
Magnus Dammf5914992009-11-08 16:34:43 +0100180 } else {
Denys Vlasenkof836f012011-02-10 23:02:28 +0100181 unsigned eta, secs, hours;
182
183 totalsize -= beg_size; /* now it's "total to upload" */
184
185 /* Estimated remaining time =
186 * estimated_sec_to_dl_totalsize_bytes - elapsed_sec =
187 * totalsize / average_bytes_sec_so_far - elapsed =
188 * totalsize / (transferred/elapsed) - elapsed =
189 * totalsize * elapsed / transferred - elapsed
190 */
191 eta = totalsize * elapsed / transferred - elapsed;
Denys Vlasenko838d4bb2011-02-10 23:35:52 +0100192 if (eta >= 1000*60*60)
193 eta = 1000*60*60 - 1;
Denys Vlasenkof836f012011-02-10 23:02:28 +0100194 secs = eta % 3600;
195 hours = eta / 3600;
Denys Vlasenko838d4bb2011-02-10 23:35:52 +0100196 fprintf(stderr, "%3u:%02u:%02u ETA", hours, secs / 60, secs % 60);
Magnus Dammf5914992009-11-08 16:34:43 +0100197 }
198}