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