1*90685d2cSjp161948 /*
2*90685d2cSjp161948 * Copyright (c) 2003 Nils Nordman. All rights reserved.
3*90685d2cSjp161948 *
4*90685d2cSjp161948 * Redistribution and use in source and binary forms, with or without
5*90685d2cSjp161948 * modification, are permitted provided that the following conditions
6*90685d2cSjp161948 * are met:
7*90685d2cSjp161948 * 1. Redistributions of source code must retain the above copyright
8*90685d2cSjp161948 * notice, this list of conditions and the following disclaimer.
9*90685d2cSjp161948 * 2. Redistributions in binary form must reproduce the above copyright
10*90685d2cSjp161948 * notice, this list of conditions and the following disclaimer in the
11*90685d2cSjp161948 * documentation and/or other materials provided with the distribution.
12*90685d2cSjp161948 *
13*90685d2cSjp161948 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14*90685d2cSjp161948 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15*90685d2cSjp161948 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16*90685d2cSjp161948 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17*90685d2cSjp161948 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18*90685d2cSjp161948 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19*90685d2cSjp161948 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20*90685d2cSjp161948 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21*90685d2cSjp161948 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22*90685d2cSjp161948 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23*90685d2cSjp161948 */
24*90685d2cSjp161948
25*90685d2cSjp161948 /* $OpenBSD: progressmeter.c,v 1.37 2006/08/03 03:34:42 deraadt Exp $ */
26*90685d2cSjp161948
27*90685d2cSjp161948 #pragma ident "%Z%%M% %I% %E% SMI"
28*90685d2cSjp161948
29*90685d2cSjp161948 #include "includes.h"
30*90685d2cSjp161948
31*90685d2cSjp161948 #include <sys/types.h>
32*90685d2cSjp161948 #include <sys/ioctl.h>
33*90685d2cSjp161948 #include <sys/uio.h>
34*90685d2cSjp161948
35*90685d2cSjp161948 #include <errno.h>
36*90685d2cSjp161948 #include <signal.h>
37*90685d2cSjp161948 #include <stdio.h>
38*90685d2cSjp161948 #include <string.h>
39*90685d2cSjp161948 #include <time.h>
40*90685d2cSjp161948 #include <unistd.h>
41*90685d2cSjp161948
42*90685d2cSjp161948 #include "progressmeter.h"
43*90685d2cSjp161948 #include "atomicio.h"
44*90685d2cSjp161948 #include "misc.h"
45*90685d2cSjp161948
46*90685d2cSjp161948 #define DEFAULT_WINSIZE 80
47*90685d2cSjp161948 #define MAX_WINSIZE 512
48*90685d2cSjp161948 #define PADDING 1 /* padding between the progress indicators */
49*90685d2cSjp161948 #define UPDATE_INTERVAL 1 /* update the progress meter every second */
50*90685d2cSjp161948 #define STALL_TIME 5 /* we're stalled after this many seconds */
51*90685d2cSjp161948
52*90685d2cSjp161948 /* determines whether we can output to the terminal */
53*90685d2cSjp161948 static int can_output(void);
54*90685d2cSjp161948
55*90685d2cSjp161948 /* formats and inserts the specified size into the given buffer */
56*90685d2cSjp161948 static void format_size(char *, int, off_t);
57*90685d2cSjp161948 static void format_rate(char *, int, off_t);
58*90685d2cSjp161948
59*90685d2cSjp161948 /* window resizing */
60*90685d2cSjp161948 static void sig_winch(int);
61*90685d2cSjp161948 static void setscreensize(void);
62*90685d2cSjp161948
63*90685d2cSjp161948 /* updates the progressmeter to reflect the current state of the transfer */
64*90685d2cSjp161948 void refresh_progress_meter(void);
65*90685d2cSjp161948
66*90685d2cSjp161948 /* signal handler for updating the progress meter */
67*90685d2cSjp161948 static void update_progress_meter(int);
68*90685d2cSjp161948
69*90685d2cSjp161948 static time_t start; /* start progress */
70*90685d2cSjp161948 static time_t last_update; /* last progress update */
71*90685d2cSjp161948 static char *file; /* name of the file being transferred */
72*90685d2cSjp161948 static off_t end_pos; /* ending position of transfer */
73*90685d2cSjp161948 static off_t cur_pos; /* transfer position as of last refresh */
74*90685d2cSjp161948 static volatile off_t *counter; /* progress counter */
75*90685d2cSjp161948 static long stalled; /* how long we have been stalled */
76*90685d2cSjp161948 static int bytes_per_second; /* current speed in bytes per second */
77*90685d2cSjp161948 static int win_size; /* terminal window size */
78*90685d2cSjp161948 static volatile sig_atomic_t win_resized; /* for window resizing */
79*90685d2cSjp161948
80*90685d2cSjp161948 /* units for format_size */
81*90685d2cSjp161948 static const char unit[] = " KMGT";
82*90685d2cSjp161948
83*90685d2cSjp161948 static int
can_output(void)84*90685d2cSjp161948 can_output(void)
85*90685d2cSjp161948 {
86*90685d2cSjp161948 return (getpgrp() == tcgetpgrp(STDOUT_FILENO));
87*90685d2cSjp161948 }
88*90685d2cSjp161948
89*90685d2cSjp161948 static void
format_rate(char * buf,int size,off_t bytes)90*90685d2cSjp161948 format_rate(char *buf, int size, off_t bytes)
91*90685d2cSjp161948 {
92*90685d2cSjp161948 int i;
93*90685d2cSjp161948
94*90685d2cSjp161948 bytes *= 100;
95*90685d2cSjp161948 for (i = 0; bytes >= 100*1000 && unit[i] != 'T'; i++)
96*90685d2cSjp161948 bytes = (bytes + 512) / 1024;
97*90685d2cSjp161948 if (i == 0) {
98*90685d2cSjp161948 i++;
99*90685d2cSjp161948 bytes = (bytes + 512) / 1024;
100*90685d2cSjp161948 }
101*90685d2cSjp161948 snprintf(buf, size, "%3lld.%1lld%c%s",
102*90685d2cSjp161948 (long long) (bytes + 5) / 100,
103*90685d2cSjp161948 (long long) (bytes + 5) / 10 % 10,
104*90685d2cSjp161948 unit[i],
105*90685d2cSjp161948 i ? "B" : " ");
106*90685d2cSjp161948 }
107*90685d2cSjp161948
108*90685d2cSjp161948 static void
format_size(char * buf,int size,off_t bytes)109*90685d2cSjp161948 format_size(char *buf, int size, off_t bytes)
110*90685d2cSjp161948 {
111*90685d2cSjp161948 int i;
112*90685d2cSjp161948
113*90685d2cSjp161948 for (i = 0; bytes >= 10000 && unit[i] != 'T'; i++)
114*90685d2cSjp161948 bytes = (bytes + 512) / 1024;
115*90685d2cSjp161948 snprintf(buf, size, "%4lld%c%s",
116*90685d2cSjp161948 (long long) bytes,
117*90685d2cSjp161948 unit[i],
118*90685d2cSjp161948 i ? "B" : " ");
119*90685d2cSjp161948 }
120*90685d2cSjp161948
121*90685d2cSjp161948 void
refresh_progress_meter(void)122*90685d2cSjp161948 refresh_progress_meter(void)
123*90685d2cSjp161948 {
124*90685d2cSjp161948 char buf[MAX_WINSIZE + 1];
125*90685d2cSjp161948 time_t now;
126*90685d2cSjp161948 off_t transferred;
127*90685d2cSjp161948 double elapsed;
128*90685d2cSjp161948 int percent;
129*90685d2cSjp161948 off_t bytes_left;
130*90685d2cSjp161948 int cur_speed;
131*90685d2cSjp161948 int hours, minutes, seconds;
132*90685d2cSjp161948 int i, len;
133*90685d2cSjp161948 int file_len;
134*90685d2cSjp161948
135*90685d2cSjp161948 transferred = *counter - cur_pos;
136*90685d2cSjp161948 cur_pos = *counter;
137*90685d2cSjp161948 now = time(NULL);
138*90685d2cSjp161948 bytes_left = end_pos - cur_pos;
139*90685d2cSjp161948
140*90685d2cSjp161948 if (bytes_left > 0)
141*90685d2cSjp161948 elapsed = now - last_update;
142*90685d2cSjp161948 else {
143*90685d2cSjp161948 elapsed = now - start;
144*90685d2cSjp161948 /* Calculate true total speed when done */
145*90685d2cSjp161948 transferred = end_pos;
146*90685d2cSjp161948 bytes_per_second = 0;
147*90685d2cSjp161948 }
148*90685d2cSjp161948
149*90685d2cSjp161948 /* calculate speed */
150*90685d2cSjp161948 if (elapsed != 0)
151*90685d2cSjp161948 cur_speed = (int)(transferred / elapsed);
152*90685d2cSjp161948 else
153*90685d2cSjp161948 cur_speed = transferred;
154*90685d2cSjp161948
155*90685d2cSjp161948 #define AGE_FACTOR 0.9
156*90685d2cSjp161948 if (bytes_per_second != 0) {
157*90685d2cSjp161948 bytes_per_second = (int)((bytes_per_second * AGE_FACTOR) +
158*90685d2cSjp161948 (cur_speed * (1.0 - AGE_FACTOR)));
159*90685d2cSjp161948 } else
160*90685d2cSjp161948 bytes_per_second = cur_speed;
161*90685d2cSjp161948
162*90685d2cSjp161948 /* filename */
163*90685d2cSjp161948 buf[0] = '\0';
164*90685d2cSjp161948 file_len = win_size - 35;
165*90685d2cSjp161948 if (file_len > 0) {
166*90685d2cSjp161948 len = snprintf(buf, file_len + 1, "\r%s", file);
167*90685d2cSjp161948 if (len < 0)
168*90685d2cSjp161948 len = 0;
169*90685d2cSjp161948 if (len >= file_len + 1)
170*90685d2cSjp161948 len = file_len;
171*90685d2cSjp161948 for (i = len; i < file_len; i++)
172*90685d2cSjp161948 buf[i] = ' ';
173*90685d2cSjp161948 buf[file_len] = '\0';
174*90685d2cSjp161948 }
175*90685d2cSjp161948
176*90685d2cSjp161948 /* percent of transfer done */
177*90685d2cSjp161948 if (end_pos != 0)
178*90685d2cSjp161948 percent = (int)(((float)cur_pos / end_pos) * 100);
179*90685d2cSjp161948 else
180*90685d2cSjp161948 percent = 100;
181*90685d2cSjp161948 snprintf(buf + strlen(buf), win_size - strlen(buf),
182*90685d2cSjp161948 " %3d%% ", percent);
183*90685d2cSjp161948
184*90685d2cSjp161948 /* amount transferred */
185*90685d2cSjp161948 format_size(buf + strlen(buf), win_size - strlen(buf),
186*90685d2cSjp161948 cur_pos);
187*90685d2cSjp161948 strlcat(buf, " ", win_size);
188*90685d2cSjp161948
189*90685d2cSjp161948 /* bandwidth usage */
190*90685d2cSjp161948 format_rate(buf + strlen(buf), win_size - strlen(buf),
191*90685d2cSjp161948 (off_t)bytes_per_second);
192*90685d2cSjp161948 strlcat(buf, "/s ", win_size);
193*90685d2cSjp161948
194*90685d2cSjp161948 /* ETA */
195*90685d2cSjp161948 if (!transferred)
196*90685d2cSjp161948 stalled += elapsed;
197*90685d2cSjp161948 else
198*90685d2cSjp161948 stalled = 0;
199*90685d2cSjp161948
200*90685d2cSjp161948 if (stalled >= STALL_TIME)
201*90685d2cSjp161948 strlcat(buf, "- stalled -", win_size);
202*90685d2cSjp161948 else if (bytes_per_second == 0 && bytes_left)
203*90685d2cSjp161948 strlcat(buf, " --:-- ETA", win_size);
204*90685d2cSjp161948 else {
205*90685d2cSjp161948 if (bytes_left > 0)
206*90685d2cSjp161948 seconds = bytes_left / bytes_per_second;
207*90685d2cSjp161948 else
208*90685d2cSjp161948 seconds = (int)elapsed;
209*90685d2cSjp161948
210*90685d2cSjp161948 hours = seconds / 3600;
211*90685d2cSjp161948 seconds -= hours * 3600;
212*90685d2cSjp161948 minutes = seconds / 60;
213*90685d2cSjp161948 seconds -= minutes * 60;
214*90685d2cSjp161948
215*90685d2cSjp161948 if (hours != 0)
216*90685d2cSjp161948 snprintf(buf + strlen(buf), win_size - strlen(buf),
217*90685d2cSjp161948 "%d:%02d:%02d", hours, minutes, seconds);
218*90685d2cSjp161948 else
219*90685d2cSjp161948 snprintf(buf + strlen(buf), win_size - strlen(buf),
220*90685d2cSjp161948 " %02d:%02d", minutes, seconds);
221*90685d2cSjp161948
222*90685d2cSjp161948 if (bytes_left > 0)
223*90685d2cSjp161948 strlcat(buf, " ETA", win_size);
224*90685d2cSjp161948 else
225*90685d2cSjp161948 strlcat(buf, " ", win_size);
226*90685d2cSjp161948 }
227*90685d2cSjp161948
228*90685d2cSjp161948 atomicio(vwrite, STDOUT_FILENO, buf, win_size - 1);
229*90685d2cSjp161948 last_update = now;
230*90685d2cSjp161948 }
231*90685d2cSjp161948
232*90685d2cSjp161948 /*ARGSUSED*/
233*90685d2cSjp161948 static void
update_progress_meter(int ignore)234*90685d2cSjp161948 update_progress_meter(int ignore)
235*90685d2cSjp161948 {
236*90685d2cSjp161948 int save_errno;
237*90685d2cSjp161948
238*90685d2cSjp161948 save_errno = errno;
239*90685d2cSjp161948
240*90685d2cSjp161948 if (win_resized) {
241*90685d2cSjp161948 setscreensize();
242*90685d2cSjp161948 win_resized = 0;
243*90685d2cSjp161948 }
244*90685d2cSjp161948 if (can_output())
245*90685d2cSjp161948 refresh_progress_meter();
246*90685d2cSjp161948
247*90685d2cSjp161948 signal(SIGALRM, update_progress_meter);
248*90685d2cSjp161948 alarm(UPDATE_INTERVAL);
249*90685d2cSjp161948 errno = save_errno;
250*90685d2cSjp161948 }
251*90685d2cSjp161948
252*90685d2cSjp161948 void
start_progress_meter(char * f,off_t filesize,off_t * ctr)253*90685d2cSjp161948 start_progress_meter(char *f, off_t filesize, off_t *ctr)
254*90685d2cSjp161948 {
255*90685d2cSjp161948 start = last_update = time(NULL);
256*90685d2cSjp161948 file = f;
257*90685d2cSjp161948 end_pos = filesize;
258*90685d2cSjp161948 cur_pos = 0;
259*90685d2cSjp161948 counter = ctr;
260*90685d2cSjp161948 stalled = 0;
261*90685d2cSjp161948 bytes_per_second = 0;
262*90685d2cSjp161948
263*90685d2cSjp161948 setscreensize();
264*90685d2cSjp161948 if (can_output())
265*90685d2cSjp161948 refresh_progress_meter();
266*90685d2cSjp161948
267*90685d2cSjp161948 signal(SIGALRM, update_progress_meter);
268*90685d2cSjp161948 signal(SIGWINCH, sig_winch);
269*90685d2cSjp161948 alarm(UPDATE_INTERVAL);
270*90685d2cSjp161948 }
271*90685d2cSjp161948
272*90685d2cSjp161948 void
stop_progress_meter(void)273*90685d2cSjp161948 stop_progress_meter(void)
274*90685d2cSjp161948 {
275*90685d2cSjp161948 alarm(0);
276*90685d2cSjp161948
277*90685d2cSjp161948 if (!can_output())
278*90685d2cSjp161948 return;
279*90685d2cSjp161948
280*90685d2cSjp161948 /* Ensure we complete the progress */
281*90685d2cSjp161948 if (cur_pos != end_pos)
282*90685d2cSjp161948 refresh_progress_meter();
283*90685d2cSjp161948
284*90685d2cSjp161948 atomicio(vwrite, STDOUT_FILENO, "\n", 1);
285*90685d2cSjp161948 }
286*90685d2cSjp161948
287*90685d2cSjp161948 /*ARGSUSED*/
288*90685d2cSjp161948 static void
sig_winch(int sig)289*90685d2cSjp161948 sig_winch(int sig)
290*90685d2cSjp161948 {
291*90685d2cSjp161948 win_resized = 1;
292*90685d2cSjp161948 }
293*90685d2cSjp161948
294*90685d2cSjp161948 static void
setscreensize(void)295*90685d2cSjp161948 setscreensize(void)
296*90685d2cSjp161948 {
297*90685d2cSjp161948 struct winsize winsize;
298*90685d2cSjp161948
299*90685d2cSjp161948 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &winsize) != -1 &&
300*90685d2cSjp161948 winsize.ws_col != 0) {
301*90685d2cSjp161948 if (winsize.ws_col > MAX_WINSIZE)
302*90685d2cSjp161948 win_size = MAX_WINSIZE;
303*90685d2cSjp161948 else
304*90685d2cSjp161948 win_size = winsize.ws_col;
305*90685d2cSjp161948 } else
306*90685d2cSjp161948 win_size = DEFAULT_WINSIZE;
307*90685d2cSjp161948 win_size += 1; /* trailing \0 */
308*90685d2cSjp161948 }
309