[dev] [sbase] [PATCH 1/3] mv, cp: Preserve nanosecond timestamps

From: Michael Forney <mforney_AT_mforney.org>
Date: Sat, 16 May 2015 04:57:25 +0000

Otherwise, we run into problems in a typical autoconf-based build
system:

  - config.status is created at some point between two seconds.
  - config.status is run, generating Makefile by first writing to a file
    in /tmp, and then mv-ing it to Makefile.
  - If this mv happens before the beginning of the next second, Makefile
    will be created with the same tv_sec as config.status, but with
    tv_nsec = 0.
  - When make runs, it sees that Makefile is older than config.status,
    and re-runs config.status to generate Makefile.
---
 libutil/cp.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/libutil/cp.c b/libutil/cp.c
index 32e0dc6..0f098d3 100644
--- a/libutil/cp.c
+++ b/libutil/cp.c
_AT_@ -30,7 +30,7 @@ cp(const char *s1, const char *s2, int depth)
 	FILE *f1, *f2;
 	struct dirent *d;
 	struct stat st;
-	struct utimbuf ut;
+	struct timespec times[2];
 	ssize_t r;
 	int (*statf)(const char *, struct stat *);
 	char target[PATH_MAX], ns1[PATH_MAX], ns2[PATH_MAX], *statf_name;
_AT_@ -154,9 +154,9 @@ cp(const char *s1, const char *s2, int depth)
 	if (cp_aflag || cp_pflag) {
 		/* timestamp and owner*/
 		if (!S_ISLNK(st.st_mode)) {
-			ut.actime = st.st_atime;
-			ut.modtime = st.st_mtime;
-			utime(s2, &ut);
+			times[0] = st.st_atim;
+			times[1] = st.st_mtim;
+			utimensat(AT_FDCWD, s2, times, 0);
 
 			if (chown(s2, st.st_uid, st.st_gid) < 0) {
 				weprintf("chown %s:", s2);
-- 
2.1.3.1.g339ec9c
Received on Sat May 16 2015 - 06:57:25 CEST

This archive was generated by hypermail 2.3.0 : Sat May 16 2015 - 07:12:08 CEST