blob: 98a30e11bbc3174230c4ae1d951ddbbd08c1bcb0 [file] [log] [blame]
Darren Tucker39972492006-07-12 22:22:46 +10001/* $OpenBSD: progressmeter.c,v 1.30 2006/07/11 20:07:25 stevesk Exp $ */
Damien Miller6fd00e02003-01-10 21:46:02 +11002/*
Darren Tucker06ef75b2003-08-02 23:28:38 +10003 * Copyright (c) 2003 Nils Nordman. All rights reserved.
Damien Miller6fd00e02003-01-10 21:46:02 +11004 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
Damien Miller6fd00e02003-01-10 21:46:02 +110026#include "includes.h"
Damien Miller17e91c02006-03-15 11:28:34 +110027
28#include <sys/ioctl.h>
Damien Miller6fd00e02003-01-10 21:46:02 +110029
Darren Tucker39972492006-07-12 22:22:46 +100030#include <errno.h>
Damien Miller6ff3cad2006-03-15 11:52:09 +110031#include <signal.h>
32
Damien Miller71a51412003-01-14 22:23:23 +110033#include "progressmeter.h"
Darren Tucker06ef75b2003-08-02 23:28:38 +100034#include "atomicio.h"
Damien Miller0cbb9de2003-06-04 22:56:15 +100035#include "misc.h"
Damien Miller6fd00e02003-01-10 21:46:02 +110036
Darren Tucker06ef75b2003-08-02 23:28:38 +100037#define DEFAULT_WINSIZE 80
38#define MAX_WINSIZE 512
39#define PADDING 1 /* padding between the progress indicators */
40#define UPDATE_INTERVAL 1 /* update the progress meter every second */
41#define STALL_TIME 5 /* we're stalled after this many seconds */
Damien Miller6fd00e02003-01-10 21:46:02 +110042
Darren Tucker06ef75b2003-08-02 23:28:38 +100043/* determines whether we can output to the terminal */
44static int can_output(void);
45
46/* formats and inserts the specified size into the given buffer */
47static void format_size(char *, int, off_t);
48static void format_rate(char *, int, off_t);
49
Damien Miller05656962005-06-16 13:18:04 +100050/* window resizing */
51static void sig_winch(int);
52static void setscreensize(void);
53
Darren Tucker06ef75b2003-08-02 23:28:38 +100054/* updates the progressmeter to reflect the current state of the transfer */
55void refresh_progress_meter(void);
56
57/* signal handler for updating the progress meter */
Damien Miller6fd00e02003-01-10 21:46:02 +110058static void update_progress_meter(int);
59
Darren Tuckerfc959702004-07-17 16:12:08 +100060static time_t start; /* start progress */
61static time_t last_update; /* last progress update */
62static char *file; /* name of the file being transferred */
63static off_t end_pos; /* ending position of transfer */
64static off_t cur_pos; /* transfer position as of last refresh */
Darren Tucker06ef75b2003-08-02 23:28:38 +100065static volatile off_t *counter; /* progress counter */
Darren Tuckerfc959702004-07-17 16:12:08 +100066static long stalled; /* how long we have been stalled */
67static int bytes_per_second; /* current speed in bytes per second */
68static int win_size; /* terminal window size */
Damien Miller05656962005-06-16 13:18:04 +100069static volatile sig_atomic_t win_resized; /* for window resizing */
Damien Miller6fd00e02003-01-10 21:46:02 +110070
Darren Tucker06ef75b2003-08-02 23:28:38 +100071/* units for format_size */
72static const char unit[] = " KMGT";
Damien Miller6fd00e02003-01-10 21:46:02 +110073
Darren Tucker06ef75b2003-08-02 23:28:38 +100074static int
75can_output(void)
76{
77 return (getpgrp() == tcgetpgrp(STDOUT_FILENO));
78}
Damien Miller6fd00e02003-01-10 21:46:02 +110079
Darren Tucker06ef75b2003-08-02 23:28:38 +100080static void
81format_rate(char *buf, int size, off_t bytes)
82{
83 int i;
Damien Miller6fd00e02003-01-10 21:46:02 +110084
Darren Tucker06ef75b2003-08-02 23:28:38 +100085 bytes *= 100;
86 for (i = 0; bytes >= 100*1000 && unit[i] != 'T'; i++)
87 bytes = (bytes + 512) / 1024;
88 if (i == 0) {
89 i++;
90 bytes = (bytes + 512) / 1024;
91 }
92 snprintf(buf, size, "%3lld.%1lld%c%s",
Darren Tuckere0be3042005-11-25 14:44:55 +110093 (long long) (bytes + 5) / 100,
94 (long long) (bytes + 5) / 10 % 10,
Darren Tucker06ef75b2003-08-02 23:28:38 +100095 unit[i],
96 i ? "B" : " ");
97}
Damien Miller6fd00e02003-01-10 21:46:02 +110098
Darren Tucker06ef75b2003-08-02 23:28:38 +100099static void
100format_size(char *buf, int size, off_t bytes)
101{
102 int i;
Damien Miller6fd00e02003-01-10 21:46:02 +1100103
Darren Tucker06ef75b2003-08-02 23:28:38 +1000104 for (i = 0; bytes >= 10000 && unit[i] != 'T'; i++)
105 bytes = (bytes + 512) / 1024;
106 snprintf(buf, size, "%4lld%c%s",
Darren Tuckere0be3042005-11-25 14:44:55 +1100107 (long long) bytes,
Darren Tucker06ef75b2003-08-02 23:28:38 +1000108 unit[i],
109 i ? "B" : " ");
110}
Damien Miller6fd00e02003-01-10 21:46:02 +1100111
112void
Darren Tucker06ef75b2003-08-02 23:28:38 +1000113refresh_progress_meter(void)
Damien Miller6fd00e02003-01-10 21:46:02 +1100114{
Darren Tucker06ef75b2003-08-02 23:28:38 +1000115 char buf[MAX_WINSIZE + 1];
116 time_t now;
117 off_t transferred;
118 double elapsed;
119 int percent;
Darren Tucker7f73a492004-02-06 16:41:37 +1100120 off_t bytes_left;
Darren Tucker06ef75b2003-08-02 23:28:38 +1000121 int cur_speed;
122 int hours, minutes, seconds;
123 int i, len;
124 int file_len;
Damien Miller6fd00e02003-01-10 21:46:02 +1100125
Darren Tucker06ef75b2003-08-02 23:28:38 +1000126 transferred = *counter - cur_pos;
127 cur_pos = *counter;
128 now = time(NULL);
129 bytes_left = end_pos - cur_pos;
Damien Miller6fd00e02003-01-10 21:46:02 +1100130
Darren Tucker06ef75b2003-08-02 23:28:38 +1000131 if (bytes_left > 0)
132 elapsed = now - last_update;
Darren Tucker1fb04252003-12-09 19:07:13 +1100133 else {
Darren Tucker06ef75b2003-08-02 23:28:38 +1000134 elapsed = now - start;
Darren Tucker1fb04252003-12-09 19:07:13 +1100135 /* Calculate true total speed when done */
136 transferred = end_pos;
137 bytes_per_second = 0;
138 }
Darren Tucker06ef75b2003-08-02 23:28:38 +1000139
140 /* calculate speed */
141 if (elapsed != 0)
142 cur_speed = (transferred / elapsed);
143 else
Darren Tucker1fb04252003-12-09 19:07:13 +1100144 cur_speed = transferred;
Darren Tucker06ef75b2003-08-02 23:28:38 +1000145
146#define AGE_FACTOR 0.9
147 if (bytes_per_second != 0) {
148 bytes_per_second = (bytes_per_second * AGE_FACTOR) +
149 (cur_speed * (1.0 - AGE_FACTOR));
150 } else
151 bytes_per_second = cur_speed;
152
153 /* filename */
154 buf[0] = '\0';
155 file_len = win_size - 35;
156 if (file_len > 0) {
Damien Miller17af1762003-09-02 22:53:01 +1000157 len = snprintf(buf, file_len + 1, "\r%s", file);
Damien Millerb5829f52003-09-02 22:53:32 +1000158 if (len < 0)
159 len = 0;
Damien Miller41bfc292005-05-26 12:07:32 +1000160 if (len >= file_len + 1)
161 len = file_len;
Darren Tucker06ef75b2003-08-02 23:28:38 +1000162 for (i = len; i < file_len; i++ )
163 buf[i] = ' ';
164 buf[file_len] = '\0';
165 }
166
167 /* percent of transfer done */
168 if (end_pos != 0)
169 percent = ((float)cur_pos / end_pos) * 100;
170 else
171 percent = 100;
172 snprintf(buf + strlen(buf), win_size - strlen(buf),
173 " %3d%% ", percent);
174
175 /* amount transferred */
176 format_size(buf + strlen(buf), win_size - strlen(buf),
177 cur_pos);
178 strlcat(buf, " ", win_size);
179
180 /* bandwidth usage */
181 format_rate(buf + strlen(buf), win_size - strlen(buf),
Darren Tucker1f8311c2004-05-13 16:39:33 +1000182 (off_t)bytes_per_second);
Darren Tucker06ef75b2003-08-02 23:28:38 +1000183 strlcat(buf, "/s ", win_size);
184
185 /* ETA */
186 if (!transferred)
187 stalled += elapsed;
188 else
189 stalled = 0;
190
191 if (stalled >= STALL_TIME)
192 strlcat(buf, "- stalled -", win_size);
193 else if (bytes_per_second == 0 && bytes_left)
194 strlcat(buf, " --:-- ETA", win_size);
195 else {
196 if (bytes_left > 0)
197 seconds = bytes_left / bytes_per_second;
198 else
199 seconds = elapsed;
200
201 hours = seconds / 3600;
202 seconds -= hours * 3600;
203 minutes = seconds / 60;
204 seconds -= minutes * 60;
205
206 if (hours != 0)
207 snprintf(buf + strlen(buf), win_size - strlen(buf),
208 "%d:%02d:%02d", hours, minutes, seconds);
209 else
210 snprintf(buf + strlen(buf), win_size - strlen(buf),
211 " %02d:%02d", minutes, seconds);
212
213 if (bytes_left > 0)
214 strlcat(buf, " ETA", win_size);
215 else
216 strlcat(buf, " ", win_size);
217 }
218
Darren Tucker6cc310b2003-10-02 16:15:15 +1000219 atomicio(vwrite, STDOUT_FILENO, buf, win_size - 1);
Darren Tucker06ef75b2003-08-02 23:28:38 +1000220 last_update = now;
221}
222
223static void
224update_progress_meter(int ignore)
225{
226 int save_errno;
227
228 save_errno = errno;
229
Damien Miller05656962005-06-16 13:18:04 +1000230 if (win_resized) {
231 setscreensize();
232 win_resized = 0;
233 }
Darren Tucker06ef75b2003-08-02 23:28:38 +1000234 if (can_output())
235 refresh_progress_meter();
236
Ben Lindstrom5ade9ab2003-08-25 01:16:21 +0000237 signal(SIGALRM, update_progress_meter);
Darren Tucker06ef75b2003-08-02 23:28:38 +1000238 alarm(UPDATE_INTERVAL);
239 errno = save_errno;
240}
241
242void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000243start_progress_meter(char *f, off_t filesize, off_t *ctr)
Darren Tucker06ef75b2003-08-02 23:28:38 +1000244{
Darren Tucker06ef75b2003-08-02 23:28:38 +1000245 start = last_update = time(NULL);
246 file = f;
247 end_pos = filesize;
248 cur_pos = 0;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000249 counter = ctr;
Darren Tucker06ef75b2003-08-02 23:28:38 +1000250 stalled = 0;
251 bytes_per_second = 0;
252
Damien Miller05656962005-06-16 13:18:04 +1000253 setscreensize();
Darren Tucker06ef75b2003-08-02 23:28:38 +1000254 if (can_output())
255 refresh_progress_meter();
256
Ben Lindstrom5ade9ab2003-08-25 01:16:21 +0000257 signal(SIGALRM, update_progress_meter);
Damien Miller05656962005-06-16 13:18:04 +1000258 signal(SIGWINCH, sig_winch);
Darren Tucker06ef75b2003-08-02 23:28:38 +1000259 alarm(UPDATE_INTERVAL);
Damien Miller6fd00e02003-01-10 21:46:02 +1100260}
261
262void
Damien Miller2b92d322003-06-11 22:05:06 +1000263stop_progress_meter(void)
Damien Miller6fd00e02003-01-10 21:46:02 +1100264{
265 alarm(0);
Damien Miller6fd00e02003-01-10 21:46:02 +1100266
Darren Tucker06ef75b2003-08-02 23:28:38 +1000267 if (!can_output())
Damien Miller6fd00e02003-01-10 21:46:02 +1100268 return;
269
Darren Tucker06ef75b2003-08-02 23:28:38 +1000270 /* Ensure we complete the progress */
271 if (cur_pos != end_pos)
272 refresh_progress_meter();
Damien Miller6fd00e02003-01-10 21:46:02 +1100273
Darren Tucker06ef75b2003-08-02 23:28:38 +1000274 atomicio(vwrite, STDOUT_FILENO, "\n", 1);
Damien Miller6fd00e02003-01-10 21:46:02 +1100275}
Damien Miller05656962005-06-16 13:18:04 +1000276
Damien Millerf0b15df2006-03-26 13:59:20 +1100277/*ARGSUSED*/
Damien Miller05656962005-06-16 13:18:04 +1000278static void
279sig_winch(int sig)
280{
281 win_resized = 1;
282}
283
284static void
285setscreensize(void)
286{
287 struct winsize winsize;
288
289 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &winsize) != -1 &&
290 winsize.ws_col != 0) {
291 if (winsize.ws_col > MAX_WINSIZE)
292 win_size = MAX_WINSIZE;
293 else
294 win_size = winsize.ws_col;
295 } else
296 win_size = DEFAULT_WINSIZE;
297 win_size += 1; /* trailing \0 */
298}