From 949106bc0938a0ba99baafbd3359d61d30952d73 Mon Sep 17 00:00:00 2001 From: ShirkNeko <109797057+ShirkNeko@users.noreply.github.com> Date: Mon, 14 Apr 2025 15:39:43 +0800 Subject: [PATCH] Refactoring KPM module loading logic, adding modification event handling, improving error handling --- userspace/ksud/src/kpm.rs | 62 +++++++++++++++++++++++++-------------- 1 file changed, 40 insertions(+), 22 deletions(-) diff --git a/userspace/ksud/src/kpm.rs b/userspace/ksud/src/kpm.rs index b730678b..0613e611 100644 --- a/userspace/ksud/src/kpm.rs +++ b/userspace/ksud/src/kpm.rs @@ -8,7 +8,6 @@ use std::ffi::OsStr; pub const KPM_DIR: &str = "/data/adb/kpm"; pub const KPMMGR_PATH: &str = "/data/adb/ksu/bin/kpmmgr"; - // 确保 KPM 目录存在,如果不存在则创建 pub fn ensure_kpm_dir() -> Result<()> { if !Path::new(KPM_DIR).exists() { @@ -74,29 +73,22 @@ fn handle_remove_event(paths: Vec) { } } +fn handle_modify_event(paths: Vec) { + for path in paths { + log::info!("Modified file: {}", path.display()); + } +} + // 加载 KPM 模块 -fn load_kpm_modules() -> Result<()> { - if !Path::new(kpm::KPM_DIR).exists() { - log::warn!("KPM directory does not exist: {}", kpm::KPM_DIR); - return Ok(()); +pub fn load_kpm(path: &Path) -> Result<()> { + let path_str = path.to_str().ok_or_else(|| anyhow!("Invalid path: {}", path.display()))?; + let status = std::process::Command::new(KPMMGR_PATH) + .args(["load", path_str, ""]) + .status()?; + + if status.success() { + log::info!("Loaded KPM: {}", path.display()); } - - for entry in std::fs::read_dir(kpm::KPM_DIR)? { - let path = entry?.path(); - if let Some(file_name) = path.file_stem() { - if let Some(file_name_str) = file_name.to_str() { - log::info!("Loading KPM module: {}", file_name_str); - } - } - - if path.extension().is_some_and(|ext| ext == "kpm") { - match kpm::load_kpm(&path) { - Ok(()) => log::info!("Successfully loaded KPM module: {}", path.display()), - Err(e) => log::warn!("Failed to load KPM module {}: {}", path.display(), e), - } - } - } - Ok(()) } @@ -175,5 +167,31 @@ pub fn load_existing_kpms() -> Result<()> { } } } + Ok(()) +} + +// 加载 KPM 模块 +pub fn load_kpm_modules() -> Result<()> { + if !Path::new(KPM_DIR).exists() { + log::warn!("KPM directory does not exist: {}", KPM_DIR); + return Ok(()); + } + + for entry in std::fs::read_dir(KPM_DIR)? { + let path = entry?.path(); + if let Some(file_name) = path.file_stem() { + if let Some(file_name_str) = file_name.to_str() { + log::info!("Loading KPM module: {}", file_name_str); + } + } + + if path.extension().is_some_and(|ext| ext == "kpm") { + match load_kpm(&path) { + Ok(()) => log::info!("Successfully loaded KPM module: {}", path.display()), + Err(e) => log::warn!("Failed to load KPM module {}: {}", path.display(), e), + } + } + } + Ok(()) } \ No newline at end of file