Discussion:
copy-file: don't lose sub-second timestamps
(too old to reply)
Bruno Haible
2017-05-01 11:27:50 UTC
Permalink
Raw Message
The copy-file module, so far, loses the fractional part of the seconds of the
timestamp of the file being copied. This fixes it.


2017-05-01 Bruno Haible <***@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.

diff --git a/lib/copy-file.c b/lib/copy-file.c
index b3f02a7..462ea1c 100644
--- a/lib/copy-file.c
+++ b/lib/copy-file.c
@@ -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 @@ qcopy_file_preserving (const char *src_filename, const char *dest_filename)
#endif

/* Preserve the access and modification times. */
-#if HAVE_UTIME
- {
- struct utimbuf ut;
-
- ut.actime = statbuf.st_atime;
- ut.modtime = statbuf.st_mtime;
- utime (dest_filename, &ut);
- }
-#elif HAVE_UTIMES
{
- struct timeval ut[2];
+ struct timespec ts[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);
+ ts[0] = get_stat_atime (&statbuf);
+ ts[1] = get_stat_mtime (&statbuf);
+ utimens (dest_filename, ts);
}
-#endif

#if HAVE_CHOWN
/* Preserve the owner and group. */
diff --git a/m4/copy-file.m4 b/m4/copy-file.m4
index 4b5db8a..c29f793 100644
--- a/m4/copy-file.m4
+++ b/m4/copy-file.m4
@@ -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 @@ dnl with or without modifications, as long as this notice is preserved.
AC_DEFUN([gl_COPY_FILE],
[
AC_CHECK_HEADERS_ONCE([unistd.h])
- AC_CHECK_FUNCS([chown utime utimes])
+ AC_CHECK_FUNCS([chown])
])
diff --git a/modules/copy-file b/modules/copy-file
index 7238f55..e94a22c 100644
--- a/modules/copy-file
+++ b/modules/copy-file
@@ -17,9 +17,10 @@ ignore-value
open
quote
safe-read
+stat-time
stdlib
unistd
-utime-h
+utimens
xalloc

configure.ac:

Loading...