]> Savannah Git Hosting - gnulib.git/commitdiff
nap.h: Port to native Windows.
authorBruno Haible <bruno@clisp.org>
Sun, 23 Apr 2017 17:19:35 +0000 (19:19 +0200)
committerBruno Haible <bruno@clisp.org>
Mon, 24 Apr 2017 22:02:53 +0000 (00:02 +0200)
* tests/nap.h (nap_get_stat): Renamed from get_stat. Remove argument fd;
use nap_fd instead. On native Windows, close and reopen nap_fd.
(nap_works): Don't compare the ctimes, because on native Windows, these
are the creation times.
(nap): Update.

ChangeLog
tests/nap.h

index bde4ac7dd247c4bea74cedcd0b8e1baaf42fdbae..27cfacb86462d07a48b3963dd5e86c590cc4ab66 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2017-04-23  Bruno Haible  <bruno@clisp.org>
+
+       nap.h: Port to native Windows.
+       * tests/nap.h (nap_get_stat): Renamed from get_stat. Remove argument fd;
+       use nap_fd instead. On native Windows, close and reopen nap_fd.
+       (nap_works): Don't compare the ctimes, because on native Windows, these
+       are the creation times.
+       (nap): Update.
+
 2017-04-23  Bruno Haible  <bruno@clisp.org>
 
        nap.h: Fix logic.
index ce8f95a5e59c5be9b1e87de1142d7319eb51c5e0..00de83fa1fdb54c04bd5125c9e32fdf886f1910e 100644 (file)
@@ -22,6 +22,9 @@
 # include <limits.h>
 # include <stdbool.h>
 
+/* Name of the witness file.  */
+#define TEMPFILE BASE "nap.tmp"
+
 /* File descriptor used for the witness file.  */
 static int nap_fd = -1;
 
@@ -48,36 +51,46 @@ diff_timespec (struct timespec a, struct timespec b)
   return INT_MAX;
 }
 
+/* If DO_WRITE, bump the modification time of the file designated by NAP_FD.
+   Then fetch the new STAT information of NAP_FD.  */
 static void
-get_stat (int fd, struct stat *st, int do_write)
+nap_get_stat (struct stat *st, int do_write)
 {
   if (do_write)
-    ASSERT (write (fd, "\n", 1) == 1);
-  ASSERT (fstat (fd, st) == 0);
+    {
+      ASSERT (write (nap_fd, "\n", 1) == 1);
+#if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
+      /* On native Windows, the modification times are not changed until NAP_FD
+         is closed. See
+         https://msdn.microsoft.com/en-us/library/windows/desktop/aa365747(v=vs.85).aspx */
+      close (nap_fd);
+      nap_fd = open (TEMPFILE, O_RDWR, 0600);
+      ASSERT (nap_fd != -1);
+      lseek (nap_fd, 0, SEEK_END);
+    }
+#endif
+  ASSERT (fstat (nap_fd, st) == 0);
 }
 
 /* Given a file whose descriptor is FD, see whether delaying by DELAY
-   nanoseconds causes a change in a file's ctime and mtime.
+   nanoseconds causes a change in a file's mtime.
    OLD_ST is the file's status, recently gotten.  */
 static bool
-nap_works (int fd, int delay, struct stat old_st)
+nap_works (int delay, struct stat old_st)
 {
   struct stat st;
   struct timespec delay_spec;
   delay_spec.tv_sec = delay / 1000000000;
   delay_spec.tv_nsec = delay % 1000000000;
   ASSERT (nanosleep (&delay_spec, 0) == 0);
-  get_stat (fd, &st, 1);
+  nap_get_stat (&st, 1);
 
-  if (   diff_timespec (get_stat_ctime (&st), get_stat_ctime (&old_st))
-      && diff_timespec (get_stat_mtime (&st), get_stat_mtime (&old_st)))
+  if (diff_timespec (get_stat_mtime (&st), get_stat_mtime (&old_st)))
     return true;
 
   return false;
 }
 
-#define TEMPFILE BASE "nap.tmp"
-
 static void
 clear_temp_file (void)
 {
@@ -105,12 +118,12 @@ nap (void)
     {
       atexit (clear_temp_file);
       ASSERT ((nap_fd = creat (TEMPFILE, 0600)) != -1);
-      get_stat (nap_fd, &old_st, 0);
+      nap_get_stat (&old_st, 0);
     }
   else
     {
       ASSERT (0 <= nap_fd);
-      get_stat (nap_fd, &old_st, 1);
+      nap_get_stat (&old_st, 1);
     }
 
   if (1 < delay)