Merge pull request #3392 from sylvestre/mv-itself

mv: trigger an error when doing mv dir1 dir2 dir2
This commit is contained in:
Sylvestre Ledru 2022-04-13 09:16:24 +02:00 committed by GitHub
commit 8cd359c556
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
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