From a3aa7003ea76c3423b5da9c4873fb8d7d2f75056 Mon Sep 17 00:00:00 2001 From: Sergey Poznyakoff Date: Mon, 21 Mar 2016 13:35:32 +0200 Subject: [PATCH] Fix ckmtime * gnulib.modules: Use timespec-sub * tests/ckmtime.c: Use second resolution. --- gnulib.modules | 1 + tests/ckmtime.c | 43 ++++++++++++++++++++++++++++++++----------- 2 files changed, 33 insertions(+), 11 deletions(-) diff --git a/gnulib.modules b/gnulib.modules index fe5ab737..9a393f99 100644 --- a/gnulib.modules +++ b/gnulib.modules @@ -93,6 +93,7 @@ strtoul strtoumax symlinkat timespec +timespec-sub unlinkat unlinkdir unlocked-io diff --git a/tests/ckmtime.c b/tests/ckmtime.c index 75b9e6c3..9fde42db 100644 --- a/tests/ckmtime.c +++ b/tests/ckmtime.c @@ -18,28 +18,49 @@ #include #include #include +#include #include #include +#include #include #include +#define TEMPLATE "ckmtime.XXXXXX" +#define BILLION 1000000000 + +/* Some filesystems can slightly offset the timestamps of newly created files. + To compensate for it, tar testsuite waits at least 1 second before creating + next level of incremental backups. + + However, NFS mounts can offset the timestamps by bigger amounts. + + This program returns with success (0) if a newly created file is assigned + mtime matching the system time to the nearest second. +*/ int main (int argc, char **argv) { - FILE *fp; - struct timeval tv; + int fd; + char name[sizeof(TEMPLATE)]; struct stat st; - struct timespec ts; + struct timespec ts, td; + double diff; - assert (gettimeofday (&tv, NULL) == 0); - ts.tv_sec = tv.tv_sec; - ts.tv_nsec = tv.tv_usec * 1000; + gettime (&ts); - fp = tmpfile (); - assert (fp != NULL); - assert (fstat (fileno (fp), &st) == 0); - fclose (fp); - if (timespec_cmp (get_stat_mtime (&st), ts) >= 0) + strcpy (name, TEMPLATE); + umask (077); + fd = mkstemp (name); + assert (fd != -1); + unlink (name); + assert (fstat (fd, &st) == 0); + close (fd); + + td = timespec_sub (get_stat_mtime (&st), ts); + diff = td.tv_sec * BILLION + td.tv_nsec; + if (diff < 0) + diff = - diff; + if (diff / BILLION >= 1) { fprintf (stderr, "file timestamp unreliable\n"); return 1; -- 2.11.4.GIT