From c3eb8b45798133dba06defd72f6280fd1ce3fcd3 Mon Sep 17 00:00:00 2001 From: Collin Funk Date: Sat, 6 Dec 2025 17:19:55 -0800 Subject: [PATCH] shred: use gethrxtime instead of time 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 | 1 + src/shred.c | 19 ++++++++++--------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/local.mk b/src/local.mk index 49bb982071..88b9f0e6c2 100644 --- a/src/local.mk +++ b/src/local.mk @@ -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) diff --git a/src/shred.c b/src/shred.c index be2fa9f2be..2a91b3b1e3 100644 --- a/src/shred.c +++ b/src/shred.c @@ -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 -- 2.47.3