1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
|
From 32517af75ac8c32b3ff4870e14ff28418696c554 Mon Sep 17 00:00:00 2001
From: Sergey Poznyakoff <gray@gnu.org>
Date: Tue, 7 Sep 2021 08:02:25 +0300
Subject: Determine if st_mtim is present in struct stat
* configure.ac: Check for st_mtim and st_mtimespec in struct stat.
The former is POSIX, the latter is used instead of it on some systems
(reportedly, Darwin and NetBSD).
* src/systems.h [!HAVE_STRUCT_STAT_ST_MTIM]: Use st_mtimespec if
available.
* src/gdbmshell.c (print_snapshot): Fall back to st_mtime if nanosecond
precision is not available.
* src/gdbmsync.c (timespec_cmp): Take two pointers to struct stat as
arguments. Use the right time field, depending on the configuration
settings. All uses changed.
Forwarded: not-needed
---
configure.ac | 4 +++-
src/gdbmshell.c | 4 ++++
src/gdbmsync.c | 19 +++++++++++++------
src/systems.h | 7 +++++++
4 files changed, 27 insertions(+), 7 deletions(-)
diff --git a/configure.ac b/configure.ac
index 4e5b62d..2c22ace 100644
--- a/configure.ac
+++ b/configure.ac
@@ -108,7 +108,9 @@ then
fi
AC_TYPE_OFF_T
AC_CHECK_SIZEOF(off_t)
-AC_CHECK_MEMBERS([struct stat.st_blksize])
+AC_CHECK_MEMBERS([struct stat.st_blksize, struct stat.st_mtim, struct stat.st_mtimespec],[],[],
+ [#include <sys/types.h>
+ #include <sys/stat.h>])
AM_CONDITIONAL([COMPAT_OPT], [test "$want_compat" = yes])
diff --git a/src/gdbmshell.c b/src/gdbmshell.c
index 0961ca3..578b7cb 100644
--- a/src/gdbmshell.c
+++ b/src/gdbmshell.c
@@ -1084,7 +1084,11 @@ print_snapshot (char const *snapname, FILE *fp)
fprintf (fp, "%s: ", snapname);
fprintf (fp, "%03o %s ", st.st_mode & 0777,
decode_mode (st.st_mode, buf));
+#if HAVE_STRUCT_STAT_ST_MTIM
fprintf (fp, "%ld.%09ld", st.st_mtim.tv_sec, st.st_mtim.tv_nsec);
+#else
+ fprintf (fp, "%ld [%s]", st.st_mtime, _("insufficient precision"));
+#endif
if (S_ISREG (st.st_mode))
{
GDBM_FILE dbf;
diff --git a/src/gdbmsync.c b/src/gdbmsync.c
index d8fe369..ed20ef7 100644
--- a/src/gdbmsync.c
+++ b/src/gdbmsync.c
@@ -220,16 +220,23 @@ gdbm_failure_atomic (GDBM_FILE dbf, const char *even, const char *odd)
}
static inline int
-timespec_cmp (struct timespec const *a, struct timespec const *b)
+timespec_cmp (struct stat const *a, struct stat const *b)
{
- if (a->tv_sec < b->tv_sec)
+#if HAVE_STRUCT_STAT_ST_MTIM
+ if (a->st_mtim.tv_sec < b->st_mtim.tv_sec)
return -1;
- if (a->tv_sec > b->tv_sec)
+ if (a->st_mtim.tv_sec > b->st_mtim.tv_sec)
return 1;
- if (a->tv_nsec < b->tv_nsec)
+ if (a->st_mtim.tv_nsec < b->st_mtim.tv_nsec)
return -1;
- if (a->tv_nsec > b->tv_nsec)
+ if (a->st_mtim.tv_nsec > b->st_mtim.tv_nsec)
return 1;
+#else
+ if (a->st_mtime < b->st_mtime)
+ return -1;
+ if (a->st_mtime > b->st_mtime)
+ return 1;
+#endif
return 0;
}
@@ -374,7 +381,7 @@ gdbm_latest_snapshot (const char *even, const char *odd, const char **ret)
* Select the newer snapshot, i.e. the one whose mtime
* is greater than the other's
*/
- switch (timespec_cmp (&st_even.st_mtim, &st_odd.st_mtim))
+ switch (timespec_cmp (&st_even, &st_odd))
{
case -1:
*ret = odd;
diff --git a/src/systems.h b/src/systems.h
index 1ca9caa..d96f21f 100644
--- a/src/systems.h
+++ b/src/systems.h
@@ -52,6 +52,13 @@
# define STATBLKSIZE(st) 1024
#endif
+#if ! HAVE_STRUCT_STAT_ST_MTIM
+# if HAVE_STRUCT_STAT_ST_MTIMESPEC
+# define st_mtim st_mtimespec
+# define HAVE_STRUCT_STAT_ST_MTIM 1
+# endif
+#endif
+
#ifndef STDERR_FILENO
# define STDERR_FILENO 2
#endif
--
cgit v1.2.1
|