jcs's openbsd hax
openbsd
at jcs 301 lines 7.7 kB view raw
1/* $OpenBSD: progressmeter.c,v 1.56 2025/06/11 13:27:11 dtucker 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 */ 51static int can_output(void); 52 53/* window resizing */ 54static void sig_winch(int); 55static void setscreensize(void); 56 57/* signal handler for updating the progress meter */ 58static void sig_alarm(int); 59 60static double start; /* start progress */ 61static double last_update; /* last progress update */ 62static const char *file; /* name of the file being transferred */ 63static off_t start_pos; /* initial position of transfer */ 64static off_t end_pos; /* ending position of transfer */ 65static off_t cur_pos; /* transfer position as of last refresh */ 66static volatile off_t *counter; /* progress counter */ 67static long stalled; /* how long we have been stalled */ 68static int bytes_per_second; /* current speed in bytes per second */ 69static int win_size; /* terminal window size */ 70static volatile sig_atomic_t win_resized; /* for window resizing */ 71static volatile sig_atomic_t alarm_fired; 72 73/* units for format_size */ 74static const char unit[] = " KMGT"; 75 76static int 77can_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 85static const char * 86format_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 /* Display at least KB, even when rate is low or zero. */ 95 if (i == 0) { 96 i++; 97 bytes = (bytes + 512) / 1024; 98 } 99 snprintf(buf, sizeof(buf), "%3lld.%1lld%cB", 100 (long long) (bytes + 5) / 100, 101 (long long) (bytes + 5) / 10 % 10, 102 unit[i]); 103 return buf; 104} 105 106static const char * 107format_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 121void 122refresh_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 (file == NULL || (!force_update && !alarm_fired && !win_resized) || 134 !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 238static void 239sig_alarm(int ignore) 240{ 241 alarm_fired = 1; 242 alarm(UPDATE_INTERVAL); 243} 244 245void 246start_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 265void 266stop_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 file = NULL; 279} 280 281static void 282sig_winch(int sig) 283{ 284 win_resized = 1; 285} 286 287static void 288setscreensize(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}