Hey,

Use the monotonic clock when logging progress in cdio(1) and ftp(1).

This keeps the progress log from blipping or stalling if the system
time is changed, e.g., in the midst of a transfer or rip.

ok?

--
Scott Cheloha

Index: usr.bin/cdio/mmc.c
===================================================================
RCS file: /cvs/src/usr.bin/cdio/mmc.c,v
retrieving revision 1.30
diff -u -p -r1.30 mmc.c
--- usr.bin/cdio/mmc.c  16 Jan 2015 06:40:06 -0000      1.30
+++ usr.bin/cdio/mmc.c  23 Dec 2017 02:24:07 -0000
@@ -16,6 +16,7 @@
  */
 
 #include <sys/limits.h>
+#include <sys/time.h>
 #include <sys/types.h>
 #include <sys/scsiio.h>
 #include <sys/param.h> /* setbit, isset */
@@ -27,6 +28,7 @@
 #include <fcntl.h>
 #include <stdio.h>
 #include <string.h>
+#include <time.h>
 #include <unistd.h>
 #include "extern.h"
 
@@ -433,7 +435,7 @@ writetao(struct track_head *thp)
 int
 writetrack(struct track_info *tr, int track)
 {
-       struct timeval tv, otv, atv;
+       struct timespec ts, ots, ats;
        u_char databuf[65536], nblk;
        u_int end_lba, lba, tmp;
        scsireq_t scr;
@@ -451,9 +453,9 @@ writetrack(struct track_info *tr, int tr
        scr.senselen = SENSEBUFLEN;
        scr.flags = SCCMD_ESCAPE|SCCMD_WRITE;
 
-       timerclear(&otv);
-       atv.tv_sec = 1;
-       atv.tv_usec = 0;
+       timespecclear(&ots);
+       ats.tv_sec = 1;
+       ats.tv_nsec = 0;
 
        if (get_nwa(&lba) != SCCMD_OK) {
                warnx("cannot get next writable address");
@@ -500,13 +502,13 @@ again:
                        }
                        lba += nblk;
 
-                       gettimeofday(&tv, NULL);
-                       if (lba == end_lba || timercmp(&tv, &otv, >)) {
+                       clock_gettime(CLOCK_MONOTONIC, &ts);
+                       if (lba == end_lba || timespeccmp(&ts, &ots, >)) {
                                fprintf(stderr,
                                    "\rtrack %02d '%c' %08u/%08u %3d%%",
                                    track, tr->type,
                                    lba, end_lba, 100 * lba / end_lba);
-                               timeradd(&tv, &atv, &otv);
+                               timespecadd(&ts, &ats, &ots);
                        }
                        tmp = htobe32(lba); /* update lba in cdb */
                        memcpy(&scr.cmd[2], &tmp, sizeof(tmp));
Index: usr.bin/cdio/rip.c
===================================================================
RCS file: /cvs/src/usr.bin/cdio/rip.c,v
retrieving revision 1.16
diff -u -p -r1.16 rip.c
--- usr.bin/cdio/rip.c  20 Aug 2015 22:32:41 -0000      1.16
+++ usr.bin/cdio/rip.c  23 Dec 2017 02:24:07 -0000
@@ -23,6 +23,7 @@
 #include <sys/ioctl.h>
 #include <sys/scsiio.h>
 #include <sys/stat.h>
+#include <sys/time.h>
 
 #include <scsi/scsi_all.h>
 #include <scsi/scsi_disk.h>
@@ -37,6 +38,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <time.h>
 #include <unistd.h>
 
 #include "extern.h"
@@ -362,7 +364,7 @@ read_data_sector(u_int32_t lba, u_char *
 int
 read_track(struct track *ti)
 {
-       struct timeval tv, otv, atv;
+       struct timespec ts, ots, ats;
        u_int32_t i, blksize, n_sec;
        u_char *sec;
        int error;
@@ -373,18 +375,18 @@ read_track(struct track *ti)
        if (sec == NULL)
                return (-1);
 
-       timerclear(&otv);
-       atv.tv_sec = 1;
-       atv.tv_usec = 0;
+       timespecclear(&ots);
+       ats.tv_sec = 1;
+       ats.tv_nsec = 0;
 
        for (i = 0; i < n_sec; ) {
-               gettimeofday(&tv, NULL);
-               if (timercmp(&tv, &otv, >)) {
+               clock_gettime(CLOCK_MONOTONIC, &ts);
+               if (timespeccmp(&ts, &ots, >)) {
                        fprintf(stderr, "\rtrack %u '%c' %08u/%08u %3u%%",
                            ti->track,
                            (ti->isaudio) ? 'a' : 'd', i, n_sec,
                            100 * i / n_sec);
-                       timeradd(&tv, &atv, &otv);
+                       timespecadd(&ts, &ats, &ots);
                }
 
                error = read_data_sector(i + ti->start_lba, sec, blksize);
Index: usr.bin/ftp/util.c
===================================================================
RCS file: /cvs/src/usr.bin/ftp/util.c,v
retrieving revision 1.85
diff -u -p -r1.85 util.c
--- usr.bin/ftp/util.c  5 Sep 2017 05:37:35 -0000       1.85
+++ usr.bin/ftp/util.c  23 Dec 2017 02:24:07 -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;
@@ -851,19 +851,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;
@@ -921,7 +921,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;
@@ -930,9 +930,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))

Reply via email to