comparison progressmeter.c @ 4:fe6bca95afa7

Makefile.in contains updated files required
author Matt Johnston <matt@ucc.asn.au>
date Tue, 01 Jun 2004 02:46:09 +0000
parents
children 5c6f9d27ea1c
comparison
equal deleted inserted replaced
-1:000000000000 4:fe6bca95afa7
1 #ifdef PROGRESS_METER
2 /*
3 * Copyright (c) 2003 Nils Nordman. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26 #include "includes.h"
27 /*RCSID("$OpenBSD: progressmeter.c,v 1.15 2003/08/31 12:14:22 markus Exp $");*/
28
29 #include "progressmeter.h"
30 #include "atomicio.h"
31 #include "scpmisc.h"
32
33 #define DEFAULT_WINSIZE 80
34 #define MAX_WINSIZE 512
35 #define PADDING 1 /* padding between the progress indicators */
36 #define UPDATE_INTERVAL 1 /* update the progress meter every second */
37 #define STALL_TIME 5 /* we're stalled after this many seconds */
38
39 /* determines whether we can output to the terminal */
40 static int can_output(void);
41
42 /* formats and inserts the specified size into the given buffer */
43 static void format_size(char *, int, off_t);
44 static void format_rate(char *, int, off_t);
45
46 /* updates the progressmeter to reflect the current state of the transfer */
47 void refresh_progress_meter(void);
48
49 /* signal handler for updating the progress meter */
50 static void update_progress_meter(int);
51
52 static time_t start; /* start progress */
53 static time_t last_update; /* last progress update */
54 static char *file; /* name of the file being transferred */
55 static off_t end_pos; /* ending position of transfer */
56 static off_t cur_pos; /* transfer position as of last refresh */
57 static volatile off_t *counter; /* progress counter */
58 static long stalled; /* how long we have been stalled */
59 static int bytes_per_second; /* current speed in bytes per second */
60 static int win_size; /* terminal window size */
61
62 /* units for format_size */
63 static const char unit[] = " KMGT";
64
65 static int
66 can_output(void)
67 {
68 return (getpgrp() == tcgetpgrp(STDOUT_FILENO));
69 }
70
71 static void
72 format_rate(char *buf, int size, off_t bytes)
73 {
74 int i;
75
76 bytes *= 100;
77 for (i = 0; bytes >= 100*1000 && unit[i] != 'T'; i++)
78 bytes = (bytes + 512) / 1024;
79 if (i == 0) {
80 i++;
81 bytes = (bytes + 512) / 1024;
82 }
83 snprintf(buf, size, "%3lld.%1lld%c%s",
84 (int64_t) bytes / 100,
85 (int64_t) (bytes + 5) / 10 % 10,
86 unit[i],
87 i ? "B" : " ");
88 }
89
90 static void
91 format_size(char *buf, int size, off_t bytes)
92 {
93 int i;
94
95 for (i = 0; bytes >= 10000 && unit[i] != 'T'; i++)
96 bytes = (bytes + 512) / 1024;
97 snprintf(buf, size, "%4lld%c%s",
98 (int64_t) bytes,
99 unit[i],
100 i ? "B" : " ");
101 }
102
103 void
104 refresh_progress_meter(void)
105 {
106 char buf[MAX_WINSIZE + 1];
107 time_t now;
108 off_t transferred;
109 double elapsed;
110 int percent;
111 int bytes_left;
112 int cur_speed;
113 int hours, minutes, seconds;
114 int i, len;
115 int file_len;
116
117 transferred = *counter - cur_pos;
118 cur_pos = *counter;
119 now = time(NULL);
120 bytes_left = end_pos - cur_pos;
121
122 if (bytes_left > 0)
123 elapsed = now - last_update;
124 else
125 elapsed = now - start;
126
127 /* calculate speed */
128 if (elapsed != 0)
129 cur_speed = (transferred / elapsed);
130 else
131 cur_speed = 0;
132
133 #define AGE_FACTOR 0.9
134 if (bytes_per_second != 0) {
135 bytes_per_second = (bytes_per_second * AGE_FACTOR) +
136 (cur_speed * (1.0 - AGE_FACTOR));
137 } else
138 bytes_per_second = cur_speed;
139
140 /* filename */
141 buf[0] = '\0';
142 file_len = win_size - 35;
143 if (file_len > 0) {
144 len = snprintf(buf, file_len + 1, "\r%s", file);
145 if (len < 0)
146 len = 0;
147 for (i = len; i < file_len; i++ )
148 buf[i] = ' ';
149 buf[file_len] = '\0';
150 }
151
152 /* percent of transfer done */
153 if (end_pos != 0)
154 percent = ((float)cur_pos / end_pos) * 100;
155 else
156 percent = 100;
157 snprintf(buf + strlen(buf), win_size - strlen(buf),
158 " %3d%% ", percent);
159
160 /* amount transferred */
161 format_size(buf + strlen(buf), win_size - strlen(buf),
162 cur_pos);
163 strlcat(buf, " ", win_size);
164
165 /* bandwidth usage */
166 format_rate(buf + strlen(buf), win_size - strlen(buf),
167 bytes_per_second);
168 strlcat(buf, "/s ", win_size);
169
170 /* ETA */
171 if (!transferred)
172 stalled += elapsed;
173 else
174 stalled = 0;
175
176 if (stalled >= STALL_TIME)
177 strlcat(buf, "- stalled -", win_size);
178 else if (bytes_per_second == 0 && bytes_left)
179 strlcat(buf, " --:-- ETA", win_size);
180 else {
181 if (bytes_left > 0)
182 seconds = bytes_left / bytes_per_second;
183 else
184 seconds = elapsed;
185
186 hours = seconds / 3600;
187 seconds -= hours * 3600;
188 minutes = seconds / 60;
189 seconds -= minutes * 60;
190
191 if (hours != 0)
192 snprintf(buf + strlen(buf), win_size - strlen(buf),
193 "%d:%02d:%02d", hours, minutes, seconds);
194 else
195 snprintf(buf + strlen(buf), win_size - strlen(buf),
196 " %02d:%02d", minutes, seconds);
197
198 if (bytes_left > 0)
199 strlcat(buf, " ETA", win_size);
200 else
201 strlcat(buf, " ", win_size);
202 }
203
204 atomicio(vwrite, STDOUT_FILENO, buf, win_size);
205 last_update = now;
206 }
207
208 static void
209 update_progress_meter(int ignore)
210 {
211 int save_errno;
212
213 save_errno = errno;
214
215 if (can_output())
216 refresh_progress_meter();
217
218 signal(SIGALRM, update_progress_meter);
219 alarm(UPDATE_INTERVAL);
220 errno = save_errno;
221 }
222
223 void
224 start_progress_meter(char *f, off_t filesize, off_t *stat)
225 {
226 struct winsize winsize;
227
228 start = last_update = time(NULL);
229 file = f;
230 end_pos = filesize;
231 cur_pos = 0;
232 counter = stat;
233 stalled = 0;
234 bytes_per_second = 0;
235
236 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &winsize) != -1 &&
237 winsize.ws_col != 0) {
238 if (winsize.ws_col > MAX_WINSIZE)
239 win_size = MAX_WINSIZE;
240 else
241 win_size = winsize.ws_col;
242 } else
243 win_size = DEFAULT_WINSIZE;
244 win_size += 1; /* trailing \0 */
245
246 if (can_output())
247 refresh_progress_meter();
248
249 signal(SIGALRM, update_progress_meter);
250 alarm(UPDATE_INTERVAL);
251 }
252
253 void
254 stop_progress_meter(void)
255 {
256 alarm(0);
257
258 if (!can_output())
259 return;
260
261 /* Ensure we complete the progress */
262 if (cur_pos != end_pos)
263 refresh_progress_meter();
264
265 atomicio(vwrite, STDOUT_FILENO, "\n", 1);
266 }
267 #endif /* PROGRESS_METER */