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

Merge pull request #5483 from cakebaker/mv_fix_5481

mv: fix subdir detection
This commit is contained in:
Sylvestre Ledru 2023-12-25 22:02:23 +01:00 committed by GitHub
commit db9f4cc315
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 22 deletions

View file

@ -359,29 +359,15 @@ fn handle_two_paths(source: &Path, target: &Path, opts: &Options) -> UResult<()>
} else { } else {
Err(MvError::DirectoryToNonDirectory(target.quote().to_string()).into()) Err(MvError::DirectoryToNonDirectory(target.quote().to_string()).into())
} }
} else {
// Check that source & target do not contain same subdir/dir when both exist // Check that source & target do not contain same subdir/dir when both exist
// mkdir dir1/dir2; mv dir1 dir1/dir2 // mkdir dir1/dir2; mv dir1 dir1/dir2
let target_contains_itself = target } else if target.starts_with(source) {
.as_os_str() Err(MvError::SelfTargetSubdirectory(
.to_str()
.ok_or("not a valid unicode string")
.and_then(|t| {
source
.as_os_str()
.to_str()
.ok_or("not a valid unicode string")
.map(|s| t.contains(s))
})
.unwrap();
if target_contains_itself {
return Err(MvError::SelfTargetSubdirectory(
source.display().to_string(), source.display().to_string(),
target.display().to_string(), target.display().to_string(),
) )
.into()); .into())
} } else {
move_files_into_dir(&[source.to_path_buf()], target, opts) move_files_into_dir(&[source.to_path_buf()], target, opts)
} }
} else if target.exists() && source.is_dir() { } else if target.exists() && source.is_dir() {

View file

@ -1455,6 +1455,20 @@ fn test_mv_directory_into_subdirectory_of_itself_fails() {
); );
} }
#[test]
fn test_mv_dir_into_dir_with_source_name_a_prefix_of_target_name() {
let (at, mut ucmd) = at_and_ucmd!();
let source = "test";
let target = "test2";
at.mkdir(source);
at.mkdir(target);
ucmd.arg(source).arg(target).succeeds().no_output();
assert!(at.dir_exists(&format!("{target}/{source}")));
}
#[test] #[test]
fn test_mv_file_into_dir_where_both_are_files() { fn test_mv_file_into_dir_where_both_are_files() {
let scene = TestScenario::new(util_name!()); let scene = TestScenario::new(util_name!());