blob: ff99acf715b4dd093572940e03b8c83ee9767d8d [file] [log] [blame]
Damien Miller6fd00e02003-01-10 21:46:02 +11001/*
Darren Tucker06ef75b2003-08-02 23:28:38 +10002 * Copyright (c) 2003 Nils Nordman. All rights reserved.
Damien Miller6fd00e02003-01-10 21:46:02 +11003 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 */
24
Damien Miller6fd00e02003-01-10 21:46:02 +110025#include "includes.h"
Damien Miller17e91c02006-03-15 11:28:34 +110026
27#include <sys/ioctl.h>
Damien Miller6fd00e02003-01-10 21:46:02 +110028
Damien Miller6ff3cad2006-03-15 11:52:09 +110029#include <signal.h>
30
Damien Miller71a51412003-01-14 22:23:23 +110031#include "progressmeter.h"
Darren Tucker06ef75b2003-08-02 23:28:38 +100032#include "atomicio.h"
Damien Miller0cbb9de2003-06-04 22:56:15 +100033#include "misc.h"
Damien Miller6fd00e02003-01-10 21:46:02 +110034
Darren Tucker06ef75b2003-08-02 23:28:38 +100035#define DEFAULT_WINSIZE 80
36#define MAX_WINSIZE 512
37#define PADDING 1 /* padding between the progress indicators */
38#define UPDATE_INTERVAL 1 /* update the progress meter every second */
39#define STALL_TIME 5 /* we're stalled after this many seconds */
Damien Miller6fd00e02003-01-10 21:46:02 +110040
Darren Tucker06ef75b2003-08-02 23:28:38 +100041/* determines whether we can output to the terminal */
42static int can_output(void);
43
44/* formats and inserts the specified size into the given buffer */
45static void format_size(char *, int, off_t);
46static void format_rate(char *, int, off_t);
47
Damien Miller05656962005-06-16 13:18:04 +100048/* window resizing */
49static void sig_winch(int);
50static void setscreensize(void);
51
Darren Tucker06ef75b2003-08-02 23:28:38 +100052/* updates the progressmeter to reflect the current state of the transfer */
53void refresh_progress_meter(void);
54
55/* signal handler for updating the progress meter */
Damien Miller6fd00e02003-01-10 21:46:02 +110056static void update_progress_meter(int);
57
Darren Tuckerfc959702004-07-17 16:12:08 +100058static time_t start; /* start progress */
59static time_t last_update; /* last progress update */
60static char *file; /* name of the file being transferred */
61static off_t end_pos; /* ending position of transfer */
62static off_t cur_pos; /* transfer position as of last refresh */
Darren Tucker06ef75b2003-08-02 23:28:38 +100063static volatile off_t *counter; /* progress counter */
Darren Tuckerfc959702004-07-17 16:12:08 +100064static long stalled; /* how long we have been stalled */
65static int bytes_per_second; /* current speed in bytes per second */
66static int win_size; /* terminal window size */
Damien Miller05656962005-06-16 13:18:04 +100067static volatile sig_atomic_t win_resized; /* for window resizing */
Damien Miller6fd00e02003-01-10 21:46:02 +110068
Darren Tucker06ef75b2003-08-02 23:28:38 +100069/* units for format_size */
70static const char unit[] = " KMGT";
Damien Miller6fd00e02003-01-10 21:46:02 +110071
Darren Tucker06ef75b2003-08-02 23:28:38 +100072static int
73can_output(void)
74{
75 return (getpgrp() == tcgetpgrp(STDOUT_FILENO));
76}
Damien Miller6fd00e02003-01-10 21:46:02 +110077
Darren Tucker06ef75b2003-08-02 23:28:38 +100078static void
79format_rate(char *buf, int size, off_t bytes)
80{
81 int i;
Damien Miller6fd00e02003-01-10 21:46:02 +110082
Darren Tucker06ef75b2003-08-02 23:28:38 +100083 bytes *= 100;
84 for (i = 0; bytes >= 100*1000 && unit[i] != 'T'; i++)
85 bytes = (bytes + 512) / 1024;
86 if (i == 0) {
87 i++;
88 bytes = (bytes + 512) / 1024;
89 }
90 snprintf(buf, size, "%3lld.%1lld%c%s",
Darren Tuckere0be3042005-11-25 14:44:55 +110091 (long long) (bytes + 5) / 100,
92 (long long) (bytes + 5) / 10 % 10,
Darren Tucker06ef75b2003-08-02 23:28:38 +100093 unit[i],
94 i ? "B" : " ");
95}
Damien Miller6fd00e02003-01-10 21:46:02 +110096
Darren Tucker06ef75b2003-08-02 23:28:38 +100097static void
98format_size(char *buf, int size, off_t bytes)
99{
100 int i;
Damien Miller6fd00e02003-01-10 21:46:02 +1100101
Darren Tucker06ef75b2003-08-02 23:28:38 +1000102 for (i = 0; bytes >= 10000 && unit[i] != 'T'; i++)
103 bytes = (bytes + 512) / 1024;
104 snprintf(buf, size, "%4lld%c%s",
Darren Tuckere0be3042005-11-25 14:44:55 +1100105 (long long) bytes,
Darren Tucker06ef75b2003-08-02 23:28:38 +1000106 unit[i],
107 i ? "B" : " ");
108}
Damien Miller6fd00e02003-01-10 21:46:02 +1100109
110void
Darren Tucker06ef75b2003-08-02 23:28:38 +1000111refresh_progress_meter(void)
Damien Miller6fd00e02003-01-10 21:46:02 +1100112{
Darren Tucker06ef75b2003-08-02 23:28:38 +1000113 char buf[MAX_WINSIZE + 1];
114 time_t now;
115 off_t transferred;
116 double elapsed;
117 int percent;
Darren Tucker7f73a492004-02-06 16:41:37 +1100118 off_t bytes_left;
Darren Tucker06ef75b2003-08-02 23:28:38 +1000119 int cur_speed;
120 int hours, minutes, seconds;
121 int i, len;
122 int file_len;
Damien Miller6fd00e02003-01-10 21:46:02 +1100123
Darren Tucker06ef75b2003-08-02 23:28:38 +1000124 transferred = *counter - cur_pos;
125 cur_pos = *counter;
126 now = time(NULL);
127 bytes_left = end_pos - cur_pos;
Damien Miller6fd00e02003-01-10 21:46:02 +1100128
Darren Tucker06ef75b2003-08-02 23:28:38 +1000129 if (bytes_left > 0)
130 elapsed = now - last_update;
Darren Tucker1fb04252003-12-09 19:07:13 +1100131 else {
Darren Tucker06ef75b2003-08-02 23:28:38 +1000132 elapsed = now - start;
Darren Tucker1fb04252003-12-09 19:07:13 +1100133 /* Calculate true total speed when done */
134 transferred = end_pos;
135 bytes_per_second = 0;
136 }
Darren Tucker06ef75b2003-08-02 23:28:38 +1000137
138 /* calculate speed */
139 if (elapsed != 0)
140 cur_speed = (transferred / elapsed);
141 else
Darren Tucker1fb04252003-12-09 19:07:13 +1100142 cur_speed = transferred;
Darren Tucker06ef75b2003-08-02 23:28:38 +1000143
144#define AGE_FACTOR 0.9
145 if (bytes_per_second != 0) {
146 bytes_per_second = (bytes_per_second * AGE_FACTOR) +
147 (cur_speed * (1.0 - AGE_FACTOR));
148 } else
149 bytes_per_second = cur_speed;
150
151 /* filename */
152 buf[0] = '\0';
153 file_len = win_size - 35;
154 if (file_len > 0) {
Damien Miller17af1762003-09-02 22:53:01 +1000155 len = snprintf(buf, file_len + 1, "\r%s", file);
Damien Millerb5829f52003-09-02 22:53:32 +1000156 if (len < 0)
157 len = 0;
Damien Miller41bfc292005-05-26 12:07:32 +1000158 if (len >= file_len + 1)
159 len = file_len;
Darren Tucker06ef75b2003-08-02 23:28:38 +1000160 for (i = len; i < file_len; i++ )
161 buf[i] = ' ';
162 buf[file_len] = '\0';
163 }
164
165 /* percent of transfer done */
166 if (end_pos != 0)
167 percent = ((float)cur_pos / end_pos) * 100;
168 else
169 percent = 100;
170 snprintf(buf + strlen(buf), win_size - strlen(buf),
171 " %3d%% ", percent);
172
173 /* amount transferred */
174 format_size(buf + strlen(buf), win_size - strlen(buf),
175 cur_pos);
176 strlcat(buf, " ", win_size);
177
178 /* bandwidth usage */
179 format_rate(buf + strlen(buf), win_size - strlen(buf),
Darren Tucker1f8311c2004-05-13 16:39:33 +1000180 (off_t)bytes_per_second);
Darren Tucker06ef75b2003-08-02 23:28:38 +1000181 strlcat(buf, "/s ", win_size);
182
183 /* ETA */
184 if (!transferred)
185 stalled += elapsed;
186 else
187 stalled = 0;
188
189 if (stalled >= STALL_TIME)
190 strlcat(buf, "- stalled -", win_size);
191 else if (bytes_per_second == 0 && bytes_left)
192 strlcat(buf, " --:-- ETA", win_size);
193 else {
194 if (bytes_left > 0)
195 seconds = bytes_left / bytes_per_second;
196 else
197 seconds = elapsed;
198
199 hours = seconds / 3600;
200 seconds -= hours * 3600;
201 minutes = seconds / 60;
202 seconds -= minutes * 60;
203
204 if (hours != 0)
205 snprintf(buf + strlen(buf), win_size - strlen(buf),
206 "%d:%02d:%02d", hours, minutes, seconds);
207 else
208 snprintf(buf + strlen(buf), win_size - strlen(buf),
209 " %02d:%02d", minutes, seconds);
210
211 if (bytes_left > 0)
212 strlcat(buf, " ETA", win_size);
213 else
214 strlcat(buf, " ", win_size);
215 }
216
Darren Tucker6cc310b2003-10-02 16:15:15 +1000217 atomicio(vwrite, STDOUT_FILENO, buf, win_size - 1);
Darren Tucker06ef75b2003-08-02 23:28:38 +1000218 last_update = now;
219}
220
221static void
222update_progress_meter(int ignore)
223{
224 int save_errno;
225
226 save_errno = errno;
227
Damien Miller05656962005-06-16 13:18:04 +1000228 if (win_resized) {
229 setscreensize();
230 win_resized = 0;
231 }
Darren Tucker06ef75b2003-08-02 23:28:38 +1000232 if (can_output())
233 refresh_progress_meter();
234
Ben Lindstrom5ade9ab2003-08-25 01:16:21 +0000235 signal(SIGALRM, update_progress_meter);
Darren Tucker06ef75b2003-08-02 23:28:38 +1000236 alarm(UPDATE_INTERVAL);
237 errno = save_errno;
238}
239
240void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000241start_progress_meter(char *f, off_t filesize, off_t *ctr)
Darren Tucker06ef75b2003-08-02 23:28:38 +1000242{
Darren Tucker06ef75b2003-08-02 23:28:38 +1000243 start = last_update = time(NULL);
244 file = f;
245 end_pos = filesize;
246 cur_pos = 0;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000247 counter = ctr;
Darren Tucker06ef75b2003-08-02 23:28:38 +1000248 stalled = 0;
249 bytes_per_second = 0;
250
Damien Miller05656962005-06-16 13:18:04 +1000251 setscreensize();
Darren Tucker06ef75b2003-08-02 23:28:38 +1000252 if (can_output())
253 refresh_progress_meter();
254
Ben Lindstrom5ade9ab2003-08-25 01:16:21 +0000255 signal(SIGALRM, update_progress_meter);
Damien Miller05656962005-06-16 13:18:04 +1000256 signal(SIGWINCH, sig_winch);
Darren Tucker06ef75b2003-08-02 23:28:38 +1000257 alarm(UPDATE_INTERVAL);
Damien Miller6fd00e02003-01-10 21:46:02 +1100258}
259
260void
Damien Miller2b92d322003-06-11 22:05:06 +1000261stop_progress_meter(void)
Damien Miller6fd00e02003-01-10 21:46:02 +1100262{
263 alarm(0);
Damien Miller6fd00e02003-01-10 21:46:02 +1100264
Darren Tucker06ef75b2003-08-02 23:28:38 +1000265 if (!can_output())
Damien Miller6fd00e02003-01-10 21:46:02 +1100266 return;
267
Darren Tucker06ef75b2003-08-02 23:28:38 +1000268 /* Ensure we complete the progress */
269 if (cur_pos != end_pos)
270 refresh_progress_meter();
Damien Miller6fd00e02003-01-10 21:46:02 +1100271
Darren Tucker06ef75b2003-08-02 23:28:38 +1000272 atomicio(vwrite, STDOUT_FILENO, "\n", 1);
Damien Miller6fd00e02003-01-10 21:46:02 +1100273}
Damien Miller05656962005-06-16 13:18:04 +1000274
Damien Millerf0b15df2006-03-26 13:59:20 +1100275/*ARGSUSED*/
Damien Miller05656962005-06-16 13:18:04 +1000276static void
277sig_winch(int sig)
278{
279 win_resized = 1;
280}
281
282static void
283setscreensize(void)
284{
285 struct winsize winsize;
286
287 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &winsize) != -1 &&
288 winsize.ws_col != 0) {
289 if (winsize.ws_col > MAX_WINSIZE)
290 win_size = MAX_WINSIZE;
291 else
292 win_size = winsize.ws_col;
293 } else
294 win_size = DEFAULT_WINSIZE;
295 win_size += 1; /* trailing \0 */
296}