From a7a493a604be66bddd5cd947d67078506961d3aa Mon Sep 17 00:00:00 2001 From: ALBIN BABU VARGHESE Date: Wed, 4 Jun 2025 11:25:53 -0400 Subject: [PATCH] mv: moving dangling symlinks into directories (#8064) * Fix linting and style issues * Change condition to not fail for dangling symlinks The function `move_files_into_dir` had a condition that failed when a dangling symlink was moved into a folder, which resulted in a file or directory doesn't exist error * Added a test case --- src/uu/mv/src/mv.rs | 2 +- tests/by-util/test_mv.rs | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/uu/mv/src/mv.rs b/src/uu/mv/src/mv.rs index edfa505c5..c7f920204 100644 --- a/src/uu/mv/src/mv.rs +++ b/src/uu/mv/src/mv.rs @@ -526,7 +526,7 @@ fn move_files_into_dir(files: &[PathBuf], target_dir: &Path, options: &Options) }; for sourcepath in files { - if !sourcepath.exists() { + if sourcepath.symlink_metadata().is_err() { show!(MvError::NoSuchFile(sourcepath.quote().to_string())); continue; } diff --git a/tests/by-util/test_mv.rs b/tests/by-util/test_mv.rs index 577f6a758..b20c4b2b6 100644 --- a/tests/by-util/test_mv.rs +++ b/tests/by-util/test_mv.rs @@ -443,6 +443,19 @@ fn test_mv_same_hardlink() { .stderr_is(format!("mv: '{file_a}' and '{file_b}' are the same file\n")); } +#[test] +#[cfg(all(unix, not(target_os = "android")))] +fn test_mv_dangling_symlink_to_folder() { + let (at, mut ucmd) = at_and_ucmd!(); + + at.symlink_file("404", "abc"); + at.mkdir("x"); + + ucmd.arg("abc").arg("x").succeeds(); + + assert!(at.symlink_exists("x/abc")); +} + #[test] #[cfg(all(unix, not(target_os = "android")))] fn test_mv_same_symlink() {