blob: 5ae02493b4dbb7fb3482dc47a2535bc49b91a28f [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 Miller6ff3cad2006-03-15 11:52:09 +110026RCSID("$OpenBSD: progressmeter.c,v 1.26 2006/02/20 17:02:44 stevesk Exp $");
Damien Miller17e91c02006-03-15 11:28:34 +110027
28#include <sys/ioctl.h>
Damien Miller6fd00e02003-01-10 21:46:02 +110029
Damien Miller6ff3cad2006-03-15 11:52:09 +110030#include <signal.h>
31
Damien Miller71a51412003-01-14 22:23:23 +110032#include "progressmeter.h"
Darren Tucker06ef75b2003-08-02 23:28:38 +100033#include "atomicio.h"
Damien Miller0cbb9de2003-06-04 22:56:15 +100034#include "misc.h"
Damien Miller6fd00e02003-01-10 21:46:02 +110035
Darren Tucker06ef75b2003-08-02 23:28:38 +100036#define DEFAULT_WINSIZE 80
37#define MAX_WINSIZE 512
38#define PADDING 1 /* padding between the progress indicators */
39#define UPDATE_INTERVAL 1 /* update the progress meter every second */
40#define STALL_TIME 5 /* we're stalled after this many seconds */
Damien Miller6fd00e02003-01-10 21:46:02 +110041
Darren Tucker06ef75b2003-08-02 23:28:38 +100042/* determines whether we can output to the terminal */
43static int can_output(void);
44
45/* formats and inserts the specified size into the given buffer */
46static void format_size(char *, int, off_t);
47static void format_rate(char *, int, off_t);
48
Damien Miller05656962005-06-16 13:18:04 +100049/* window resizing */
50static void sig_winch(int);
51static void setscreensize(void);
52
Darren Tucker06ef75b2003-08-02 23:28:38 +100053/* updates the progressmeter to reflect the current state of the transfer */
54void refresh_progress_meter(void);
55
56/* signal handler for updating the progress meter */
Damien Miller6fd00e02003-01-10 21:46:02 +110057static void update_progress_meter(int);
58
Darren Tuckerfc959702004-07-17 16:12:08 +100059static time_t start; /* start progress */
60static time_t last_update; /* last progress update */
61static char *file; /* name of the file being transferred */
62static off_t end_pos; /* ending position of transfer */
63static off_t cur_pos; /* transfer position as of last refresh */
Darren Tucker06ef75b2003-08-02 23:28:38 +100064static volatile off_t *counter; /* progress counter */
Darren Tuckerfc959702004-07-17 16:12:08 +100065static long stalled; /* how long we have been stalled */
66static int bytes_per_second; /* current speed in bytes per second */
67static int win_size; /* terminal window size */
Damien Miller05656962005-06-16 13:18:04 +100068static volatile sig_atomic_t win_resized; /* for window resizing */
Damien Miller6fd00e02003-01-10 21:46:02 +110069
Darren Tucker06ef75b2003-08-02 23:28:38 +100070/* units for format_size */
71static const char unit[] = " KMGT";
Damien Miller6fd00e02003-01-10 21:46:02 +110072
Darren Tucker06ef75b2003-08-02 23:28:38 +100073static int
74can_output(void)
75{
76 return (getpgrp() == tcgetpgrp(STDOUT_FILENO));
77}
Damien Miller6fd00e02003-01-10 21:46:02 +110078
Darren Tucker06ef75b2003-08-02 23:28:38 +100079static void
80format_rate(char *buf, int size, off_t bytes)
81{
82 int i;
Damien Miller6fd00e02003-01-10 21:46:02 +110083
Darren Tucker06ef75b2003-08-02 23:28:38 +100084 bytes *= 100;
85 for (i = 0; bytes >= 100*1000 && unit[i] != 'T'; i++)
86 bytes = (bytes + 512) / 1024;
87 if (i == 0) {
88 i++;
89 bytes = (bytes + 512) / 1024;
90 }
91 snprintf(buf, size, "%3lld.%1lld%c%s",
Darren Tuckere0be3042005-11-25 14:44:55 +110092 (long long) (bytes + 5) / 100,
93 (long long) (bytes + 5) / 10 % 10,
Darren Tucker06ef75b2003-08-02 23:28:38 +100094 unit[i],
95 i ? "B" : " ");
96}
Damien Miller6fd00e02003-01-10 21:46:02 +110097
Darren Tucker06ef75b2003-08-02 23:28:38 +100098static void
99format_size(char *buf, int size, off_t bytes)
100{
101 int i;
Damien Miller6fd00e02003-01-10 21:46:02 +1100102
Darren Tucker06ef75b2003-08-02 23:28:38 +1000103 for (i = 0; bytes >= 10000 && unit[i] != 'T'; i++)
104 bytes = (bytes + 512) / 1024;
105 snprintf(buf, size, "%4lld%c%s",
Darren Tuckere0be3042005-11-25 14:44:55 +1100106 (long long) bytes,
Darren Tucker06ef75b2003-08-02 23:28:38 +1000107 unit[i],
108 i ? "B" : " ");
109}
Damien Miller6fd00e02003-01-10 21:46:02 +1100110
111void
Darren Tucker06ef75b2003-08-02 23:28:38 +1000112refresh_progress_meter(void)
Damien Miller6fd00e02003-01-10 21:46:02 +1100113{
Darren Tucker06ef75b2003-08-02 23:28:38 +1000114 char buf[MAX_WINSIZE + 1];
115 time_t now;
116 off_t transferred;
117 double elapsed;
118 int percent;
Darren Tucker7f73a492004-02-06 16:41:37 +1100119 off_t bytes_left;
Darren Tucker06ef75b2003-08-02 23:28:38 +1000120 int cur_speed;
121 int hours, minutes, seconds;
122 int i, len;
123 int file_len;
Damien Miller6fd00e02003-01-10 21:46:02 +1100124
Darren Tucker06ef75b2003-08-02 23:28:38 +1000125 transferred = *counter - cur_pos;
126 cur_pos = *counter;
127 now = time(NULL);
128 bytes_left = end_pos - cur_pos;
Damien Miller6fd00e02003-01-10 21:46:02 +1100129
Darren Tucker06ef75b2003-08-02 23:28:38 +1000130 if (bytes_left > 0)
131 elapsed = now - last_update;
Darren Tucker1fb04252003-12-09 19:07:13 +1100132 else {
Darren Tucker06ef75b2003-08-02 23:28:38 +1000133 elapsed = now - start;
Darren Tucker1fb04252003-12-09 19:07:13 +1100134 /* Calculate true total speed when done */
135 transferred = end_pos;
136 bytes_per_second = 0;
137 }
Darren Tucker06ef75b2003-08-02 23:28:38 +1000138
139 /* calculate speed */
140 if (elapsed != 0)
141 cur_speed = (transferred / elapsed);
142 else
Darren Tucker1fb04252003-12-09 19:07:13 +1100143 cur_speed = transferred;
Darren Tucker06ef75b2003-08-02 23:28:38 +1000144
145#define AGE_FACTOR 0.9
146 if (bytes_per_second != 0) {
147 bytes_per_second = (bytes_per_second * AGE_FACTOR) +
148 (cur_speed * (1.0 - AGE_FACTOR));
149 } else
150 bytes_per_second = cur_speed;
151
152 /* filename */
153 buf[0] = '\0';
154 file_len = win_size - 35;
155 if (file_len > 0) {
Damien Miller17af1762003-09-02 22:53:01 +1000156 len = snprintf(buf, file_len + 1, "\r%s", file);
Damien Millerb5829f52003-09-02 22:53:32 +1000157 if (len < 0)
158 len = 0;
Damien Miller41bfc292005-05-26 12:07:32 +1000159 if (len >= file_len + 1)
160 len = file_len;
Darren Tucker06ef75b2003-08-02 23:28:38 +1000161 for (i = len; i < file_len; i++ )
162 buf[i] = ' ';
163 buf[file_len] = '\0';
164 }
165
166 /* percent of transfer done */
167 if (end_pos != 0)
168 percent = ((float)cur_pos / end_pos) * 100;
169 else
170 percent = 100;
171 snprintf(buf + strlen(buf), win_size - strlen(buf),
172 " %3d%% ", percent);
173
174 /* amount transferred */
175 format_size(buf + strlen(buf), win_size - strlen(buf),
176 cur_pos);
177 strlcat(buf, " ", win_size);
178
179 /* bandwidth usage */
180 format_rate(buf + strlen(buf), win_size - strlen(buf),
Darren Tucker1f8311c2004-05-13 16:39:33 +1000181 (off_t)bytes_per_second);
Darren Tucker06ef75b2003-08-02 23:28:38 +1000182 strlcat(buf, "/s ", win_size);
183
184 /* ETA */
185 if (!transferred)
186 stalled += elapsed;
187 else
188 stalled = 0;
189
190 if (stalled >= STALL_TIME)
191 strlcat(buf, "- stalled -", win_size);
192 else if (bytes_per_second == 0 && bytes_left)
193 strlcat(buf, " --:-- ETA", win_size);
194 else {
195 if (bytes_left > 0)
196 seconds = bytes_left / bytes_per_second;
197 else
198 seconds = elapsed;
199
200 hours = seconds / 3600;
201 seconds -= hours * 3600;
202 minutes = seconds / 60;
203 seconds -= minutes * 60;
204
205 if (hours != 0)
206 snprintf(buf + strlen(buf), win_size - strlen(buf),
207 "%d:%02d:%02d", hours, minutes, seconds);
208 else
209 snprintf(buf + strlen(buf), win_size - strlen(buf),
210 " %02d:%02d", minutes, seconds);
211
212 if (bytes_left > 0)
213 strlcat(buf, " ETA", win_size);
214 else
215 strlcat(buf, " ", win_size);
216 }
217
Darren Tucker6cc310b2003-10-02 16:15:15 +1000218 atomicio(vwrite, STDOUT_FILENO, buf, win_size - 1);
Darren Tucker06ef75b2003-08-02 23:28:38 +1000219 last_update = now;
220}
221
222static void
223update_progress_meter(int ignore)
224{
225 int save_errno;
226
227 save_errno = errno;
228
Damien Miller05656962005-06-16 13:18:04 +1000229 if (win_resized) {
230 setscreensize();
231 win_resized = 0;
232 }
Darren Tucker06ef75b2003-08-02 23:28:38 +1000233 if (can_output())
234 refresh_progress_meter();
235
Ben Lindstrom5ade9ab2003-08-25 01:16:21 +0000236 signal(SIGALRM, update_progress_meter);
Darren Tucker06ef75b2003-08-02 23:28:38 +1000237 alarm(UPDATE_INTERVAL);
238 errno = save_errno;
239}
240
241void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000242start_progress_meter(char *f, off_t filesize, off_t *ctr)
Darren Tucker06ef75b2003-08-02 23:28:38 +1000243{
Darren Tucker06ef75b2003-08-02 23:28:38 +1000244 start = last_update = time(NULL);
245 file = f;
246 end_pos = filesize;
247 cur_pos = 0;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000248 counter = ctr;
Darren Tucker06ef75b2003-08-02 23:28:38 +1000249 stalled = 0;
250 bytes_per_second = 0;
251
Damien Miller05656962005-06-16 13:18:04 +1000252 setscreensize();
Darren Tucker06ef75b2003-08-02 23:28:38 +1000253 if (can_output())
254 refresh_progress_meter();
255
Ben Lindstrom5ade9ab2003-08-25 01:16:21 +0000256 signal(SIGALRM, update_progress_meter);
Damien Miller05656962005-06-16 13:18:04 +1000257 signal(SIGWINCH, sig_winch);
Darren Tucker06ef75b2003-08-02 23:28:38 +1000258 alarm(UPDATE_INTERVAL);
Damien Miller6fd00e02003-01-10 21:46:02 +1100259}
260
261void
Damien Miller2b92d322003-06-11 22:05:06 +1000262stop_progress_meter(void)
Damien Miller6fd00e02003-01-10 21:46:02 +1100263{
264 alarm(0);
Damien Miller6fd00e02003-01-10 21:46:02 +1100265
Darren Tucker06ef75b2003-08-02 23:28:38 +1000266 if (!can_output())
Damien Miller6fd00e02003-01-10 21:46:02 +1100267 return;
268
Darren Tucker06ef75b2003-08-02 23:28:38 +1000269 /* Ensure we complete the progress */
270 if (cur_pos != end_pos)
271 refresh_progress_meter();
Damien Miller6fd00e02003-01-10 21:46:02 +1100272
Darren Tucker06ef75b2003-08-02 23:28:38 +1000273 atomicio(vwrite, STDOUT_FILENO, "\n", 1);
Damien Miller6fd00e02003-01-10 21:46:02 +1100274}
Damien Miller05656962005-06-16 13:18:04 +1000275
276static 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}