Annotation of src/usr.bin/ssh/progressmeter.c, Revision 1.52
1.52 ! guenther 1: /* $OpenBSD: progressmeter.c,v 1.51 2023/02/22 03:56:43 djm Exp $ */
1.1 fgsch 2: /*
1.9 markus 3: * Copyright (c) 2003 Nils Nordman. All rights reserved.
1.1 fgsch 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:
1.31 stevesk 26: #include <sys/types.h>
1.25 stevesk 27: #include <sys/ioctl.h>
1.37 deraadt 28: #include <sys/uio.h>
1.26 stevesk 29:
1.30 stevesk 30: #include <errno.h>
1.51 djm 31: #include <limits.h>
32: #include <signal.h>
1.26 stevesk 33: #include <signal.h>
1.46 dtucker 34: #include <stdarg.h>
1.51 djm 35: #include <stdlib.h>
1.35 stevesk 36: #include <stdio.h>
1.34 stevesk 37: #include <string.h>
1.33 stevesk 38: #include <time.h>
1.31 stevesk 39: #include <unistd.h>
1.1 fgsch 40:
1.9 markus 41: #include "progressmeter.h"
42: #include "atomicio.h"
1.39 dtucker 43: #include "misc.h"
1.46 dtucker 44: #include "utf8.h"
1.9 markus 45:
46: #define DEFAULT_WINSIZE 80
47: #define MAX_WINSIZE 512
48: #define PADDING 1 /* padding between the progress indicators */
49: #define UPDATE_INTERVAL 1 /* update the progress meter every second */
50: #define STALL_TIME 5 /* we're stalled after this many seconds */
51:
52: /* determines whether we can output to the terminal */
53: static int can_output(void);
1.1 fgsch 54:
1.24 jaredy 55: /* window resizing */
56: static void sig_winch(int);
57: static void setscreensize(void);
58:
1.9 markus 59: /* signal handler for updating the progress meter */
1.46 dtucker 60: static void sig_alarm(int);
1.1 fgsch 61:
1.42 dtucker 62: static double start; /* start progress */
63: static double last_update; /* last progress update */
1.41 djm 64: static const char *file; /* name of the file being transferred */
1.40 djm 65: static off_t start_pos; /* initial position of transfer */
1.22 deraadt 66: static off_t end_pos; /* ending position of transfer */
67: static off_t cur_pos; /* transfer position as of last refresh */
1.9 markus 68: static volatile off_t *counter; /* progress counter */
1.22 deraadt 69: static long stalled; /* how long we have been stalled */
70: static int bytes_per_second; /* current speed in bytes per second */
71: static int win_size; /* terminal window size */
1.24 jaredy 72: static volatile sig_atomic_t win_resized; /* for window resizing */
1.46 dtucker 73: static volatile sig_atomic_t alarm_fired;
1.9 markus 74:
1.13 markus 75: /* units for format_size */
76: static const char unit[] = " KMGT";
77:
1.9 markus 78: static int
79: can_output(void)
80: {
81: return (getpgrp() == tcgetpgrp(STDOUT_FILENO));
82: }
83:
1.51 djm 84: /* size needed to format integer type v, using (nbits(v) * log2(10) / 10) */
85: #define STRING_SIZE(v) (((sizeof(v) * 8 * 4) / 10) + 1)
86:
87: static const char *
88: format_rate(off_t bytes)
1.13 markus 89: {
90: int i;
1.51 djm 91: static char buf[STRING_SIZE(bytes) * 2 + 16];
1.13 markus 92:
93: bytes *= 100;
94: for (i = 0; bytes >= 100*1000 && unit[i] != 'T'; i++)
95: bytes = (bytes + 512) / 1024;
96: if (i == 0) {
97: i++;
98: bytes = (bytes + 512) / 1024;
99: }
1.51 djm 100: snprintf(buf, sizeof(buf), "%3lld.%1lld%c%s",
1.17 markus 101: (long long) (bytes + 5) / 100,
1.13 markus 102: (long long) (bytes + 5) / 10 % 10,
103: unit[i],
104: i ? "B" : " ");
1.51 djm 105: return buf;
1.13 markus 106: }
107:
1.51 djm 108: static const char *
109: format_size(off_t bytes)
1.9 markus 110: {
111: int i;
1.51 djm 112: static char buf[STRING_SIZE(bytes) + 16];
1.1 fgsch 113:
1.11 markus 114: for (i = 0; bytes >= 10000 && unit[i] != 'T'; i++)
1.13 markus 115: bytes = (bytes + 512) / 1024;
1.51 djm 116: snprintf(buf, sizeof(buf), "%4lld%c%s",
1.9 markus 117: (long long) bytes,
118: unit[i],
119: i ? "B" : " ");
1.51 djm 120: return buf;
1.9 markus 121: }
1.1 fgsch 122:
1.9 markus 123: void
1.47 dtucker 124: refresh_progress_meter(int force_update)
1.9 markus 125: {
1.51 djm 126: char *buf = NULL, *obuf = NULL;
1.9 markus 127: off_t transferred;
1.42 dtucker 128: double elapsed, now;
1.9 markus 129: int percent;
1.19 markus 130: off_t bytes_left;
1.9 markus 131: int cur_speed;
132: int hours, minutes, seconds;
1.51 djm 133: int file_len, cols;
1.9 markus 134:
1.47 dtucker 135: if ((!force_update && !alarm_fired && !win_resized) || !can_output())
1.46 dtucker 136: return;
137: alarm_fired = 0;
138:
139: if (win_resized) {
140: setscreensize();
141: win_resized = 0;
142: }
143:
1.40 djm 144: transferred = *counter - (cur_pos ? cur_pos : start_pos);
1.9 markus 145: cur_pos = *counter;
1.42 dtucker 146: now = monotime_double();
1.9 markus 147: bytes_left = end_pos - cur_pos;
1.1 fgsch 148:
1.9 markus 149: if (bytes_left > 0)
150: elapsed = now - last_update;
1.18 markus 151: else {
1.9 markus 152: elapsed = now - start;
1.18 markus 153: /* Calculate true total speed when done */
1.40 djm 154: transferred = end_pos - start_pos;
1.18 markus 155: bytes_per_second = 0;
156: }
1.1 fgsch 157:
1.9 markus 158: /* calculate speed */
159: if (elapsed != 0)
160: cur_speed = (transferred / elapsed);
161: else
1.18 markus 162: cur_speed = transferred;
1.1 fgsch 163:
1.9 markus 164: #define AGE_FACTOR 0.9
165: if (bytes_per_second != 0) {
166: bytes_per_second = (bytes_per_second * AGE_FACTOR) +
167: (cur_speed * (1.0 - AGE_FACTOR));
168: } else
169: bytes_per_second = cur_speed;
1.1 fgsch 170:
1.51 djm 171: last_update = now;
172:
173: /* Don't bother if we can't even display the completion percentage */
174: if (win_size < 4)
175: return;
176:
1.9 markus 177: /* filename */
1.51 djm 178: file_len = cols = win_size - 36;
1.9 markus 179: if (file_len > 0) {
1.51 djm 180: asmprintf(&buf, INT_MAX, &cols, "%-*s", file_len, file);
181: /* If we used fewer columns than expected then pad */
182: if (cols < file_len)
183: xextendf(&buf, NULL, "%*s", file_len - cols, "");
1.9 markus 184: }
185: /* percent of transfer done */
1.45 dtucker 186: if (end_pos == 0 || cur_pos == end_pos)
187: percent = 100;
188: else
1.9 markus 189: percent = ((float)cur_pos / end_pos) * 100;
190:
1.51 djm 191: /* percent / amount transferred / bandwidth usage */
192: xextendf(&buf, NULL, " %3d%% %s %s/s ", percent, format_size(cur_pos),
193: format_rate((off_t)bytes_per_second));
1.9 markus 194:
195: /* ETA */
196: if (!transferred)
197: stalled += elapsed;
198: else
199: stalled = 0;
1.1 fgsch 200:
1.9 markus 201: if (stalled >= STALL_TIME)
1.51 djm 202: xextendf(&buf, NULL, "- stalled -");
1.9 markus 203: else if (bytes_per_second == 0 && bytes_left)
1.51 djm 204: xextendf(&buf, NULL, " --:-- ETA");
1.9 markus 205: else {
206: if (bytes_left > 0)
207: seconds = bytes_left / bytes_per_second;
208: else
209: seconds = elapsed;
1.1 fgsch 210:
1.9 markus 211: hours = seconds / 3600;
212: seconds -= hours * 3600;
213: minutes = seconds / 60;
214: seconds -= minutes * 60;
215:
1.51 djm 216: if (hours != 0) {
217: xextendf(&buf, NULL, "%d:%02d:%02d",
218: hours, minutes, seconds);
219: } else
220: xextendf(&buf, NULL, " %02d:%02d", minutes, seconds);
1.1 fgsch 221:
1.9 markus 222: if (bytes_left > 0)
1.51 djm 223: xextendf(&buf, NULL, " ETA");
1.9 markus 224: else
1.51 djm 225: xextendf(&buf, NULL, " ");
1.9 markus 226: }
1.1 fgsch 227:
1.51 djm 228: /* Finally, truncate string at window width */
229: cols = win_size - 1;
230: asmprintf(&obuf, INT_MAX, &cols, " %s", buf);
231: if (obuf != NULL) {
232: *obuf = '\r'; /* must insert as asmprintf() would escape it */
233: atomicio(vwrite, STDOUT_FILENO, obuf, strlen(obuf));
234: }
235: free(buf);
236: free(obuf);
1.1 fgsch 237: }
238:
239: static void
1.46 dtucker 240: sig_alarm(int ignore)
1.1 fgsch 241: {
1.46 dtucker 242: alarm_fired = 1;
1.9 markus 243: alarm(UPDATE_INTERVAL);
1.1 fgsch 244: }
245:
1.9 markus 246: void
1.41 djm 247: start_progress_meter(const char *f, off_t filesize, off_t *ctr)
1.1 fgsch 248: {
1.42 dtucker 249: start = last_update = monotime_double();
1.9 markus 250: file = f;
1.40 djm 251: start_pos = *ctr;
1.9 markus 252: end_pos = filesize;
253: cur_pos = 0;
1.21 avsm 254: counter = ctr;
1.9 markus 255: stalled = 0;
256: bytes_per_second = 0;
257:
1.24 jaredy 258: setscreensize();
1.47 dtucker 259: refresh_progress_meter(1);
1.1 fgsch 260:
1.50 dtucker 261: ssh_signal(SIGALRM, sig_alarm);
262: ssh_signal(SIGWINCH, sig_winch);
1.9 markus 263: alarm(UPDATE_INTERVAL);
1.1 fgsch 264: }
265:
1.9 markus 266: void
267: stop_progress_meter(void)
1.1 fgsch 268: {
1.9 markus 269: alarm(0);
1.1 fgsch 270:
1.9 markus 271: if (!can_output())
1.1 fgsch 272: return;
273:
1.9 markus 274: /* Ensure we complete the progress */
275: if (cur_pos != end_pos)
1.47 dtucker 276: refresh_progress_meter(1);
1.12 markus 277:
1.9 markus 278: atomicio(vwrite, STDOUT_FILENO, "\n", 1);
1.24 jaredy 279: }
280:
281: static void
282: sig_winch(int sig)
283: {
284: win_resized = 1;
285: }
286:
287: static void
288: setscreensize(void)
289: {
290: struct winsize winsize;
291:
292: if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &winsize) != -1 &&
293: winsize.ws_col != 0) {
294: if (winsize.ws_col > MAX_WINSIZE)
295: win_size = MAX_WINSIZE;
296: else
297: win_size = winsize.ws_col;
298: } else
299: win_size = DEFAULT_WINSIZE;
300: win_size += 1; /* trailing \0 */
1.1 fgsch 301: }