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