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 "includes.h" 27 28 #include <sys/types.h> 29 #include <sys/ioctl.h> 30 #include <sys/uio.h> 31 32 #include <errno.h> 33 #include <limits.h> 34 #include <signal.h> 35 #include <stdarg.h> 36 #include <stdlib.h> 37 #include <stdio.h> 38 #include <string.h> 39 #include <time.h> 40 #include <unistd.h> 41 42 #include "progressmeter.h" 43 #include "atomicio.h" 44 #include "misc.h" 45 #include "utf8.h" 46 47 #define DEFAULT_WINSIZE 80 48 #define MAX_WINSIZE 512 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); 54 55 /* window resizing */ 56 static void sig_winch(int); 57 static void setscreensize(void); 58 59 /* signal handler for updating the progress meter */ 60 static void sig_alarm(int); 61 62 static double start; /* start progress */ 63 static double last_update; /* last progress update */ 64 static const char *file; /* name of the file being transferred */ 65 static off_t start_pos; /* initial position of transfer */ 66 static off_t end_pos; /* ending position of transfer */ 67 static off_t cur_pos; /* transfer position as of last refresh */ 68 static volatile off_t *counter; /* progress counter */ 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 */ 72 static volatile sig_atomic_t win_resized; /* for window resizing */ 73 static volatile sig_atomic_t alarm_fired; 74 75 /* units for format_size */ 76 static const char unit[] = " KMGT"; 77 78 static int 79 can_output(void) 80 { 81 return (getpgrp() == tcgetpgrp(STDOUT_FILENO)); 82 } 83 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) 89 { 90 int i; 91 static char buf[STRING_SIZE(bytes) * 2 + 16]; 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 } 100 snprintf(buf, sizeof(buf), "%3lld.%1lld%c%s", 101 (long long) (bytes + 5) / 100, 102 (long long) (bytes + 5) / 10 % 10, 103 unit[i], 104 i ? "B" : " "); 105 return buf; 106 } 107 108 static const char * 109 format_size(off_t bytes) 110 { 111 int i; 112 static char buf[STRING_SIZE(bytes) + 16]; 113 114 for (i = 0; bytes >= 10000 && unit[i] != 'T'; i++) 115 bytes = (bytes + 512) / 1024; 116 snprintf(buf, sizeof(buf), "%4lld%c%s", 117 (long long) bytes, 118 unit[i], 119 i ? "B" : " "); 120 return buf; 121 } 122 123 void 124 refresh_progress_meter(int force_update) 125 { 126 char *buf = NULL, *obuf = NULL; 127 off_t transferred; 128 double elapsed, now; 129 int percent; 130 off_t bytes_left; 131 int cur_speed; 132 int hours, minutes, seconds; 133 int file_len, cols; 134 135 if ((!force_update && !alarm_fired && !win_resized) || !can_output()) 136 return; 137 alarm_fired = 0; 138 139 if (win_resized) { 140 setscreensize(); 141 win_resized = 0; 142 } 143 144 transferred = *counter - (cur_pos ? cur_pos : start_pos); 145 cur_pos = *counter; 146 now = monotime_double(); 147 bytes_left = end_pos - cur_pos; 148 149 if (bytes_left > 0) 150 elapsed = now - last_update; 151 else { 152 elapsed = now - start; 153 /* Calculate true total speed when done */ 154 transferred = end_pos - start_pos; 155 bytes_per_second = 0; 156 } 157 158 /* calculate speed */ 159 if (elapsed != 0) 160 cur_speed = (transferred / elapsed); 161 else 162 cur_speed = transferred; 163 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; 170 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 177 /* filename */ 178 file_len = cols = win_size - 36; 179 if (file_len > 0) { 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, ""); 184 } 185 /* percent of transfer done */ 186 if (end_pos == 0 || cur_pos == end_pos) 187 percent = 100; 188 else 189 percent = ((float)cur_pos / end_pos) * 100; 190 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)); 194 195 /* ETA */ 196 if (!transferred) 197 stalled += elapsed; 198 else 199 stalled = 0; 200 201 if (stalled >= STALL_TIME) 202 xextendf(&buf, NULL, "- stalled -"); 203 else if (bytes_per_second == 0 && bytes_left) 204 xextendf(&buf, NULL, " --:-- ETA"); 205 else { 206 if (bytes_left > 0) 207 seconds = bytes_left / bytes_per_second; 208 else 209 seconds = elapsed; 210 211 hours = seconds / 3600; 212 seconds -= hours * 3600; 213 minutes = seconds / 60; 214 seconds -= minutes * 60; 215 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); 221 222 if (bytes_left > 0) 223 xextendf(&buf, NULL, " ETA"); 224 else 225 xextendf(&buf, NULL, " "); 226 } 227 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); 237 } 238 239 static void 240 sig_alarm(int ignore) 241 { 242 alarm_fired = 1; 243 alarm(UPDATE_INTERVAL); 244 } 245 246 void 247 start_progress_meter(const char *f, off_t filesize, off_t *ctr) 248 { 249 start = last_update = monotime_double(); 250 file = f; 251 start_pos = *ctr; 252 end_pos = filesize; 253 cur_pos = 0; 254 counter = ctr; 255 stalled = 0; 256 bytes_per_second = 0; 257 258 setscreensize(); 259 refresh_progress_meter(1); 260 261 ssh_signal(SIGALRM, sig_alarm); 262 ssh_signal(SIGWINCH, sig_winch); 263 alarm(UPDATE_INTERVAL); 264 } 265 266 void 267 stop_progress_meter(void) 268 { 269 alarm(0); 270 271 if (!can_output()) 272 return; 273 274 /* Ensure we complete the progress */ 275 if (cur_pos != end_pos) 276 refresh_progress_meter(1); 277 278 atomicio(vwrite, STDOUT_FILENO, "\n", 1); 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 */ 301 } 302