From dc533a915a86a367f303f6b79332d50325a30535 Mon Sep 17 00:00:00 2001 From: Ulrich Hornung Date: Wed, 17 Jan 2024 10:07:34 +0100 Subject: [PATCH] install: fix strip program stdout and destination hyphen handling #5718 (#5848) * Fix missing dependency to "process" to make it compile. * fix issue of not forwarding stdout from strip program * fix issue of applying "./" redundantly * cargo fmt --- src/uu/install/Cargo.toml | 1 + src/uu/install/src/install.rs | 22 +++++++++------------- tests/by-util/test_install.rs | 15 ++++++++++++++- 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/src/uu/install/Cargo.toml b/src/uu/install/Cargo.toml index 011038006..cc687efdb 100644 --- a/src/uu/install/Cargo.toml +++ b/src/uu/install/Cargo.toml @@ -25,6 +25,7 @@ uucore = { workspace = true, features = [ "mode", "perms", "entries", + "process", ] } [[bin]] diff --git a/src/uu/install/src/install.rs b/src/uu/install/src/install.rs index e568149ac..9955be7b2 100644 --- a/src/uu/install/src/install.rs +++ b/src/uu/install/src/install.rs @@ -776,27 +776,23 @@ fn copy_file(from: &Path, to: &Path) -> UResult<()> { /// fn strip_file(to: &Path, b: &Behavior) -> UResult<()> { // Check if the filename starts with a hyphen and adjust the path - let to = if to - .file_name() - .unwrap_or_default() - .to_str() - .unwrap_or_default() - .starts_with('-') - { + let to_str = to.as_os_str().to_str().unwrap_or_default(); + let to = if to_str.starts_with('-') { let mut new_path = PathBuf::from("."); new_path.push(to); new_path } else { to.to_path_buf() }; - match process::Command::new(&b.strip_program).arg(&to).output() { - Ok(o) => { - if !o.status.success() { + match process::Command::new(&b.strip_program).arg(&to).status() { + Ok(status) => { + if !status.success() { // Follow GNU's behavior: if strip fails, removes the target let _ = fs::remove_file(to); - return Err(InstallError::StripProgramFailed( - String::from_utf8(o.stderr).unwrap_or_default(), - ) + return Err(InstallError::StripProgramFailed(format!( + "strip process terminated abnormally - exit code: {}", + status.code().unwrap() + )) .into()); } } diff --git a/tests/by-util/test_install.rs b/tests/by-util/test_install.rs index 3db25c81f..6b1d76e55 100644 --- a/tests/by-util/test_install.rs +++ b/tests/by-util/test_install.rs @@ -699,7 +699,20 @@ fn test_install_and_strip_with_program_hyphen() { .arg("src") .arg("-dest") .succeeds() - .no_stderr(); + .no_stderr() + .stdout_is("./-dest\n"); + + scene + .ucmd() + .arg("-s") + .arg("--strip-program") + .arg("./no-hyphen") + .arg("--") + .arg("src") + .arg("./-dest") + .succeeds() + .no_stderr() + .stdout_is("./-dest\n"); } #[test]