Refactoring KPM module loading logic, removing existing KPM loading functions, simplifying code and enhancing error handling

This commit is contained in:
ShirkNeko
2025-04-14 15:51:47 +08:00
parent 949106bc09
commit aa20d04d3a
2 changed files with 7 additions and 40 deletions

View File

@@ -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(())
}

View File

@@ -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()),