diff options
author | Sergey Poznyakoff <gray@gnu.org> | 2021-09-07 08:02:25 +0300 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org> | 2021-09-07 08:08:29 +0300 |
commit | 32517af75ac8c32b3ff4870e14ff28418696c554 (patch) | |
tree | afa94e0713b8dc70c9e17036e07be02caaff8ddd /src | |
parent | 4d5be2bb19bd299a5bd3525c4e28f571929d70de (diff) | |
download | gdbm-32517af75ac8c32b3ff4870e14ff28418696c554.tar.gz gdbm-32517af75ac8c32b3ff4870e14ff28418696c554.tar.bz2 |
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.
Diffstat (limited to 'src')
-rw-r--r-- | src/gdbmshell.c | 4 | ||||
-rw-r--r-- | src/gdbmsync.c | 19 | ||||
-rw-r--r-- | src/systems.h | 7 |
3 files changed, 24 insertions, 6 deletions
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 |