From aa20d04d3a72669cb2ac2bf4955209377786e3cf Mon Sep 17 00:00:00 2001 From: ShirkNeko <109797057+ShirkNeko@users.noreply.github.com> Date: Mon, 14 Apr 2025 15:51:47 +0800 Subject: [PATCH] Refactoring KPM module loading logic, removing existing KPM loading functions, simplifying code and enhancing error handling --- userspace/ksud/src/init_event.rs | 19 +------------------ userspace/ksud/src/kpm.rs | 28 ++++++---------------------- 2 files changed, 7 insertions(+), 40 deletions(-) diff --git a/userspace/ksud/src/init_event.rs b/userspace/ksud/src/init_event.rs index 7885788e..a1d6d662 100644 --- a/userspace/ksud/src/init_event.rs +++ b/userspace/ksud/src/init_event.rs @@ -102,24 +102,7 @@ pub fn on_post_data_fs() -> Result<()> { run_stage("post-mount", true); // load kpm modules - 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() { - if file_name_str.is_empty() { - log::warn!("Invalid KPM file name: {}", path.display()); - continue; - } - } - } - - 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), - } - } - } + load_kpm_modules()?; Ok(()) } diff --git a/userspace/ksud/src/kpm.rs b/userspace/ksud/src/kpm.rs index 0613e611..92121258 100644 --- a/userspace/ksud/src/kpm.rs +++ b/userspace/ksud/src/kpm.rs @@ -18,7 +18,6 @@ pub fn ensure_kpm_dir() -> Result<()> { pub fn start_kpm_watcher() -> Result<()> { ensure_kpm_dir()?; - load_existing_kpms()?; // 检查是否处于安全模式 if crate::utils::is_safe_mode() { @@ -155,36 +154,21 @@ pub fn remove_all_kpms() -> Result<()> { Ok(()) } -// 系统启动后加载所有现有的 KPM 模块 -pub fn load_existing_kpms() -> Result<()> { - ensure_kpm_dir()?; - - for entry in fs::read_dir(KPM_DIR)? { - let path = entry?.path(); - if path.extension().map_or(false, |ext| ext == "kpm") { - if let Err(e) = load_kpm(&path) { - log::warn!("Failed to load {}: {}", path.display(), e); - } - } - } - 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(()); - } + ensure_kpm_dir()?; 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 file_name_str.is_empty() { + log::warn!("Invalid KPM file name: {}", path.display()); + continue; + } } } - + if path.extension().is_some_and(|ext| ext == "kpm") { match load_kpm(&path) { Ok(()) => log::info!("Successfully loaded KPM module: {}", path.display()),