1 /* vi: set sw=4 ts=4: */
5 /* Original copyright notice which applies to the CONFIG_FEATURE_WGET_STATUSBAR stuff,
6 * much of which was blatantly stolen from openssh.
9 * Copyright (c) 1992, 1993
10 * The Regents of the University of California. All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
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.
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
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.
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
40 #include <sys/types.h>
45 #include <libubox/utils.h>
49 /* Seconds when xfer considered "stalled" */
54 wh_helper(int value, int def_val, const char *env_name)
57 char *s = getenv(env_name);
61 if (value <= 1 || value >= 30000)
70 return wh_helper(0, 80, "COLUMNS");
72 struct winsize win = {0};
73 ioctl(2, TIOCGWINSZ, &win);
74 return wh_helper(win.ws_col, 80, "COLUMNS");
82 clock_gettime(CLOCK_MONOTONIC, &tv);
87 progress_init(struct progress *p, const char *curfile)
89 p->curfile = strdup(curfile);
90 p->start_sec = monotonic_sec();
91 p->last_update_sec = p->start_sec;
92 p->last_change_sec = p->start_sec;
96 /* File already had beg_size bytes.
97 * Then we started downloading.
98 * We downloaded "transferred" bytes so far.
99 * Download is expected to stop when total size (beg_size + transferred)
100 * will be "totalsize" bytes.
101 * If totalsize == 0, then it is unknown.
104 progress_update(struct progress *p, off_t beg_size,
105 off_t transferred, off_t totalsize)
107 off_t beg_and_transferred;
108 unsigned since_last_update, elapsed;
112 //transferred = 1234; /* use for stall detection testing */
113 //totalsize = 0; /* use for unknown size download testing */
115 elapsed = monotonic_sec();
116 since_last_update = elapsed - p->last_update_sec;
117 p->last_update_sec = elapsed;
119 if (totalsize != 0 && transferred >= totalsize - beg_size) {
120 /* Last call. Do not skip this update */
121 transferred = totalsize - beg_size; /* sanitize just in case */
123 else if (since_last_update == 0) {
125 * Do not update on every call
126 * (we can be called on every network read!)
133 * Scale sizes down if they are close to overflowing.
134 * This allows calculations like (100 * transferred / totalsize)
135 * without risking overflow: we guarantee 10 highest bits to be 0.
136 * Introduced error is less than 1 / 2^12 ~= 0.025%
138 if (ULONG_MAX > 0xffffffff || sizeof(off_t) == 4 || sizeof(off_t) != 8) {
140 * 64-bit CPU || small off_t: in either case,
141 * >> is cheap, single-word operation.
142 * ... || strange off_t: also use this code
143 * (it is safe, just suboptimal wrt code size),
144 * because 32/64 optimized one works only for 64-bit off_t.
146 if (totalsize >= (1 << 22)) {
153 /* 32-bit CPU and 64-bit off_t.
154 * Use a 40-bit shift, it is easier to do on 32-bit CPU.
156 /* ONE suppresses "warning: shift count >= width of type" */
157 #define ONE (sizeof(off_t) > 4)
158 if (totalsize >= (off_t)(1ULL << 54*ONE)) {
159 totalsize = (uint32_t)(totalsize >> 32*ONE) >> 8;
160 beg_size = (uint32_t)(beg_size >> 32*ONE) >> 8;
161 transferred = (uint32_t)(transferred >> 32*ONE) >> 8;
166 fprintf(stderr, "\r%-20.20s", p->curfile);
168 beg_and_transferred = beg_size + transferred;
170 if (totalsize != 0) {
171 unsigned ratio = 100 * beg_and_transferred / totalsize;
172 fprintf(stderr, "%4u%%", ratio);
174 barlength = get_tty2_width() - 49;
176 /* god bless gcc for variable arrays :) */
177 char buf[barlength + 1];
178 unsigned stars = (unsigned)barlength * beg_and_transferred / totalsize;
179 memset(buf, ' ', barlength);
180 buf[barlength] = '\0';
181 memset(buf, '*', stars);
182 fprintf(stderr, " |%s|", buf);
186 while (beg_and_transferred >= 100000) {
187 beg_and_transferred >>= 10;
190 /* see http://en.wikipedia.org/wiki/Tera */
191 fprintf(stderr, "%6u%c", (unsigned)beg_and_transferred, " kMGTPEZY"[kiloscale]);
193 since_last_update = elapsed - p->last_change_sec;
194 if ((unsigned)transferred != p->last_size) {
195 p->last_change_sec = elapsed;
196 p->last_size = (unsigned)transferred;
197 if (since_last_update >= STALLTIME) {
198 /* We "cut out" these seconds from elapsed time
199 * by adjusting start time */
200 p->start_sec += since_last_update;
202 since_last_update = 0; /* we are un-stalled now */
205 elapsed -= p->start_sec; /* now it's "elapsed since start" */
207 if (since_last_update >= STALLTIME) {
208 fprintf(stderr, " - stalled -");
209 } else if (!totalsize || !transferred || (int)elapsed < 0) {
210 fprintf(stderr, " --:--:-- ETA");
212 unsigned eta, secs, hours;
214 totalsize -= beg_size; /* now it's "total to upload" */
216 /* Estimated remaining time =
217 * estimated_sec_to_dl_totalsize_bytes - elapsed_sec =
218 * totalsize / average_bytes_sec_so_far - elapsed =
219 * totalsize / (transferred/elapsed) - elapsed =
220 * totalsize * elapsed / transferred - elapsed
222 eta = totalsize * elapsed / transferred - elapsed;
223 if (eta >= 1000*60*60)
224 eta = 1000*60*60 - 1;
227 fprintf(stderr, "%3u:%02u:%02u ETA", hours, secs / 60, secs % 60);