From 9ca7c3e107c8af9b5330da26c28298c7e3f5e05b Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Thu, 21 Dec 2023 00:17:49 +0100 Subject: [PATCH] install: with -t, check if we aren't passed a file Should pass tests/install/basic-1 --- src/uu/install/src/install.rs | 11 +++++++++++ tests/by-util/test_install.rs | 24 ++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/src/uu/install/src/install.rs b/src/uu/install/src/install.rs index 43925a7f8..7cb305a25 100644 --- a/src/uu/install/src/install.rs +++ b/src/uu/install/src/install.rs @@ -66,6 +66,7 @@ enum InstallError { InvalidUser(String), InvalidGroup(String), OmittingDirectory(PathBuf), + NotADirectory(PathBuf), } impl UError for InstallError { @@ -120,6 +121,9 @@ impl Display for InstallError { Self::InvalidUser(user) => write!(f, "invalid user: {}", user.quote()), Self::InvalidGroup(group) => write!(f, "invalid group: {}", group.quote()), Self::OmittingDirectory(dir) => write!(f, "omitting directory {}", dir.quote()), + Self::NotADirectory(dir) => { + write!(f, "failed to access {}: Not a directory", dir.quote()) + } } } } @@ -583,6 +587,13 @@ fn standard(mut paths: Vec, b: &Behavior) -> UResult<()> { } } } + if b.target_dir.is_some() { + let p = to_create.unwrap(); + + if !p.exists() || !p.is_dir() { + return Err(InstallError::NotADirectory(p.to_path_buf()).into()); + } + } } if sources.len() > 1 || is_potential_directory_path(&target) { diff --git a/tests/by-util/test_install.rs b/tests/by-util/test_install.rs index 7387748c6..9c8f929f4 100644 --- a/tests/by-util/test_install.rs +++ b/tests/by-util/test_install.rs @@ -1538,3 +1538,27 @@ fn test_install_compare_option() { .code_is(1) .stderr_contains("Options --compare and --strip are mutually exclusive"); } + +#[test] +// Matches part of tests/install/basic-1 +fn test_t_exist_dir() { + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; + + let source1 = "file"; + let target_dir = "sub4/"; + let target_file = "sub4/file_exists"; + + at.touch(source1); + at.mkdir(target_dir); + at.touch(target_file); + + scene + .ucmd() + .arg("-t") + .arg(target_file) + .arg("-Dv") + .arg(source1) + .fails() + .stderr_contains("failed to access 'sub4/file_exists': Not a directory"); +}