Skip to content
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
10 changes: 10 additions & 0 deletions src/uu/install/src/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ enum InstallError {

#[error("failed to access {}: Not a directory", .0.quote())]
NotADirectory(PathBuf),

#[error("cannot overwrite directory {} with non-directory {}", .0.quote(), .1.quote())]
OverrideDirectoryFailed(PathBuf, PathBuf),
}

impl UError for InstallError {
Expand Down Expand Up @@ -748,6 +751,13 @@ fn copy_normal_file(from: &Path, to: &Path) -> UResult<()> {
/// Returns an empty Result or an error in case of failure.
///
fn copy_file(from: &Path, to: &Path) -> UResult<()> {
if to.is_dir() && !from.is_dir() {
return Err(InstallError::OverrideDirectoryFailed(
to.to_path_buf().clone(),
from.to_path_buf().clone(),
)
.into());
}
// fs::copy fails if destination is a invalid symlink.
// so lets just remove all existing files at destination before copy.
if let Err(e) = fs::remove_file(to) {
Expand Down
14 changes: 14 additions & 0 deletions tests/by-util/test_install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1764,3 +1764,17 @@ fn test_install_from_stdin() {
assert!(at.file_exists(target));
assert_eq!(at.read(target), test_string);
}

#[test]
fn test_install_failing_copy_file_to_target_contain_subdir_with_same_name() {
let (at, mut ucmd) = at_and_ucmd!();
let file = "file";
let dir1 = "dir1";

at.touch(file);
at.mkdir_all(&format!("{dir1}/{file}"));
ucmd.arg(file)
.arg(dir1)
.fails()
.stderr_contains("cannot overwrite directory");
}
Loading