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