Skip to content

Commit

Permalink
fix: set executable bit on brioche binary if not already set (#112)
Browse files Browse the repository at this point in the history
* fix: set executable bit on brioche binary if not already set

The code changes in `self_update.rs` add logic to set the executable bit on the `brioche` binary if it's not already set. This ensures that the binary can be executed properly after a self update.

Signed-off-by: Jérémy Audiger <jeremy.audiger@icloud.com>

* refactor: apply the file permissions on the temporary file

Signed-off-by: Jérémy Audiger <jeremy.audiger@icloud.com>

---------

Signed-off-by: Jérémy Audiger <jeremy.audiger@icloud.com>
  • Loading branch information
jaudiger authored Aug 26, 2024
1 parent 153f9bf commit 3517aec
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions crates/brioche/src/self_update.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{
collections::HashMap,
io::{IsTerminal, Write as _},
os::unix::fs::PermissionsExt,
};

use anyhow::Context as _;
Expand Down Expand Up @@ -73,9 +74,30 @@ pub async fn self_update(args: SelfUpdateArgs) -> anyhow::Result<bool> {

println!("Downloaded update");

// Write the update to a temporary file
tokio::fs::write(&brioche_path_temp, new_update)
.await
.with_context(|| format!("failed to write update to {}", brioche_path_temp.display()))?;

// Set the executable bit if it's not already set
let mut permissions = tokio::fs::metadata(&brioche_path_temp)
.await
.with_context(|| format!("failed to get metadata for {}", brioche_path_temp.display()))?
.permissions();
const EXECUTABLE_BIT: u32 = 0o111;
if permissions.mode() & EXECUTABLE_BIT != EXECUTABLE_BIT {
permissions.set_mode(permissions.mode() | EXECUTABLE_BIT);
tokio::fs::set_permissions(&brioche_path_temp, permissions)
.await
.with_context(|| {
format!(
"failed to set executable bit on {}",
brioche_path_temp.display()
)
})?;
}

// Rename the temporary file to the actual file
tokio::fs::rename(&brioche_path_temp, &brioche_path)
.await
.with_context(|| {
Expand Down

0 comments on commit 3517aec

Please sign in to comment.