From b7a6b37f934970d7736598a53932a37a80cfc654 Mon Sep 17 00:00:00 2001 From: Jake Wallin Date: Sun, 22 Nov 2026 23:30:21 +0000 Subject: [PATCH] mv: resolve directory targets for single-source exchanges For a single source and a directory target, exchange the source with the entry of the same name inside the directory. An explicit `-T` still exchanges the two operands directly. Add regression tests for existing and missing entries. Both check that an unrelated file stays in the target directory. Closes #16778 --- src/uu/mv/src/mv.rs | 14 ++++++-------- tests/by-util/test_mv.rs | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 8 deletions(-) diff --git a/src/uu/mv/src/mv.rs b/src/uu/mv/src/mv.rs index 4b042c015c8..17943170b3a 100644 --- a/src/uu/mv/src/mv.rs +++ b/src/uu/mv/src/mv.rs @@ -633,9 +633,8 @@ pub fn mv(files: &[OsString], opts: &Options) -> UResult<()> { /// Atomically exchange two paths with `--exchange` (renameat2 `RENAME_EXCHANGE`). /// -/// With more than two operands, the last operand must be a directory, and -/// each of the other operands is exchanged with the file of the same name -/// inside that directory (matching GNU `mv --exchange a b c dir/`). +/// Unless `-T` is given, a directory destination selects the entry with the +/// same name as each source. #[cfg(any(target_os = "linux", target_os = "android"))] fn exchange_paths(paths: &[PathBuf], opts: &Options) -> UResult<()> { match paths { @@ -643,11 +642,8 @@ fn exchange_paths(paths: &[PathBuf], opts: &Options) -> UResult<()> { 1, translate!("mv-error-exchange-two-operands"), )), - [from, to] => exchange_two_paths(from, to, opts), - [sources @ .., target_dir] => { - if !target_dir.is_dir() { - return Err(MvError::TargetNotADirectory(target_dir.quote().to_string()).into()); - } + [from, to] if opts.no_target_dir => exchange_two_paths(from, to, opts), + [sources @ .., target_dir] if target_dir.is_dir() => { for source in sources { let name = source .file_name() @@ -656,6 +652,8 @@ fn exchange_paths(paths: &[PathBuf], opts: &Options) -> UResult<()> { } Ok(()) } + [from, to] => exchange_two_paths(from, to, opts), + [.., target] => Err(MvError::TargetNotADirectory(target.quote().to_string()).into()), } } diff --git a/tests/by-util/test_mv.rs b/tests/by-util/test_mv.rs index e828d29a2e2..9343751c11c 100644 --- a/tests/by-util/test_mv.rs +++ b/tests/by-util/test_mv.rs @@ -3926,6 +3926,45 @@ fn test_mv_exchange_file_and_dir() { assert_eq!(at.read("branch"), "payload"); } +#[cfg(any(target_os = "linux", target_os = "android"))] +#[uutests::wasi_ignore(UnsupportedStdApi)] +#[test] +fn test_mv_exchange_single_source_into_directory() { + let (at, mut ucmd) = at_and_ucmd!(); + at.mkdir("staging"); + at.write("staging/manifest", "staged"); + at.mkdir("release"); + at.write("release/manifest", "current"); + at.write("release/notes", "other"); + + ucmd.args(&["--exchange", "staging/manifest", "release/"]) + .succeeds() + .no_output(); + + assert_eq!(at.read("staging/manifest"), "current"); + assert_eq!(at.read("release/manifest"), "staged"); + assert_eq!(at.read("release/notes"), "other"); +} + +#[cfg(any(target_os = "linux", target_os = "android"))] +#[uutests::wasi_ignore(UnsupportedStdApi)] +#[test] +fn test_mv_exchange_single_source_missing_directory_entry() { + let (at, mut ucmd) = at_and_ucmd!(); + at.write("manifest", "staged"); + at.mkdir("release"); + at.write("release/notes", "other"); + + ucmd.args(&["--exchange", "manifest", "release"]) + .fails() + .code_is(1) + .stderr_contains("release/manifest"); + + assert_eq!(at.read("manifest"), "staged"); + assert_eq!(at.read("release/notes"), "other"); + assert!(!at.plus("release/manifest").exists()); +} + #[cfg(any(target_os = "linux", target_os = "android"))] #[test] #[cfg(not(wasi_runner))]