GNU bug report logs - #70558
ln -sfr stats the wrong file

Previous Next

Package: coreutils;

Reported by: David Leonard <d+gnubugs <at> adaptive-enterprises.com>

Date: Thu, 25 Apr 2024 05:34:08 UTC

Severity: normal

To reply to this bug, email your comments to 70558 AT debbugs.gnu.org.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to bug-coreutils <at> gnu.org:
bug#70558; Package coreutils. (Thu, 25 Apr 2024 05:34:10 GMT) Full text and rfc822 format available.

Acknowledgement sent to David Leonard <d+gnubugs <at> adaptive-enterprises.com>:
New bug report received and forwarded. Copy sent to bug-coreutils <at> gnu.org. (Thu, 25 Apr 2024 05:34:11 GMT) Full text and rfc822 format available.

Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):

From: David Leonard <d+gnubugs <at> adaptive-enterprises.com>
To: bug-coreutils <at> gnu.org
Subject: ln -sfr stats the wrong file
Date: Thu, 25 Apr 2024 03:20:27 +1000 (AEST)
When analyzing file accesses with strace, I noticed that ln stats an
unrelated file. This only happens with the opions -sfr and when the dest
symlink already exists.

Here's a reproduction script:

    # setup
    mkdir -p a b
    > b/x
    ln -fs anything a/x

    # ln -sfr b/x a/x
    strace -o trace.out --decode-fds=path -e %file \
	src/ln -sfr b/x a/x

    test $(readlink a/x) = ../b/x
    grep stat trace.out

The invalid stat that happens is:

    newfstatat(AT_FDCWD</tmp/lnbug>, "../b/x", 0x7ffdb94a0270, 0) = -1 ENOENT (No such file or directory)

The cause seems to be in src/ln.c's do_link() where the `source` "b/x" is
converted to "../b/x" by

        if (relative)
              source = rel_source = convert_abs_rel (source, dest);

and then the test to see if deleting the destination file would delete
the source works by checking hard links on the source.

	if (source_status != 0)
	    source_status = stat (source, &source_stats);

Except that -r made source relative to the dest directory.




This bug report was last modified 194 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.