52ab5b
From 7a4094d382e74aaed0a0b8356dc24d64952852f9 Mon Sep 17 00:00:00 2001
52ab5b
From: Pavel Raiskup <praiskup@redhat.com>
52ab5b
Date: Fri, 3 Jul 2020 12:32:58 +0200
52ab5b
Subject: [PATCH] Extract: retain times for symlinks
52ab5b
52ab5b
Original report by Pat Riehecky at
52ab5b
https://bugzilla.redhat.com/1486364
52ab5b
52ab5b
* src/copyin.c (copyin_device): Don't check for retain_time_flag
52ab5b
global, it's done by set_file_times.
52ab5b
(copyin_link): Call set_file_times to restore symlink times.
52ab5b
* src/util.c (set_perms): Don't check for retain_time_flag global,
52ab5b
done by set_file_times call.
52ab5b
(set_file_times): Do nothing if retain_time_flag global is false.
52ab5b
* src/copypass.c (process_copy_pass): Call set_file_times for
52ab5b
symlinks.
52ab5b
---
52ab5b
 src/copyin.c   | 5 ++---
52ab5b
 src/copypass.c | 2 ++
52ab5b
 src/util.c     | 6 ++++--
52ab5b
 3 files changed, 8 insertions(+), 5 deletions(-)
52ab5b
52ab5b
diff --git a/src/copyin.c b/src/copyin.c
52ab5b
index 183b5b5..267ed4b 100644
52ab5b
--- a/src/copyin.c
52ab5b
+++ b/src/copyin.c
52ab5b
@@ -639,9 +639,7 @@ copyin_device (struct cpio_file_stat* file_hdr)
52ab5b
   /* chown may have turned off some permissions we wanted. */
52ab5b
   if (chmod (file_hdr->c_name, file_hdr->c_mode) < 0)
52ab5b
     chmod_error_details (file_hdr->c_name, file_hdr->c_mode);
52ab5b
-  if (retain_time_flag)
52ab5b
-    set_file_times (-1, file_hdr->c_name, file_hdr->c_mtime,
52ab5b
-		    file_hdr->c_mtime);
52ab5b
+  set_file_times (-1, file_hdr->c_name, file_hdr->c_mtime, file_hdr->c_mtime);
52ab5b
 }
52ab5b
 
52ab5b
 static void
52ab5b
@@ -692,6 +690,7 @@ copyin_link (struct cpio_file_stat *file_hdr, int in_file_des)
52ab5b
   	  && errno != EPERM)
52ab5b
 	chown_error_details (file_hdr->c_name, uid, gid);
52ab5b
     }
52ab5b
+  set_file_times (-1, file_hdr->c_name, file_hdr->c_mtime, file_hdr->c_mtime);
52ab5b
   free (link_name);
52ab5b
 }
52ab5b
 
52ab5b
diff --git a/src/copypass.c b/src/copypass.c
52ab5b
index c5a9899..b4e7169 100644
52ab5b
--- a/src/copypass.c
52ab5b
+++ b/src/copypass.c
52ab5b
@@ -317,6 +317,8 @@ process_copy_pass ()
52ab5b
 		  && errno != EPERM)
52ab5b
 	        chown_error_details (output_name.ds_string, uid, gid);
52ab5b
             }
52ab5b
+		  set_file_times (-1, output_name.ds_string,
52ab5b
+        				  in_file_stat.st_atime, in_file_stat.st_mtime);
52ab5b
 	  free (link_name);
52ab5b
 	}
52ab5b
 #endif
52ab5b
diff --git a/src/util.c b/src/util.c
52ab5b
index 6ff6032..11f9c30 100644
52ab5b
--- a/src/util.c
52ab5b
+++ b/src/util.c
52ab5b
@@ -1389,7 +1389,6 @@ set_perms (int fd, struct cpio_file_stat *header)
52ab5b
        we have to refer to it using name+ instead of name.  */
52ab5b
     file_hdr->c_name [cdf_char] = '+';
52ab5b
 #endif
52ab5b
-  if (retain_time_flag)
52ab5b
     set_file_times (fd, header->c_name, header->c_mtime, header->c_mtime);
52ab5b
 }
52ab5b
 
52ab5b
@@ -1398,6 +1397,8 @@ set_file_times (int fd,
52ab5b
 		const char *name, unsigned long atime, unsigned long mtime)
52ab5b
 {
52ab5b
   struct timespec ts[2];
52ab5b
+  if (!retain_time_flag)
52ab5b
+    return;
52ab5b
   
52ab5b
   memset (&ts, 0, sizeof ts);
52ab5b
 
52ab5b
@@ -1406,7 +1407,8 @@ set_file_times (int fd,
52ab5b
 
52ab5b
   /* Silently ignore EROFS because reading the file won't have upset its 
52ab5b
      timestamp if it's on a read-only filesystem. */
52ab5b
-  if (fdutimens (fd, name, ts) < 0 && errno != EROFS)
52ab5b
+  if ((fd >= 0 ? fdutimens (fd, NULL, ts) : lutimens (name, ts)) < 0
52ab5b
+      && errno != EROFS)
52ab5b
     utime_error (name);
52ab5b
 }
52ab5b
 
52ab5b
-- 
52ab5b
2.24.1
52ab5b