]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
shred: use gethrxtime instead of time
authorCollin Funk <collin.funk1@gmail.com>
Sun, 7 Dec 2025 01:19:55 +0000 (17:19 -0800)
committerCollin Funk <collin.funk1@gmail.com>
Sun, 7 Dec 2025 19:37:17 +0000 (11:37 -0800)
The gethrxtime function uses a monotonic clock if available on the
system. The effect should be unnoticeable since we print --verbose
information every 5 seconds, but is more correct and we already do the
same in 'dd'.

* src/local.mk (src_shred_LDADD): Add $(GETHRXTIME_LIB).
* src/shred.c (dopass): Use xtime_t instead of time_t. Use gethrxtime
instead of time.

src/local.mk
src/shred.c

index 49bb9820715282ad1980f11d4aa6cf5214f11e44..88b9f0e6c24cec818a756940946ee318be5f0c79 100644 (file)
@@ -273,6 +273,7 @@ src_touch_LDADD += $(CLOCK_TIME_LIB)
 
 # for gethrxtime
 src_dd_LDADD += $(GETHRXTIME_LIB)
+src_shred_LDADD += $(GETHRXTIME_LIB)
 
 # for cap_get_file
 src_ls_LDADD += $(LIB_CAP)
index be2fa9f2be2f6e2bd28cc3078b760f91d9c92fbc..2a91b3b1e3e480424c5aa8814144fbaf9c841613 100644 (file)
@@ -89,6 +89,7 @@
 #include "assure.h"
 #include "xdectoint.h"
 #include "fcntl--.h"
+#include "gethrxtime.h"
 #include "human.h"
 #include "randint.h"
 #include "randread.h"
@@ -401,12 +402,12 @@ dopass (int fd, struct stat const *st, char const *qname, off_t *sizep,
         unsigned long int k, unsigned long int n)
 {
   off_t size = *sizep;
-  off_t offset;                        /* Current file position */
-  time_t thresh IF_LINT ( = 0);        /* Time to maybe print next status update */
-  time_t now = 0;              /* Current time */
-  size_t lim;                  /* Amount of data to try writing */
-  size_t soff;                 /* Offset into buffer for next write */
-  ssize_t ssize;               /* Return value from write */
+  off_t offset;                /* Current file position */
+  xtime_t prev IF_LINT ( = 0); /* Time we printed the previous update.  */
+  xtime_t now = 0;             /* Current time */
+  size_t lim;                  /* Amount of data to try writing */
+  size_t soff;                 /* Offset into buffer for next write */
+  ssize_t ssize;               /* Return value from write */
 
   /* Fill pattern buffer.  Aligning it to a page so we can do direct I/O.  */
   size_t page_size = getpagesize ();
@@ -456,7 +457,7 @@ dopass (int fd, struct stat const *st, char const *qname, off_t *sizep,
   if (n)
     {
       error (0, 0, _("%s: pass %lu/%lu (%s)..."), qname, k, n, pass_string);
-      thresh = time (nullptr) + VERBOSE_UPDATE;
+      prev = gethrxtime ();
       previous_human_offset = "";
     }
 
@@ -546,7 +547,7 @@ dopass (int fd, struct stat const *st, char const *qname, off_t *sizep,
 
       /* Time to print progress? */
       if (n && ((done && *previous_human_offset)
-                || thresh <= (now = time (nullptr))))
+                || VERBOSE_UPDATE <= xtime_sec ((now = gethrxtime ()) - prev)))
         {
           char offset_buf[LONGEST_HUMAN_READABLE + 1];
           char size_buf[LONGEST_HUMAN_READABLE + 1];
@@ -582,7 +583,7 @@ dopass (int fd, struct stat const *st, char const *qname, off_t *sizep,
 
               strcpy (previous_offset_buf, human_offset);
               previous_human_offset = previous_offset_buf;
-              thresh = now + VERBOSE_UPDATE;
+              prev = now;
 
               /*
                * Force periodic syncs to keep displayed progress accurate