1 /* $OpenBSD: progressmeter.c,v 1.53 2023/04/12 14:22:04 jsg Exp $ */
3 * Copyright (c) 2003 Nils Nordman. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
28 #include <sys/types.h>
29 #include <sys/ioctl.h>
42 #include "progressmeter.h"
47 #define DEFAULT_WINSIZE 80
48 #define MAX_WINSIZE 512
49 #define PADDING 1 /* padding between the progress indicators */
50 #define UPDATE_INTERVAL 1 /* update the progress meter every second */
51 #define STALL_TIME 5 /* we're stalled after this many seconds */
53 /* determines whether we can output to the terminal */
54 static int can_output(void);
57 static void sig_winch(int);
58 static void setscreensize(void);
60 /* signal handler for updating the progress meter */
61 static void sig_alarm(int);
63 static double start
; /* start progress */
64 static double last_update
; /* last progress update */
65 static const char *file
; /* name of the file being transferred */
66 static off_t start_pos
; /* initial position of transfer */
67 static off_t end_pos
; /* ending position of transfer */
68 static off_t cur_pos
; /* transfer position as of last refresh */
69 static volatile off_t
*counter
; /* progress counter */
70 static long stalled
; /* how long we have been stalled */
71 static int bytes_per_second
; /* current speed in bytes per second */
72 static int win_size
; /* terminal window size */
73 static volatile sig_atomic_t win_resized
; /* for window resizing */
74 static volatile sig_atomic_t alarm_fired
;
76 /* units for format_size */
77 static const char unit
[] = " KMGT";
82 return (getpgrp() == tcgetpgrp(STDOUT_FILENO
));
85 /* size needed to format integer type v, using (nbits(v) * log2(10) / 10) */
86 #define STRING_SIZE(v) (((sizeof(v) * 8 * 4) / 10) + 1)
89 format_rate(off_t bytes
)
92 static char buf
[STRING_SIZE(bytes
) * 2 + 16];
95 for (i
= 0; bytes
>= 100*1000 && unit
[i
] != 'T'; i
++)
96 bytes
= (bytes
+ 512) / 1024;
99 bytes
= (bytes
+ 512) / 1024;
101 snprintf(buf
, sizeof(buf
), "%3lld.%1lld%c%s",
102 (long long) (bytes
+ 5) / 100,
103 (long long) (bytes
+ 5) / 10 % 10,
110 format_size(off_t bytes
)
113 static char buf
[STRING_SIZE(bytes
) + 16];
115 for (i
= 0; bytes
>= 10000 && unit
[i
] != 'T'; i
++)
116 bytes
= (bytes
+ 512) / 1024;
117 snprintf(buf
, sizeof(buf
), "%4lld%c%s",
125 refresh_progress_meter(int force_update
)
127 char *buf
= NULL
, *obuf
= NULL
;
133 int hours
, minutes
, seconds
;
136 if ((!force_update
&& !alarm_fired
&& !win_resized
) || !can_output())
145 transferred
= *counter
- (cur_pos
? cur_pos
: start_pos
);
147 now
= monotime_double();
148 bytes_left
= end_pos
- cur_pos
;
151 elapsed
= now
- last_update
;
153 elapsed
= now
- start
;
154 /* Calculate true total speed when done */
155 transferred
= end_pos
- start_pos
;
156 bytes_per_second
= 0;
159 /* calculate speed */
161 cur_speed
= (transferred
/ elapsed
);
163 cur_speed
= transferred
;
165 #define AGE_FACTOR 0.9
166 if (bytes_per_second
!= 0) {
167 bytes_per_second
= (bytes_per_second
* AGE_FACTOR
) +
168 (cur_speed
* (1.0 - AGE_FACTOR
));
170 bytes_per_second
= cur_speed
;
174 /* Don't bother if we can't even display the completion percentage */
179 file_len
= cols
= win_size
- 36;
181 asmprintf(&buf
, INT_MAX
, &cols
, "%-*s", file_len
, file
);
182 /* If we used fewer columns than expected then pad */
184 xextendf(&buf
, NULL
, "%*s", file_len
- cols
, "");
186 /* percent of transfer done */
187 if (end_pos
== 0 || cur_pos
== end_pos
)
190 percent
= ((float)cur_pos
/ end_pos
) * 100;
192 /* percent / amount transferred / bandwidth usage */
193 xextendf(&buf
, NULL
, " %3d%% %s %s/s ", percent
, format_size(cur_pos
),
194 format_rate((off_t
)bytes_per_second
));
202 if (stalled
>= STALL_TIME
)
203 xextendf(&buf
, NULL
, "- stalled -");
204 else if (bytes_per_second
== 0 && bytes_left
)
205 xextendf(&buf
, NULL
, " --:-- ETA");
208 seconds
= bytes_left
/ bytes_per_second
;
212 hours
= seconds
/ 3600;
213 seconds
-= hours
* 3600;
214 minutes
= seconds
/ 60;
215 seconds
-= minutes
* 60;
218 xextendf(&buf
, NULL
, "%d:%02d:%02d",
219 hours
, minutes
, seconds
);
221 xextendf(&buf
, NULL
, " %02d:%02d", minutes
, seconds
);
224 xextendf(&buf
, NULL
, " ETA");
226 xextendf(&buf
, NULL
, " ");
229 /* Finally, truncate string at window width */
231 asmprintf(&obuf
, INT_MAX
, &cols
, " %s", buf
);
233 *obuf
= '\r'; /* must insert as asmprintf() would escape it */
234 atomicio(vwrite
, STDOUT_FILENO
, obuf
, strlen(obuf
));
241 sig_alarm(int ignore
)
244 alarm(UPDATE_INTERVAL
);
248 start_progress_meter(const char *f
, off_t filesize
, off_t
*ctr
)
250 start
= last_update
= monotime_double();
257 bytes_per_second
= 0;
260 refresh_progress_meter(1);
262 ssh_signal(SIGALRM
, sig_alarm
);
263 ssh_signal(SIGWINCH
, sig_winch
);
264 alarm(UPDATE_INTERVAL
);
268 stop_progress_meter(void)
275 /* Ensure we complete the progress */
276 if (cur_pos
!= end_pos
)
277 refresh_progress_meter(1);
279 atomicio(vwrite
, STDOUT_FILENO
, "\n", 1);
291 struct winsize winsize
;
293 if (ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &winsize
) != -1 &&
294 winsize
.ws_col
!= 0) {
295 if (winsize
.ws_col
> MAX_WINSIZE
)
296 win_size
= MAX_WINSIZE
;
298 win_size
= winsize
.ws_col
;
300 win_size
= DEFAULT_WINSIZE
;
301 win_size
+= 1; /* trailing \0 */