Annotation of src/usr.bin/ssh/progressmeter.c, Revision 1.51
1.51 ! djm 1: /* $OpenBSD: progressmeter.c,v 1.50 2020/01/23 07:10:22 dtucker 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:
1.32 stevesk 239: /*ARGSUSED*/
1.1 fgsch 240: static void
1.46 dtucker 241: sig_alarm(int ignore)
1.1 fgsch 242: {
1.46 dtucker 243: alarm_fired = 1;
1.9 markus 244: alarm(UPDATE_INTERVAL);
1.1 fgsch 245: }
246:
1.9 markus 247: void
1.41 djm 248: start_progress_meter(const char *f, off_t filesize, off_t *ctr)
1.1 fgsch 249: {
1.42 dtucker 250: start = last_update = monotime_double();
1.9 markus 251: file = f;
1.40 djm 252: start_pos = *ctr;
1.9 markus 253: end_pos = filesize;
254: cur_pos = 0;
1.21 avsm 255: counter = ctr;
1.9 markus 256: stalled = 0;
257: bytes_per_second = 0;
258:
1.24 jaredy 259: setscreensize();
1.47 dtucker 260: refresh_progress_meter(1);
1.1 fgsch 261:
1.50 dtucker 262: ssh_signal(SIGALRM, sig_alarm);
263: ssh_signal(SIGWINCH, sig_winch);
1.9 markus 264: alarm(UPDATE_INTERVAL);
1.1 fgsch 265: }
266:
1.9 markus 267: void
268: stop_progress_meter(void)
1.1 fgsch 269: {
1.9 markus 270: alarm(0);
1.1 fgsch 271:
1.9 markus 272: if (!can_output())
1.1 fgsch 273: return;
274:
1.9 markus 275: /* Ensure we complete the progress */
276: if (cur_pos != end_pos)
1.47 dtucker 277: refresh_progress_meter(1);
1.12 markus 278:
1.9 markus 279: atomicio(vwrite, STDOUT_FILENO, "\n", 1);
1.24 jaredy 280: }
281:
1.28 deraadt 282: /*ARGSUSED*/
1.24 jaredy 283: static void
284: sig_winch(int sig)
285: {
286: win_resized = 1;
287: }
288:
289: static void
290: setscreensize(void)
291: {
292: struct winsize winsize;
293:
294: if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &winsize) != -1 &&
295: winsize.ws_col != 0) {
296: if (winsize.ws_col > MAX_WINSIZE)
297: win_size = MAX_WINSIZE;
298: else
299: win_size = winsize.ws_col;
300: } else
301: win_size = DEFAULT_WINSIZE;
302: win_size += 1; /* trailing \0 */
1.1 fgsch 303: }