Hi,
Same deal here as in dd(1). We're displaying an elapsed time
so we want a monotonic clock.
Because everything printed is derived from elapsed, this one
was relatively simple.
Light testing shows this fixes the corner case where the host
time is wound backward mid-transfer.
Feedback?
--
Scott Cheloha
Index: usr.bin/ftp/util.c
===================================================================
RCS file: /cvs/src/usr.bin/ftp/util.c,v
retrieving revision 1.84
diff -u -p -r1.84 util.c
--- usr.bin/ftp/util.c 21 Jan 2017 08:33:07 -0000 1.84
+++ usr.bin/ftp/util.c 19 Aug 2017 14:24:44 -0000
@@ -744,7 +744,7 @@ updateprogressmeter(int signo)
* with flag = 0
* - After the transfer, call with flag = 1
*/
-static struct timeval start;
+static struct timespec start;
char *action;
@@ -757,21 +757,21 @@ progressmeter(int flag, const char *file
*/
static const char prefixes[] = " KMGTP";
- static struct timeval lastupdate;
+ static struct timespec lastupdate;
static off_t lastsize;
static char *title = NULL;
- struct timeval now, td, wait;
+ struct timespec now, td, wait;
off_t cursize, abbrevsize;
double elapsed;
int ratio, barlength, i, remaining, overhead = 30;
char buf[512];
if (flag == -1) {
- (void)gettimeofday(&start, NULL);
+ clock_gettime(CLOCK_MONOTONIC, &start);
lastupdate = start;
lastsize = restart_point;
}
- (void)gettimeofday(&now, NULL);
+ clock_gettime(CLOCK_MONOTONIC, &now);
if (!progress || filesize < 0)
return;
cursize = bytes + restart_point;
@@ -849,19 +849,19 @@ progressmeter(int flag, const char *file
" %5lld %c%c ", (long long)abbrevsize, prefixes[i],
prefixes[i] == ' ' ? ' ' : 'B');
- timersub(&now, &lastupdate, &wait);
+ timespecsub(&now, &lastupdate, &wait);
if (cursize > lastsize) {
lastupdate = now;
lastsize = cursize;
if (wait.tv_sec >= STALLTIME) { /* fudge out stalled time */
start.tv_sec += wait.tv_sec;
- start.tv_usec += wait.tv_usec;
+ start.tv_nsec += wait.tv_nsec;
}
wait.tv_sec = 0;
}
- timersub(&now, &start, &td);
- elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
+ timespecsub(&now, &start, &td);
+ elapsed = td.tv_sec + (td.tv_nsec / 1000000000.0);
if (flag == 1) {
i = (int)elapsed / 3600;
@@ -919,7 +919,7 @@ progressmeter(int flag, const char *file
void
ptransfer(int siginfo)
{
- struct timeval now, td;
+ struct timespec now, td;
double elapsed;
off_t bs;
int meg, remaining, hh;
@@ -928,9 +928,9 @@ ptransfer(int siginfo)
if (!verbose && !siginfo)
return;
- (void)gettimeofday(&now, NULL);
- timersub(&now, &start, &td);
- elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
+ clock_gettime(CLOCK_MONOTONIC, &now);
+ timespecsub(&now, &start, &td);
+ elapsed = td.tv_sec + (td.tv_nsec / 1000000000.0);
bs = bytes / (elapsed == 0.0 ? 1 : elapsed);
meg = 0;
if (bs > (1024 * 1024))