Skip to content

Commit

Permalink
install: support when a hyphen is passed
Browse files Browse the repository at this point in the history
Should fix: tests/install/strip-program.sh
  • Loading branch information
sylvestre committed Dec 23, 2023
1 parent 18388c0 commit f8335a9
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
23 changes: 20 additions & 3 deletions src/uu/install/src/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -749,11 +749,28 @@ fn copy_file(from: &Path, to: &Path) -> UResult<()> {
/// Returns an empty Result or an error in case of failure.
///
fn strip_file(to: &Path, b: &Behavior) -> UResult<()> {
match process::Command::new(&b.strip_program).arg(to).output() {
// Check if the filename starts with a hyphen and adjust it
let safe_to = if to
.file_name()
.unwrap_or_default()
.to_str()
.unwrap_or_default()
.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(&safe_to)
.output()
{
Ok(o) => {
if !o.status.success() {
// Follow GNU's behavior: if strip fails, removes the target
let _ = fs::remove_file(to);
let _ = fs::remove_file(safe_to);
return Err(InstallError::StripProgramFailed(
String::from_utf8(o.stderr).unwrap_or_default(),
)
Expand All @@ -762,7 +779,7 @@ fn strip_file(to: &Path, b: &Behavior) -> UResult<()> {
}
Err(e) => {
// Follow GNU's behavior: if strip fails, removes the target
let _ = fs::remove_file(to);
let _ = fs::remove_file(safe_to);
return Err(InstallError::StripProgramFailed(e.to_string()).into());
}
}
Expand Down
28 changes: 28 additions & 0 deletions tests/by-util/test_install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,34 @@ fn test_install_and_strip_with_program() {
assert!(!stdout.contains(STRIP_SOURCE_FILE_SYMBOL));
}

#[cfg(all(unix, feature = "chmod"))]
#[test]
// FixME: Freebsd fails on 'No such file or directory'
#[cfg(not(any(windows, target_os = "freebsd")))]
fn test_install_and_strip_with_program_hyphen() {
let scene = TestScenario::new(util_name!());

let at = &scene.fixtures;
let content = r#"#!/bin/sh
echo $1 &> /tmp/a.log
printf -- '%s\n' "$1" | grep '^[^-]'
"#;
at.write("no-hyphen", content);
scene.ccmd("chmod").arg("+x").arg("no-hyphen").succeeds();

at.touch("src");
scene
.ucmd()
.arg("-s")
.arg("--strip-program")
.arg("./no-hyphen")
.arg("--")
.arg("src")
.arg("-dest")
.succeeds()
.no_stderr();
}

#[test]
#[cfg(not(windows))]
fn test_install_and_strip_with_invalid_program() {
Expand Down

0 comments on commit f8335a9

Please sign in to comment.