From b936ce9696515023632750b48a9e8984268f2b67 Mon Sep 17 00:00:00 2001 From: Lucien Greathouse Date: Tue, 24 May 2022 19:31:53 -0400 Subject: [PATCH] Mark executables as executable on Unix (#14) * Mark executables as executable on Unix * Update changelog --- CHANGELOG.md | 3 +++ src/tool_storage.rs | 33 +++++++++++++++++++++++---------- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 23f58ce..039ea72 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,9 @@ # Aftman Changelog ## Unreleased Changes +* Aftman now correctly marks executables as executable on Unix platforms. ([#14]) + +[#14]: https://github.com/LPGhatguy/aftman/pull/14 ## [0.2.2] (May 23, 2022) * Fixed building on non-Windows platforms diff --git a/src/tool_storage.rs b/src/tool_storage.rs index 99e337c..a51923c 100644 --- a/src/tool_storage.rs +++ b/src/tool_storage.rs @@ -320,6 +320,27 @@ impl ToolStorage { Ok(()) } + fn install_executable(&self, id: &ToolId, mut contents: impl Read) -> anyhow::Result<()> { + let output_path = self.exe_path(id); + + fs_err::create_dir_all(output_path.parent().unwrap())?; + + let mut output = BufWriter::new(File::create(&output_path)?); + io::copy(&mut contents, &mut output)?; + output.flush()?; + + #[cfg(unix)] + { + use std::fs::{set_permissions, Permissions}; + use std::os::unix::fs::PermissionsExt; + + set_permissions(&output_path, Permissions::from_mode(0o755)) + .context("failed to mark executable as executable")?; + } + + Ok(()) + } + fn install_artifact(&self, id: &ToolId, artifact: impl Read + Seek) -> anyhow::Result<()> { let output_path = self.exe_path(id); let expected_name = format!("{}{EXE_SUFFIX}", id.name().name()); @@ -333,11 +354,7 @@ impl ToolStorage { if file.name() == expected_name { log::debug!("Installing file {} from archive...", file.name()); - - let mut output = BufWriter::new(File::create(&output_path)?); - io::copy(&mut file, &mut output)?; - output.flush()?; - + self.install_executable(id, &mut file)?; return Ok(()); } } @@ -349,11 +366,7 @@ impl ToolStorage { if file.name().ends_with(EXE_SUFFIX) { log::debug!("Installing file {} from archive...", file.name()); - - let mut output = BufWriter::new(File::create(&output_path)?); - io::copy(&mut file, &mut output)?; - output.flush()?; - + self.install_executable(id, &mut file)?; return Ok(()); } }