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