2023-04-12 16:22:04 +02:00
|
|
|
/* $OpenBSD: progressmeter.c,v 1.53 2023/04/12 14:22:04 jsg Exp $ */
|
2003-01-10 11:46:02 +01:00
|
|
|
/*
|
2003-08-02 15:28:38 +02:00
|
|
|
* Copyright (c) 2003 Nils Nordman. All rights reserved.
|
2003-01-10 11:46:02 +01:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "includes.h"
|
2006-03-15 01:28:34 +01:00
|
|
|
|
2006-07-24 06:01:23 +02:00
|
|
|
#include <sys/types.h>
|
2006-03-15 01:28:34 +01:00
|
|
|
#include <sys/ioctl.h>
|
2006-08-05 04:39:39 +02:00
|
|
|
#include <sys/uio.h>
|
2003-01-10 11:46:02 +01:00
|
|
|
|
2006-07-12 14:22:46 +02:00
|
|
|
#include <errno.h>
|
2023-02-22 04:56:43 +01:00
|
|
|
#include <limits.h>
|
|
|
|
#include <signal.h>
|
2019-01-23 09:01:46 +01:00
|
|
|
#include <stdarg.h>
|
2023-02-22 04:56:43 +01:00
|
|
|
#include <stdlib.h>
|
2006-08-05 03:37:59 +02:00
|
|
|
#include <stdio.h>
|
2006-07-24 06:13:33 +02:00
|
|
|
#include <string.h>
|
2006-07-24 06:09:40 +02:00
|
|
|
#include <time.h>
|
2006-07-24 06:01:23 +02:00
|
|
|
#include <unistd.h>
|
2006-03-15 01:52:09 +01:00
|
|
|
|
2003-01-14 12:23:23 +01:00
|
|
|
#include "progressmeter.h"
|
2003-08-02 15:28:38 +02:00
|
|
|
#include "atomicio.h"
|
2003-06-04 14:56:15 +02:00
|
|
|
#include "misc.h"
|
2019-01-23 09:01:46 +01:00
|
|
|
#include "utf8.h"
|
2003-01-10 11:46:02 +01:00
|
|
|
|
2003-08-02 15:28:38 +02:00
|
|
|
#define DEFAULT_WINSIZE 80
|
|
|
|
#define MAX_WINSIZE 512
|
|
|
|
#define PADDING 1 /* padding between the progress indicators */
|
|
|
|
#define UPDATE_INTERVAL 1 /* update the progress meter every second */
|
|
|
|
#define STALL_TIME 5 /* we're stalled after this many seconds */
|
2003-01-10 11:46:02 +01:00
|
|
|
|
2003-08-02 15:28:38 +02:00
|
|
|
/* determines whether we can output to the terminal */
|
|
|
|
static int can_output(void);
|
2003-01-10 11:46:02 +01:00
|
|
|
|
2005-06-16 05:18:04 +02:00
|
|
|
/* window resizing */
|
|
|
|
static void sig_winch(int);
|
|
|
|
static void setscreensize(void);
|
|
|
|
|
2003-08-02 15:28:38 +02:00
|
|
|
/* signal handler for updating the progress meter */
|
2019-01-23 09:01:46 +01:00
|
|
|
static void sig_alarm(int);
|
2003-01-10 11:46:02 +01:00
|
|
|
|
2016-03-02 23:42:40 +01:00
|
|
|
static double start; /* start progress */
|
|
|
|
static double last_update; /* last progress update */
|
2015-01-14 14:54:13 +01:00
|
|
|
static const char *file; /* name of the file being transferred */
|
2013-10-10 01:25:09 +02:00
|
|
|
static off_t start_pos; /* initial position of transfer */
|
2004-07-17 08:12:08 +02:00
|
|
|
static off_t end_pos; /* ending position of transfer */
|
|
|
|
static off_t cur_pos; /* transfer position as of last refresh */
|
2003-08-02 15:28:38 +02:00
|
|
|
static volatile off_t *counter; /* progress counter */
|
2004-07-17 08:12:08 +02:00
|
|
|
static long stalled; /* how long we have been stalled */
|
|
|
|
static int bytes_per_second; /* current speed in bytes per second */
|
|
|
|
static int win_size; /* terminal window size */
|
2005-06-16 05:18:04 +02:00
|
|
|
static volatile sig_atomic_t win_resized; /* for window resizing */
|
2019-01-23 09:01:46 +01:00
|
|
|
static volatile sig_atomic_t alarm_fired;
|
2003-01-10 11:46:02 +01:00
|
|
|
|
2003-08-02 15:28:38 +02:00
|
|
|
/* units for format_size */
|
|
|
|
static const char unit[] = " KMGT";
|
2003-01-10 11:46:02 +01:00
|
|
|
|
2003-08-02 15:28:38 +02:00
|
|
|
static int
|
|
|
|
can_output(void)
|
|
|
|
{
|
|
|
|
return (getpgrp() == tcgetpgrp(STDOUT_FILENO));
|
|
|
|
}
|
2003-01-10 11:46:02 +01:00
|
|
|
|
2023-02-22 04:56:43 +01:00
|
|
|
/* size needed to format integer type v, using (nbits(v) * log2(10) / 10) */
|
|
|
|
#define STRING_SIZE(v) (((sizeof(v) * 8 * 4) / 10) + 1)
|
|
|
|
|
|
|
|
static const char *
|
|
|
|
format_rate(off_t bytes)
|
2003-08-02 15:28:38 +02:00
|
|
|
{
|
|
|
|
int i;
|
2023-02-22 04:56:43 +01:00
|
|
|
static char buf[STRING_SIZE(bytes) * 2 + 16];
|
2003-08-02 15:28:38 +02:00
|
|
|
|
|
|
|
bytes *= 100;
|
|
|
|
for (i = 0; bytes >= 100*1000 && unit[i] != 'T'; i++)
|
|
|
|
bytes = (bytes + 512) / 1024;
|
|
|
|
if (i == 0) {
|
|
|
|
i++;
|
|
|
|
bytes = (bytes + 512) / 1024;
|
|
|
|
}
|
2023-02-22 04:56:43 +01:00
|
|
|
snprintf(buf, sizeof(buf), "%3lld.%1lld%c%s",
|
2005-11-25 04:44:55 +01:00
|
|
|
(long long) (bytes + 5) / 100,
|
|
|
|
(long long) (bytes + 5) / 10 % 10,
|
2003-08-02 15:28:38 +02:00
|
|
|
unit[i],
|
|
|
|
i ? "B" : " ");
|
2023-02-22 04:56:43 +01:00
|
|
|
return buf;
|
2003-08-02 15:28:38 +02:00
|
|
|
}
|
2003-01-10 11:46:02 +01:00
|
|
|
|
2023-02-22 04:56:43 +01:00
|
|
|
static const char *
|
|
|
|
format_size(off_t bytes)
|
2003-08-02 15:28:38 +02:00
|
|
|
{
|
|
|
|
int i;
|
2023-02-22 04:56:43 +01:00
|
|
|
static char buf[STRING_SIZE(bytes) + 16];
|
2003-08-02 15:28:38 +02:00
|
|
|
|
|
|
|
for (i = 0; bytes >= 10000 && unit[i] != 'T'; i++)
|
|
|
|
bytes = (bytes + 512) / 1024;
|
2023-02-22 04:56:43 +01:00
|
|
|
snprintf(buf, sizeof(buf), "%4lld%c%s",
|
2005-11-25 04:44:55 +01:00
|
|
|
(long long) bytes,
|
2003-08-02 15:28:38 +02:00
|
|
|
unit[i],
|
|
|
|
i ? "B" : " ");
|
2023-02-22 04:56:43 +01:00
|
|
|
return buf;
|
2003-08-02 15:28:38 +02:00
|
|
|
}
|
2003-01-10 11:46:02 +01:00
|
|
|
|
|
|
|
void
|
2019-01-24 17:52:17 +01:00
|
|
|
refresh_progress_meter(int force_update)
|
2003-01-10 11:46:02 +01:00
|
|
|
{
|
2023-02-22 04:56:43 +01:00
|
|
|
char *buf = NULL, *obuf = NULL;
|
2003-08-02 15:28:38 +02:00
|
|
|
off_t transferred;
|
2016-03-02 23:42:40 +01:00
|
|
|
double elapsed, now;
|
2003-08-02 15:28:38 +02:00
|
|
|
int percent;
|
2004-02-06 06:41:37 +01:00
|
|
|
off_t bytes_left;
|
2003-08-02 15:28:38 +02:00
|
|
|
int cur_speed;
|
|
|
|
int hours, minutes, seconds;
|
2023-02-22 04:56:43 +01:00
|
|
|
int file_len, cols;
|
2003-08-02 15:28:38 +02:00
|
|
|
|
2019-01-24 17:52:17 +01:00
|
|
|
if ((!force_update && !alarm_fired && !win_resized) || !can_output())
|
2019-01-23 09:01:46 +01:00
|
|
|
return;
|
|
|
|
alarm_fired = 0;
|
|
|
|
|
|
|
|
if (win_resized) {
|
|
|
|
setscreensize();
|
|
|
|
win_resized = 0;
|
|
|
|
}
|
|
|
|
|
2013-10-10 01:25:09 +02:00
|
|
|
transferred = *counter - (cur_pos ? cur_pos : start_pos);
|
2003-08-02 15:28:38 +02:00
|
|
|
cur_pos = *counter;
|
2016-03-02 23:42:40 +01:00
|
|
|
now = monotime_double();
|
2003-08-02 15:28:38 +02:00
|
|
|
bytes_left = end_pos - cur_pos;
|
|
|
|
|
|
|
|
if (bytes_left > 0)
|
|
|
|
elapsed = now - last_update;
|
2003-12-09 09:07:13 +01:00
|
|
|
else {
|
2003-08-02 15:28:38 +02:00
|
|
|
elapsed = now - start;
|
2003-12-09 09:07:13 +01:00
|
|
|
/* Calculate true total speed when done */
|
2013-10-10 01:25:09 +02:00
|
|
|
transferred = end_pos - start_pos;
|
2003-12-09 09:07:13 +01:00
|
|
|
bytes_per_second = 0;
|
|
|
|
}
|
2003-01-10 11:46:02 +01:00
|
|
|
|
2003-08-02 15:28:38 +02:00
|
|
|
/* calculate speed */
|
|
|
|
if (elapsed != 0)
|
|
|
|
cur_speed = (transferred / elapsed);
|
|
|
|
else
|
2003-12-09 09:07:13 +01:00
|
|
|
cur_speed = transferred;
|
2003-01-10 11:46:02 +01:00
|
|
|
|
2003-08-02 15:28:38 +02:00
|
|
|
#define AGE_FACTOR 0.9
|
|
|
|
if (bytes_per_second != 0) {
|
|
|
|
bytes_per_second = (bytes_per_second * AGE_FACTOR) +
|
|
|
|
(cur_speed * (1.0 - AGE_FACTOR));
|
|
|
|
} else
|
|
|
|
bytes_per_second = cur_speed;
|
|
|
|
|
2023-02-22 04:56:43 +01:00
|
|
|
last_update = now;
|
|
|
|
|
|
|
|
/* Don't bother if we can't even display the completion percentage */
|
|
|
|
if (win_size < 4)
|
|
|
|
return;
|
|
|
|
|
2003-08-02 15:28:38 +02:00
|
|
|
/* filename */
|
2023-02-22 04:56:43 +01:00
|
|
|
file_len = cols = win_size - 36;
|
2003-08-02 15:28:38 +02:00
|
|
|
if (file_len > 0) {
|
2023-02-22 04:56:43 +01:00
|
|
|
asmprintf(&buf, INT_MAX, &cols, "%-*s", file_len, file);
|
|
|
|
/* If we used fewer columns than expected then pad */
|
|
|
|
if (cols < file_len)
|
|
|
|
xextendf(&buf, NULL, "%*s", file_len - cols, "");
|
2003-08-02 15:28:38 +02:00
|
|
|
}
|
|
|
|
/* percent of transfer done */
|
2016-06-30 07:17:05 +02:00
|
|
|
if (end_pos == 0 || cur_pos == end_pos)
|
2003-08-02 15:28:38 +02:00
|
|
|
percent = 100;
|
2016-06-30 07:17:05 +02:00
|
|
|
else
|
|
|
|
percent = ((float)cur_pos / end_pos) * 100;
|
2003-08-02 15:28:38 +02:00
|
|
|
|
2023-02-22 04:56:43 +01:00
|
|
|
/* percent / amount transferred / bandwidth usage */
|
|
|
|
xextendf(&buf, NULL, " %3d%% %s %s/s ", percent, format_size(cur_pos),
|
|
|
|
format_rate((off_t)bytes_per_second));
|
2003-08-02 15:28:38 +02:00
|
|
|
|
|
|
|
/* ETA */
|
|
|
|
if (!transferred)
|
|
|
|
stalled += elapsed;
|
|
|
|
else
|
|
|
|
stalled = 0;
|
|
|
|
|
|
|
|
if (stalled >= STALL_TIME)
|
2023-02-22 04:56:43 +01:00
|
|
|
xextendf(&buf, NULL, "- stalled -");
|
2003-08-02 15:28:38 +02:00
|
|
|
else if (bytes_per_second == 0 && bytes_left)
|
2023-02-22 04:56:43 +01:00
|
|
|
xextendf(&buf, NULL, " --:-- ETA");
|
2003-08-02 15:28:38 +02:00
|
|
|
else {
|
|
|
|
if (bytes_left > 0)
|
|
|
|
seconds = bytes_left / bytes_per_second;
|
|
|
|
else
|
|
|
|
seconds = elapsed;
|
|
|
|
|
|
|
|
hours = seconds / 3600;
|
|
|
|
seconds -= hours * 3600;
|
|
|
|
minutes = seconds / 60;
|
|
|
|
seconds -= minutes * 60;
|
|
|
|
|
2023-02-22 04:56:43 +01:00
|
|
|
if (hours != 0) {
|
|
|
|
xextendf(&buf, NULL, "%d:%02d:%02d",
|
|
|
|
hours, minutes, seconds);
|
|
|
|
} else
|
|
|
|
xextendf(&buf, NULL, " %02d:%02d", minutes, seconds);
|
2003-08-02 15:28:38 +02:00
|
|
|
|
|
|
|
if (bytes_left > 0)
|
2023-02-22 04:56:43 +01:00
|
|
|
xextendf(&buf, NULL, " ETA");
|
2003-08-02 15:28:38 +02:00
|
|
|
else
|
2023-02-22 04:56:43 +01:00
|
|
|
xextendf(&buf, NULL, " ");
|
2003-08-02 15:28:38 +02:00
|
|
|
}
|
|
|
|
|
2023-02-22 04:56:43 +01:00
|
|
|
/* Finally, truncate string at window width */
|
|
|
|
cols = win_size - 1;
|
|
|
|
asmprintf(&obuf, INT_MAX, &cols, " %s", buf);
|
|
|
|
if (obuf != NULL) {
|
|
|
|
*obuf = '\r'; /* must insert as asmprintf() would escape it */
|
|
|
|
atomicio(vwrite, STDOUT_FILENO, obuf, strlen(obuf));
|
|
|
|
}
|
|
|
|
free(buf);
|
|
|
|
free(obuf);
|
2003-01-10 11:46:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2019-01-23 09:01:46 +01:00
|
|
|
sig_alarm(int ignore)
|
2003-01-10 11:46:02 +01:00
|
|
|
{
|
2019-01-23 09:01:46 +01:00
|
|
|
alarm_fired = 1;
|
2003-08-02 15:28:38 +02:00
|
|
|
alarm(UPDATE_INTERVAL);
|
2003-01-10 11:46:02 +01:00
|
|
|
}
|
|
|
|
|
2003-08-02 15:28:38 +02:00
|
|
|
void
|
2015-01-14 14:54:13 +01:00
|
|
|
start_progress_meter(const char *f, off_t filesize, off_t *ctr)
|
2003-01-10 11:46:02 +01:00
|
|
|
{
|
2016-03-02 23:42:40 +01:00
|
|
|
start = last_update = monotime_double();
|
2003-08-02 15:28:38 +02:00
|
|
|
file = f;
|
2013-10-10 01:25:09 +02:00
|
|
|
start_pos = *ctr;
|
2003-08-02 15:28:38 +02:00
|
|
|
end_pos = filesize;
|
|
|
|
cur_pos = 0;
|
2004-06-22 04:56:01 +02:00
|
|
|
counter = ctr;
|
2003-08-02 15:28:38 +02:00
|
|
|
stalled = 0;
|
|
|
|
bytes_per_second = 0;
|
|
|
|
|
2005-06-16 05:18:04 +02:00
|
|
|
setscreensize();
|
2019-01-24 17:52:17 +01:00
|
|
|
refresh_progress_meter(1);
|
2003-01-10 11:46:02 +01:00
|
|
|
|
2020-01-23 08:10:22 +01:00
|
|
|
ssh_signal(SIGALRM, sig_alarm);
|
|
|
|
ssh_signal(SIGWINCH, sig_winch);
|
2003-08-02 15:28:38 +02:00
|
|
|
alarm(UPDATE_INTERVAL);
|
2003-01-10 11:46:02 +01:00
|
|
|
}
|
|
|
|
|
2003-08-02 15:28:38 +02:00
|
|
|
void
|
|
|
|
stop_progress_meter(void)
|
2003-01-10 11:46:02 +01:00
|
|
|
{
|
2003-08-02 15:28:38 +02:00
|
|
|
alarm(0);
|
2003-01-10 11:46:02 +01:00
|
|
|
|
2003-08-02 15:28:38 +02:00
|
|
|
if (!can_output())
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Ensure we complete the progress */
|
|
|
|
if (cur_pos != end_pos)
|
2019-01-24 17:52:17 +01:00
|
|
|
refresh_progress_meter(1);
|
2003-08-02 15:28:38 +02:00
|
|
|
|
|
|
|
atomicio(vwrite, STDOUT_FILENO, "\n", 1);
|
2003-01-10 11:46:02 +01:00
|
|
|
}
|
2005-06-16 05:18:04 +02:00
|
|
|
|
|
|
|
static void
|
|
|
|
sig_winch(int sig)
|
|
|
|
{
|
|
|
|
win_resized = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
setscreensize(void)
|
|
|
|
{
|
|
|
|
struct winsize winsize;
|
|
|
|
|
|
|
|
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &winsize) != -1 &&
|
|
|
|
winsize.ws_col != 0) {
|
|
|
|
if (winsize.ws_col > MAX_WINSIZE)
|
|
|
|
win_size = MAX_WINSIZE;
|
|
|
|
else
|
|
|
|
win_size = winsize.ws_col;
|
|
|
|
} else
|
|
|
|
win_size = DEFAULT_WINSIZE;
|
|
|
|
win_size += 1; /* trailing \0 */
|
|
|
|
}
|