richardphibel / rpms / librepo

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