]> andersk Git - openssh.git/blame - progressmeter.c
- millert@cvs.openbsd.org 2003/04/07 21:58:05
[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"
08210665 67RCSID("$OpenBSD: progressmeter.c,v 1.6 2003/04/07 21:58:05 millert 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"
c9c38b73 75
76/* Number of seconds before xfer considered "stalled". */
77#define STALLTIME 5
78/* alarm() interval for updating progress meter. */
79#define PROGRESSTIME 1
80
81/* Signal handler used for updating the progress meter. */
82static void update_progress_meter(int);
83
84/* Returns non-zero if we are the foreground process. */
85static int foregroundproc(void);
86
87/* Returns width of the terminal (for progress meter calculations). */
88static int get_tty_width(void);
89
90/* Visual statistics about files as they are transferred. */
b7fd001f 91static void draw_progress_meter(void);
c9c38b73 92
93/* Time a transfer started. */
94static struct timeval start;
95
96/* Number of bytes of current file transferred so far. */
97static volatile off_t *statbytes;
98
99/* Total size of current file. */
100static off_t totalbytes;
101
102/* Name of current file being transferred. */
103static char *curfile;
104
105/* Time of last update. */
106static struct timeval lastupdate;
107
108/* Size at the time of the last update. */
109static off_t lastsize;
110
111void
112start_progress_meter(char *file, off_t filesize, off_t *counter)
113{
114 if ((curfile = basename(file)) == NULL)
115 curfile = file;
116
117 totalbytes = filesize;
118 statbytes = counter;
119 (void) gettimeofday(&start, (struct timezone *) 0);
120 lastupdate = start;
121 lastsize = 0;
122
123 draw_progress_meter();
124 signal(SIGALRM, update_progress_meter);
125 alarm(PROGRESSTIME);
126}
127
128void
129stop_progress_meter()
130{
131 alarm(0);
132 draw_progress_meter();
9c4cd3ef 133 if (foregroundproc() != 0)
134 atomicio(write, fileno(stdout), "\n", 1);
c9c38b73 135}
136
137static void
138update_progress_meter(int ignore)
139{
140 int save_errno = errno;
141
142 draw_progress_meter();
143 signal(SIGALRM, update_progress_meter);
144 alarm(PROGRESSTIME);
145 errno = save_errno;
146}
147
148static int
149foregroundproc(void)
150{
151 static pid_t pgrp = -1;
152 int ctty_pgrp;
153
154 if (pgrp == -1)
155 pgrp = getpgrp();
156
7d132853 157#ifdef HAVE_TCGETPGRP
158 return ((ctty_pgrp = tcgetpgrp(STDOUT_FILENO)) != -1 &&
159 ctty_pgrp == pgrp);
160#else
c9c38b73 161 return ((ioctl(STDOUT_FILENO, TIOCGPGRP, &ctty_pgrp) != -1 &&
162 ctty_pgrp == pgrp));
7d132853 163#endif
c9c38b73 164}
165
166static void
167draw_progress_meter()
168{
169 static const char spaces[] = " "
170 " "
171 " "
172 " "
173 " "
174 " ";
175 static const char prefixes[] = " KMGTP";
176 struct timeval now, td, wait;
177 off_t cursize, abbrevsize, bytespersec;
178 double elapsed;
179 int ratio, remaining, i, ai, bi, nspaces;
180 char buf[512];
181
182 if (foregroundproc() == 0)
183 return;
184
185 (void) gettimeofday(&now, (struct timezone *) 0);
186 cursize = *statbytes;
187 if (totalbytes != 0) {
188 ratio = 100.0 * cursize / totalbytes;
189 ratio = MAX(ratio, 0);
190 ratio = MIN(ratio, 100);
191 } else
192 ratio = 100;
193
194 abbrevsize = cursize;
195 for (ai = 0; abbrevsize >= 10000 && ai < sizeof(prefixes); ai++)
196 abbrevsize >>= 10;
197
198 timersub(&now, &lastupdate, &wait);
199 if (cursize > lastsize) {
200 lastupdate = now;
201 lastsize = cursize;
202 wait.tv_sec = 0;
203 }
204 timersub(&now, &start, &td);
205 elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
206
207 bytespersec = 0;
208 if (cursize > 0) {
209 bytespersec = cursize;
210 if (elapsed > 0.0)
211 bytespersec /= elapsed;
212 }
213 for (bi = 1; bytespersec >= 1024000 && bi < sizeof(prefixes); bi++)
214 bytespersec >>= 10;
215
216 nspaces = MIN(get_tty_width() - 79, sizeof(spaces) - 1);
217
115dc20f 218#ifdef HAVE_LONG_LONG_INT
c9c38b73 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,
225 (long long)abbrevsize,
226 prefixes[ai],
227 ai == 0 ? ' ' : 'B',
228 (long long)(bytespersec / 1024),
229 (int)((bytespersec % 1024) * 10 / 1024),
230 prefixes[bi]
231 );
115dc20f 232#else
233 /* XXX: Handle integer overflow? */
234 snprintf(buf, sizeof(buf),
235 "\r%-45.45s%.*s%3d%% %4lu%c%c %3lu.%01d%cB/s",
236 curfile,
237 nspaces,
238 spaces,
239 ratio,
240 (u_long)abbrevsize,
241 prefixes[ai],
242 ai == 0 ? ' ' : 'B',
243 (u_long)(bytespersec / 1024),
244 (int)((bytespersec % 1024) * 10 / 1024),
245 prefixes[bi]
246 );
247#endif
c9c38b73 248
249 if (cursize <= 0 || elapsed <= 0.0 || cursize > totalbytes) {
250 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
251 " --:-- ETA");
252 } else if (wait.tv_sec >= STALLTIME) {
253 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
254 " - stalled -");
255 } else {
256 if (cursize != totalbytes)
257 remaining = (int)(totalbytes / (cursize / elapsed) -
258 elapsed);
259 else
260 remaining = elapsed;
261
262 i = remaining / 3600;
263 if (i)
264 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
265 "%2d:", i);
266 else
267 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
268 " ");
269 i = remaining % 3600;
270 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
271 "%02d:%02d%s", i / 60, i % 60,
272 (cursize != totalbytes) ? " ETA" : " ");
273 }
274 atomicio(write, fileno(stdout), buf, strlen(buf));
275}
276
277static int
278get_tty_width(void)
279{
280 struct winsize winsize;
281
282 if (ioctl(fileno(stdout), TIOCGWINSZ, &winsize) != -1)
283 return (winsize.ws_col ? winsize.ws_col : 80);
284 else
285 return (80);
286}
This page took 0.0932809999999999 seconds and 5 git commands to generate.