1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 11:37:44 +00:00

mv: trigger an error when doing mv dir1 dir2 dir2

This commit is contained in:
Sylvestre Ledru 2022-04-12 22:37:38 +02:00
parent 57b8caf1d0
commit 920633c0ea
2 changed files with 35 additions and 1 deletions

View file

@ -285,7 +285,23 @@ fn exec(files: &[OsString], b: &Behavior) -> UResult<()> {
));
}
let target_dir = paths.last().unwrap();
move_files_into_dir(&paths[..paths.len() - 1], target_dir, b)
let sources = &paths[..paths.len() - 1];
// Check if we have mv dir1 dir2 dir2
// And generate an error if this is the case
if sources.contains(target_dir) {
return Err(USimpleError::new(
1,
format!(
"cannot move {} to a subdirectory of itself, '{}/{}'",
target_dir.quote(),
target_dir.display(),
target_dir.display()
),
));
}
move_files_into_dir(sources, target_dir, b)
}
}
}

View file

@ -823,6 +823,24 @@ fn test_mv_interactive_error() {
.is_empty());
}
#[test]
fn test_mv_info_self() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
let dir1 = "dir1";
let dir2 = "dir2";
at.mkdir(dir1);
at.mkdir(dir2);
scene
.ucmd()
.arg(dir1)
.arg(dir2)
.arg(dir2)
.fails()
.stderr_contains("mv: cannot move 'dir2' to a subdirectory of itself, 'dir2/dir2'");
}
// Todo:
// $ at.touch a b