Annotation of src/usr.bin/ssh/progressmeter.c, Revision 1.16
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.16 ! markus 26: RCSID("$OpenBSD: progressmeter.c,v 1.15 2003/08/31 12:14:22 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) {
1.14 markus 142: len = snprintf(buf, file_len + 1, "\r%s", file);
1.15 markus 143: if (len < 0)
144: len = 0;
1.9 markus 145: for (i = len; i < file_len; i++ )
146: buf[i] = ' ';
147: buf[file_len] = '\0';
148: }
1.1 fgsch 149:
1.9 markus 150: /* percent of transfer done */
151: if (end_pos != 0)
152: percent = ((float)cur_pos / end_pos) * 100;
153: else
154: percent = 100;
155: snprintf(buf + strlen(buf), win_size - strlen(buf),
156: " %3d%% ", percent);
157:
158: /* amount transferred */
159: format_size(buf + strlen(buf), win_size - strlen(buf),
160: cur_pos);
161: strlcat(buf, " ", win_size);
162:
163: /* bandwidth usage */
1.13 markus 164: format_rate(buf + strlen(buf), win_size - strlen(buf),
1.9 markus 165: bytes_per_second);
166: strlcat(buf, "/s ", win_size);
167:
168: /* ETA */
169: if (!transferred)
170: stalled += elapsed;
171: else
172: stalled = 0;
1.1 fgsch 173:
1.9 markus 174: if (stalled >= STALL_TIME)
1.10 markus 175: strlcat(buf, "- stalled -", win_size);
1.9 markus 176: else if (bytes_per_second == 0 && bytes_left)
177: strlcat(buf, " --:-- ETA", win_size);
178: else {
179: if (bytes_left > 0)
180: seconds = bytes_left / bytes_per_second;
181: else
182: seconds = elapsed;
1.1 fgsch 183:
1.9 markus 184: hours = seconds / 3600;
185: seconds -= hours * 3600;
186: minutes = seconds / 60;
187: seconds -= minutes * 60;
188:
189: if (hours != 0)
190: snprintf(buf + strlen(buf), win_size - strlen(buf),
191: "%d:%02d:%02d", hours, minutes, seconds);
192: else
193: snprintf(buf + strlen(buf), win_size - strlen(buf),
194: " %02d:%02d", minutes, seconds);
1.1 fgsch 195:
1.9 markus 196: if (bytes_left > 0)
197: strlcat(buf, " ETA", win_size);
198: else
199: strlcat(buf, " ", win_size);
200: }
1.1 fgsch 201:
1.16 ! markus 202: atomicio(vwrite, STDOUT_FILENO, buf, win_size - 1);
1.9 markus 203: last_update = now;
1.1 fgsch 204: }
205:
206: static void
207: update_progress_meter(int ignore)
208: {
1.9 markus 209: int save_errno;
210:
211: save_errno = errno;
212:
213: if (can_output())
214: refresh_progress_meter();
1.1 fgsch 215:
216: signal(SIGALRM, update_progress_meter);
1.9 markus 217: alarm(UPDATE_INTERVAL);
1.1 fgsch 218: errno = save_errno;
219: }
220:
1.9 markus 221: void
222: start_progress_meter(char *f, off_t filesize, off_t *stat)
1.1 fgsch 223: {
1.9 markus 224: struct winsize winsize;
1.1 fgsch 225:
1.9 markus 226: start = last_update = time(NULL);
227: file = f;
228: end_pos = filesize;
229: cur_pos = 0;
230: counter = stat;
231: stalled = 0;
232: bytes_per_second = 0;
233:
234: if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &winsize) != -1 &&
235: winsize.ws_col != 0) {
236: if (winsize.ws_col > MAX_WINSIZE)
237: win_size = MAX_WINSIZE;
238: else
239: win_size = winsize.ws_col;
240: } else
241: win_size = DEFAULT_WINSIZE;
242: win_size += 1; /* trailing \0 */
1.12 markus 243:
1.9 markus 244: if (can_output())
245: refresh_progress_meter();
1.1 fgsch 246:
1.9 markus 247: signal(SIGALRM, update_progress_meter);
248: alarm(UPDATE_INTERVAL);
1.1 fgsch 249: }
250:
1.9 markus 251: void
252: stop_progress_meter(void)
1.1 fgsch 253: {
1.9 markus 254: alarm(0);
1.1 fgsch 255:
1.9 markus 256: if (!can_output())
1.1 fgsch 257: return;
258:
1.9 markus 259: /* Ensure we complete the progress */
260: if (cur_pos != end_pos)
261: refresh_progress_meter();
1.12 markus 262:
1.9 markus 263: atomicio(vwrite, STDOUT_FILENO, "\n", 1);
1.1 fgsch 264: }