Refactoring KPM module loading logic, removing existing KPM loading functions, simplifying code and enhancing error handling
This commit is contained in:
@@ -102,24 +102,7 @@ pub fn on_post_data_fs() -> Result<()> {
|
|||||||
run_stage("post-mount", true);
|
run_stage("post-mount", true);
|
||||||
|
|
||||||
// load kpm modules
|
// load kpm modules
|
||||||
for entry in std::fs::read_dir(kpm::KPM_DIR)? {
|
load_kpm_modules()?;
|
||||||
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),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,7 +18,6 @@ pub fn ensure_kpm_dir() -> Result<()> {
|
|||||||
|
|
||||||
pub fn start_kpm_watcher() -> Result<()> {
|
pub fn start_kpm_watcher() -> Result<()> {
|
||||||
ensure_kpm_dir()?;
|
ensure_kpm_dir()?;
|
||||||
load_existing_kpms()?;
|
|
||||||
|
|
||||||
// 检查是否处于安全模式
|
// 检查是否处于安全模式
|
||||||
if crate::utils::is_safe_mode() {
|
if crate::utils::is_safe_mode() {
|
||||||
@@ -155,33 +154,18 @@ pub fn remove_all_kpms() -> Result<()> {
|
|||||||
Ok(())
|
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 模块
|
// 加载 KPM 模块
|
||||||
pub fn load_kpm_modules() -> Result<()> {
|
pub fn load_kpm_modules() -> Result<()> {
|
||||||
if !Path::new(KPM_DIR).exists() {
|
ensure_kpm_dir()?;
|
||||||
log::warn!("KPM directory does not exist: {}", KPM_DIR);
|
|
||||||
return Ok(());
|
|
||||||
}
|
|
||||||
|
|
||||||
for entry in std::fs::read_dir(KPM_DIR)? {
|
for entry in std::fs::read_dir(KPM_DIR)? {
|
||||||
let path = entry?.path();
|
let path = entry?.path();
|
||||||
if let Some(file_name) = path.file_stem() {
|
if let Some(file_name) = path.file_stem() {
|
||||||
if let Some(file_name_str) = file_name.to_str() {
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user