refactor(src/utils): refactor directory creation and permission handling in create_file

- New Features
- Enhancements: Simplify directory creation error handling
- Bug Fixes: Ensure proper early return when creating parent directories fails
This commit is contained in:
sankalp committed 2026-03-12 00:48:11 +05:45
1 parent 857c7ebb28
commit 7846c1f5d2
1 file changed
+22 -21
+22 -21
View File
@@ -19,22 +19,22 @@ pub fn create_file(path: &str, content: &str, executable: bool) {
let path_obj = Path::new(path); let path_obj = Path::new(path);
if let Some(parent) = path_obj.parent() if let Some(parent) = path_obj.parent()
&& !parent.exists() && !parent.exists()
&& let Err(e) = fs::create_dir_all(parent) { && let Err(e) = fs::create_dir_all(parent)
error!("Failed to create directories for '{}': {}", path, e); {
return; error!("Failed to create directories for '{}': {}", path, e);
} return;
}
match fs::write(path_obj, content) { match fs::write(path_obj, content) {
Ok(_) => { Ok(_) => {
info!("Created file {}", path); info!("Created file {}", path);
debug!("{}", content); debug!("{}", content);
if executable if executable && let Ok(mut perms) = fs::metadata(path_obj).map(|m| m.permissions()) {
&& let Ok(mut perms) = fs::metadata(path_obj).map(|m| m.permissions()) { perms.set_mode(perms.mode() | 0o111);
perms.set_mode(perms.mode() | 0o111); let _ = fs::set_permissions(path_obj, perms);
let _ = fs::set_permissions(path_obj, perms); info!("Added execution privilege to file {}", path);
info!("Added execution privilege to file {}", path); }
}
} }
Err(e) => { Err(e) => {
error!("Failed to create file '{}': {}", path, e); error!("Failed to create file '{}': {}", path, e);
@@ -77,17 +77,18 @@ pub fn rebuild_initramfs() {
} }
if let Ok(which) = Command::new("which").arg("systemd-inhibit").output() if let Ok(which) = Command::new("which").arg("systemd-inhibit").output()
&& which.status.success() { && which.status.success()
let mut new_cmd = vec![ {
"systemd-inhibit", let mut new_cmd = vec![
"--who=lapctl", "systemd-inhibit",
"--why", "--who=lapctl",
"Rebuilding initramfs", "--why",
"--", "Rebuilding initramfs",
]; "--",
new_cmd.extend(command.iter()); ];
command = new_cmd; new_cmd.extend(command.iter());
} command = new_cmd;
}
if !command.is_empty() { if !command.is_empty() {
println!("Rebuilding the initramfs..."); println!("Rebuilding the initramfs...");