diff --git a/crates/detection/Cargo.toml b/crates/detection/Cargo.toml index 8a2903c..fe9fbb6 100644 --- a/crates/detection/Cargo.toml +++ b/crates/detection/Cargo.toml @@ -3,5 +3,5 @@ name = "humanode-distribution-detection" version = "0.1.0" edition = "2021" -[dependencies] +[target.'cfg(not(windows))'.dependencies] libc = "0.2" diff --git a/crates/detection/src/implementation/default.rs b/crates/detection/src/implementation/default.rs new file mode 100644 index 0000000..cee292f --- /dev/null +++ b/crates/detection/src/implementation/default.rs @@ -0,0 +1,26 @@ +//! The default implementation. + +use std::ffi::{c_char, CStr}; + +use crate::Info; + +/// Obtain the system information. +#[allow(unsafe_code)] +pub fn detect() -> std::io::Result { + let mut value = unsafe { std::mem::zeroed() }; + if unsafe { libc::uname(&mut value) } != 0 { + return Err(std::io::Error::last_os_error()); + } + Ok(Info { + platform: to_string(&value.sysname[..]), + arch: to_string(&value.machine[..]), + }) +} + +/// Assume a valid C string and copy it into a new Rust string with a lossy +/// UTF-8 conversion. +#[allow(unsafe_code)] +fn to_string(buf: &[c_char]) -> String { + let c_str = unsafe { CStr::from_ptr(buf.as_ptr()) }; + c_str.to_string_lossy().into_owned() +} diff --git a/crates/detection/src/implementation/windows.rs b/crates/detection/src/implementation/windows.rs new file mode 100644 index 0000000..d682dc0 --- /dev/null +++ b/crates/detection/src/implementation/windows.rs @@ -0,0 +1,15 @@ +//! Windows specific implementation. + +use crate::Info; + +/// Obtain the system information. +pub fn detect() -> std::io::Result { + #[cfg(target_arch = "x86_64")] + let arch = "x86_64"; + #[cfg(target_arch = "aarch64")] + let arch = "aarch64"; + Ok(Info { + platform: "Windows".into(), + arch: arch.into(), + }) +} diff --git a/crates/detection/src/lib.rs b/crates/detection/src/lib.rs index 5700573..7e29bd2 100644 --- a/crates/detection/src/lib.rs +++ b/crates/detection/src/lib.rs @@ -1,6 +1,12 @@ //! The detection machinery. -use std::ffi::{c_char, CStr}; +#[cfg(not(windows))] +#[path = "implementation/default.rs"] +mod implementation; + +#[cfg(windows)] +#[path = "implementation/windows.rs"] +mod implementation; /// Info about the system. #[derive(Debug)] @@ -12,22 +18,6 @@ pub struct Info { } /// Obtain the system information. -#[allow(unsafe_code)] pub fn detect() -> std::io::Result { - let mut value = unsafe { std::mem::zeroed() }; - if unsafe { libc::uname(&mut value) } != 0 { - return Err(std::io::Error::last_os_error()); - } - Ok(Info { - platform: to_string(&value.sysname[..]), - arch: to_string(&value.machine[..]), - }) -} - -/// Assume a valid C string and copy it into a new Rust string with a lossy -/// UTF-8 conversion. -#[allow(unsafe_code)] -fn to_string(buf: &[c_char]) -> String { - let c_str = unsafe { CStr::from_ptr(buf.as_ptr()) }; - c_str.to_string_lossy().into_owned() + self::implementation::detect() } diff --git a/crates/installer/src/install.rs b/crates/installer/src/install.rs index 666d432..d149ac4 100644 --- a/crates/installer/src/install.rs +++ b/crates/installer/src/install.rs @@ -1,6 +1,6 @@ //! Installation logic. -use std::{fs::Permissions, path::PathBuf}; +use std::path::PathBuf; use digest::Digest; use humanode_distribution_schema::manifest::Binary; @@ -190,7 +190,8 @@ pub async fn install(params: Params) -> Result<(), InstallationError> { ]; for executable in executables { let path = base_path.join(executable.0); - use std::os::unix::prelude::PermissionsExt; + + use std::{fs::Permissions, os::unix::prelude::PermissionsExt}; tokio::fs::set_permissions(&path, Permissions::from_mode(0o755)) .await .map_err(|error| InstallationError::SetFilePermissions { path, error })?;