ksud: tweaks for setcon

This commit is contained in:
tiann
2023-02-03 09:55:03 +08:00
parent a05edb3872
commit d80c282644

View File

@@ -1,5 +1,6 @@
use anyhow::Result; use anyhow::Result;
use jwalk::{Parallelism::Serial, WalkDir}; use jwalk::{Parallelism::Serial, WalkDir};
use std::path::Path;
#[cfg(unix)] #[cfg(unix)]
use anyhow::{Context, Ok}; use anyhow::{Context, Ok};
@@ -8,29 +9,30 @@ 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";
const _ADB_CON: &str = "u:object_r:adb_data_file:s0"; const _ADB_CON: &str = "u:object_r:adb_data_file:s0";
const SELINUX_XATTR : &str = "security.selinux";
pub fn setcon(path: &str, con: &str) -> Result<()> { pub fn setcon<P: AsRef<Path>>(path: P, con: &str) -> Result<()> {
#[cfg(unix)] #[cfg(unix)]
setxattr(path, "security.selinux", con, XattrFlags::empty()) setxattr(&path, SELINUX_XATTR, con, XattrFlags::empty())
.with_context(|| format!("Failed to change SELinux context for {path}"))?; .with_context(|| format!("Failed to change SELinux context for {}", path.as_ref().display()))?;
Ok(()) Ok(())
} }
#[cfg(unix)] #[cfg(unix)]
pub fn setsyscon(path: &str) -> Result<()> { pub fn setsyscon<P: AsRef<Path>>(path: P) -> Result<()> {
setcon(path, SYSTEM_CON) setcon(path, SYSTEM_CON)
} }
#[cfg(not(unix))] #[cfg(not(unix))]
pub fn setsyscon(_path: &str) -> Result<()> { pub fn setsyscon<P: AsRef<Path>>(path: P) -> Result<()> {
unimplemented!() unimplemented!()
} }
pub fn restore_syscon(dir: &str) -> 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(unix)]
setxattr(&path, "security.selinux", 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()),
)?; )?;
} }