- stevesk@cvs.openbsd.org 2006/07/14 01:15:28
[openssh-git.git] / progressmeter.c
blob98a30e11bbc3174230c4ae1d951ddbbd08c1bcb0
1 /* $OpenBSD: progressmeter.c,v 1.30 2006/07/11 20:07:25 stevesk Exp $ */
2 /*
3 * Copyright (c) 2003 Nils Nordman. All rights reserved.
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.
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.
26 #include "includes.h"
28 #include <sys/ioctl.h>
30 #include <errno.h>
31 #include <signal.h>
33 #include "progressmeter.h"
34 #include "atomicio.h"
35 #include "misc.h"
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 */
43 /* determines whether we can output to the terminal */
44 static int can_output(void);
46 /* formats and inserts the specified size into the given buffer */
47 static void format_size(char *, int, off_t);
48 static void format_rate(char *, int, off_t);
50 /* window resizing */
51 static void sig_winch(int);
52 static void setscreensize(void);
54 /* updates the progressmeter to reflect the current state of the transfer */
55 void refresh_progress_meter(void);
57 /* signal handler for updating the progress meter */
58 static void update_progress_meter(int);
60 static time_t start; /* start progress */
61 static time_t last_update; /* last progress update */
62 static char *file; /* name of the file being transferred */
63 static off_t end_pos; /* ending position of transfer */
64 static off_t cur_pos; /* transfer position as of last refresh */
65 static volatile off_t *counter; /* progress counter */
66 static long stalled; /* how long we have been stalled */
67 static int bytes_per_second; /* current speed in bytes per second */
68 static int win_size; /* terminal window size */
69 static volatile sig_atomic_t win_resized; /* for window resizing */
71 /* units for format_size */
72 static const char unit[] = " KMGT";
74 static int
75 can_output(void)
77 return (getpgrp() == tcgetpgrp(STDOUT_FILENO));
80 static void
81 format_rate(char *buf, int size, off_t bytes)
83 int i;
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;
92 snprintf(buf, size, "%3lld.%1lld%c%s",
93 (long long) (bytes + 5) / 100,
94 (long long) (bytes + 5) / 10 % 10,
95 unit[i],
96 i ? "B" : " ");
99 static void
100 format_size(char *buf, int size, off_t bytes)
102 int i;
104 for (i = 0; bytes >= 10000 && unit[i] != 'T'; i++)
105 bytes = (bytes + 512) / 1024;
106 snprintf(buf, size, "%4lld%c%s",
107 (long long) bytes,
108 unit[i],
109 i ? "B" : " ");
112 void
113 refresh_progress_meter(void)
115 char buf[MAX_WINSIZE + 1];
116 time_t now;
117 off_t transferred;
118 double elapsed;
119 int percent;
120 off_t bytes_left;
121 int cur_speed;
122 int hours, minutes, seconds;
123 int i, len;
124 int file_len;
126 transferred = *counter - cur_pos;
127 cur_pos = *counter;
128 now = time(NULL);
129 bytes_left = end_pos - cur_pos;
131 if (bytes_left > 0)
132 elapsed = now - last_update;
133 else {
134 elapsed = now - start;
135 /* Calculate true total speed when done */
136 transferred = end_pos;
137 bytes_per_second = 0;
140 /* calculate speed */
141 if (elapsed != 0)
142 cur_speed = (transferred / elapsed);
143 else
144 cur_speed = transferred;
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;
153 /* filename */
154 buf[0] = '\0';
155 file_len = win_size - 35;
156 if (file_len > 0) {
157 len = snprintf(buf, file_len + 1, "\r%s", file);
158 if (len < 0)
159 len = 0;
160 if (len >= file_len + 1)
161 len = file_len;
162 for (i = len; i < file_len; i++ )
163 buf[i] = ' ';
164 buf[file_len] = '\0';
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);
175 /* amount transferred */
176 format_size(buf + strlen(buf), win_size - strlen(buf),
177 cur_pos);
178 strlcat(buf, " ", win_size);
180 /* bandwidth usage */
181 format_rate(buf + strlen(buf), win_size - strlen(buf),
182 (off_t)bytes_per_second);
183 strlcat(buf, "/s ", win_size);
185 /* ETA */
186 if (!transferred)
187 stalled += elapsed;
188 else
189 stalled = 0;
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;
201 hours = seconds / 3600;
202 seconds -= hours * 3600;
203 minutes = seconds / 60;
204 seconds -= minutes * 60;
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);
213 if (bytes_left > 0)
214 strlcat(buf, " ETA", win_size);
215 else
216 strlcat(buf, " ", win_size);
219 atomicio(vwrite, STDOUT_FILENO, buf, win_size - 1);
220 last_update = now;
223 static void
224 update_progress_meter(int ignore)
226 int save_errno;
228 save_errno = errno;
230 if (win_resized) {
231 setscreensize();
232 win_resized = 0;
234 if (can_output())
235 refresh_progress_meter();
237 signal(SIGALRM, update_progress_meter);
238 alarm(UPDATE_INTERVAL);
239 errno = save_errno;
242 void
243 start_progress_meter(char *f, off_t filesize, off_t *ctr)
245 start = last_update = time(NULL);
246 file = f;
247 end_pos = filesize;
248 cur_pos = 0;
249 counter = ctr;
250 stalled = 0;
251 bytes_per_second = 0;
253 setscreensize();
254 if (can_output())
255 refresh_progress_meter();
257 signal(SIGALRM, update_progress_meter);
258 signal(SIGWINCH, sig_winch);
259 alarm(UPDATE_INTERVAL);
262 void
263 stop_progress_meter(void)
265 alarm(0);
267 if (!can_output())
268 return;
270 /* Ensure we complete the progress */
271 if (cur_pos != end_pos)
272 refresh_progress_meter();
274 atomicio(vwrite, STDOUT_FILENO, "\n", 1);
277 /*ARGSUSED*/
278 static void
279 sig_winch(int sig)
281 win_resized = 1;
284 static void
285 setscreensize(void)
287 struct winsize winsize;
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 */