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
This commit is contained in:
1 file changed
+7
-11
+7
-11
@@ -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...");
|
||||
|
||||
Reference in new issue
Block a user