blob: 17272fd175864717ac1e734766363416747f6f57 [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 *
Denys Vlasenko95f79532017-08-02 14:26:33 +020028 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND
Magnus Dammf5914992009-11-08 16:34:43 +010029 * 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
Denys Vlasenkod55e1392011-02-11 18:56:13 +010048void FAST_FUNC bb_progress_init(bb_progress_t *p, const char *curfile)
Magnus Dammf5914992009-11-08 16:34:43 +010049{
Denys Vlasenkod55e1392011-02-11 18:56:13 +010050#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 Dammf5914992009-11-08 16:34:43 +010056 p->start_sec = monotonic_sec();
Denys Vlasenkoab8d00d2011-02-11 19:09:30 +010057 p->last_update_sec = p->start_sec;
58 p->last_change_sec = p->start_sec;
59 p->last_size = 0;
Magnus Dammf5914992009-11-08 16:34:43 +010060}
61
Denys Vlasenkof836f012011-02-10 23:02:28 +010062/* File already had beg_size bytes.
63 * Then we started downloading.
64 * We downloaded "transferred" bytes so far.
65 * Download is expected to stop when total size (beg_size + transferred)
66 * will be "totalsize" bytes.
67 * If totalsize == 0, then it is unknown.
68 */
Magnus Dammf5914992009-11-08 16:34:43 +010069void FAST_FUNC bb_progress_update(bb_progress_t *p,
Denys Vlasenkof836f012011-02-10 23:02:28 +010070 uoff_t beg_size,
Denys Vlasenko805aa9f2011-02-10 14:25:51 +010071 uoff_t transferred,
72 uoff_t totalsize)
Magnus Dammf5914992009-11-08 16:34:43 +010073{
Denys Vlasenkoab843e32018-02-07 17:47:39 +010074 unsigned beg_and_transferred; /* does not need uoff_t, see scaling code below */
Magnus Dammf5914992009-11-08 16:34:43 +010075 unsigned since_last_update, elapsed;
Denys Vlasenkod3d65342015-10-23 02:01:38 +020076 int notty;
Denys Vlasenko805aa9f2011-02-10 14:25:51 +010077 int kiloscale;
Magnus Dammf5914992009-11-08 16:34:43 +010078
Denys Vlasenkoab8d00d2011-02-11 19:09:30 +010079 //transferred = 1234; /* use for stall detection testing */
80 //totalsize = 0; /* use for unknown size download testing */
81
Bradley M. Kuhnc97131c2010-08-08 02:51:20 +020082 elapsed = monotonic_sec();
Denys Vlasenkoab8d00d2011-02-11 19:09:30 +010083 since_last_update = elapsed - p->last_update_sec;
84 p->last_update_sec = elapsed;
Denys Vlasenkoe52e67c2011-02-11 12:59:11 +010085
86 if (totalsize != 0 && transferred >= totalsize - beg_size) {
87 /* Last call. Do not skip this update */
88 transferred = totalsize - beg_size; /* sanitize just in case */
89 }
90 else if (since_last_update == 0) {
91 /*
92 * Do not update on every call
93 * (we can be called on every network read!)
94 */
Denys Vlasenko84dba9c2011-01-10 12:51:44 +010095 return;
Denys Vlasenkoe52e67c2011-02-11 12:59:11 +010096 }
Bradley M. Kuhnc97131c2010-08-08 02:51:20 +020097
Denys Vlasenko805aa9f2011-02-10 14:25:51 +010098 kiloscale = 0;
Denys Vlasenkof836f012011-02-10 23:02:28 +010099 /*
100 * Scale sizes down if they are close to overflowing.
101 * This allows calculations like (100 * transferred / totalsize)
102 * without risking overflow: we guarantee 10 highest bits to be 0.
103 * Introduced error is less than 1 / 2^12 ~= 0.025%
104 */
Denys Vlasenkoab843e32018-02-07 17:47:39 +0100105 while (totalsize >= (1 << 22)) {
106 totalsize >>= 10;
107 beg_size >>= 10;
108 transferred >>= 10;
109 kiloscale++;
Magnus Dammf5914992009-11-08 16:34:43 +0100110 }
Denys Vlasenkoab843e32018-02-07 17:47:39 +0100111 /* If they were huge, now they are scaled down to [4194303,4096] range.
112 * (N * totalsize) won't overflow 32 bits for N up to 1024.
113 * The downside is that files larger than 4194303 kbytes (>4GB)
114 * never show kbytes download size, they show "0M","1M"... right away
115 * since kiloscale is already >1.
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 Dammf5914992009-11-08 16:34:43 +0100123
Denys Vlasenkod3d65342015-10-23 02:01:38 +0200124 notty = !isatty(STDERR_FILENO);
125
Denys Vlasenkoab8d00d2011-02-11 19:09:30 +0100126 if (ENABLE_UNICODE_SUPPORT)
Denys Vlasenkod3d65342015-10-23 02:01:38 +0200127 fprintf(stderr, "\r%s" + notty, p->curfile);
Denys Vlasenkoab8d00d2011-02-11 19:09:30 +0100128 else
Denys Vlasenkod3d65342015-10-23 02:01:38 +0200129 fprintf(stderr, "\r%-20.20s" + notty, p->curfile);
Denys Vlasenkoab8d00d2011-02-11 19:09:30 +0100130
Denys Vlasenkoe52e67c2011-02-11 12:59:11 +0100131 beg_and_transferred = beg_size + transferred;
Denys Vlasenko805aa9f2011-02-10 14:25:51 +0100132
Denys Vlasenkoab8d00d2011-02-11 19:09:30 +0100133 if (totalsize != 0) {
Denys Vlasenkod3d65342015-10-23 02:01:38 +0200134 int barlength;
Denys Vlasenkoab8d00d2011-02-11 19:09:30 +0100135 unsigned ratio = 100 * beg_and_transferred / totalsize;
136 fprintf(stderr, "%4u%%", ratio);
Magnus Dammf5914992009-11-08 16:34:43 +0100137
Denys Vlasenko641caae2015-10-23 01:44:22 +0200138 barlength = get_terminal_width(2) - 49;
Denys Vlasenkoab8d00d2011-02-11 19:09:30 +0100139 if (barlength > 0) {
Denys Vlasenkoab843e32018-02-07 17:47:39 +0100140 if (barlength > 999)
141 barlength = 999;
142 {
143 /* god bless gcc for variable arrays :) */
144 char buf[barlength + 1];
145 unsigned stars = (unsigned)barlength * beg_and_transferred / totalsize;
146 /* can't overflow ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ */
147 memset(buf, ' ', barlength);
148 buf[barlength] = '\0';
149 memset(buf, '*', stars);
150 fprintf(stderr, " |%s|", buf);
151 }
Denys Vlasenkoab8d00d2011-02-11 19:09:30 +0100152 }
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 Vlasenkoab8d00d2011-02-11 19:09:30 +0100163 since_last_update = elapsed - p->last_change_sec;
164 if ((unsigned)transferred != p->last_size) {
165 p->last_change_sec = elapsed;
166 p->last_size = (unsigned)transferred;
Magnus Dammf5914992009-11-08 16:34:43 +0100167 if (since_last_update >= STALLTIME) {
Denys Vlasenkoe52e67c2011-02-11 12:59:11 +0100168 /* We "cut out" these seconds from elapsed time
Magnus Dammf5914992009-11-08 16:34:43 +0100169 * by adjusting start time */
170 p->start_sec += since_last_update;
171 }
172 since_last_update = 0; /* we are un-stalled now */
173 }
Denys Vlasenkof836f012011-02-10 23:02:28 +0100174
Magnus Dammf5914992009-11-08 16:34:43 +0100175 elapsed -= p->start_sec; /* now it's "elapsed since start" */
176
177 if (since_last_update >= STALLTIME) {
Denys Vlasenko838d4bb2011-02-10 23:35:52 +0100178 fprintf(stderr, " - stalled -");
Denys Vlasenkoab8d00d2011-02-11 19:09:30 +0100179 } else if (!totalsize || !transferred || (int)elapsed < 0) {
Denys Vlasenko838d4bb2011-02-10 23:35:52 +0100180 fprintf(stderr, " --:--:-- ETA");
Magnus Dammf5914992009-11-08 16:34:43 +0100181 } else {
Denys Vlasenkof836f012011-02-10 23:02:28 +0100182 unsigned eta, secs, hours;
Denys Vlasenkoab843e32018-02-07 17:47:39 +0100183 unsigned bytes;
Denys Vlasenkof836f012011-02-10 23:02:28 +0100184
Denys Vlasenkoab843e32018-02-07 17:47:39 +0100185 bytes = totalsize - beg_size;
Denys Vlasenkof836f012011-02-10 23:02:28 +0100186
187 /* Estimated remaining time =
Denys Vlasenkoab843e32018-02-07 17:47:39 +0100188 * estimated_sec_to_dl_bytes - elapsed_sec =
189 * bytes / average_bytes_sec_so_far - elapsed =
190 * bytes / (transferred/elapsed) - elapsed =
191 * bytes * elapsed / transferred - elapsed
Denys Vlasenkof836f012011-02-10 23:02:28 +0100192 */
Denys Vlasenkoab843e32018-02-07 17:47:39 +0100193 eta = (unsigned long)bytes * elapsed / transferred - elapsed;
194 /* if 32bit, can overflow ^^^^^^^^^^, but this would only show bad ETA */
Denys Vlasenko838d4bb2011-02-10 23:35:52 +0100195 if (eta >= 1000*60*60)
196 eta = 1000*60*60 - 1;
Denys Vlasenkof836f012011-02-10 23:02:28 +0100197 secs = eta % 3600;
198 hours = eta / 3600;
Denys Vlasenko838d4bb2011-02-10 23:35:52 +0100199 fprintf(stderr, "%3u:%02u:%02u ETA", hours, secs / 60, secs % 60);
Magnus Dammf5914992009-11-08 16:34:43 +0100200 }
Denys Vlasenkod3d65342015-10-23 02:01:38 +0200201 if (notty)
202 fputc('\n', stderr);
Magnus Dammf5914992009-11-08 16:34:43 +0100203}