Detect universal binaries and +x them

This commit is contained in:
caesay
2024-06-01 13:05:39 +01:00
parent f32d6f88ff
commit 6575031442
6 changed files with 55 additions and 24 deletions

7
src/Rust/Cargo.lock generated
View File

@@ -122,12 +122,6 @@ version = "0.21.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9d297deb1925b89f2ccc13d7635fa0714f12c87adce1c75356b39ca9b7178567"
[[package]]
name = "bindet"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5afee99ef5f7527f3944f2bf4f5d443749fa47d43eb1d4f83a36e839be7900a3"
[[package]]
name = "bitflags"
version = "1.3.2"
@@ -1575,7 +1569,6 @@ name = "velopack"
version = "0.0.0-local"
dependencies = [
"anyhow",
"bindet",
"chrono",
"clap",
"derivative",

View File

@@ -82,7 +82,6 @@ time = "0.3"
[target.'cfg(unix)'.dependencies]
native-dialog = "0.7"
waitpid-any = "0.2"
bindet = "0.3"
dialog = "0.3"
libc = "0.2"

View File

@@ -305,21 +305,11 @@ impl BundleInfo<'_> {
// on macos, we need to chmod +x the executable files
#[cfg(target_os = "macos")]
{
if let Ok(file) = std::fs::OpenOptions::new().read(true).open(&file_path_on_disk) {
let buf = std::io::BufReader::new(file);
if let Ok(det) = bindet::detect(buf).map_err(|e| e.kind()) {
if let Some(matches) = det {
for m in matches.likely_to_be {
if m == bindet::FileType::Mach {
if let Err(e) = std::fs::set_permissions(&file_path_on_disk, std::fs::Permissions::from_mode(0o755)) {
warn!("Failed to set executable permissions on '{}': {}", file_path_on_disk.to_string_lossy(), e);
} else {
info!(" {} Set executable permissions on '{}'", i, file_path_on_disk.to_string_lossy());
}
break;
}
}
}
if let Ok(true) = super::macho::is_macho_image(&file_path_on_disk) {
if let Err(e) = std::fs::set_permissions(&file_path_on_disk, std::fs::Permissions::from_mode(0o755)) {
warn!("Failed to set executable permissions on '{}': {}", file_path_on_disk.to_string_lossy(), e);
} else {
info!(" {} Set executable permissions on '{}'", i, file_path_on_disk.to_string_lossy());
}
}
}

View File

@@ -0,0 +1,44 @@
use std::fs::File;
use std::io::{self, Read};
use std::path::Path;
#[derive(Debug)]
#[allow(non_camel_case_types)]
enum MagicMachO {
MH_MAGIC = 0xfeedface,
MH_CIGAM = 0xcefaedfe,
MH_MAGIC_64 = 0xfeedfacf,
MH_CIGAM_64 = 0xcffaedfe,
// https://developer.apple.com/documentation/kernel/fat_header/1558632-magic/
// https://opensource.apple.com/source/file/file-80.40.2/file/magic/Magdir/cafebabe.auto.html
FAT_MAGIC = 0xcafebabe,
FAT_CIGAM = 0xbebafeca,
}
impl MagicMachO {
fn from_u32(value: u32) -> Option<Self> {
match value {
0xfeedface => Some(MagicMachO::MH_MAGIC),
0xcefaedfe => Some(MagicMachO::MH_CIGAM),
0xfeedfacf => Some(MagicMachO::MH_MAGIC_64),
0xcffaedfe => Some(MagicMachO::MH_CIGAM_64),
0xcafebabe => Some(MagicMachO::FAT_MAGIC),
0xbebafeca => Some(MagicMachO::FAT_CIGAM),
_ => None,
}
}
}
pub fn is_macho_image<P: AsRef<Path>>(file_path: P) -> io::Result<bool> {
let file_path = file_path.as_ref();
let mut file = File::open(file_path)?;
let mut buffer = [0; 4];
if file.metadata()?.len() < 256 {
return Ok(false);
}
file.read_exact(&mut buffer)?;
let magic = u32::from_be_bytes(buffer);
Ok(MagicMachO::from_u32(magic).is_some())
}

View File

@@ -1,5 +1,6 @@
pub mod bundle;
pub mod download;
pub mod macho;
mod dialogs_const;
mod dialogs_common;

View File

@@ -7,7 +7,11 @@ public class MachO
MH_MAGIC = 0xfeedface,
MH_CIGAM = 0xcefaedfe,
MH_MAGIC_64 = 0xfeedfacf,
MH_CIGAM_64 = 0xcffaedfe
MH_CIGAM_64 = 0xcffaedfe,
// https://developer.apple.com/documentation/kernel/fat_header/1558632-magic/
// https://opensource.apple.com/source/file/file-80.40.2/file/magic/Magdir/cafebabe.auto.html
FAT_MAGIC = 0xcafebabe,
FAT_CIGAM = 0xbebafeca,
}
public static bool IsMachOImage(string filePath)