]> andersk Git - openssh.git/blame - progressmeter.c
- stevesk@cvs.openbsd.org 2006/07/11 20:07:25
[openssh.git] / progressmeter.c
CommitLineData
028094f4 1/* $OpenBSD: progressmeter.c,v 1.30 2006/07/11 20:07:25 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
28#include <sys/ioctl.h>
c9c38b73 29
028094f4 30#include <errno.h>
ada68823 31#include <signal.h>
32
b7fd001f 33#include "progressmeter.h"
178b1a1d 34#include "atomicio.h"
8d9bb5dd 35#include "misc.h"
c9c38b73 36
178b1a1d 37#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 */
c9c38b73 42
178b1a1d 43/* determines whether we can output to the terminal */
44static int can_output(void);
c9c38b73 45
178b1a1d 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);
c9c38b73 49
826563dc 50/* window resizing */
51static void sig_winch(int);
52static void setscreensize(void);
53
178b1a1d 54/* updates the progressmeter to reflect the current state of the transfer */
55void refresh_progress_meter(void);
c9c38b73 56
178b1a1d 57/* signal handler for updating the progress meter */
58static void update_progress_meter(int);
c9c38b73 59
f2107e97 60static 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 */
178b1a1d 65static volatile off_t *counter; /* progress counter */
f2107e97 66static 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 */
826563dc 69static volatile sig_atomic_t win_resized; /* for window resizing */
c9c38b73 70
178b1a1d 71/* units for format_size */
72static const char unit[] = " KMGT";
c9c38b73 73
178b1a1d 74static int
75can_output(void)
76{
77 return (getpgrp() == tcgetpgrp(STDOUT_FILENO));
78}
c9c38b73 79
178b1a1d 80static void
81format_rate(char *buf, int size, off_t bytes)
82{
83 int i;
84
85 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",
419094c6 93 (long long) (bytes + 5) / 100,
94 (long long) (bytes + 5) / 10 % 10,
178b1a1d 95 unit[i],
96 i ? "B" : " ");
97}
c9c38b73 98
178b1a1d 99static void
100format_size(char *buf, int size, off_t bytes)
101{
102 int i;
103
104 for (i = 0; bytes >= 10000 && unit[i] != 'T'; i++)
105 bytes = (bytes + 512) / 1024;
106 snprintf(buf, size, "%4lld%c%s",
419094c6 107 (long long) bytes,
178b1a1d 108 unit[i],
109 i ? "B" : " ");
110}
c9c38b73 111
112void
178b1a1d 113refresh_progress_meter(void)
c9c38b73 114{
178b1a1d 115 char buf[MAX_WINSIZE + 1];
116 time_t now;
117 off_t transferred;
118 double elapsed;
119 int percent;
7b0a59c9 120 off_t bytes_left;
178b1a1d 121 int cur_speed;
122 int hours, minutes, seconds;
123 int i, len;
124 int file_len;
125
126 transferred = *counter - cur_pos;
127 cur_pos = *counter;
128 now = time(NULL);
129 bytes_left = end_pos - cur_pos;
130
131 if (bytes_left > 0)
132 elapsed = now - last_update;
adfde93f 133 else {
178b1a1d 134 elapsed = now - start;
adfde93f 135 /* Calculate true total speed when done */
136 transferred = end_pos;
137 bytes_per_second = 0;
138 }
c9c38b73 139
178b1a1d 140 /* calculate speed */
141 if (elapsed != 0)
142 cur_speed = (transferred / elapsed);
143 else
adfde93f 144 cur_speed = transferred;
c9c38b73 145
178b1a1d 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) {
8e382949 157 len = snprintf(buf, file_len + 1, "\r%s", file);
3845a9ac 158 if (len < 0)
159 len = 0;
4b826d75 160 if (len >= file_len + 1)
161 len = file_len;
178b1a1d 162 for (i = len; i < file_len; i++ )
163 buf[i] = ' ';
164 buf[file_len] = '\0';
165 }
c9c38b73 166
178b1a1d 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),
f6be21a0 182 (off_t)bytes_per_second);
178b1a1d 183 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
0469be42 219 atomicio(vwrite, STDOUT_FILENO, buf, win_size - 1);
178b1a1d 220 last_update = now;
c9c38b73 221}
222
223static void
224update_progress_meter(int ignore)
225{
178b1a1d 226 int save_errno;
227
228 save_errno = errno;
229
826563dc 230 if (win_resized) {
231 setscreensize();
232 win_resized = 0;
233 }
178b1a1d 234 if (can_output())
235 refresh_progress_meter();
c9c38b73 236
f00d1f78 237 signal(SIGALRM, update_progress_meter);
178b1a1d 238 alarm(UPDATE_INTERVAL);
c9c38b73 239 errno = save_errno;
240}
241
178b1a1d 242void
ca75d7de 243start_progress_meter(char *f, off_t filesize, off_t *ctr)
c9c38b73 244{
178b1a1d 245 start = last_update = time(NULL);
246 file = f;
247 end_pos = filesize;
248 cur_pos = 0;
ca75d7de 249 counter = ctr;
178b1a1d 250 stalled = 0;
251 bytes_per_second = 0;
252
826563dc 253 setscreensize();
178b1a1d 254 if (can_output())
255 refresh_progress_meter();
c9c38b73 256
f00d1f78 257 signal(SIGALRM, update_progress_meter);
826563dc 258 signal(SIGWINCH, sig_winch);
178b1a1d 259 alarm(UPDATE_INTERVAL);
c9c38b73 260}
261
178b1a1d 262void
263stop_progress_meter(void)
c9c38b73 264{
178b1a1d 265 alarm(0);
c9c38b73 266
178b1a1d 267 if (!can_output())
268 return;
269
270 /* Ensure we complete the progress */
271 if (cur_pos != end_pos)
272 refresh_progress_meter();
273
274 atomicio(vwrite, STDOUT_FILENO, "\n", 1);
c9c38b73 275}
826563dc 276
3efa8ea9 277/*ARGSUSED*/
826563dc 278static 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}
This page took 0.885216 seconds and 5 git commands to generate.