Damien Miller | 6fd00e0 | 2003-01-10 21:46:02 +1100 | [diff] [blame] | 1 | /* |
Darren Tucker | 06ef75b | 2003-08-02 23:28:38 +1000 | [diff] [blame] | 2 | * Copyright (c) 2003 Nils Nordman. All rights reserved. |
Damien Miller | 6fd00e0 | 2003-01-10 21:46:02 +1100 | [diff] [blame] | 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 14 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 15 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 16 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 17 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 18 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 19 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 20 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 21 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 22 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 23 | */ |
| 24 | |
Damien Miller | 6fd00e0 | 2003-01-10 21:46:02 +1100 | [diff] [blame] | 25 | #include "includes.h" |
Damien Miller | 17e91c0 | 2006-03-15 11:28:34 +1100 | [diff] [blame] | 26 | RCSID("$OpenBSD: progressmeter.c,v 1.25 2006/02/10 00:27:13 stevesk Exp $"); |
| 27 | |
| 28 | #include <sys/ioctl.h> |
Damien Miller | 6fd00e0 | 2003-01-10 21:46:02 +1100 | [diff] [blame] | 29 | |
Damien Miller | 71a5141 | 2003-01-14 22:23:23 +1100 | [diff] [blame] | 30 | #include "progressmeter.h" |
Darren Tucker | 06ef75b | 2003-08-02 23:28:38 +1000 | [diff] [blame] | 31 | #include "atomicio.h" |
Damien Miller | 0cbb9de | 2003-06-04 22:56:15 +1000 | [diff] [blame] | 32 | #include "misc.h" |
Damien Miller | 6fd00e0 | 2003-01-10 21:46:02 +1100 | [diff] [blame] | 33 | |
Darren Tucker | 06ef75b | 2003-08-02 23:28:38 +1000 | [diff] [blame] | 34 | #define DEFAULT_WINSIZE 80 |
| 35 | #define MAX_WINSIZE 512 |
| 36 | #define PADDING 1 /* padding between the progress indicators */ |
| 37 | #define UPDATE_INTERVAL 1 /* update the progress meter every second */ |
| 38 | #define STALL_TIME 5 /* we're stalled after this many seconds */ |
Damien Miller | 6fd00e0 | 2003-01-10 21:46:02 +1100 | [diff] [blame] | 39 | |
Darren Tucker | 06ef75b | 2003-08-02 23:28:38 +1000 | [diff] [blame] | 40 | /* determines whether we can output to the terminal */ |
| 41 | static int can_output(void); |
| 42 | |
| 43 | /* formats and inserts the specified size into the given buffer */ |
| 44 | static void format_size(char *, int, off_t); |
| 45 | static void format_rate(char *, int, off_t); |
| 46 | |
Damien Miller | 0565696 | 2005-06-16 13:18:04 +1000 | [diff] [blame] | 47 | /* window resizing */ |
| 48 | static void sig_winch(int); |
| 49 | static void setscreensize(void); |
| 50 | |
Darren Tucker | 06ef75b | 2003-08-02 23:28:38 +1000 | [diff] [blame] | 51 | /* updates the progressmeter to reflect the current state of the transfer */ |
| 52 | void refresh_progress_meter(void); |
| 53 | |
| 54 | /* signal handler for updating the progress meter */ |
Damien Miller | 6fd00e0 | 2003-01-10 21:46:02 +1100 | [diff] [blame] | 55 | static void update_progress_meter(int); |
| 56 | |
Darren Tucker | fc95970 | 2004-07-17 16:12:08 +1000 | [diff] [blame] | 57 | static time_t start; /* start progress */ |
| 58 | static time_t last_update; /* last progress update */ |
| 59 | static char *file; /* name of the file being transferred */ |
| 60 | static off_t end_pos; /* ending position of transfer */ |
| 61 | static off_t cur_pos; /* transfer position as of last refresh */ |
Darren Tucker | 06ef75b | 2003-08-02 23:28:38 +1000 | [diff] [blame] | 62 | static volatile off_t *counter; /* progress counter */ |
Darren Tucker | fc95970 | 2004-07-17 16:12:08 +1000 | [diff] [blame] | 63 | static long stalled; /* how long we have been stalled */ |
| 64 | static int bytes_per_second; /* current speed in bytes per second */ |
| 65 | static int win_size; /* terminal window size */ |
Damien Miller | 0565696 | 2005-06-16 13:18:04 +1000 | [diff] [blame] | 66 | static volatile sig_atomic_t win_resized; /* for window resizing */ |
Damien Miller | 6fd00e0 | 2003-01-10 21:46:02 +1100 | [diff] [blame] | 67 | |
Darren Tucker | 06ef75b | 2003-08-02 23:28:38 +1000 | [diff] [blame] | 68 | /* units for format_size */ |
| 69 | static const char unit[] = " KMGT"; |
Damien Miller | 6fd00e0 | 2003-01-10 21:46:02 +1100 | [diff] [blame] | 70 | |
Darren Tucker | 06ef75b | 2003-08-02 23:28:38 +1000 | [diff] [blame] | 71 | static int |
| 72 | can_output(void) |
| 73 | { |
| 74 | return (getpgrp() == tcgetpgrp(STDOUT_FILENO)); |
| 75 | } |
Damien Miller | 6fd00e0 | 2003-01-10 21:46:02 +1100 | [diff] [blame] | 76 | |
Darren Tucker | 06ef75b | 2003-08-02 23:28:38 +1000 | [diff] [blame] | 77 | static void |
| 78 | format_rate(char *buf, int size, off_t bytes) |
| 79 | { |
| 80 | int i; |
Damien Miller | 6fd00e0 | 2003-01-10 21:46:02 +1100 | [diff] [blame] | 81 | |
Darren Tucker | 06ef75b | 2003-08-02 23:28:38 +1000 | [diff] [blame] | 82 | bytes *= 100; |
| 83 | for (i = 0; bytes >= 100*1000 && unit[i] != 'T'; i++) |
| 84 | bytes = (bytes + 512) / 1024; |
| 85 | if (i == 0) { |
| 86 | i++; |
| 87 | bytes = (bytes + 512) / 1024; |
| 88 | } |
| 89 | snprintf(buf, size, "%3lld.%1lld%c%s", |
Darren Tucker | e0be304 | 2005-11-25 14:44:55 +1100 | [diff] [blame] | 90 | (long long) (bytes + 5) / 100, |
| 91 | (long long) (bytes + 5) / 10 % 10, |
Darren Tucker | 06ef75b | 2003-08-02 23:28:38 +1000 | [diff] [blame] | 92 | unit[i], |
| 93 | i ? "B" : " "); |
| 94 | } |
Damien Miller | 6fd00e0 | 2003-01-10 21:46:02 +1100 | [diff] [blame] | 95 | |
Darren Tucker | 06ef75b | 2003-08-02 23:28:38 +1000 | [diff] [blame] | 96 | static void |
| 97 | format_size(char *buf, int size, off_t bytes) |
| 98 | { |
| 99 | int i; |
Damien Miller | 6fd00e0 | 2003-01-10 21:46:02 +1100 | [diff] [blame] | 100 | |
Darren Tucker | 06ef75b | 2003-08-02 23:28:38 +1000 | [diff] [blame] | 101 | for (i = 0; bytes >= 10000 && unit[i] != 'T'; i++) |
| 102 | bytes = (bytes + 512) / 1024; |
| 103 | snprintf(buf, size, "%4lld%c%s", |
Darren Tucker | e0be304 | 2005-11-25 14:44:55 +1100 | [diff] [blame] | 104 | (long long) bytes, |
Darren Tucker | 06ef75b | 2003-08-02 23:28:38 +1000 | [diff] [blame] | 105 | unit[i], |
| 106 | i ? "B" : " "); |
| 107 | } |
Damien Miller | 6fd00e0 | 2003-01-10 21:46:02 +1100 | [diff] [blame] | 108 | |
| 109 | void |
Darren Tucker | 06ef75b | 2003-08-02 23:28:38 +1000 | [diff] [blame] | 110 | refresh_progress_meter(void) |
Damien Miller | 6fd00e0 | 2003-01-10 21:46:02 +1100 | [diff] [blame] | 111 | { |
Darren Tucker | 06ef75b | 2003-08-02 23:28:38 +1000 | [diff] [blame] | 112 | char buf[MAX_WINSIZE + 1]; |
| 113 | time_t now; |
| 114 | off_t transferred; |
| 115 | double elapsed; |
| 116 | int percent; |
Darren Tucker | 7f73a49 | 2004-02-06 16:41:37 +1100 | [diff] [blame] | 117 | off_t bytes_left; |
Darren Tucker | 06ef75b | 2003-08-02 23:28:38 +1000 | [diff] [blame] | 118 | int cur_speed; |
| 119 | int hours, minutes, seconds; |
| 120 | int i, len; |
| 121 | int file_len; |
Damien Miller | 6fd00e0 | 2003-01-10 21:46:02 +1100 | [diff] [blame] | 122 | |
Darren Tucker | 06ef75b | 2003-08-02 23:28:38 +1000 | [diff] [blame] | 123 | transferred = *counter - cur_pos; |
| 124 | cur_pos = *counter; |
| 125 | now = time(NULL); |
| 126 | bytes_left = end_pos - cur_pos; |
Damien Miller | 6fd00e0 | 2003-01-10 21:46:02 +1100 | [diff] [blame] | 127 | |
Darren Tucker | 06ef75b | 2003-08-02 23:28:38 +1000 | [diff] [blame] | 128 | if (bytes_left > 0) |
| 129 | elapsed = now - last_update; |
Darren Tucker | 1fb0425 | 2003-12-09 19:07:13 +1100 | [diff] [blame] | 130 | else { |
Darren Tucker | 06ef75b | 2003-08-02 23:28:38 +1000 | [diff] [blame] | 131 | elapsed = now - start; |
Darren Tucker | 1fb0425 | 2003-12-09 19:07:13 +1100 | [diff] [blame] | 132 | /* Calculate true total speed when done */ |
| 133 | transferred = end_pos; |
| 134 | bytes_per_second = 0; |
| 135 | } |
Darren Tucker | 06ef75b | 2003-08-02 23:28:38 +1000 | [diff] [blame] | 136 | |
| 137 | /* calculate speed */ |
| 138 | if (elapsed != 0) |
| 139 | cur_speed = (transferred / elapsed); |
| 140 | else |
Darren Tucker | 1fb0425 | 2003-12-09 19:07:13 +1100 | [diff] [blame] | 141 | cur_speed = transferred; |
Darren Tucker | 06ef75b | 2003-08-02 23:28:38 +1000 | [diff] [blame] | 142 | |
| 143 | #define AGE_FACTOR 0.9 |
| 144 | if (bytes_per_second != 0) { |
| 145 | bytes_per_second = (bytes_per_second * AGE_FACTOR) + |
| 146 | (cur_speed * (1.0 - AGE_FACTOR)); |
| 147 | } else |
| 148 | bytes_per_second = cur_speed; |
| 149 | |
| 150 | /* filename */ |
| 151 | buf[0] = '\0'; |
| 152 | file_len = win_size - 35; |
| 153 | if (file_len > 0) { |
Damien Miller | 17af176 | 2003-09-02 22:53:01 +1000 | [diff] [blame] | 154 | len = snprintf(buf, file_len + 1, "\r%s", file); |
Damien Miller | b5829f5 | 2003-09-02 22:53:32 +1000 | [diff] [blame] | 155 | if (len < 0) |
| 156 | len = 0; |
Damien Miller | 41bfc29 | 2005-05-26 12:07:32 +1000 | [diff] [blame] | 157 | if (len >= file_len + 1) |
| 158 | len = file_len; |
Darren Tucker | 06ef75b | 2003-08-02 23:28:38 +1000 | [diff] [blame] | 159 | for (i = len; i < file_len; i++ ) |
| 160 | buf[i] = ' '; |
| 161 | buf[file_len] = '\0'; |
| 162 | } |
| 163 | |
| 164 | /* percent of transfer done */ |
| 165 | if (end_pos != 0) |
| 166 | percent = ((float)cur_pos / end_pos) * 100; |
| 167 | else |
| 168 | percent = 100; |
| 169 | snprintf(buf + strlen(buf), win_size - strlen(buf), |
| 170 | " %3d%% ", percent); |
| 171 | |
| 172 | /* amount transferred */ |
| 173 | format_size(buf + strlen(buf), win_size - strlen(buf), |
| 174 | cur_pos); |
| 175 | strlcat(buf, " ", win_size); |
| 176 | |
| 177 | /* bandwidth usage */ |
| 178 | format_rate(buf + strlen(buf), win_size - strlen(buf), |
Darren Tucker | 1f8311c | 2004-05-13 16:39:33 +1000 | [diff] [blame] | 179 | (off_t)bytes_per_second); |
Darren Tucker | 06ef75b | 2003-08-02 23:28:38 +1000 | [diff] [blame] | 180 | strlcat(buf, "/s ", win_size); |
| 181 | |
| 182 | /* ETA */ |
| 183 | if (!transferred) |
| 184 | stalled += elapsed; |
| 185 | else |
| 186 | stalled = 0; |
| 187 | |
| 188 | if (stalled >= STALL_TIME) |
| 189 | strlcat(buf, "- stalled -", win_size); |
| 190 | else if (bytes_per_second == 0 && bytes_left) |
| 191 | strlcat(buf, " --:-- ETA", win_size); |
| 192 | else { |
| 193 | if (bytes_left > 0) |
| 194 | seconds = bytes_left / bytes_per_second; |
| 195 | else |
| 196 | seconds = elapsed; |
| 197 | |
| 198 | hours = seconds / 3600; |
| 199 | seconds -= hours * 3600; |
| 200 | minutes = seconds / 60; |
| 201 | seconds -= minutes * 60; |
| 202 | |
| 203 | if (hours != 0) |
| 204 | snprintf(buf + strlen(buf), win_size - strlen(buf), |
| 205 | "%d:%02d:%02d", hours, minutes, seconds); |
| 206 | else |
| 207 | snprintf(buf + strlen(buf), win_size - strlen(buf), |
| 208 | " %02d:%02d", minutes, seconds); |
| 209 | |
| 210 | if (bytes_left > 0) |
| 211 | strlcat(buf, " ETA", win_size); |
| 212 | else |
| 213 | strlcat(buf, " ", win_size); |
| 214 | } |
| 215 | |
Darren Tucker | 6cc310b | 2003-10-02 16:15:15 +1000 | [diff] [blame] | 216 | atomicio(vwrite, STDOUT_FILENO, buf, win_size - 1); |
Darren Tucker | 06ef75b | 2003-08-02 23:28:38 +1000 | [diff] [blame] | 217 | last_update = now; |
| 218 | } |
| 219 | |
| 220 | static void |
| 221 | update_progress_meter(int ignore) |
| 222 | { |
| 223 | int save_errno; |
| 224 | |
| 225 | save_errno = errno; |
| 226 | |
Damien Miller | 0565696 | 2005-06-16 13:18:04 +1000 | [diff] [blame] | 227 | if (win_resized) { |
| 228 | setscreensize(); |
| 229 | win_resized = 0; |
| 230 | } |
Darren Tucker | 06ef75b | 2003-08-02 23:28:38 +1000 | [diff] [blame] | 231 | if (can_output()) |
| 232 | refresh_progress_meter(); |
| 233 | |
Ben Lindstrom | 5ade9ab | 2003-08-25 01:16:21 +0000 | [diff] [blame] | 234 | signal(SIGALRM, update_progress_meter); |
Darren Tucker | 06ef75b | 2003-08-02 23:28:38 +1000 | [diff] [blame] | 235 | alarm(UPDATE_INTERVAL); |
| 236 | errno = save_errno; |
| 237 | } |
| 238 | |
| 239 | void |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 240 | start_progress_meter(char *f, off_t filesize, off_t *ctr) |
Darren Tucker | 06ef75b | 2003-08-02 23:28:38 +1000 | [diff] [blame] | 241 | { |
Darren Tucker | 06ef75b | 2003-08-02 23:28:38 +1000 | [diff] [blame] | 242 | start = last_update = time(NULL); |
| 243 | file = f; |
| 244 | end_pos = filesize; |
| 245 | cur_pos = 0; |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 246 | counter = ctr; |
Darren Tucker | 06ef75b | 2003-08-02 23:28:38 +1000 | [diff] [blame] | 247 | stalled = 0; |
| 248 | bytes_per_second = 0; |
| 249 | |
Damien Miller | 0565696 | 2005-06-16 13:18:04 +1000 | [diff] [blame] | 250 | setscreensize(); |
Darren Tucker | 06ef75b | 2003-08-02 23:28:38 +1000 | [diff] [blame] | 251 | if (can_output()) |
| 252 | refresh_progress_meter(); |
| 253 | |
Ben Lindstrom | 5ade9ab | 2003-08-25 01:16:21 +0000 | [diff] [blame] | 254 | signal(SIGALRM, update_progress_meter); |
Damien Miller | 0565696 | 2005-06-16 13:18:04 +1000 | [diff] [blame] | 255 | signal(SIGWINCH, sig_winch); |
Darren Tucker | 06ef75b | 2003-08-02 23:28:38 +1000 | [diff] [blame] | 256 | alarm(UPDATE_INTERVAL); |
Damien Miller | 6fd00e0 | 2003-01-10 21:46:02 +1100 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | void |
Damien Miller | 2b92d32 | 2003-06-11 22:05:06 +1000 | [diff] [blame] | 260 | stop_progress_meter(void) |
Damien Miller | 6fd00e0 | 2003-01-10 21:46:02 +1100 | [diff] [blame] | 261 | { |
| 262 | alarm(0); |
Damien Miller | 6fd00e0 | 2003-01-10 21:46:02 +1100 | [diff] [blame] | 263 | |
Darren Tucker | 06ef75b | 2003-08-02 23:28:38 +1000 | [diff] [blame] | 264 | if (!can_output()) |
Damien Miller | 6fd00e0 | 2003-01-10 21:46:02 +1100 | [diff] [blame] | 265 | return; |
| 266 | |
Darren Tucker | 06ef75b | 2003-08-02 23:28:38 +1000 | [diff] [blame] | 267 | /* Ensure we complete the progress */ |
| 268 | if (cur_pos != end_pos) |
| 269 | refresh_progress_meter(); |
Damien Miller | 6fd00e0 | 2003-01-10 21:46:02 +1100 | [diff] [blame] | 270 | |
Darren Tucker | 06ef75b | 2003-08-02 23:28:38 +1000 | [diff] [blame] | 271 | atomicio(vwrite, STDOUT_FILENO, "\n", 1); |
Damien Miller | 6fd00e0 | 2003-01-10 21:46:02 +1100 | [diff] [blame] | 272 | } |
Damien Miller | 0565696 | 2005-06-16 13:18:04 +1000 | [diff] [blame] | 273 | |
| 274 | static void |
| 275 | sig_winch(int sig) |
| 276 | { |
| 277 | win_resized = 1; |
| 278 | } |
| 279 | |
| 280 | static void |
| 281 | setscreensize(void) |
| 282 | { |
| 283 | struct winsize winsize; |
| 284 | |
| 285 | if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &winsize) != -1 && |
| 286 | winsize.ws_col != 0) { |
| 287 | if (winsize.ws_col > MAX_WINSIZE) |
| 288 | win_size = MAX_WINSIZE; |
| 289 | else |
| 290 | win_size = winsize.ws_col; |
| 291 | } else |
| 292 | win_size = DEFAULT_WINSIZE; |
| 293 | win_size += 1; /* trailing \0 */ |
| 294 | } |