richardphibel / rpms / librepo

Forked from rpms/librepo 2 years ago
Clone
4e242b
From e6f48ae9bff7b5dc8027d043aa1bffa53d507a42 Mon Sep 17 00:00:00 2001
4e242b
From: =?UTF-8?q?Ale=C5=A1=20Mat=C4=9Bj?= <amatej@redhat.com>
4e242b
Date: Thu, 5 May 2022 12:44:27 +0200
4e242b
Subject: [PATCH] Use nanosec precision for timestamp of checksum cache
4e242b
 (RhBug:2077864)
4e242b
4e242b
= changelog =
4e242b
msg: Use nanosec precision for timestamp of checksum cache
4e242b
type: bugfix
4e242b
resolves: https://bugzilla.redhat.com/show_bug.cgi?id=2077864
4e242b
---
4e242b
 librepo/checksum.c    | 7 +++++--
4e242b
 tests/test_checksum.c | 6 +++++-
4e242b
 2 files changed, 10 insertions(+), 3 deletions(-)
4e242b
4e242b
diff --git a/librepo/checksum.c b/librepo/checksum.c
4e242b
index 6bba53c..d82cb5c 100644
4e242b
--- a/librepo/checksum.c
4e242b
+++ b/librepo/checksum.c
4e242b
@@ -18,6 +18,7 @@
4e242b
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
4e242b
  */
4e242b
 
4e242b
+#define _POSIX_C_SOURCE 200809L
4e242b
 #include <glib.h>
4e242b
 #include <glib/gprintf.h>
4e242b
 #include <assert.h>
4e242b
@@ -217,16 +218,18 @@ lr_checksum_fd_compare(LrChecksumType type,
4e242b
         return FALSE;
4e242b
     }
4e242b
 
4e242b
-    time_t timestamp = -1;
4e242b
+    long long timestamp = -1;
4e242b
 
4e242b
     if (caching) {
4e242b
         struct stat st;
4e242b
         if (fstat(fd, &st) == 0) {
4e242b
             timestamp = st.st_mtime;
4e242b
+            timestamp *= 1000000000; //convert sec timestamp to nanosec timestamp
4e242b
+            timestamp += st.st_mtim.tv_nsec;
4e242b
         }
4e242b
     }
4e242b
 
4e242b
-    _cleanup_free_ gchar *timestamp_str = g_strdup_printf("%lli", (long long)timestamp);
4e242b
+    _cleanup_free_ gchar *timestamp_str = g_strdup_printf("%lli", timestamp);
4e242b
     const char *type_str = lr_checksum_type_to_str(type);
4e242b
     _cleanup_free_ gchar *timestamp_key = g_strconcat(XATTR_CHKSUM_PREFIX, "mtime", NULL);
4e242b
     _cleanup_free_ gchar *checksum_key = g_strconcat(XATTR_CHKSUM_PREFIX, type_str, NULL);
4e242b
diff --git a/tests/test_checksum.c b/tests/test_checksum.c
4e242b
index cd28cd1..548f588 100644
4e242b
--- a/tests/test_checksum.c
4e242b
+++ b/tests/test_checksum.c
4e242b
@@ -1,3 +1,4 @@
4e242b
+#define _POSIX_C_SOURCE 200809L
4e242b
 #define _GNU_SOURCE
4e242b
 #include <errno.h>
4e242b
 #include <stdlib.h>
4e242b
@@ -150,7 +151,10 @@ START_TEST(test_cached_checksum_matches)
4e242b
     // stored timestamp matches the file mtime
4e242b
     ret = stat(filename, &st);
4e242b
     ck_assert_int_eq(ret, 0);
4e242b
-    mtime_str = g_strdup_printf("%lli", (long long) st.st_mtime);
4e242b
+    long long timestamp = st.st_mtime;
4e242b
+    timestamp *= 1000000000; //convert sec timestamp to nanosec timestamp
4e242b
+    timestamp += st.st_mtim.tv_nsec;
4e242b
+    mtime_str = g_strdup_printf("%lli", timestamp);
4e242b
     attr_ret = GETXATTR(filename, timestamp_key, &buf, sizeof(buf)-1);
4e242b
     ck_assert(attr_ret != -1);
4e242b
     buf[attr_ret] = 0;
4e242b
-- 
4e242b
2.36.1
4e242b