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