Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mv: fix subdir detection #5483

Merged
merged 1 commit into from
Dec 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 8 additions & 22 deletions src/uu/mv/src/mv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,29 +358,15 @@ fn handle_two_paths(source: &Path, target: &Path, opts: &Options) -> UResult<()>
} else {
Err(MvError::DirectoryToNonDirectory(target.quote().to_string()).into())
}
// Check that source & target do not contain same subdir/dir when both exist
// mkdir dir1/dir2; mv dir1 dir1/dir2
} else if target.starts_with(source) {
Err(MvError::SelfTargetSubdirectory(
source.display().to_string(),
target.display().to_string(),
)
.into())
} else {
// Check that source & target do not contain same subdir/dir when both exist
// mkdir dir1/dir2; mv dir1 dir1/dir2
let target_contains_itself = target
.as_os_str()
.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(),
target.display().to_string(),
)
.into());
}
move_files_into_dir(&[source.to_path_buf()], target, opts)
}
} else if target.exists() && source.is_dir() {
Expand Down
14 changes: 14 additions & 0 deletions tests/by-util/test_mv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
fn test_mv_file_into_dir_where_both_are_files() {
let scene = TestScenario::new(util_name!());
Expand Down
Loading