]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
hardlink: use monotonic time like other utils
authorKarel Zak <kzak@redhat.com>
Thu, 4 Feb 2021 12:43:05 +0000 (13:43 +0100)
committerKarel Zak <kzak@redhat.com>
Wed, 17 Feb 2021 10:50:21 +0000 (11:50 +0100)
- use out gettime_monotonic()
- use timeval for calculation rather than double

Signed-off-by: Karel Zak <kzak@redhat.com>
misc-utils/Makemodule.am
misc-utils/hardlink.c

index 2c0405884e926d18e34b7396156c098dbae25eb2..be83bc7d56864f46ee16657acb53831c55127718 100644 (file)
@@ -222,8 +222,8 @@ endif
 
 if BUILD_HARDLINK
 usrbin_exec_PROGRAMS += hardlink
-hardlink_SOURCES = misc-utils/hardlink.c
-hardlink_LDADD = $(LDADD) libcommon.la
+hardlink_SOURCES = misc-utils/hardlink.c lib/monotonic.c
+hardlink_LDADD = $(LDADD) libcommon.la $(REALTIME_LIBS)
 hardlink_CFLAGS = $(AM_CFLAGS)
 if HAVE_PCRE2_POSIX
 hardlink_LDADD += $(PCRE2_POSIX_LIBS)
index b9e38aa25fc2f58604bc7c37c5cd2a6694070aca..624bf0547afba205a524672eb9a3d6fffdf2652c 100644 (file)
@@ -41,6 +41,7 @@
 #include "c.h"
 #include "xalloc.h"
 #include "strutils.h"
+#include "monotonic.h"
 
 /* Use libpcre2posix if it's available */
 #ifdef HAVE_PCRE2_POSIX
@@ -110,7 +111,7 @@ enum log_level {
  * @xattr_comparisons: The number of extended attribute comparisons
  * @comparisons: The number of comparisons
  * @saved: The (exaggerated) amount of space saved
- * @start_time: The time we started at, in seconds since some unspecified point
+ * @start_time: The time we started at
  */
 static struct statistics {
     int started;
@@ -119,7 +120,7 @@ static struct statistics {
     size_t xattr_comparisons;
     size_t comparisons;
     double saved;
-    double start_time;
+    struct timeval start_time;
 } stats;
 
 /**
@@ -240,19 +241,6 @@ static const char *format(double bytes)
     return buf;
 }
 
-/**
- * gettime() - Get the current time from the system
- */
-static double gettime(void)
-{
-    struct timeval tv = { 0, 0 };
-
-    if (gettimeofday(&tv, NULL) != 0)
-        jlog(JLOG_SYSERR, "Cannot read current time");
-
-    return (double) tv.tv_sec + (double) tv.tv_usec / 1000000;
-}
-
 /**
  * regexec_any - Match against multiple regular expressions
  * @pregs: A linked list of regular expressions
@@ -323,6 +311,11 @@ static int compare_nodes_ino(const void *_a, const void *_b)
  */
 static void print_stats(void)
 {
+    struct timeval end = { 0, 0 }, delta = { 0, 0 };
+
+    gettime_monotonic(&end);
+    timersub(&end, &stats.start_time, &delta);
+
     jlog(JLOG_SUMMARY, "Mode:     %s", opts.dry_run ? "dry-run" : "real");
     jlog(JLOG_SUMMARY, "Files:    %zu", stats.files);
     jlog(JLOG_SUMMARY, "Linked:   %zu files", stats.linked);
@@ -331,7 +324,8 @@ static void print_stats(void)
 #endif
     jlog(JLOG_SUMMARY, "Compared: %zu files", stats.comparisons);
     jlog(JLOG_SUMMARY, "Saved:    %s", format(stats.saved));
-    jlog(JLOG_SUMMARY, "Duration: %.2f seconds", gettime() - stats.start_time);
+    jlog(JLOG_SUMMARY, "Duration: %ld.%06ld seconds",
+                   (long)delta.tv_sec, (long)delta.tv_usec);
 }
 
 /**
@@ -1062,7 +1056,6 @@ int main(int argc, char *argv[])
 
     /* Pretty print numeric output */
     setlocale(LC_NUMERIC, "");
-    stats.start_time = gettime();
 
     if (atexit(to_be_called_atexit) != 0)
         err(EXIT_FAILURE, _("cannot register exit handler"));
@@ -1072,6 +1065,7 @@ int main(int argc, char *argv[])
     if (optind == argc)
        errx(EXIT_FAILURE, _("no directory of dile specified"));
 
+    gettime_monotonic(&stats.start_time);
     stats.started = TRUE;
 
     for (; optind < argc; optind++)