]> andersk Git - gssapi-openssh.git/blame - openssh/progressmeter.c
Import of OpenSSH 3.9p1
[gssapi-openssh.git] / openssh / progressmeter.c
CommitLineData
6a9b3198 1/*
0fff78ff 2 * Copyright (c) 2003 Nils Nordman. All rights reserved.
6a9b3198 3 *
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
6a9b3198 25#include "includes.h"
c9f39d2c 26RCSID("$OpenBSD: progressmeter.c,v 1.22 2004/07/11 17:48:47 deraadt Exp $");
6a9b3198 27
6a9b3198 28#include "progressmeter.h"
0fff78ff 29#include "atomicio.h"
30#include "misc.h"
6a9b3198 31
0fff78ff 32#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 */
6a9b3198 37
0fff78ff 38/* determines whether we can output to the terminal */
39static int can_output(void);
6a9b3198 40
0fff78ff 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);
6a9b3198 44
0fff78ff 45/* updates the progressmeter to reflect the current state of the transfer */
46void refresh_progress_meter(void);
6a9b3198 47
0fff78ff 48/* signal handler for updating the progress meter */
49static void update_progress_meter(int);
6a9b3198 50
c9f39d2c 51static 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 */
0fff78ff 56static volatile off_t *counter; /* progress counter */
c9f39d2c 57static 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 */
6a9b3198 60
0fff78ff 61/* units for format_size */
62static const char unit[] = " KMGT";
6a9b3198 63
0fff78ff 64static int
65can_output(void)
66{
67 return (getpgrp() == tcgetpgrp(STDOUT_FILENO));
68}
6a9b3198 69
0fff78ff 70static void
71format_rate(char *buf, int size, off_t bytes)
72{
73 int i;
74
75 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",
cdd66111 83 (int64_t) (bytes + 5) / 100,
0fff78ff 84 (int64_t) (bytes + 5) / 10 % 10,
85 unit[i],
86 i ? "B" : " ");
87}
6a9b3198 88
0fff78ff 89static void
90format_size(char *buf, int size, off_t bytes)
91{
92 int i;
93
94 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}
6a9b3198 101
102void
0fff78ff 103refresh_progress_meter(void)
6a9b3198 104{
0fff78ff 105 char buf[MAX_WINSIZE + 1];
106 time_t now;
107 off_t transferred;
108 double elapsed;
109 int percent;
cdd66111 110 off_t bytes_left;
0fff78ff 111 int cur_speed;
112 int hours, minutes, seconds;
113 int i, len;
114 int file_len;
115
116 transferred = *counter - cur_pos;
117 cur_pos = *counter;
118 now = time(NULL);
119 bytes_left = end_pos - cur_pos;
120
121 if (bytes_left > 0)
122 elapsed = now - last_update;
cdd66111 123 else {
0fff78ff 124 elapsed = now - start;
cdd66111 125 /* Calculate true total speed when done */
126 transferred = end_pos;
127 bytes_per_second = 0;
128 }
6a9b3198 129
0fff78ff 130 /* calculate speed */
131 if (elapsed != 0)
132 cur_speed = (transferred / elapsed);
133 else
cdd66111 134 cur_speed = transferred;
6a9b3198 135
0fff78ff 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) {
147 len = snprintf(buf, file_len + 1, "\r%s", file);
148 if (len < 0)
149 len = 0;
150 for (i = len; i < file_len; i++ )
151 buf[i] = ' ';
152 buf[file_len] = '\0';
153 }
6a9b3198 154
0fff78ff 155 /* percent of transfer done */
156 if (end_pos != 0)
157 percent = ((float)cur_pos / end_pos) * 100;
158 else
159 percent = 100;
160 snprintf(buf + strlen(buf), win_size - strlen(buf),
161 " %3d%% ", percent);
162
163 /* amount transferred */
164 format_size(buf + strlen(buf), win_size - strlen(buf),
165 cur_pos);
166 strlcat(buf, " ", win_size);
167
168 /* bandwidth usage */
169 format_rate(buf + strlen(buf), win_size - strlen(buf),
c9f39d2c 170 (off_t)bytes_per_second);
0fff78ff 171 strlcat(buf, "/s ", win_size);
172
173 /* ETA */
174 if (!transferred)
175 stalled += elapsed;
176 else
177 stalled = 0;
178
179 if (stalled >= STALL_TIME)
180 strlcat(buf, "- stalled -", win_size);
181 else if (bytes_per_second == 0 && bytes_left)
182 strlcat(buf, " --:-- ETA", win_size);
183 else {
184 if (bytes_left > 0)
185 seconds = bytes_left / bytes_per_second;
186 else
187 seconds = elapsed;
188
189 hours = seconds / 3600;
190 seconds -= hours * 3600;
191 minutes = seconds / 60;
192 seconds -= minutes * 60;
193
194 if (hours != 0)
195 snprintf(buf + strlen(buf), win_size - strlen(buf),
196 "%d:%02d:%02d", hours, minutes, seconds);
197 else
198 snprintf(buf + strlen(buf), win_size - strlen(buf),
199 " %02d:%02d", minutes, seconds);
200
201 if (bytes_left > 0)
202 strlcat(buf, " ETA", win_size);
203 else
204 strlcat(buf, " ", win_size);
205 }
206
cdd66111 207 atomicio(vwrite, STDOUT_FILENO, buf, win_size - 1);
0fff78ff 208 last_update = now;
6a9b3198 209}
210
211static void
212update_progress_meter(int ignore)
213{
0fff78ff 214 int save_errno;
215
216 save_errno = errno;
217
218 if (can_output())
219 refresh_progress_meter();
6a9b3198 220
6a9b3198 221 signal(SIGALRM, update_progress_meter);
0fff78ff 222 alarm(UPDATE_INTERVAL);
6a9b3198 223 errno = save_errno;
224}
225
0fff78ff 226void
c9f39d2c 227start_progress_meter(char *f, off_t filesize, off_t *ctr)
6a9b3198 228{
0fff78ff 229 struct winsize winsize;
6a9b3198 230
0fff78ff 231 start = last_update = time(NULL);
232 file = f;
233 end_pos = filesize;
234 cur_pos = 0;
c9f39d2c 235 counter = ctr;
0fff78ff 236 stalled = 0;
237 bytes_per_second = 0;
238
239 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &winsize) != -1 &&
240 winsize.ws_col != 0) {
241 if (winsize.ws_col > MAX_WINSIZE)
242 win_size = MAX_WINSIZE;
243 else
244 win_size = winsize.ws_col;
6a9b3198 245 } else
0fff78ff 246 win_size = DEFAULT_WINSIZE;
247 win_size += 1; /* trailing \0 */
6a9b3198 248
0fff78ff 249 if (can_output())
250 refresh_progress_meter();
6a9b3198 251
0fff78ff 252 signal(SIGALRM, update_progress_meter);
253 alarm(UPDATE_INTERVAL);
6a9b3198 254}
255
0fff78ff 256void
257stop_progress_meter(void)
6a9b3198 258{
0fff78ff 259 alarm(0);
6a9b3198 260
0fff78ff 261 if (!can_output())
262 return;
263
264 /* Ensure we complete the progress */
265 if (cur_pos != end_pos)
266 refresh_progress_meter();
267
268 atomicio(vwrite, STDOUT_FILENO, "\n", 1);
6a9b3198 269}
This page took 0.599628 seconds and 5 git commands to generate.