Damien Miller | 6fd00e0 | 2003-01-10 21:46:02 +1100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 1999 Theo de Raadt. All rights reserved. |
| 3 | * Copyright (c) 1999 Aaron Campbell. 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 | /* |
Damien Miller | 5f16a5e | 2003-04-09 20:51:55 +1000 | [diff] [blame] | 27 | * Copyright (c) 1997-2003 The NetBSD Foundation, Inc. |
| 28 | * All rights reserved. |
Damien Miller | 6fd00e0 | 2003-01-10 21:46:02 +1100 | [diff] [blame] | 29 | * |
Damien Miller | 5f16a5e | 2003-04-09 20:51:55 +1000 | [diff] [blame] | 30 | * This code is derived from software contributed to The NetBSD Foundation |
| 31 | * by Luke Mewburn. |
| 32 | * |
| 33 | * This code is derived from software contributed to The NetBSD Foundation |
| 34 | * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, |
| 35 | * NASA Ames Research Center. |
Damien Miller | 6fd00e0 | 2003-01-10 21:46:02 +1100 | [diff] [blame] | 36 | * |
| 37 | * Redistribution and use in source and binary forms, with or without |
| 38 | * modification, are permitted provided that the following conditions |
| 39 | * are met: |
| 40 | * 1. Redistributions of source code must retain the above copyright |
| 41 | * notice, this list of conditions and the following disclaimer. |
| 42 | * 2. Redistributions in binary form must reproduce the above copyright |
| 43 | * notice, this list of conditions and the following disclaimer in the |
| 44 | * documentation and/or other materials provided with the distribution. |
| 45 | * 3. All advertising materials mentioning features or use of this software |
| 46 | * must display the following acknowledgement: |
Damien Miller | 5f16a5e | 2003-04-09 20:51:55 +1000 | [diff] [blame] | 47 | * This product includes software developed by the NetBSD |
| 48 | * Foundation, Inc. and its contributors. |
| 49 | * 4. Neither the name of The NetBSD Foundation nor the names of its |
| 50 | * contributors may be used to endorse or promote products derived |
| 51 | * from this software without specific prior written permission. |
Damien Miller | 6fd00e0 | 2003-01-10 21:46:02 +1100 | [diff] [blame] | 52 | * |
Damien Miller | 5f16a5e | 2003-04-09 20:51:55 +1000 | [diff] [blame] | 53 | * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS |
| 54 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
| 55 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 56 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS |
| 57 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 58 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 59 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 60 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 61 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 62 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 63 | * POSSIBILITY OF SUCH DAMAGE. |
Damien Miller | 6fd00e0 | 2003-01-10 21:46:02 +1100 | [diff] [blame] | 64 | */ |
| 65 | |
| 66 | #include "includes.h" |
Darren Tucker | 9f63f22 | 2003-07-03 13:46:56 +1000 | [diff] [blame] | 67 | RCSID("$OpenBSD: progressmeter.c,v 1.8 2003/06/28 16:23:06 deraadt Exp $"); |
Damien Miller | 6fd00e0 | 2003-01-10 21:46:02 +1100 | [diff] [blame] | 68 | |
Damien Miller | b16f874 | 2003-02-24 12:47:15 +1100 | [diff] [blame] | 69 | #ifdef HAVE_LIBGEN_H |
Damien Miller | 6fd00e0 | 2003-01-10 21:46:02 +1100 | [diff] [blame] | 70 | #include <libgen.h> |
Damien Miller | b16f874 | 2003-02-24 12:47:15 +1100 | [diff] [blame] | 71 | #endif |
Damien Miller | 6fd00e0 | 2003-01-10 21:46:02 +1100 | [diff] [blame] | 72 | |
| 73 | #include "atomicio.h" |
Damien Miller | 71a5141 | 2003-01-14 22:23:23 +1100 | [diff] [blame] | 74 | #include "progressmeter.h" |
Damien Miller | 0cbb9de | 2003-06-04 22:56:15 +1000 | [diff] [blame] | 75 | #include "misc.h" |
Damien Miller | 6fd00e0 | 2003-01-10 21:46:02 +1100 | [diff] [blame] | 76 | |
| 77 | /* Number of seconds before xfer considered "stalled". */ |
| 78 | #define STALLTIME 5 |
| 79 | /* alarm() interval for updating progress meter. */ |
| 80 | #define PROGRESSTIME 1 |
| 81 | |
| 82 | /* Signal handler used for updating the progress meter. */ |
| 83 | static void update_progress_meter(int); |
| 84 | |
| 85 | /* Returns non-zero if we are the foreground process. */ |
| 86 | static int foregroundproc(void); |
| 87 | |
| 88 | /* Returns width of the terminal (for progress meter calculations). */ |
| 89 | static int get_tty_width(void); |
| 90 | |
| 91 | /* Visual statistics about files as they are transferred. */ |
Damien Miller | 71a5141 | 2003-01-14 22:23:23 +1100 | [diff] [blame] | 92 | static void draw_progress_meter(void); |
Damien Miller | 6fd00e0 | 2003-01-10 21:46:02 +1100 | [diff] [blame] | 93 | |
| 94 | /* Time a transfer started. */ |
| 95 | static struct timeval start; |
| 96 | |
| 97 | /* Number of bytes of current file transferred so far. */ |
| 98 | static volatile off_t *statbytes; |
| 99 | |
| 100 | /* Total size of current file. */ |
| 101 | static off_t totalbytes; |
| 102 | |
| 103 | /* Name of current file being transferred. */ |
| 104 | static char *curfile; |
| 105 | |
| 106 | /* Time of last update. */ |
| 107 | static struct timeval lastupdate; |
| 108 | |
| 109 | /* Size at the time of the last update. */ |
| 110 | static off_t lastsize; |
| 111 | |
| 112 | void |
| 113 | start_progress_meter(char *file, off_t filesize, off_t *counter) |
| 114 | { |
| 115 | if ((curfile = basename(file)) == NULL) |
| 116 | curfile = file; |
| 117 | |
| 118 | totalbytes = filesize; |
| 119 | statbytes = counter; |
| 120 | (void) gettimeofday(&start, (struct timezone *) 0); |
| 121 | lastupdate = start; |
| 122 | lastsize = 0; |
| 123 | |
| 124 | draw_progress_meter(); |
Damien Miller | 0cbb9de | 2003-06-04 22:56:15 +1000 | [diff] [blame] | 125 | mysignal(SIGALRM, update_progress_meter); |
Damien Miller | 6fd00e0 | 2003-01-10 21:46:02 +1100 | [diff] [blame] | 126 | alarm(PROGRESSTIME); |
| 127 | } |
| 128 | |
| 129 | void |
Damien Miller | 2b92d32 | 2003-06-11 22:05:06 +1000 | [diff] [blame] | 130 | stop_progress_meter(void) |
Damien Miller | 6fd00e0 | 2003-01-10 21:46:02 +1100 | [diff] [blame] | 131 | { |
| 132 | alarm(0); |
| 133 | draw_progress_meter(); |
Damien Miller | 05f5578 | 2003-03-20 10:08:05 +1100 | [diff] [blame] | 134 | if (foregroundproc() != 0) |
Darren Tucker | 9f63f22 | 2003-07-03 13:46:56 +1000 | [diff] [blame] | 135 | atomicio(vwrite, fileno(stdout), "\n", 1); |
Damien Miller | 6fd00e0 | 2003-01-10 21:46:02 +1100 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | static void |
| 139 | update_progress_meter(int ignore) |
| 140 | { |
| 141 | int save_errno = errno; |
| 142 | |
| 143 | draw_progress_meter(); |
Damien Miller | 0cbb9de | 2003-06-04 22:56:15 +1000 | [diff] [blame] | 144 | mysignal(SIGALRM, update_progress_meter); |
Damien Miller | 6fd00e0 | 2003-01-10 21:46:02 +1100 | [diff] [blame] | 145 | alarm(PROGRESSTIME); |
| 146 | errno = save_errno; |
| 147 | } |
| 148 | |
| 149 | static int |
| 150 | foregroundproc(void) |
| 151 | { |
| 152 | static pid_t pgrp = -1; |
| 153 | int ctty_pgrp; |
| 154 | |
| 155 | if (pgrp == -1) |
| 156 | pgrp = getpgrp(); |
| 157 | |
Damien Miller | b16f874 | 2003-02-24 12:47:15 +1100 | [diff] [blame] | 158 | #ifdef HAVE_TCGETPGRP |
| 159 | return ((ctty_pgrp = tcgetpgrp(STDOUT_FILENO)) != -1 && |
| 160 | ctty_pgrp == pgrp); |
| 161 | #else |
Damien Miller | 6fd00e0 | 2003-01-10 21:46:02 +1100 | [diff] [blame] | 162 | return ((ioctl(STDOUT_FILENO, TIOCGPGRP, &ctty_pgrp) != -1 && |
| 163 | ctty_pgrp == pgrp)); |
Damien Miller | b16f874 | 2003-02-24 12:47:15 +1100 | [diff] [blame] | 164 | #endif |
Damien Miller | 6fd00e0 | 2003-01-10 21:46:02 +1100 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | static void |
Damien Miller | 2b92d32 | 2003-06-11 22:05:06 +1000 | [diff] [blame] | 168 | draw_progress_meter(void) |
Damien Miller | 6fd00e0 | 2003-01-10 21:46:02 +1100 | [diff] [blame] | 169 | { |
| 170 | static const char spaces[] = " " |
| 171 | " " |
| 172 | " " |
| 173 | " " |
| 174 | " " |
| 175 | " "; |
| 176 | static const char prefixes[] = " KMGTP"; |
| 177 | struct timeval now, td, wait; |
| 178 | off_t cursize, abbrevsize, bytespersec; |
| 179 | double elapsed; |
| 180 | int ratio, remaining, i, ai, bi, nspaces; |
| 181 | char buf[512]; |
| 182 | |
| 183 | if (foregroundproc() == 0) |
| 184 | return; |
| 185 | |
| 186 | (void) gettimeofday(&now, (struct timezone *) 0); |
| 187 | cursize = *statbytes; |
| 188 | if (totalbytes != 0) { |
| 189 | ratio = 100.0 * cursize / totalbytes; |
| 190 | ratio = MAX(ratio, 0); |
| 191 | ratio = MIN(ratio, 100); |
| 192 | } else |
| 193 | ratio = 100; |
| 194 | |
| 195 | abbrevsize = cursize; |
| 196 | for (ai = 0; abbrevsize >= 10000 && ai < sizeof(prefixes); ai++) |
| 197 | abbrevsize >>= 10; |
| 198 | |
| 199 | timersub(&now, &lastupdate, &wait); |
| 200 | if (cursize > lastsize) { |
| 201 | lastupdate = now; |
| 202 | lastsize = cursize; |
| 203 | wait.tv_sec = 0; |
| 204 | } |
| 205 | timersub(&now, &start, &td); |
| 206 | elapsed = td.tv_sec + (td.tv_usec / 1000000.0); |
| 207 | |
| 208 | bytespersec = 0; |
| 209 | if (cursize > 0) { |
| 210 | bytespersec = cursize; |
| 211 | if (elapsed > 0.0) |
| 212 | bytespersec /= elapsed; |
| 213 | } |
| 214 | for (bi = 1; bytespersec >= 1024000 && bi < sizeof(prefixes); bi++) |
| 215 | bytespersec >>= 10; |
| 216 | |
| 217 | nspaces = MIN(get_tty_width() - 79, sizeof(spaces) - 1); |
| 218 | |
| 219 | snprintf(buf, sizeof(buf), |
| 220 | "\r%-45.45s%.*s%3d%% %4lld%c%c %3lld.%01d%cB/s", |
| 221 | curfile, |
| 222 | nspaces, |
| 223 | spaces, |
| 224 | ratio, |
Ben Lindstrom | 0e7f436 | 2003-04-28 23:30:43 +0000 | [diff] [blame] | 225 | (int64_t)abbrevsize, |
Damien Miller | 6fd00e0 | 2003-01-10 21:46:02 +1100 | [diff] [blame] | 226 | prefixes[ai], |
| 227 | ai == 0 ? ' ' : 'B', |
Ben Lindstrom | 0e7f436 | 2003-04-28 23:30:43 +0000 | [diff] [blame] | 228 | (int64_t)(bytespersec / 1024), |
Damien Miller | 6fd00e0 | 2003-01-10 21:46:02 +1100 | [diff] [blame] | 229 | (int)((bytespersec % 1024) * 10 / 1024), |
| 230 | prefixes[bi] |
| 231 | ); |
Damien Miller | 6fd00e0 | 2003-01-10 21:46:02 +1100 | [diff] [blame] | 232 | |
| 233 | if (cursize <= 0 || elapsed <= 0.0 || cursize > totalbytes) { |
| 234 | snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), |
| 235 | " --:-- ETA"); |
| 236 | } else if (wait.tv_sec >= STALLTIME) { |
| 237 | snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), |
| 238 | " - stalled -"); |
| 239 | } else { |
| 240 | if (cursize != totalbytes) |
| 241 | remaining = (int)(totalbytes / (cursize / elapsed) - |
| 242 | elapsed); |
| 243 | else |
| 244 | remaining = elapsed; |
| 245 | |
| 246 | i = remaining / 3600; |
| 247 | if (i) |
| 248 | snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), |
| 249 | "%2d:", i); |
| 250 | else |
| 251 | snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), |
| 252 | " "); |
| 253 | i = remaining % 3600; |
| 254 | snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), |
| 255 | "%02d:%02d%s", i / 60, i % 60, |
| 256 | (cursize != totalbytes) ? " ETA" : " "); |
| 257 | } |
Darren Tucker | 9f63f22 | 2003-07-03 13:46:56 +1000 | [diff] [blame] | 258 | atomicio(vwrite, fileno(stdout), buf, strlen(buf)); |
Damien Miller | 6fd00e0 | 2003-01-10 21:46:02 +1100 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | static int |
| 262 | get_tty_width(void) |
| 263 | { |
| 264 | struct winsize winsize; |
| 265 | |
| 266 | if (ioctl(fileno(stdout), TIOCGWINSZ, &winsize) != -1) |
| 267 | return (winsize.ws_col ? winsize.ws_col : 80); |
| 268 | else |
| 269 | return (80); |
| 270 | } |