1/*	$NetBSD: progressmeter.c,v 1.15 2023/10/25 20:19:57 christos Exp $	*/
2/* $OpenBSD: progressmeter.c,v 1.53 2023/04/12 14:22:04 jsg Exp $ */
3
4/*
5 * Copyright (c) 2003 Nils Nordman.  All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include "includes.h"
29__RCSID("$NetBSD: progressmeter.c,v 1.15 2023/10/25 20:19:57 christos Exp $");
30#include <sys/types.h>
31#include <sys/ioctl.h>
32#include <sys/uio.h>
33
34#include <errno.h>
35#include <limits.h>
36#include <signal.h>
37#include <stdarg.h>
38#include <stdlib.h>
39#include <stdio.h>
40#include <string.h>
41#include <time.h>
42#include <unistd.h>
43
44#include "progressmeter.h"
45#include "atomicio.h"
46#include "misc.h"
47#include "utf8.h"
48
49#define DEFAULT_WINSIZE 80
50#define MAX_WINSIZE 512
51#define PADDING 1		/* padding between the progress indicators */
52#define UPDATE_INTERVAL 1	/* update the progress meter every second */
53#define STALL_TIME 5		/* we're stalled after this many seconds */
54
55/* determines whether we can output to the terminal */
56static int can_output(void);
57
58/* window resizing */
59static void sig_winch(int);
60static void setscreensize(void);
61
62/* signal handler for updating the progress meter */
63static void sig_alarm(int);
64
65static double start;		/* start progress */
66static double last_update;	/* last progress update */
67static const char *file;	/* name of the file being transferred */
68static off_t start_pos;		/* initial position of transfer */
69static off_t end_pos;		/* ending position of transfer */
70static off_t cur_pos;		/* transfer position as of last refresh */
71static off_t last_pos;
72static off_t max_delta_pos = 0;
73static volatile off_t *counter;	/* progress counter */
74static long stalled;		/* how long we have been stalled */
75static int bytes_per_second;	/* current speed in bytes per second */
76static int win_size;		/* terminal window size */
77static volatile sig_atomic_t win_resized; /* for window resizing */
78static volatile sig_atomic_t alarm_fired;
79
80/* units for format_size */
81static const char unit[] = " KMGT";
82
83static int
84can_output(void)
85{
86	return (getpgrp() == tcgetpgrp(STDOUT_FILENO));
87}
88
89/* size needed to format integer type v, using (nbits(v) * log2(10) / 10) */
90#define STRING_SIZE(v) (((sizeof(v) * 8 * 4) / 10) + 1)
91
92static const char *
93format_rate(off_t bytes)
94{
95	int i;
96	static char buf[STRING_SIZE(bytes) * 2 + 16];
97
98	bytes *= 100;
99	for (i = 0; bytes >= 100*1000 && unit[i] != 'T'; i++)
100		bytes = (bytes + 512) / 1024;
101	if (i == 0) {
102		i++;
103		bytes = (bytes + 512) / 1024;
104	}
105	snprintf(buf, sizeof(buf), "%3lld.%1lld%c%s",
106	    (long long) (bytes + 5) / 100,
107	    (long long) (bytes + 5) / 10 % 10,
108	    unit[i],
109	    i ? "B" : " ");
110	return buf;
111}
112
113static const char *
114format_size(off_t bytes)
115{
116	int i;
117	static char buf[STRING_SIZE(bytes) + 16];
118
119	for (i = 0; bytes >= 10000 && unit[i] != 'T'; i++)
120		bytes = (bytes + 512) / 1024;
121	snprintf(buf, sizeof(buf), "%4lld%c%s",
122	    (long long) bytes,
123	    unit[i],
124	    i ? "B" : " ");
125	return buf;
126}
127
128void
129refresh_progress_meter(int force_update)
130{
131	char *buf = NULL, *obuf = NULL;
132	off_t transferred;
133	double elapsed, now;
134	int percent;
135	off_t bytes_left;
136	int cur_speed;
137	int hours, minutes, seconds;
138	int file_len, cols;
139	off_t delta_pos;
140
141	if ((!force_update && !alarm_fired && !win_resized) || !can_output())
142		return;
143	alarm_fired = 0;
144
145	if (win_resized) {
146		setscreensize();
147		win_resized = 0;
148	}
149
150	transferred = *counter - (cur_pos ? cur_pos : start_pos);
151	cur_pos = *counter;
152	now = monotime_double();
153	bytes_left = end_pos - cur_pos;
154
155	delta_pos = cur_pos - last_pos;
156	if (delta_pos > max_delta_pos)
157		max_delta_pos = delta_pos;
158
159	if (bytes_left > 0)
160		elapsed = now - last_update;
161	else {
162		elapsed = now - start;
163		/* Calculate true total speed when done */
164		transferred = end_pos - start_pos;
165		bytes_per_second = 0;
166	}
167
168	/* calculate speed */
169	if (elapsed != 0)
170		cur_speed = (transferred / elapsed);
171	else
172		cur_speed = transferred;
173
174#define AGE_FACTOR 0.9
175	if (bytes_per_second != 0) {
176		bytes_per_second = (bytes_per_second * AGE_FACTOR) +
177		    (cur_speed * (1.0 - AGE_FACTOR));
178	} else
179		bytes_per_second = cur_speed;
180
181	last_update = now;
182
183	/* Don't bother if we can't even display the completion percentage */
184	if (win_size < 4)
185		return;
186
187	/* filename */
188	file_len = cols = win_size - 36;
189	if (file_len > 0) {
190		asmprintf(&buf, INT_MAX, &cols, "%-*s", file_len, file);
191		/* If we used fewer columns than expected then pad */
192		if (cols < file_len)
193			xextendf(&buf, NULL, "%*s", file_len - cols, "");
194	}
195	/* percent of transfer done */
196	if (end_pos == 0 || cur_pos == end_pos)
197		percent = 100;
198	else
199		percent = ((float)cur_pos / end_pos) * 100;
200
201	/* percent / amount transferred / bandwidth usage */
202	xextendf(&buf, NULL, " %3d%% %s %s/s ", percent, format_size(cur_pos),
203	    format_rate((off_t)bytes_per_second));
204
205	/* ETA */
206	if (!transferred)
207		stalled += elapsed;
208	else
209		stalled = 0;
210
211	if (stalled >= STALL_TIME)
212		xextendf(&buf, NULL, "- stalled -");
213	else if (bytes_per_second == 0 && bytes_left)
214		xextendf(&buf, NULL, "  --:-- ETA");
215	else {
216		if (bytes_left > 0)
217			seconds = bytes_left / bytes_per_second;
218		else
219			seconds = elapsed;
220
221		hours = seconds / 3600;
222		seconds -= hours * 3600;
223		minutes = seconds / 60;
224		seconds -= minutes * 60;
225
226		if (hours != 0) {
227			xextendf(&buf, NULL, "%d:%02d:%02d",
228			    hours, minutes, seconds);
229		} else
230			xextendf(&buf, NULL, "  %02d:%02d", minutes, seconds);
231
232		if (bytes_left > 0)
233			xextendf(&buf, NULL, " ETA");
234		else
235			xextendf(&buf, NULL, "    ");
236	}
237
238	/* Finally, truncate string at window width */
239	cols = win_size - 1;
240	asmprintf(&obuf, INT_MAX, &cols, " %s", buf);
241	if (obuf != NULL) {
242		*obuf = '\r'; /* must insert as asmprintf() would escape it */
243		atomicio(vwrite, STDOUT_FILENO, obuf, strlen(obuf));
244	}
245	free(buf);
246	free(obuf);
247}
248
249static void
250sig_alarm(int ignore)
251{
252	alarm_fired = 1;
253	alarm(UPDATE_INTERVAL);
254}
255
256void
257start_progress_meter(const char *f, off_t filesize, off_t *ctr)
258{
259	start = last_update = monotime_double();
260	file = f;
261	start_pos = *ctr;
262	end_pos = filesize;
263	cur_pos = 0;
264	counter = ctr;
265	stalled = 0;
266	bytes_per_second = 0;
267
268	setscreensize();
269	refresh_progress_meter(1);
270
271	ssh_signal(SIGALRM, sig_alarm);
272	ssh_signal(SIGWINCH, sig_winch);
273	alarm(UPDATE_INTERVAL);
274}
275
276void
277stop_progress_meter(void)
278{
279	alarm(0);
280
281	if (!can_output())
282		return;
283
284	/* Ensure we complete the progress */
285	if (cur_pos != end_pos)
286		refresh_progress_meter(1);
287
288	atomicio(vwrite, STDOUT_FILENO, __UNCONST("\n"), 1);
289}
290
291static void
292sig_winch(int sig)
293{
294	win_resized = 1;
295}
296
297static void
298setscreensize(void)
299{
300	struct winsize winsize;
301
302	if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &winsize) != -1 &&
303	    winsize.ws_col != 0) {
304		if (winsize.ws_col > MAX_WINSIZE)
305			win_size = MAX_WINSIZE;
306		else
307			win_size = winsize.ws_col;
308	} else
309		win_size = DEFAULT_WINSIZE;
310	win_size += 1;					/* trailing \0 */
311}
312