ksud: fix issues found by clippy (#167)

These issues are mostly found by `cargo clippy -- -W clippy::pedantic`.
This commit is contained in:
skbeh
2023-02-03 09:45:07 +08:00
committed by GitHub
parent bea93f6ad7
commit 219ea1c458
13 changed files with 217 additions and 245 deletions

View File

@@ -1,33 +1,37 @@
use anyhow::{Context, Ok, Result};
#[cfg(target_os = "android")]
use extattr::{setxattr, Flags as XattrFlags};
use anyhow::Result;
use jwalk::{Parallelism::Serial, WalkDir};
#[cfg(unix)]
use anyhow::{Context, Ok};
#[cfg(unix)]
use extattr::{setxattr, Flags as XattrFlags};
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")]
#[cfg(unix)]
setxattr(path, "security.selinux", con, XattrFlags::empty())
.with_context(|| format!("Failed to change SELinux context for {path}"))?;
Ok(())
}
#[cfg(unix)]
pub fn setsyscon(path: &str) -> Result<()> {
setcon(path, SYSTEM_CON)
}
#[cfg(not(unix))]
pub fn setsyscon(_path: &str) -> Result<()> {
unimplemented!()
}
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")]
#[cfg(unix)]
setxattr(&path, "security.selinux", SYSTEM_CON, XattrFlags::empty()).with_context(
|| {
format!(
"Failed to change SELinux context for {}",
path.to_str().unwrap()
)
},
|| format!("Failed to change SELinux context for {}", path.display()),
)?;
}
}