]>
Commit | Line | Data |
---|---|---|
c9c38b73 | 1 | /* |
178b1a1d | 2 | * Copyright (c) 2003 Nils Nordman. All rights reserved. |
c9c38b73 | 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 | ||
c9c38b73 | 25 | #include "includes.h" |
bd7c69ea | 26 | |
27 | #include <sys/ioctl.h> | |
c9c38b73 | 28 | |
ada68823 | 29 | #include <signal.h> |
30 | ||
b7fd001f | 31 | #include "progressmeter.h" |
178b1a1d | 32 | #include "atomicio.h" |
8d9bb5dd | 33 | #include "misc.h" |
c9c38b73 | 34 | |
178b1a1d | 35 | #define DEFAULT_WINSIZE 80 |
36 | #define MAX_WINSIZE 512 | |
37 | #define PADDING 1 /* padding between the progress indicators */ | |
38 | #define UPDATE_INTERVAL 1 /* update the progress meter every second */ | |
39 | #define STALL_TIME 5 /* we're stalled after this many seconds */ | |
c9c38b73 | 40 | |
178b1a1d | 41 | /* determines whether we can output to the terminal */ |
42 | static int can_output(void); | |
c9c38b73 | 43 | |
178b1a1d | 44 | /* formats and inserts the specified size into the given buffer */ |
45 | static void format_size(char *, int, off_t); | |
46 | static void format_rate(char *, int, off_t); | |
c9c38b73 | 47 | |
826563dc | 48 | /* window resizing */ |
49 | static void sig_winch(int); | |
50 | static void setscreensize(void); | |
51 | ||
178b1a1d | 52 | /* updates the progressmeter to reflect the current state of the transfer */ |
53 | void refresh_progress_meter(void); | |
c9c38b73 | 54 | |
178b1a1d | 55 | /* signal handler for updating the progress meter */ |
56 | static void update_progress_meter(int); | |
c9c38b73 | 57 | |
f2107e97 | 58 | static time_t start; /* start progress */ |
59 | static time_t last_update; /* last progress update */ | |
60 | static char *file; /* name of the file being transferred */ | |
61 | static off_t end_pos; /* ending position of transfer */ | |
62 | static off_t cur_pos; /* transfer position as of last refresh */ | |
178b1a1d | 63 | static volatile off_t *counter; /* progress counter */ |
f2107e97 | 64 | static long stalled; /* how long we have been stalled */ |
65 | static int bytes_per_second; /* current speed in bytes per second */ | |
66 | static int win_size; /* terminal window size */ | |
826563dc | 67 | static volatile sig_atomic_t win_resized; /* for window resizing */ |
c9c38b73 | 68 | |
178b1a1d | 69 | /* units for format_size */ |
70 | static const char unit[] = " KMGT"; | |
c9c38b73 | 71 | |
178b1a1d | 72 | static int |
73 | can_output(void) | |
74 | { | |
75 | return (getpgrp() == tcgetpgrp(STDOUT_FILENO)); | |
76 | } | |
c9c38b73 | 77 | |
178b1a1d | 78 | static void |
79 | format_rate(char *buf, int size, off_t bytes) | |
80 | { | |
81 | int i; | |
82 | ||
83 | bytes *= 100; | |
84 | for (i = 0; bytes >= 100*1000 && unit[i] != 'T'; i++) | |
85 | bytes = (bytes + 512) / 1024; | |
86 | if (i == 0) { | |
87 | i++; | |
88 | bytes = (bytes + 512) / 1024; | |
89 | } | |
90 | snprintf(buf, size, "%3lld.%1lld%c%s", | |
419094c6 | 91 | (long long) (bytes + 5) / 100, |
92 | (long long) (bytes + 5) / 10 % 10, | |
178b1a1d | 93 | unit[i], |
94 | i ? "B" : " "); | |
95 | } | |
c9c38b73 | 96 | |
178b1a1d | 97 | static void |
98 | format_size(char *buf, int size, off_t bytes) | |
99 | { | |
100 | int i; | |
101 | ||
102 | for (i = 0; bytes >= 10000 && unit[i] != 'T'; i++) | |
103 | bytes = (bytes + 512) / 1024; | |
104 | snprintf(buf, size, "%4lld%c%s", | |
419094c6 | 105 | (long long) bytes, |
178b1a1d | 106 | unit[i], |
107 | i ? "B" : " "); | |
108 | } | |
c9c38b73 | 109 | |
110 | void | |
178b1a1d | 111 | refresh_progress_meter(void) |
c9c38b73 | 112 | { |
178b1a1d | 113 | char buf[MAX_WINSIZE + 1]; |
114 | time_t now; | |
115 | off_t transferred; | |
116 | double elapsed; | |
117 | int percent; | |
7b0a59c9 | 118 | off_t bytes_left; |
178b1a1d | 119 | int cur_speed; |
120 | int hours, minutes, seconds; | |
121 | int i, len; | |
122 | int file_len; | |
123 | ||
124 | transferred = *counter - cur_pos; | |
125 | cur_pos = *counter; | |
126 | now = time(NULL); | |
127 | bytes_left = end_pos - cur_pos; | |
128 | ||
129 | if (bytes_left > 0) | |
130 | elapsed = now - last_update; | |
adfde93f | 131 | else { |
178b1a1d | 132 | elapsed = now - start; |
adfde93f | 133 | /* Calculate true total speed when done */ |
134 | transferred = end_pos; | |
135 | bytes_per_second = 0; | |
136 | } | |
c9c38b73 | 137 | |
178b1a1d | 138 | /* calculate speed */ |
139 | if (elapsed != 0) | |
140 | cur_speed = (transferred / elapsed); | |
141 | else | |
adfde93f | 142 | cur_speed = transferred; |
c9c38b73 | 143 | |
178b1a1d | 144 | #define AGE_FACTOR 0.9 |
145 | if (bytes_per_second != 0) { | |
146 | bytes_per_second = (bytes_per_second * AGE_FACTOR) + | |
147 | (cur_speed * (1.0 - AGE_FACTOR)); | |
148 | } else | |
149 | bytes_per_second = cur_speed; | |
150 | ||
151 | /* filename */ | |
152 | buf[0] = '\0'; | |
153 | file_len = win_size - 35; | |
154 | if (file_len > 0) { | |
8e382949 | 155 | len = snprintf(buf, file_len + 1, "\r%s", file); |
3845a9ac | 156 | if (len < 0) |
157 | len = 0; | |
4b826d75 | 158 | if (len >= file_len + 1) |
159 | len = file_len; | |
178b1a1d | 160 | for (i = len; i < file_len; i++ ) |
161 | buf[i] = ' '; | |
162 | buf[file_len] = '\0'; | |
163 | } | |
c9c38b73 | 164 | |
178b1a1d | 165 | /* percent of transfer done */ |
166 | if (end_pos != 0) | |
167 | percent = ((float)cur_pos / end_pos) * 100; | |
168 | else | |
169 | percent = 100; | |
170 | snprintf(buf + strlen(buf), win_size - strlen(buf), | |
171 | " %3d%% ", percent); | |
172 | ||
173 | /* amount transferred */ | |
174 | format_size(buf + strlen(buf), win_size - strlen(buf), | |
175 | cur_pos); | |
176 | strlcat(buf, " ", win_size); | |
177 | ||
178 | /* bandwidth usage */ | |
179 | format_rate(buf + strlen(buf), win_size - strlen(buf), | |
f6be21a0 | 180 | (off_t)bytes_per_second); |
178b1a1d | 181 | strlcat(buf, "/s ", win_size); |
182 | ||
183 | /* ETA */ | |
184 | if (!transferred) | |
185 | stalled += elapsed; | |
186 | else | |
187 | stalled = 0; | |
188 | ||
189 | if (stalled >= STALL_TIME) | |
190 | strlcat(buf, "- stalled -", win_size); | |
191 | else if (bytes_per_second == 0 && bytes_left) | |
192 | strlcat(buf, " --:-- ETA", win_size); | |
193 | else { | |
194 | if (bytes_left > 0) | |
195 | seconds = bytes_left / bytes_per_second; | |
196 | else | |
197 | seconds = elapsed; | |
198 | ||
199 | hours = seconds / 3600; | |
200 | seconds -= hours * 3600; | |
201 | minutes = seconds / 60; | |
202 | seconds -= minutes * 60; | |
203 | ||
204 | if (hours != 0) | |
205 | snprintf(buf + strlen(buf), win_size - strlen(buf), | |
206 | "%d:%02d:%02d", hours, minutes, seconds); | |
207 | else | |
208 | snprintf(buf + strlen(buf), win_size - strlen(buf), | |
209 | " %02d:%02d", minutes, seconds); | |
210 | ||
211 | if (bytes_left > 0) | |
212 | strlcat(buf, " ETA", win_size); | |
213 | else | |
214 | strlcat(buf, " ", win_size); | |
215 | } | |
216 | ||
0469be42 | 217 | atomicio(vwrite, STDOUT_FILENO, buf, win_size - 1); |
178b1a1d | 218 | last_update = now; |
c9c38b73 | 219 | } |
220 | ||
221 | static void | |
222 | update_progress_meter(int ignore) | |
223 | { | |
178b1a1d | 224 | int save_errno; |
225 | ||
226 | save_errno = errno; | |
227 | ||
826563dc | 228 | if (win_resized) { |
229 | setscreensize(); | |
230 | win_resized = 0; | |
231 | } | |
178b1a1d | 232 | if (can_output()) |
233 | refresh_progress_meter(); | |
c9c38b73 | 234 | |
f00d1f78 | 235 | signal(SIGALRM, update_progress_meter); |
178b1a1d | 236 | alarm(UPDATE_INTERVAL); |
c9c38b73 | 237 | errno = save_errno; |
238 | } | |
239 | ||
178b1a1d | 240 | void |
ca75d7de | 241 | start_progress_meter(char *f, off_t filesize, off_t *ctr) |
c9c38b73 | 242 | { |
178b1a1d | 243 | start = last_update = time(NULL); |
244 | file = f; | |
245 | end_pos = filesize; | |
246 | cur_pos = 0; | |
ca75d7de | 247 | counter = ctr; |
178b1a1d | 248 | stalled = 0; |
249 | bytes_per_second = 0; | |
250 | ||
826563dc | 251 | setscreensize(); |
178b1a1d | 252 | if (can_output()) |
253 | refresh_progress_meter(); | |
c9c38b73 | 254 | |
f00d1f78 | 255 | signal(SIGALRM, update_progress_meter); |
826563dc | 256 | signal(SIGWINCH, sig_winch); |
178b1a1d | 257 | alarm(UPDATE_INTERVAL); |
c9c38b73 | 258 | } |
259 | ||
178b1a1d | 260 | void |
261 | stop_progress_meter(void) | |
c9c38b73 | 262 | { |
178b1a1d | 263 | alarm(0); |
c9c38b73 | 264 | |
178b1a1d | 265 | if (!can_output()) |
266 | return; | |
267 | ||
268 | /* Ensure we complete the progress */ | |
269 | if (cur_pos != end_pos) | |
270 | refresh_progress_meter(); | |
271 | ||
272 | atomicio(vwrite, STDOUT_FILENO, "\n", 1); | |
c9c38b73 | 273 | } |
826563dc | 274 | |
275 | static void | |
276 | sig_winch(int sig) | |
277 | { | |
278 | win_resized = 1; | |
279 | } | |
280 | ||
281 | static void | |
282 | setscreensize(void) | |
283 | { | |
284 | struct winsize winsize; | |
285 | ||
286 | if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &winsize) != -1 && | |
287 | winsize.ws_col != 0) { | |
288 | if (winsize.ws_col > MAX_WINSIZE) | |
289 | win_size = MAX_WINSIZE; | |
290 | else | |
291 | win_size = winsize.ws_col; | |
292 | } else | |
293 | win_size = DEFAULT_WINSIZE; | |
294 | win_size += 1; /* trailing \0 */ | |
295 | } |