From 3551b973d12249aacea0fc55465c0408ba4ffa7f Mon Sep 17 00:00:00 2001 From: S4NKALP Date: Thu, 12 Mar 2026 00:42:01 +0545 Subject: [PATCH] refactor(src/utils): simplify directory creation and systemd-inhibit check - Enhancements: Refactored conditional logic in create_file and rebuild_initramfs to reduce nesting and improve readability --- src/utils/system.rs | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/src/utils/system.rs b/src/utils/system.rs index 224cc4f..7660801 100644 --- a/src/utils/system.rs +++ b/src/utils/system.rs @@ -17,27 +17,24 @@ pub fn assert_root() { pub fn create_file(path: &str, content: &str, executable: bool) { let path_obj = Path::new(path); - if let Some(parent) = path_obj.parent() { - if !parent.exists() { - if let Err(e) = fs::create_dir_all(parent) { + if let Some(parent) = path_obj.parent() + && !parent.exists() + && let Err(e) = fs::create_dir_all(parent) { error!("Failed to create directories for '{}': {}", path, e); return; } - } - } match fs::write(path_obj, content) { Ok(_) => { info!("Created file {}", path); debug!("{}", content); - if executable { - if let Ok(mut perms) = fs::metadata(path_obj).map(|m| m.permissions()) { + if executable + && let Ok(mut perms) = fs::metadata(path_obj).map(|m| m.permissions()) { perms.set_mode(perms.mode() | 0o111); let _ = fs::set_permissions(path_obj, perms); info!("Added execution privilege to file {}", path); } - } } Err(e) => { error!("Failed to create file '{}': {}", path, e); @@ -79,8 +76,8 @@ pub fn rebuild_initramfs() { command = vec!["mkinitcpio", "-P"]; } - if let Ok(which) = Command::new("which").arg("systemd-inhibit").output() { - if which.status.success() { + if let Ok(which) = Command::new("which").arg("systemd-inhibit").output() + && which.status.success() { let mut new_cmd = vec![ "systemd-inhibit", "--who=lapctl", @@ -91,7 +88,6 @@ pub fn rebuild_initramfs() { new_cmd.extend(command.iter()); command = new_cmd; } - } if !command.is_empty() { println!("Rebuilding the initramfs...");