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