ksud: Fix macOS compile

This commit is contained in:
tiann
2023-02-03 14:46:48 +08:00
parent 8ee00839dc
commit 71cc166f72
6 changed files with 30 additions and 30 deletions

View File

@@ -32,7 +32,7 @@ rust-embed = { version = "6.4.2", features = [
"compression", # must clean build after updating binaries "compression", # must clean build after updating binaries
] } ] }
[target.'cfg(unix)'.dependencies] [target.'cfg(linux)'.dependencies]
sys-mount = "2.0.1" sys-mount = "2.0.1"
# some android specific dependencies which compiles under unix are also listed here for convenience of coding # some android specific dependencies which compiles under unix are also listed here for convenience of coding
android-properties = { version = "0.2.2", features = ["bionic-deprecated"] } android-properties = { version = "0.2.2", features = ["bionic-deprecated"] }

View File

@@ -20,7 +20,7 @@ pub const CMD_SET_SEPOLICY: u64 = 8;
const EVENT_POST_FS_DATA: u64 = 1; const EVENT_POST_FS_DATA: u64 = 1;
const EVENT_BOOT_COMPLETED: u64 = 2; const EVENT_BOOT_COMPLETED: u64 = 2;
#[cfg(unix)] #[cfg(target_os = "linux")]
pub fn grant_root() -> Result<()> { pub fn grant_root() -> Result<()> {
let mut result: u32 = 0; let mut result: u32 = 0;
unsafe { unsafe {
@@ -38,14 +38,14 @@ pub fn grant_root() -> Result<()> {
Err(std::process::Command::new("sh").exec().into()) Err(std::process::Command::new("sh").exec().into())
} }
#[cfg(not(unix))] #[cfg(not(target_os = "linux"))]
pub fn grant_root() -> Result<()> { pub fn grant_root() -> Result<()> {
unimplemented!("grant_root is only available on android"); unimplemented!("grant_root is only available on android");
} }
pub fn get_version() -> i32 { pub fn get_version() -> i32 {
let mut result: i32 = 0; let mut result: i32 = 0;
#[cfg(unix)] #[cfg(target_os = "linux")]
unsafe { unsafe {
#[allow(clippy::cast_possible_wrap)] #[allow(clippy::cast_possible_wrap)]
libc::prctl( libc::prctl(
@@ -58,7 +58,7 @@ pub fn get_version() -> i32 {
} }
fn report_event(event: u64) { fn report_event(event: u64) {
#[cfg(unix)] #[cfg(target_os = "linux")]
unsafe { unsafe {
#[allow(clippy::cast_possible_wrap)] #[allow(clippy::cast_possible_wrap)]
libc::prctl( libc::prctl(

View File

@@ -1,21 +1,21 @@
use anyhow::Result; use anyhow::Result;
#[cfg(unix)] #[cfg(target_os = "linux")]
use anyhow::{Context, Ok}; use anyhow::{Context, Ok};
#[cfg(unix)] #[cfg(target_os = "linux")]
use retry::delay::NoDelay; use retry::delay::NoDelay;
#[cfg(unix)] #[cfg(target_os = "linux")]
use sys_mount::{unmount, FilesystemType, Mount, MountFlags, Unmount, UnmountFlags}; use sys_mount::{unmount, FilesystemType, Mount, MountFlags, Unmount, UnmountFlags};
pub struct AutoMountExt4 { pub struct AutoMountExt4 {
mnt: String, mnt: String,
#[cfg(unix)] #[cfg(target_os = "linux")]
mount: Option<Mount>, mount: Option<Mount>,
auto_umount: bool, auto_umount: bool,
} }
impl AutoMountExt4 { impl AutoMountExt4 {
#[cfg(unix)] #[cfg(target_os = "linux")]
pub fn try_new(src: &str, mnt: &str, auto_umount: bool) -> Result<Self> { pub fn try_new(src: &str, mnt: &str, auto_umount: bool) -> Result<Self> {
let result = Mount::builder() let result = Mount::builder()
.fstype(FilesystemType::from("ext4")) .fstype(FilesystemType::from("ext4"))
@@ -52,12 +52,12 @@ impl AutoMountExt4 {
} }
} }
#[cfg(not(unix))] #[cfg(not(target_os = "linux"))]
pub fn try_new(_src: &str, _mnt: &str, _auto_umount: bool) -> Result<Self> { pub fn try_new(_src: &str, _mnt: &str, _auto_umount: bool) -> Result<Self> {
unimplemented!() unimplemented!()
} }
#[cfg(unix)] #[cfg(target_os = "linux")]
pub fn umount(&self) -> Result<()> { pub fn umount(&self) -> Result<()> {
if let Some(ref mount) = self.mount { if let Some(ref mount) = self.mount {
mount mount
@@ -74,7 +74,7 @@ impl AutoMountExt4 {
} }
} }
#[cfg(unix)] #[cfg(target_os = "linux")]
impl Drop for AutoMountExt4 { impl Drop for AutoMountExt4 {
fn drop(&mut self) { fn drop(&mut self) {
log::info!( log::info!(
@@ -89,7 +89,7 @@ impl Drop for AutoMountExt4 {
} }
#[allow(dead_code)] #[allow(dead_code)]
#[cfg(unix)] #[cfg(target_os = "linux")]
fn mount_image(src: &str, target: &str, autodrop: bool) -> Result<()> { fn mount_image(src: &str, target: &str, autodrop: bool) -> Result<()> {
if autodrop { if autodrop {
Mount::builder() Mount::builder()
@@ -106,7 +106,7 @@ fn mount_image(src: &str, target: &str, autodrop: bool) -> Result<()> {
} }
#[allow(dead_code)] #[allow(dead_code)]
#[cfg(unix)] #[cfg(target_os = "linux")]
pub fn mount_ext4(src: &str, target: &str, autodrop: bool) -> Result<()> { pub fn mount_ext4(src: &str, target: &str, autodrop: bool) -> Result<()> {
// umount target first. // umount target first.
let _ = umount_dir(target); let _ = umount_dir(target);
@@ -116,13 +116,13 @@ pub fn mount_ext4(src: &str, target: &str, autodrop: bool) -> Result<()> {
.map(|_| ()) .map(|_| ())
} }
#[cfg(unix)] #[cfg(target_os = "linux")]
pub fn umount_dir(src: &str) -> Result<()> { pub fn umount_dir(src: &str) -> Result<()> {
unmount(src, UnmountFlags::empty()).with_context(|| format!("Failed to umount {src}"))?; unmount(src, UnmountFlags::empty()).with_context(|| format!("Failed to umount {src}"))?;
Ok(()) Ok(())
} }
#[cfg(unix)] #[cfg(target_os = "linux")]
pub fn mount_overlay(lowerdir: &str, mnt: &str) -> Result<()> { pub fn mount_overlay(lowerdir: &str, mnt: &str) -> Result<()> {
Mount::builder() Mount::builder()
.fstype(FilesystemType::from("overlay")) .fstype(FilesystemType::from("overlay"))
@@ -133,17 +133,17 @@ pub fn mount_overlay(lowerdir: &str, mnt: &str) -> Result<()> {
.map_err(|e| anyhow::anyhow!("mount partition: {mnt} overlay failed: {e}")) .map_err(|e| anyhow::anyhow!("mount partition: {mnt} overlay failed: {e}"))
} }
#[cfg(not(unix))] #[cfg(not(target_os = "linux"))]
pub fn mount_ext4(_src: &str, _target: &str, _autodrop: bool) -> Result<()> { pub fn mount_ext4(_src: &str, _target: &str, _autodrop: bool) -> Result<()> {
unimplemented!() unimplemented!()
} }
#[cfg(not(unix))] #[cfg(not(target_os = "linux"))]
pub fn umount_dir(_src: &str) -> Result<()> { pub fn umount_dir(_src: &str) -> Result<()> {
unimplemented!() unimplemented!()
} }
#[cfg(not(unix))] #[cfg(not(target_os = "linux"))]
pub fn mount_overlay(_lowerdir: &str, _mnt: &str) -> Result<()> { pub fn mount_overlay(_lowerdir: &str, _mnt: &str) -> Result<()> {
unimplemented!() unimplemented!()
} }

View File

@@ -2,9 +2,9 @@ use anyhow::Result;
use jwalk::{Parallelism::Serial, WalkDir}; use jwalk::{Parallelism::Serial, WalkDir};
use std::path::Path; use std::path::Path;
#[cfg(unix)] #[cfg(target_os = "linux")]
use anyhow::{Context, Ok}; use anyhow::{Context, Ok};
#[cfg(unix)] #[cfg(target_os = "linux")]
use extattr::{setxattr, Flags as XattrFlags}; use extattr::{setxattr, Flags as XattrFlags};
const SYSTEM_CON: &str = "u:object_r:system_file:s0"; const SYSTEM_CON: &str = "u:object_r:system_file:s0";
@@ -12,18 +12,18 @@ const _ADB_CON: &str = "u:object_r:adb_data_file:s0";
const SELINUX_XATTR : &str = "security.selinux"; const SELINUX_XATTR : &str = "security.selinux";
pub fn setcon<P: AsRef<Path>>(path: P, con: &str) -> Result<()> { pub fn setcon<P: AsRef<Path>>(path: P, con: &str) -> Result<()> {
#[cfg(unix)] #[cfg(target_os = "linux")]
setxattr(&path, SELINUX_XATTR, con, XattrFlags::empty()) setxattr(&path, SELINUX_XATTR, con, XattrFlags::empty())
.with_context(|| format!("Failed to change SELinux context for {}", path.as_ref().display()))?; .with_context(|| format!("Failed to change SELinux context for {}", path.as_ref().display()))?;
Ok(()) Ok(())
} }
#[cfg(unix)] #[cfg(target_os = "linux")]
pub fn setsyscon<P: AsRef<Path>>(path: P) -> Result<()> { pub fn setsyscon<P: AsRef<Path>>(path: P) -> Result<()> {
setcon(path, SYSTEM_CON) setcon(path, SYSTEM_CON)
} }
#[cfg(not(unix))] #[cfg(not(target_os = "linux"))]
pub fn setsyscon<P: AsRef<Path>>(path: P) -> Result<()> { pub fn setsyscon<P: AsRef<Path>>(path: P) -> Result<()> {
unimplemented!() unimplemented!()
} }
@@ -31,7 +31,7 @@ pub fn setsyscon<P: AsRef<Path>>(path: P) -> Result<()> {
pub fn restore_syscon<P: AsRef<Path>>(dir: P) -> Result<()> { pub fn restore_syscon<P: AsRef<Path>>(dir: P) -> Result<()> {
for dir_entry in WalkDir::new(dir).parallelism(Serial) { for dir_entry in WalkDir::new(dir).parallelism(Serial) {
if let Some(path) = dir_entry.ok().map(|dir_entry| dir_entry.path()) { if let Some(path) = dir_entry.ok().map(|dir_entry| dir_entry.path()) {
#[cfg(unix)] #[cfg(target_os = "linux")]
setxattr(&path, SELINUX_XATTR, SYSTEM_CON, XattrFlags::empty()).with_context( setxattr(&path, SELINUX_XATTR, SYSTEM_CON, XattrFlags::empty()).with_context(
|| format!("Failed to change SELinux context for {}", path.display()), || format!("Failed to change SELinux context for {}", path.display()),
)?; )?;

View File

@@ -691,7 +691,7 @@ impl From<AtomicStatement> for FfiPolicy {
} }
} }
#[cfg(unix)] #[cfg(target_os = "linux")]
fn apply_one_rule<'a>(statement: &'a PolicyStatement<'a>, strict: bool) -> Result<()> { fn apply_one_rule<'a>(statement: &'a PolicyStatement<'a>, strict: bool) -> Result<()> {
let policies: Vec<AtomicStatement> = statement.try_into()?; let policies: Vec<AtomicStatement> = statement.try_into()?;
@@ -720,7 +720,7 @@ fn apply_one_rule<'a>(statement: &'a PolicyStatement<'a>, strict: bool) -> Resul
Ok(()) Ok(())
} }
#[cfg(not(unix))] #[cfg(not(target_os = "linux"))]
fn apply_one_rule<'a>(_statement: &'a PolicyStatement<'a>, _strict: bool) -> Result<()> { fn apply_one_rule<'a>(_statement: &'a PolicyStatement<'a>, _strict: bool) -> Result<()> {
unimplemented!() unimplemented!()
} }

View File

@@ -63,12 +63,12 @@ pub fn ensure_binary<T: AsRef<Path>>(path: T, contents: &[u8]) -> Result<()> {
Ok(()) Ok(())
} }
#[cfg(unix)] #[cfg(target_os = "linux")]
pub fn getprop(prop: &str) -> Option<String> { pub fn getprop(prop: &str) -> Option<String> {
android_properties::getprop(prop).value() android_properties::getprop(prop).value()
} }
#[cfg(not(unix))] #[cfg(not(target_os = "linux"))]
pub fn getprop(_prop: &str) -> Option<String> { pub fn getprop(_prop: &str) -> Option<String> {
unimplemented!() unimplemented!()
} }