Skip to content

Commit 7926f80

Browse files
zhw2101024nickorlow
authored andcommitted
install: improve error message (uutils#7794)
* friendly message install file to directory containing directory with same name * install: test install file to directory containing directory with same name
1 parent 56f53f1 commit 7926f80

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/uu/install/src/install.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ enum InstallError {
9898

9999
#[error("failed to access {}: Not a directory", .0.quote())]
100100
NotADirectory(PathBuf),
101+
102+
#[error("cannot overwrite directory {} with non-directory {}", .0.quote(), .1.quote())]
103+
OverrideDirectoryFailed(PathBuf, PathBuf),
101104
}
102105

103106
impl UError for InstallError {
@@ -748,6 +751,13 @@ fn copy_normal_file(from: &Path, to: &Path) -> UResult<()> {
748751
/// Returns an empty Result or an error in case of failure.
749752
///
750753
fn copy_file(from: &Path, to: &Path) -> UResult<()> {
754+
if to.is_dir() && !from.is_dir() {
755+
return Err(InstallError::OverrideDirectoryFailed(
756+
to.to_path_buf().clone(),
757+
from.to_path_buf().clone(),
758+
)
759+
.into());
760+
}
751761
// fs::copy fails if destination is a invalid symlink.
752762
// so lets just remove all existing files at destination before copy.
753763
if let Err(e) = fs::remove_file(to) {

tests/by-util/test_install.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1764,3 +1764,17 @@ fn test_install_from_stdin() {
17641764
assert!(at.file_exists(target));
17651765
assert_eq!(at.read(target), test_string);
17661766
}
1767+
1768+
#[test]
1769+
fn test_install_failing_copy_file_to_target_contain_subdir_with_same_name() {
1770+
let (at, mut ucmd) = at_and_ucmd!();
1771+
let file = "file";
1772+
let dir1 = "dir1";
1773+
1774+
at.touch(file);
1775+
at.mkdir_all(&format!("{dir1}/{file}"));
1776+
ucmd.arg(file)
1777+
.arg(dir1)
1778+
.fails()
1779+
.stderr_contains("cannot overwrite directory");
1780+
}

0 commit comments

Comments
 (0)