Skip to content

cp: Should refuse to create broken symlinks to parent-dir #6385

@BenWiederhake

Description

@BenWiederhake

Copying files as symbolic links to a new (sub-)directory would require constructing a relative path from the new location to the old, e.g. starting with ../.

Trying to do so would necessarily be very fragile. GNU cp even refuses to attempt it. However, uutils currently does it, very badly, resulting in completely broken links and exit code 0.

$ mkdir newdir  
$ cp -vs README.md newdir/  # GNU refuses to create broken symbolic links
'README.md' -> 'newdir/README.md'
cp: newdir/README.md: can make relative symbolic links only in current directory
[$? = 1]
$ ls newdir/  # It's empty
$ cargo run -q cp -vs README.md newdir/  # uutils returns "success"
'README.md' -> 'newdir/README.md'
$ ls newdir/  # And creates something
README.md
$ readlink newdir/README.md  # It creates a self-link
README.md
$ less newdir/README.md 
newdir/README.md: Too many levels of symbolic links
[$? = 1]

Discovered while reviewing #6380.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions