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