]> andersk Git - openssh.git/blame - progressmeter.c
- stevesk@cvs.openbsd.org 2006/07/17 01:31:10
[openssh.git] / progressmeter.c
CommitLineData
5188ba17 1/* $OpenBSD: progressmeter.c,v 1.31 2006/07/17 01:31:09 stevesk Exp $ */
c9c38b73 2/*
178b1a1d 3 * Copyright (c) 2003 Nils Nordman. All rights reserved.
c9c38b73 4 *
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
c9c38b73 26#include "includes.h"
bd7c69ea 27
5188ba17 28#include <sys/types.h>
bd7c69ea 29#include <sys/ioctl.h>
c9c38b73 30
028094f4 31#include <errno.h>
ada68823 32#include <signal.h>
5188ba17 33#include <unistd.h>
ada68823 34
b7fd001f 35#include "progressmeter.h"
178b1a1d 36#include "atomicio.h"
8d9bb5dd 37#include "misc.h"
c9c38b73 38
178b1a1d 39#define DEFAULT_WINSIZE 80
40#define MAX_WINSIZE 512
41#define PADDING 1 /* padding between the progress indicators */
42#define UPDATE_INTERVAL 1 /* update the progress meter every second */
43#define STALL_TIME 5 /* we're stalled after this many seconds */
c9c38b73 44
178b1a1d 45/* determines whether we can output to the terminal */
46static int can_output(void);
c9c38b73 47
178b1a1d 48/* formats and inserts the specified size into the given buffer */
49static void format_size(char *, int, off_t);
50static void format_rate(char *, int, off_t);
c9c38b73 51
826563dc 52/* window resizing */
53static void sig_winch(int);
54static void setscreensize(void);
55
178b1a1d 56/* updates the progressmeter to reflect the current state of the transfer */
57void refresh_progress_meter(void);
c9c38b73 58
178b1a1d 59/* signal handler for updating the progress meter */
60static void update_progress_meter(int);
c9c38b73 61
f2107e97 62static time_t start; /* start progress */
63static time_t last_update; /* last progress update */
64static char *file; /* name of the file being transferred */
65static off_t end_pos; /* ending position of transfer */
66static off_t cur_pos; /* transfer position as of last refresh */
178b1a1d 67static volatile off_t *counter; /* progress counter */
f2107e97 68static long stalled; /* how long we have been stalled */
69static int bytes_per_second; /* current speed in bytes per second */
70static int win_size; /* terminal window size */
826563dc 71static volatile sig_atomic_t win_resized; /* for window resizing */
c9c38b73 72
178b1a1d 73/* units for format_size */
74static const char unit[] = " KMGT";
c9c38b73 75
178b1a1d 76static int
77can_output(void)
78{
79 return (getpgrp() == tcgetpgrp(STDOUT_FILENO));
80}
c9c38b73 81
178b1a1d 82static void
83format_rate(char *buf, int size, off_t bytes)
84{
85 int i;
86
87 bytes *= 100;
88 for (i = 0; bytes >= 100*1000 && unit[i] != 'T'; i++)
89 bytes = (bytes + 512) / 1024;
90 if (i == 0) {
91 i++;
92 bytes = (bytes + 512) / 1024;
93 }
94 snprintf(buf, size, "%3lld.%1lld%c%s",
419094c6 95 (long long) (bytes + 5) / 100,
96 (long long) (bytes + 5) / 10 % 10,
178b1a1d 97 unit[i],
98 i ? "B" : " ");
99}
c9c38b73 100
178b1a1d 101static void
102format_size(char *buf, int size, off_t bytes)
103{
104 int i;
105
106 for (i = 0; bytes >= 10000 && unit[i] != 'T'; i++)
107 bytes = (bytes + 512) / 1024;
108 snprintf(buf, size, "%4lld%c%s",
419094c6 109 (long long) bytes,
178b1a1d 110 unit[i],
111 i ? "B" : " ");
112}
c9c38b73 113
114void
178b1a1d 115refresh_progress_meter(void)
c9c38b73 116{
178b1a1d 117 char buf[MAX_WINSIZE + 1];
118 time_t now;
119 off_t transferred;
120 double elapsed;
121 int percent;
7b0a59c9 122 off_t bytes_left;
178b1a1d 123 int cur_speed;
124 int hours, minutes, seconds;
125 int i, len;
126 int file_len;
127
128 transferred = *counter - cur_pos;
129 cur_pos = *counter;
130 now = time(NULL);
131 bytes_left = end_pos - cur_pos;
132
133 if (bytes_left > 0)
134 elapsed = now - last_update;
adfde93f 135 else {
178b1a1d 136 elapsed = now - start;
adfde93f 137 /* Calculate true total speed when done */
138 transferred = end_pos;
139 bytes_per_second = 0;
140 }
c9c38b73 141
178b1a1d 142 /* calculate speed */
143 if (elapsed != 0)
144 cur_speed = (transferred / elapsed);
145 else
adfde93f 146 cur_speed = transferred;
c9c38b73 147
178b1a1d 148#define AGE_FACTOR 0.9
149 if (bytes_per_second != 0) {
150 bytes_per_second = (bytes_per_second * AGE_FACTOR) +
151 (cur_speed * (1.0 - AGE_FACTOR));
152 } else
153 bytes_per_second = cur_speed;
154
155 /* filename */
156 buf[0] = '\0';
157 file_len = win_size - 35;
158 if (file_len > 0) {
8e382949 159 len = snprintf(buf, file_len + 1, "\r%s", file);
3845a9ac 160 if (len < 0)
161 len = 0;
4b826d75 162 if (len >= file_len + 1)
163 len = file_len;
178b1a1d 164 for (i = len; i < file_len; i++ )
165 buf[i] = ' ';
166 buf[file_len] = '\0';
167 }
c9c38b73 168
178b1a1d 169 /* percent of transfer done */
170 if (end_pos != 0)
171 percent = ((float)cur_pos / end_pos) * 100;
172 else
173 percent = 100;
174 snprintf(buf + strlen(buf), win_size - strlen(buf),
175 " %3d%% ", percent);
176
177 /* amount transferred */
178 format_size(buf + strlen(buf), win_size - strlen(buf),
179 cur_pos);
180 strlcat(buf, " ", win_size);
181
182 /* bandwidth usage */
183 format_rate(buf + strlen(buf), win_size - strlen(buf),
f6be21a0 184 (off_t)bytes_per_second);
178b1a1d 185 strlcat(buf, "/s ", win_size);
186
187 /* ETA */
188 if (!transferred)
189 stalled += elapsed;
190 else
191 stalled = 0;
192
193 if (stalled >= STALL_TIME)
194 strlcat(buf, "- stalled -", win_size);
195 else if (bytes_per_second == 0 && bytes_left)
196 strlcat(buf, " --:-- ETA", win_size);
197 else {
198 if (bytes_left > 0)
199 seconds = bytes_left / bytes_per_second;
200 else
201 seconds = elapsed;
202
203 hours = seconds / 3600;
204 seconds -= hours * 3600;
205 minutes = seconds / 60;
206 seconds -= minutes * 60;
207
208 if (hours != 0)
209 snprintf(buf + strlen(buf), win_size - strlen(buf),
210 "%d:%02d:%02d", hours, minutes, seconds);
211 else
212 snprintf(buf + strlen(buf), win_size - strlen(buf),
213 " %02d:%02d", minutes, seconds);
214
215 if (bytes_left > 0)
216 strlcat(buf, " ETA", win_size);
217 else
218 strlcat(buf, " ", win_size);
219 }
220
0469be42 221 atomicio(vwrite, STDOUT_FILENO, buf, win_size - 1);
178b1a1d 222 last_update = now;
c9c38b73 223}
224
225static void
226update_progress_meter(int ignore)
227{
178b1a1d 228 int save_errno;
229
230 save_errno = errno;
231
826563dc 232 if (win_resized) {
233 setscreensize();
234 win_resized = 0;
235 }
178b1a1d 236 if (can_output())
237 refresh_progress_meter();
c9c38b73 238
f00d1f78 239 signal(SIGALRM, update_progress_meter);
178b1a1d 240 alarm(UPDATE_INTERVAL);
c9c38b73 241 errno = save_errno;
242}
243
178b1a1d 244void
ca75d7de 245start_progress_meter(char *f, off_t filesize, off_t *ctr)
c9c38b73 246{
178b1a1d 247 start = last_update = time(NULL);
248 file = f;
249 end_pos = filesize;
250 cur_pos = 0;
ca75d7de 251 counter = ctr;
178b1a1d 252 stalled = 0;
253 bytes_per_second = 0;
254
826563dc 255 setscreensize();
178b1a1d 256 if (can_output())
257 refresh_progress_meter();
c9c38b73 258
f00d1f78 259 signal(SIGALRM, update_progress_meter);
826563dc 260 signal(SIGWINCH, sig_winch);
178b1a1d 261 alarm(UPDATE_INTERVAL);
c9c38b73 262}
263
178b1a1d 264void
265stop_progress_meter(void)
c9c38b73 266{
178b1a1d 267 alarm(0);
c9c38b73 268
178b1a1d 269 if (!can_output())
270 return;
271
272 /* Ensure we complete the progress */
273 if (cur_pos != end_pos)
274 refresh_progress_meter();
275
276 atomicio(vwrite, STDOUT_FILENO, "\n", 1);
c9c38b73 277}
826563dc 278
3efa8ea9 279/*ARGSUSED*/
826563dc 280static void
281sig_winch(int sig)
282{
283 win_resized = 1;
284}
285
286static void
287setscreensize(void)
288{
289 struct winsize winsize;
290
291 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &winsize) != -1 &&
292 winsize.ws_col != 0) {
293 if (winsize.ws_col > MAX_WINSIZE)
294 win_size = MAX_WINSIZE;
295 else
296 win_size = winsize.ws_col;
297 } else
298 win_size = DEFAULT_WINSIZE;
299 win_size += 1; /* trailing \0 */
300}
This page took 0.36978 seconds and 5 git commands to generate.