ksud: make it compilable for non-android

This commit is contained in:
tiann
2023-02-01 19:04:33 +08:00
parent 1cd18a643d
commit 619dd1ace1
10 changed files with 97 additions and 51 deletions

View File

@@ -1,4 +1,5 @@
use anyhow::{Context, Ok, Result};
#[cfg(target_os = "android")]
use extattr::{setxattr, Flags as XattrFlags};
use jwalk::{Parallelism::Serial, WalkDir};
@@ -6,6 +7,7 @@ const SYSTEM_CON: &str = "u:object_r:system_file:s0";
const _ADB_CON: &str = "u:object_r:adb_data_file:s0";
pub fn setcon(path: &str, con: &str) -> Result<()> {
#[cfg(target_os = "android")]
setxattr(path, "security.selinux", con, XattrFlags::empty())
.with_context(|| format!("Failed to change SELinux context for {path}"))?;
Ok(())
@@ -18,6 +20,7 @@ pub fn setsyscon(path: &str) -> Result<()> {
pub fn restore_syscon(dir: &str) -> Result<()> {
for dir_entry in WalkDir::new(dir).parallelism(Serial) {
if let Some(path) = dir_entry.ok().map(|dir_entry| dir_entry.path()) {
#[cfg(target_os = "android")]
setxattr(&path, "security.selinux", SYSTEM_CON, XattrFlags::empty()).with_context(
|| {
format!(