Annotation of src/usr.bin/ssh/progressmeter.c, Revision 1.9
1.1 fgsch 1: /*
1.9 ! markus 2: * Copyright (c) 2003 Nils Nordman. All rights reserved.
1.1 fgsch 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:
25: #include "includes.h"
1.8 deraadt 26: RCSID("$OpenBSD: progressmeter.c,v 1.7 2003/06/10 22:20:52 deraadt Exp $");
1.1 fgsch 27:
1.9 ! markus 28: #include "progressmeter.h"
! 29: #include "atomicio.h"
! 30:
! 31: #define DEFAULT_WINSIZE 80
! 32: #define MAX_WINSIZE 512
! 33: #define PADDING 1 /* padding between the progress indicators */
! 34: #define UPDATE_INTERVAL 1 /* update the progress meter every second */
! 35: #define STALL_TIME 5 /* we're stalled after this many seconds */
! 36:
! 37: /* determines whether we can output to the terminal */
! 38: static int can_output(void);
1.1 fgsch 39:
1.9 ! markus 40: /* formats and inserts the specified size into the given buffer */
! 41: static void format_size(char *, int, off_t);
1.1 fgsch 42:
1.9 ! markus 43: /* updates the progressmeter to reflect the current state of the transfer */
! 44: void refresh_progress_meter(void);
1.1 fgsch 45:
1.9 ! markus 46: /* signal handler for updating the progress meter */
1.1 fgsch 47: static void update_progress_meter(int);
48:
1.9 ! markus 49: static time_t start; /* start progress */
! 50: static time_t last_update; /* last progress update */
! 51: static char *file; /* name of the file being transferred */
! 52: static off_t end_pos; /* ending position of transfer */
! 53: static off_t cur_pos; /* transfer position as of last refresh */
! 54: static volatile off_t *counter; /* progress counter */
! 55: static long stalled; /* how long we have been stalled */
! 56: static int bytes_per_second; /* current speed in bytes per second */
! 57: static int win_size; /* terminal window size */
! 58:
! 59: static int
! 60: can_output(void)
! 61: {
! 62: return (getpgrp() == tcgetpgrp(STDOUT_FILENO));
! 63: }
! 64:
! 65:
! 66: static void
! 67: format_size(char *buf, int size, off_t bytes)
! 68: {
! 69: static const char unit[] = " KMGT";
! 70: int i;
1.1 fgsch 71:
1.9 ! markus 72: for (i = 0; bytes > 10240 && unit[i] != 'T'; i++)
! 73: bytes /= 1024;
! 74: snprintf(buf, size, "%6lld%c%s",
! 75: (long long) bytes,
! 76: unit[i],
! 77: i ? "B" : " ");
! 78: }
1.1 fgsch 79:
1.9 ! markus 80: void
! 81: refresh_progress_meter(void)
! 82: {
! 83: char buf[MAX_WINSIZE + 1];
! 84: time_t now;
! 85: off_t transferred;
! 86: double elapsed;
! 87: int percent;
! 88: int bytes_left;
! 89: int cur_speed;
! 90: int hours, minutes, seconds;
! 91: int i, len;
! 92: int file_len;
! 93:
! 94: transferred = *counter - cur_pos;
! 95: cur_pos = *counter;
! 96: now = time(NULL);
! 97: bytes_left = end_pos - cur_pos;
1.1 fgsch 98:
1.9 ! markus 99: if (bytes_left > 0)
! 100: elapsed = now - last_update;
! 101: else
! 102: elapsed = now - start;
1.1 fgsch 103:
1.9 ! markus 104: /* calculate speed */
! 105: if (elapsed != 0)
! 106: cur_speed = (transferred / elapsed);
! 107: else
! 108: cur_speed = 0;
1.1 fgsch 109:
1.9 ! markus 110: #define AGE_FACTOR 0.9
! 111: if (bytes_per_second != 0) {
! 112: bytes_per_second = (bytes_per_second * AGE_FACTOR) +
! 113: (cur_speed * (1.0 - AGE_FACTOR));
! 114: } else
! 115: bytes_per_second = cur_speed;
1.1 fgsch 116:
1.9 ! markus 117: memset(buf, 'a', sizeof(buf));
1.1 fgsch 118:
1.9 ! markus 119: /* filename */
! 120: buf[0] = '\0';
! 121: file_len = win_size - 36;
! 122: if (file_len > 0) {
! 123: len = snprintf(buf, file_len, "\r%s", file);
! 124: for (i = len; i < file_len; i++ )
! 125: buf[i] = ' ';
! 126: buf[file_len] = '\0';
! 127: }
1.1 fgsch 128:
1.9 ! markus 129: /* percent of transfer done */
! 130: if (end_pos != 0)
! 131: percent = ((float)cur_pos / end_pos) * 100;
! 132: else
! 133: percent = 100;
! 134: snprintf(buf + strlen(buf), win_size - strlen(buf),
! 135: " %3d%% ", percent);
! 136:
! 137: /* amount transferred */
! 138: format_size(buf + strlen(buf), win_size - strlen(buf),
! 139: cur_pos);
! 140: strlcat(buf, " ", win_size);
! 141:
! 142: /* bandwidth usage */
! 143: format_size(buf + strlen(buf), win_size - strlen(buf),
! 144: bytes_per_second);
! 145: strlcat(buf, "/s ", win_size);
! 146:
! 147: /* ETA */
! 148: if (!transferred)
! 149: stalled += elapsed;
! 150: else
! 151: stalled = 0;
1.1 fgsch 152:
1.9 ! markus 153: if (stalled >= STALL_TIME)
! 154: strlcat(buf, " - stalled -", win_size);
! 155: else if (bytes_per_second == 0 && bytes_left)
! 156: strlcat(buf, " --:-- ETA", win_size);
! 157: else {
! 158: if (bytes_left > 0)
! 159: seconds = bytes_left / bytes_per_second;
! 160: else
! 161: seconds = elapsed;
1.1 fgsch 162:
1.9 ! markus 163: hours = seconds / 3600;
! 164: seconds -= hours * 3600;
! 165: minutes = seconds / 60;
! 166: seconds -= minutes * 60;
! 167:
! 168: if (hours != 0)
! 169: snprintf(buf + strlen(buf), win_size - strlen(buf),
! 170: "%d:%02d:%02d", hours, minutes, seconds);
! 171: else
! 172: snprintf(buf + strlen(buf), win_size - strlen(buf),
! 173: " %02d:%02d", minutes, seconds);
1.1 fgsch 174:
1.9 ! markus 175: if (bytes_left > 0)
! 176: strlcat(buf, " ETA", win_size);
! 177: else
! 178: strlcat(buf, " ", win_size);
! 179: }
1.1 fgsch 180:
1.9 ! markus 181: atomicio(vwrite, STDOUT_FILENO, buf, win_size);
! 182: last_update = now;
1.1 fgsch 183: }
184:
185: static void
186: update_progress_meter(int ignore)
187: {
1.9 ! markus 188: int save_errno;
! 189:
! 190: save_errno = errno;
! 191:
! 192: if (can_output())
! 193: refresh_progress_meter();
1.1 fgsch 194:
195: signal(SIGALRM, update_progress_meter);
1.9 ! markus 196: alarm(UPDATE_INTERVAL);
1.1 fgsch 197: errno = save_errno;
198: }
199:
1.9 ! markus 200: void
! 201: start_progress_meter(char *f, off_t filesize, off_t *stat)
1.1 fgsch 202: {
1.9 ! markus 203: struct winsize winsize;
1.1 fgsch 204:
1.9 ! markus 205: start = last_update = time(NULL);
! 206: file = f;
! 207: end_pos = filesize;
! 208: cur_pos = 0;
! 209: counter = stat;
! 210: stalled = 0;
! 211: bytes_per_second = 0;
! 212:
! 213: if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &winsize) != -1 &&
! 214: winsize.ws_col != 0) {
! 215: if (winsize.ws_col > MAX_WINSIZE)
! 216: win_size = MAX_WINSIZE;
! 217: else
! 218: win_size = winsize.ws_col;
! 219: } else
! 220: win_size = DEFAULT_WINSIZE;
! 221: win_size += 1; /* trailing \0 */
! 222:
! 223: if (can_output())
! 224: refresh_progress_meter();
1.1 fgsch 225:
1.9 ! markus 226: signal(SIGALRM, update_progress_meter);
! 227: alarm(UPDATE_INTERVAL);
1.1 fgsch 228: }
229:
1.9 ! markus 230: void
! 231: stop_progress_meter(void)
1.1 fgsch 232: {
1.9 ! markus 233: alarm(0);
1.1 fgsch 234:
1.9 ! markus 235: if (!can_output())
1.1 fgsch 236: return;
237:
1.9 ! markus 238: /* Ensure we complete the progress */
! 239: if (cur_pos != end_pos)
! 240: refresh_progress_meter();
! 241:
! 242: atomicio(vwrite, STDOUT_FILENO, "\n", 1);
1.1 fgsch 243: }