blob: febe9aad59faadf8715a99368db5f58638703a6c [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 Miller41bfc292005-05-26 12:07:32 +100026RCSID("$OpenBSD: progressmeter.c,v 1.23 2005/04/28 10:17:56 moritz Exp $");
Damien Miller6fd00e02003-01-10 21:46:02 +110027
Damien Miller71a51412003-01-14 22:23:23 +110028#include "progressmeter.h"
Darren Tucker06ef75b2003-08-02 23:28:38 +100029#include "atomicio.h"
Damien Miller0cbb9de2003-06-04 22:56:15 +100030#include "misc.h"
Damien Miller6fd00e02003-01-10 21:46:02 +110031
Darren Tucker06ef75b2003-08-02 23:28:38 +100032#define DEFAULT_WINSIZE 80
33#define MAX_WINSIZE 512
34#define PADDING 1 /* padding between the progress indicators */
35#define UPDATE_INTERVAL 1 /* update the progress meter every second */
36#define STALL_TIME 5 /* we're stalled after this many seconds */
Damien Miller6fd00e02003-01-10 21:46:02 +110037
Darren Tucker06ef75b2003-08-02 23:28:38 +100038/* determines whether we can output to the terminal */
39static int can_output(void);
40
41/* formats and inserts the specified size into the given buffer */
42static void format_size(char *, int, off_t);
43static void format_rate(char *, int, off_t);
44
45/* updates the progressmeter to reflect the current state of the transfer */
46void refresh_progress_meter(void);
47
48/* signal handler for updating the progress meter */
Damien Miller6fd00e02003-01-10 21:46:02 +110049static void update_progress_meter(int);
50
Darren Tuckerfc959702004-07-17 16:12:08 +100051static time_t start; /* start progress */
52static time_t last_update; /* last progress update */
53static char *file; /* name of the file being transferred */
54static off_t end_pos; /* ending position of transfer */
55static off_t cur_pos; /* transfer position as of last refresh */
Darren Tucker06ef75b2003-08-02 23:28:38 +100056static volatile off_t *counter; /* progress counter */
Darren Tuckerfc959702004-07-17 16:12:08 +100057static long stalled; /* how long we have been stalled */
58static int bytes_per_second; /* current speed in bytes per second */
59static int win_size; /* terminal window size */
Damien Miller6fd00e02003-01-10 21:46:02 +110060
Darren Tucker06ef75b2003-08-02 23:28:38 +100061/* units for format_size */
62static const char unit[] = " KMGT";
Damien Miller6fd00e02003-01-10 21:46:02 +110063
Darren Tucker06ef75b2003-08-02 23:28:38 +100064static int
65can_output(void)
66{
67 return (getpgrp() == tcgetpgrp(STDOUT_FILENO));
68}
Damien Miller6fd00e02003-01-10 21:46:02 +110069
Darren Tucker06ef75b2003-08-02 23:28:38 +100070static void
71format_rate(char *buf, int size, off_t bytes)
72{
73 int i;
Damien Miller6fd00e02003-01-10 21:46:02 +110074
Darren Tucker06ef75b2003-08-02 23:28:38 +100075 bytes *= 100;
76 for (i = 0; bytes >= 100*1000 && unit[i] != 'T'; i++)
77 bytes = (bytes + 512) / 1024;
78 if (i == 0) {
79 i++;
80 bytes = (bytes + 512) / 1024;
81 }
82 snprintf(buf, size, "%3lld.%1lld%c%s",
Damien Miller8c5e91c2003-11-21 23:09:10 +110083 (int64_t) (bytes + 5) / 100,
Darren Tucker06ef75b2003-08-02 23:28:38 +100084 (int64_t) (bytes + 5) / 10 % 10,
85 unit[i],
86 i ? "B" : " ");
87}
Damien Miller6fd00e02003-01-10 21:46:02 +110088
Darren Tucker06ef75b2003-08-02 23:28:38 +100089static void
90format_size(char *buf, int size, off_t bytes)
91{
92 int i;
Damien Miller6fd00e02003-01-10 21:46:02 +110093
Darren Tucker06ef75b2003-08-02 23:28:38 +100094 for (i = 0; bytes >= 10000 && unit[i] != 'T'; i++)
95 bytes = (bytes + 512) / 1024;
96 snprintf(buf, size, "%4lld%c%s",
97 (int64_t) bytes,
98 unit[i],
99 i ? "B" : " ");
100}
Damien Miller6fd00e02003-01-10 21:46:02 +1100101
102void
Darren Tucker06ef75b2003-08-02 23:28:38 +1000103refresh_progress_meter(void)
Damien Miller6fd00e02003-01-10 21:46:02 +1100104{
Darren Tucker06ef75b2003-08-02 23:28:38 +1000105 char buf[MAX_WINSIZE + 1];
106 time_t now;
107 off_t transferred;
108 double elapsed;
109 int percent;
Darren Tucker7f73a492004-02-06 16:41:37 +1100110 off_t bytes_left;
Darren Tucker06ef75b2003-08-02 23:28:38 +1000111 int cur_speed;
112 int hours, minutes, seconds;
113 int i, len;
114 int file_len;
Damien Miller6fd00e02003-01-10 21:46:02 +1100115
Darren Tucker06ef75b2003-08-02 23:28:38 +1000116 transferred = *counter - cur_pos;
117 cur_pos = *counter;
118 now = time(NULL);
119 bytes_left = end_pos - cur_pos;
Damien Miller6fd00e02003-01-10 21:46:02 +1100120
Darren Tucker06ef75b2003-08-02 23:28:38 +1000121 if (bytes_left > 0)
122 elapsed = now - last_update;
Darren Tucker1fb04252003-12-09 19:07:13 +1100123 else {
Darren Tucker06ef75b2003-08-02 23:28:38 +1000124 elapsed = now - start;
Darren Tucker1fb04252003-12-09 19:07:13 +1100125 /* Calculate true total speed when done */
126 transferred = end_pos;
127 bytes_per_second = 0;
128 }
Darren Tucker06ef75b2003-08-02 23:28:38 +1000129
130 /* calculate speed */
131 if (elapsed != 0)
132 cur_speed = (transferred / elapsed);
133 else
Darren Tucker1fb04252003-12-09 19:07:13 +1100134 cur_speed = transferred;
Darren Tucker06ef75b2003-08-02 23:28:38 +1000135
136#define AGE_FACTOR 0.9
137 if (bytes_per_second != 0) {
138 bytes_per_second = (bytes_per_second * AGE_FACTOR) +
139 (cur_speed * (1.0 - AGE_FACTOR));
140 } else
141 bytes_per_second = cur_speed;
142
143 /* filename */
144 buf[0] = '\0';
145 file_len = win_size - 35;
146 if (file_len > 0) {
Damien Miller17af1762003-09-02 22:53:01 +1000147 len = snprintf(buf, file_len + 1, "\r%s", file);
Damien Millerb5829f52003-09-02 22:53:32 +1000148 if (len < 0)
149 len = 0;
Damien Miller41bfc292005-05-26 12:07:32 +1000150 if (len >= file_len + 1)
151 len = file_len;
Darren Tucker06ef75b2003-08-02 23:28:38 +1000152 for (i = len; i < file_len; i++ )
153 buf[i] = ' ';
154 buf[file_len] = '\0';
155 }
156
157 /* percent of transfer done */
158 if (end_pos != 0)
159 percent = ((float)cur_pos / end_pos) * 100;
160 else
161 percent = 100;
162 snprintf(buf + strlen(buf), win_size - strlen(buf),
163 " %3d%% ", percent);
164
165 /* amount transferred */
166 format_size(buf + strlen(buf), win_size - strlen(buf),
167 cur_pos);
168 strlcat(buf, " ", win_size);
169
170 /* bandwidth usage */
171 format_rate(buf + strlen(buf), win_size - strlen(buf),
Darren Tucker1f8311c2004-05-13 16:39:33 +1000172 (off_t)bytes_per_second);
Darren Tucker06ef75b2003-08-02 23:28:38 +1000173 strlcat(buf, "/s ", win_size);
174
175 /* ETA */
176 if (!transferred)
177 stalled += elapsed;
178 else
179 stalled = 0;
180
181 if (stalled >= STALL_TIME)
182 strlcat(buf, "- stalled -", win_size);
183 else if (bytes_per_second == 0 && bytes_left)
184 strlcat(buf, " --:-- ETA", win_size);
185 else {
186 if (bytes_left > 0)
187 seconds = bytes_left / bytes_per_second;
188 else
189 seconds = elapsed;
190
191 hours = seconds / 3600;
192 seconds -= hours * 3600;
193 minutes = seconds / 60;
194 seconds -= minutes * 60;
195
196 if (hours != 0)
197 snprintf(buf + strlen(buf), win_size - strlen(buf),
198 "%d:%02d:%02d", hours, minutes, seconds);
199 else
200 snprintf(buf + strlen(buf), win_size - strlen(buf),
201 " %02d:%02d", minutes, seconds);
202
203 if (bytes_left > 0)
204 strlcat(buf, " ETA", win_size);
205 else
206 strlcat(buf, " ", win_size);
207 }
208
Darren Tucker6cc310b2003-10-02 16:15:15 +1000209 atomicio(vwrite, STDOUT_FILENO, buf, win_size - 1);
Darren Tucker06ef75b2003-08-02 23:28:38 +1000210 last_update = now;
211}
212
213static void
214update_progress_meter(int ignore)
215{
216 int save_errno;
217
218 save_errno = errno;
219
220 if (can_output())
221 refresh_progress_meter();
222
Ben Lindstrom5ade9ab2003-08-25 01:16:21 +0000223 signal(SIGALRM, update_progress_meter);
Darren Tucker06ef75b2003-08-02 23:28:38 +1000224 alarm(UPDATE_INTERVAL);
225 errno = save_errno;
226}
227
228void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000229start_progress_meter(char *f, off_t filesize, off_t *ctr)
Darren Tucker06ef75b2003-08-02 23:28:38 +1000230{
231 struct winsize winsize;
232
233 start = last_update = time(NULL);
234 file = f;
235 end_pos = filesize;
236 cur_pos = 0;
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000237 counter = ctr;
Darren Tucker06ef75b2003-08-02 23:28:38 +1000238 stalled = 0;
239 bytes_per_second = 0;
240
241 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &winsize) != -1 &&
242 winsize.ws_col != 0) {
243 if (winsize.ws_col > MAX_WINSIZE)
244 win_size = MAX_WINSIZE;
245 else
246 win_size = winsize.ws_col;
247 } else
248 win_size = DEFAULT_WINSIZE;
249 win_size += 1; /* trailing \0 */
250
251 if (can_output())
252 refresh_progress_meter();
253
Ben Lindstrom5ade9ab2003-08-25 01:16:21 +0000254 signal(SIGALRM, update_progress_meter);
Darren Tucker06ef75b2003-08-02 23:28:38 +1000255 alarm(UPDATE_INTERVAL);
Damien Miller6fd00e02003-01-10 21:46:02 +1100256}
257
258void
Damien Miller2b92d322003-06-11 22:05:06 +1000259stop_progress_meter(void)
Damien Miller6fd00e02003-01-10 21:46:02 +1100260{
261 alarm(0);
Damien Miller6fd00e02003-01-10 21:46:02 +1100262
Darren Tucker06ef75b2003-08-02 23:28:38 +1000263 if (!can_output())
Damien Miller6fd00e02003-01-10 21:46:02 +1100264 return;
265
Darren Tucker06ef75b2003-08-02 23:28:38 +1000266 /* Ensure we complete the progress */
267 if (cur_pos != end_pos)
268 refresh_progress_meter();
Damien Miller6fd00e02003-01-10 21:46:02 +1100269
Darren Tucker06ef75b2003-08-02 23:28:38 +1000270 atomicio(vwrite, STDOUT_FILENO, "\n", 1);
Damien Miller6fd00e02003-01-10 21:46:02 +1100271}