Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/uucore/src/lib/features/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1127,10 +1127,12 @@ pub fn replace_link(target: &Path, dest: &Path, symbolic: bool) -> IOResult<()>
let basename = dest
.file_name()
.ok_or_else(|| Error::new(ErrorKind::InvalidInput, "invalid link path"))?;
// No NOFOLLOW: the parent may be a symlink to a directory, which the
// create attempt above already followed.
let dir = openat(
CWD,
parent,
OFlags::DIRECTORY | OFlags::RDONLY | OFlags::CLOEXEC | OFlags::NOFOLLOW,
OFlags::DIRECTORY | OFlags::RDONLY | OFlags::CLOEXEC,
Mode::empty(),
)?;
let mut urandom = fs::File::open("/dev/urandom")?;
Expand Down
42 changes: 42 additions & 0 deletions tests/by-util/test_ln.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,48 @@ fn test_force_replace_same_inode_leaves_no_temp_file() {
);
}

/// The destination can sit inside a directory reached through a symlink, as
/// when the target argument is a symlink to a directory. The replace must
/// follow it the same way the first create attempt does.
#[test]
#[cfg(unix)]
#[cfg_attr(
wasi_runner,
ignore = "WASI sandbox: creating a link inside a symlinked directory is denied"
)]
fn test_force_replace_in_symlinked_directory() {
let (at, mut ucmd) = at_and_ucmd!();
at.mkdir("real");
at.symlink_dir("real", "dirlink");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you please also cover the hard link case (-f without -s)? it goes through the same path

at.symlink_file("old", "real/link");

ucmd.args(&["-s", "-f", "new", "dirlink/link"]).succeeds();

assert_eq!(at.resolve_link("real/link"), "new");
}

/// Same as above for a hard link, which goes through the same replace path.
#[test]
// Android's app-private filesystem refuses hard links.
#[cfg(all(unix, not(any(target_os = "redox", target_os = "android"))))]
#[cfg_attr(
wasi_runner,
ignore = "WASI sandbox: creating a link inside a symlinked directory is denied"
)]
fn test_force_replace_hard_link_in_symlinked_directory() {
use std::os::unix::fs::MetadataExt;

let (at, mut ucmd) = at_and_ucmd!();
at.touch("new");
at.mkdir("real");
at.symlink_dir("real", "dirlink");
at.touch("real/link");

ucmd.args(&["-f", "new", "dirlink/link"]).succeeds();

assert_eq!(at.metadata("real/link").ino(), at.metadata("new").ino());
}

#[test]
fn test_symlink_overwrite_force_overrides_interactive() {
let (at, mut ucmd) = at_and_ucmd!();
Expand Down
Loading