Skip to content

Commit 2a072db

Browse files
gibkimclaude
andauthored
mv: do not follow a symlink destination when checking it (#15912)
* mv: do not follow a symlink destination when checking it The overwrite guards ran only when the destination resolved, so -n, -i, -u, --update=none(-fail) and -b were all skipped for a dangling symlink, which was then replaced. Check the destination entry with lstat instead, and use the link's own mtime and mode for -u and the overwrite prompt, as GNU mv does. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * mv: fix flaky stdin write and formatting in broken symlink tests Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * mv: keep -u from skipping a directory onto a symlink -u compared the times before the check that a directory can't replace a non-directory, so moving a directory into a target directory that holds a newer symlink of the same name was skipped and reported success. Only skip when the move itself is possible, and compare the source's own times too. On Windows, look at the symlink itself when checking that the destination is writable, as on Unix. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 31a2935 commit 2a072db

2 files changed

Lines changed: 82 additions & 8 deletions

File tree

‎src/uu/mv/src/mv.rs‎

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -814,18 +814,28 @@ fn rename(
814814
) -> io::Result<()> {
815815
let mut backup_path = None;
816816

817-
if to.exists() {
817+
// The destination entry itself is replaced, so look at it without
818+
// following symlinks: a dangling symlink is still there to protect.
819+
if to.symlink_metadata().is_ok() {
818820
if opts.update == UpdateMode::None {
819821
if opts.debug {
820822
println!("{}", translate!("mv-debug-skipped", "target" => to.quote()));
821823
}
822824
return Ok(());
823825
}
824826

825-
if (opts.update == UpdateMode::IfOlder)
826-
&& fs::metadata(from)?.modified()? <= fs::metadata(to)?.modified()?
827-
{
828-
return Ok(());
827+
if opts.update == UpdateMode::IfOlder {
828+
let from_metadata = fs::symlink_metadata(from)?;
829+
let to_metadata = fs::symlink_metadata(to)?;
830+
let from_is_dir =
831+
from_metadata.is_dir() || (path_ends_with_terminator(from) && from.is_dir());
832+
// A directory can't replace a non-directory, so don't skip it as up to
833+
// date: let the rename report the error, as GNU mv does.
834+
if (!from_is_dir || to_metadata.is_dir())
835+
&& from_metadata.modified()? <= to_metadata.modified()?
836+
{
837+
return Ok(());
838+
}
829839
}
830840

831841
if opts.update == UpdateMode::NoneFail {
@@ -1677,7 +1687,7 @@ fn is_empty_dir(path: &Path) -> bool {
16771687
/// Check if file is writable, returning the mode for potential reuse.
16781688
#[cfg(unix)]
16791689
fn is_writable(path: &Path) -> (bool, Option<u32>) {
1680-
if let Ok(metadata) = path.metadata() {
1690+
if let Ok(metadata) = path.symlink_metadata() {
16811691
let mode = metadata.permissions().mode();
16821692
// Check if user write bit is set
16831693
((mode & 0o200) != 0, Some(mode))
@@ -1689,7 +1699,7 @@ fn is_writable(path: &Path) -> (bool, Option<u32>) {
16891699
/// Check if file is writable.
16901700
#[cfg(not(unix))]
16911701
fn is_writable(path: &Path) -> (bool, Option<u32>) {
1692-
if let Ok(metadata) = path.metadata() {
1702+
if let Ok(metadata) = path.symlink_metadata() {
16931703
(!metadata.permissions().readonly(), None)
16941704
} else {
16951705
(false, None) // If we can't get metadata, prompt user to be safe
@@ -1699,7 +1709,7 @@ fn is_writable(path: &Path) -> (bool, Option<u32>) {
16991709
#[cfg(unix)]
17001710
fn get_interactive_prompt(to: &Path, cached_mode: Option<u32>) -> String {
17011711
// Use cached mode if available, otherwise fetch it
1702-
let mode = cached_mode.or_else(|| to.metadata().ok().map(|m| m.permissions().mode()));
1712+
let mode = cached_mode.or_else(|| to.symlink_metadata().ok().map(|m| m.permissions().mode()));
17031713
if let Some(mode) = mode {
17041714
let file_mode = mode & 0o777;
17051715
// Check if file is not writable by user

‎tests/by-util/test_mv.rs‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,70 @@ fn test_mv_file_to_broken_symlink_file() {
533533
assert!(!at.file_exists("file"));
534534
}
535535

536+
#[rstest]
537+
#[case::no_clobber(&["-n"], true)]
538+
#[case::update_none(&["--update=none"], true)]
539+
#[case::update_none_fail(&["--update=none-fail"], false)]
540+
#[case::update_older(&["-u"], true)]
541+
#[case::interactive(&["-i"], false)]
542+
#[cfg(all(unix, not(target_os = "android")))]
543+
fn test_mv_file_keeps_broken_symlink(#[case] args: &[&str], #[case] succeeds: bool) {
544+
let (at, mut ucmd) = at_and_ucmd!();
545+
546+
// The link is created last, so it is not older than the file for -u
547+
at.touch("file");
548+
at.symlink_file("missing-target", "broken");
549+
550+
// Only -i reads the answer; the others may exit before stdin is written
551+
let result = ucmd
552+
.args(args)
553+
.arg("file")
554+
.arg("broken")
555+
.pipe_in("n")
556+
.ignore_stdin_write_error()
557+
.run();
558+
assert_eq!(result.succeeded(), succeeds);
559+
560+
assert!(at.is_symlink("broken"));
561+
assert!(at.file_exists("file"));
562+
}
563+
564+
#[test]
565+
#[cfg(all(unix, not(target_os = "android")))]
566+
fn test_mv_update_dir_onto_newer_symlink_fails() {
567+
let (at, mut ucmd) = at_and_ucmd!();
568+
569+
// The link is created last, so it is not older than the directory for -u,
570+
// but a directory still can't replace it
571+
at.mkdir("dir");
572+
at.mkdir("target");
573+
at.symlink_file("missing-target", "target/dir");
574+
575+
ucmd.arg("-u").arg("dir").arg("target").fails();
576+
577+
assert!(at.is_symlink("target/dir"));
578+
assert!(at.dir_exists("dir"));
579+
}
580+
581+
#[test]
582+
#[cfg(all(unix, not(target_os = "android")))]
583+
fn test_mv_file_backs_up_broken_symlink() {
584+
let (at, mut ucmd) = at_and_ucmd!();
585+
586+
at.touch("file");
587+
at.symlink_file("missing-target", "broken");
588+
589+
ucmd.arg("-b")
590+
.arg("file")
591+
.arg("broken")
592+
.succeeds()
593+
.no_stderr();
594+
595+
assert!(at.is_symlink("broken~"));
596+
assert!(at.file_exists("broken"));
597+
assert!(!at.is_symlink("broken"));
598+
}
599+
536600
#[test]
537601
#[cfg(all(unix, not(target_os = "android")))]
538602
fn test_mv_file_to_broken_symlink_directory() {

0 commit comments

Comments
 (0)