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