]> andersk Git - openssh.git/blame - progressmeter.c
- djm@cvs.openbsd.org 2003/07/28 09:49:56
[openssh.git] / progressmeter.c
CommitLineData
c9c38b73 1/*
2 * Copyright (c) 1999 Theo de Raadt. All rights reserved.
3 * Copyright (c) 1999 Aaron Campbell. All rights reserved.
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
26/*
08210665 27 * Copyright (c) 1997-2003 The NetBSD Foundation, Inc.
28 * All rights reserved.
c9c38b73 29 *
08210665 30 * This code is derived from software contributed to The NetBSD Foundation
31 * by Luke Mewburn.
32 *
33 * This code is derived from software contributed to The NetBSD Foundation
34 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
35 * NASA Ames Research Center.
c9c38b73 36 *
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
39 * are met:
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. All advertising materials mentioning features or use of this software
46 * must display the following acknowledgement:
08210665 47 * This product includes software developed by the NetBSD
48 * Foundation, Inc. and its contributors.
49 * 4. Neither the name of The NetBSD Foundation nor the names of its
50 * contributors may be used to endorse or promote products derived
51 * from this software without specific prior written permission.
c9c38b73 52 *
08210665 53 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
54 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
55 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
56 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
57 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
58 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
59 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
60 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
61 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
62 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
63 * POSSIBILITY OF SUCH DAMAGE.
c9c38b73 64 */
65
66#include "includes.h"
dc54438a 67RCSID("$OpenBSD: progressmeter.c,v 1.8 2003/06/28 16:23:06 deraadt Exp $");
c9c38b73 68
7d132853 69#ifdef HAVE_LIBGEN_H
c9c38b73 70#include <libgen.h>
7d132853 71#endif
c9c38b73 72
73#include "atomicio.h"
b7fd001f 74#include "progressmeter.h"
8d9bb5dd 75#include "misc.h"
c9c38b73 76
77/* Number of seconds before xfer considered "stalled". */
78#define STALLTIME 5
79/* alarm() interval for updating progress meter. */
80#define PROGRESSTIME 1
81
82/* Signal handler used for updating the progress meter. */
83static void update_progress_meter(int);
84
85/* Returns non-zero if we are the foreground process. */
86static int foregroundproc(void);
87
88/* Returns width of the terminal (for progress meter calculations). */
89static int get_tty_width(void);
90
91/* Visual statistics about files as they are transferred. */
b7fd001f 92static void draw_progress_meter(void);
c9c38b73 93
94/* Time a transfer started. */
95static struct timeval start;
96
97/* Number of bytes of current file transferred so far. */
98static volatile off_t *statbytes;
99
100/* Total size of current file. */
101static off_t totalbytes;
102
103/* Name of current file being transferred. */
104static char *curfile;
105
106/* Time of last update. */
107static struct timeval lastupdate;
108
109/* Size at the time of the last update. */
110static off_t lastsize;
111
112void
113start_progress_meter(char *file, off_t filesize, off_t *counter)
114{
115 if ((curfile = basename(file)) == NULL)
116 curfile = file;
117
118 totalbytes = filesize;
119 statbytes = counter;
120 (void) gettimeofday(&start, (struct timezone *) 0);
121 lastupdate = start;
122 lastsize = 0;
123
124 draw_progress_meter();
8d9bb5dd 125 mysignal(SIGALRM, update_progress_meter);
c9c38b73 126 alarm(PROGRESSTIME);
127}
128
129void
0daa6547 130stop_progress_meter(void)
c9c38b73 131{
132 alarm(0);
133 draw_progress_meter();
9c4cd3ef 134 if (foregroundproc() != 0)
dc54438a 135 atomicio(vwrite, fileno(stdout), "\n", 1);
c9c38b73 136}
137
138static void
139update_progress_meter(int ignore)
140{
141 int save_errno = errno;
142
143 draw_progress_meter();
8d9bb5dd 144 mysignal(SIGALRM, update_progress_meter);
c9c38b73 145 alarm(PROGRESSTIME);
146 errno = save_errno;
147}
148
149static int
150foregroundproc(void)
151{
152 static pid_t pgrp = -1;
153 int ctty_pgrp;
154
155 if (pgrp == -1)
156 pgrp = getpgrp();
157
7d132853 158#ifdef HAVE_TCGETPGRP
159 return ((ctty_pgrp = tcgetpgrp(STDOUT_FILENO)) != -1 &&
160 ctty_pgrp == pgrp);
161#else
c9c38b73 162 return ((ioctl(STDOUT_FILENO, TIOCGPGRP, &ctty_pgrp) != -1 &&
163 ctty_pgrp == pgrp));
7d132853 164#endif
c9c38b73 165}
166
167static void
0daa6547 168draw_progress_meter(void)
c9c38b73 169{
170 static const char spaces[] = " "
171 " "
172 " "
173 " "
174 " "
175 " ";
176 static const char prefixes[] = " KMGTP";
177 struct timeval now, td, wait;
178 off_t cursize, abbrevsize, bytespersec;
179 double elapsed;
180 int ratio, remaining, i, ai, bi, nspaces;
181 char buf[512];
182
183 if (foregroundproc() == 0)
184 return;
185
186 (void) gettimeofday(&now, (struct timezone *) 0);
187 cursize = *statbytes;
188 if (totalbytes != 0) {
189 ratio = 100.0 * cursize / totalbytes;
190 ratio = MAX(ratio, 0);
191 ratio = MIN(ratio, 100);
192 } else
193 ratio = 100;
194
195 abbrevsize = cursize;
196 for (ai = 0; abbrevsize >= 10000 && ai < sizeof(prefixes); ai++)
197 abbrevsize >>= 10;
198
199 timersub(&now, &lastupdate, &wait);
200 if (cursize > lastsize) {
201 lastupdate = now;
202 lastsize = cursize;
203 wait.tv_sec = 0;
204 }
205 timersub(&now, &start, &td);
206 elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
207
208 bytespersec = 0;
209 if (cursize > 0) {
210 bytespersec = cursize;
211 if (elapsed > 0.0)
212 bytespersec /= elapsed;
213 }
214 for (bi = 1; bytespersec >= 1024000 && bi < sizeof(prefixes); bi++)
215 bytespersec >>= 10;
216
217 nspaces = MIN(get_tty_width() - 79, sizeof(spaces) - 1);
218
219 snprintf(buf, sizeof(buf),
220 "\r%-45.45s%.*s%3d%% %4lld%c%c %3lld.%01d%cB/s",
221 curfile,
222 nspaces,
223 spaces,
224 ratio,
ded9dd18 225 (int64_t)abbrevsize,
c9c38b73 226 prefixes[ai],
227 ai == 0 ? ' ' : 'B',
ded9dd18 228 (int64_t)(bytespersec / 1024),
c9c38b73 229 (int)((bytespersec % 1024) * 10 / 1024),
230 prefixes[bi]
231 );
232
233 if (cursize <= 0 || elapsed <= 0.0 || cursize > totalbytes) {
234 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
235 " --:-- ETA");
236 } else if (wait.tv_sec >= STALLTIME) {
237 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
238 " - stalled -");
239 } else {
240 if (cursize != totalbytes)
241 remaining = (int)(totalbytes / (cursize / elapsed) -
242 elapsed);
243 else
244 remaining = elapsed;
245
246 i = remaining / 3600;
247 if (i)
248 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
249 "%2d:", i);
250 else
251 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
252 " ");
253 i = remaining % 3600;
254 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
255 "%02d:%02d%s", i / 60, i % 60,
256 (cursize != totalbytes) ? " ETA" : " ");
257 }
dc54438a 258 atomicio(vwrite, fileno(stdout), buf, strlen(buf));
c9c38b73 259}
260
261static int
262get_tty_width(void)
263{
264 struct winsize winsize;
265
266 if (ioctl(fileno(stdout), TIOCGWINSZ, &winsize) != -1)
267 return (winsize.ws_col ? winsize.ws_col : 80);
268 else
269 return (80);
270}
This page took 0.259992 seconds and 5 git commands to generate.