Subject: Re: bug#8374: cp -a [-l] sometimes does not preserve timestamps of symlinks

From: Pádraig Brady <[email-masked]>
Date: Thu, 31 Mar 2011 15:11:42 +0100

On 29/03/11 14:46, Ruediger Meier wrote:
> Hi,
> 
> 
> I see you fixed that already for for cp -a
> http://marc.info/?t=124897089600001&r=1&w=2
> 
> But it does not together with option -link:
> 
> cd /tmp/
> ln -s somewhere symlink
> touch -h -t "197001010000"  symlink
> cp -a symlink symlink-a
> cp -al symlink symlink-al
> ls -l symlink*
> lrwxrwxrwx 1 rudi users 9 1970-01-01 00:00 symlink -> somewhere
> lrwxrwxrwx 1 rudi users 9 1970-01-01 00:00 symlink-a -> somewhere
> lrwxrwxrwx 1 rudi users 9 2011-03-29 13:44 symlink-al -> somewhere
> 
> 
> I'm using core utils coreutils 8.9 (opensuse 11.4).
> 
> BTW with coreutils 7.1 and 6.9 it behaves the other way, -al is correct 
> but -a not.
> 
> So probably -al is broken since you fixed -a in 7.5.

Hmm it looks now like we're creating symlinks (with wrong timestamps),
but in fact we should be creating hardlinks to symlinks.

This seems to have been changed with:
http://git.sv.gnu.org/gitweb/?p=coreutils.git;a=commit;h=3346c0af

Eric do you have any suggestions?
The following seems to make things work as expected

diff --git a/src/copy.c b/src/copy.c
index a672fd3..4f07d2c 100644
--- a/src/copy.c
+++ b/src/copy.c
@@ -2226,8 +2226,7 @@ copy_internal (char const *src_name, char const *dst_name,
      where link(2) follows the link, gnulib creates a symlink with
      identical contents, which is good enough for our purposes).  */
   else if (x->hard_link
-           && (!S_ISLNK (src_mode)
-               || x->dereference != DEREF_NEVER))
+           && !(S_ISLNK (src_mode) && x->dereference != DEREF_NEVER))
     {
        if (linkat (AT_FDCWD, src_name, AT_FDCWD, dst_name, 0))
         {

Hmm, maybe it's better to skip linkat() for symlinks altogether
since the gnulib fallback on systems that implicitly dereference,
will create symlinks, but without the right timestamps.
Or, perhaps the gnulib fallback should try to update timestamps too?

cheers,
Pádraig.