changeset 38524:d9ae6ff8a05a

copy-file: Preserve sub-second time stamps. * lib/copy-file.c: Include stat-time.h, utimens.h instead of <utime.h>. (qcopy_file_preserving): Use 'struct timespec' and utimens() to transport the time stamps from the original file to the destination file. * m4/copy-file.m4 (gl_COPY_FILE): Don't test for utime, utimes. * modules/copy-file (Depends-on): Add stat-time, utimns instead of utime-h.
author Bruno Haible <bruno@clisp.org>
date Mon, 01 May 2017 13:27:57 +0200
parents 362be3f37898
children 1315793d9f25
files ChangeLog lib/copy-file.c m4/copy-file.m4 modules/copy-file
diffstat 4 files changed, 21 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Mon May 01 12:58:28 2017 +0200
+++ b/ChangeLog	Mon May 01 13:27:57 2017 +0200
@@ -1,3 +1,14 @@
+2017-05-01  Bruno Haible  <bruno@clisp.org>
+
+	copy-file: Preserve sub-second time stamps.
+	* lib/copy-file.c: Include stat-time.h, utimens.h instead of <utime.h>.
+	(qcopy_file_preserving): Use 'struct timespec' and utimens() to
+	transport the time stamps from the original file to the destination
+	file.
+	* m4/copy-file.m4 (gl_COPY_FILE): Don't test for utime, utimes.
+	* modules/copy-file (Depends-on): Add stat-time, utimns instead of
+	utime-h.
+
 2017-05-01  Bruno Haible  <bruno@clisp.org>
 
 	wctype: Fix problems if <wchar.h> gets included after <wctype.h>.
--- a/lib/copy-file.c	Mon May 01 12:58:28 2017 +0200
+++ b/lib/copy-file.c	Mon May 01 13:27:57 2017 +0200
@@ -28,14 +28,12 @@
 #include <sys/stat.h>
 #include <unistd.h>
 
-#if HAVE_UTIME || HAVE_UTIMES
-# include <utime.h>
-#endif
-
 #include "error.h"
 #include "ignore-value.h"
 #include "safe-read.h"
 #include "full-write.h"
+#include "stat-time.h"
+#include "utimens.h"
 #include "acl.h"
 #include "binary-io.h"
 #include "quote.h"
@@ -113,23 +111,13 @@
 #endif
 
   /* Preserve the access and modification times.  */
-#if HAVE_UTIME
   {
-    struct utimbuf ut;
+    struct timespec ts[2];
 
-    ut.actime = statbuf.st_atime;
-    ut.modtime = statbuf.st_mtime;
-    utime (dest_filename, &ut);
+    ts[0] = get_stat_atime (&statbuf);
+    ts[1] = get_stat_mtime (&statbuf);
+    utimens (dest_filename, ts);
   }
-#elif HAVE_UTIMES
-  {
-    struct timeval ut[2];
-
-    ut[0].tv_sec = statbuf.st_atime; ut[0].tv_usec = 0;
-    ut[1].tv_sec = statbuf.st_mtime; ut[1].tv_usec = 0;
-    utimes (dest_filename, &ut);
-  }
-#endif
 
 #if HAVE_CHOWN
   /* Preserve the owner and group.  */
--- a/m4/copy-file.m4	Mon May 01 12:58:28 2017 +0200
+++ b/m4/copy-file.m4	Mon May 01 13:27:57 2017 +0200
@@ -1,4 +1,4 @@
-# copy-file.m4 serial 4
+# copy-file.m4 serial 5
 dnl Copyright (C) 2003, 2009-2017 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -7,5 +7,5 @@
 AC_DEFUN([gl_COPY_FILE],
 [
   AC_CHECK_HEADERS_ONCE([unistd.h])
-  AC_CHECK_FUNCS([chown utime utimes])
+  AC_CHECK_FUNCS([chown])
 ])
--- a/modules/copy-file	Mon May 01 12:58:28 2017 +0200
+++ b/modules/copy-file	Mon May 01 13:27:57 2017 +0200
@@ -17,9 +17,10 @@
 open
 quote
 safe-read
+stat-time
 stdlib
 unistd
-utime-h
+utimens
 xalloc
 
 configure.ac: