Skip to content

Commit f88f1c4

Browse files
refactor: Accept boot_dir param in rollback grub
Suggested by Gemini Accept a param `boot_dir`, fd to the boot directory, in function `rollback_grub_uki_entries`. Removes hardcoded paths and makes the code a tiny bit cleaner. Signed-off-by: Pragyan Poudyal <[email protected]>
1 parent 36add80 commit f88f1c4

File tree

1 file changed

+16
-21
lines changed

1 file changed

+16
-21
lines changed

crates/lib/src/bootc_composefs/rollback.rs

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
use std::fmt::Write;
2-
use std::path::PathBuf;
32

43
use anyhow::{anyhow, Context, Result};
54
use cap_std_ext::cap_std::ambient_authority;
65
use cap_std_ext::cap_std::fs::Dir;
7-
use cap_std_ext::{cap_std, dirext::CapStdExtDirExt};
6+
use cap_std_ext::dirext::CapStdExtDirExt;
87
use fn_error_context::context;
98
use rustix::fs::{fsync, renameat_with, AtFlags, RenameFlags};
109

@@ -82,12 +81,8 @@ pub(crate) fn rename_exchange_bls_entries(entries_dir: &Dir) -> Result<()> {
8281
}
8382

8483
#[context("Rolling back Grub UKI")]
85-
fn rollback_grub_uki_entries() -> Result<()> {
86-
let user_cfg_path = PathBuf::from("/sysroot/boot/grub2");
87-
84+
fn rollback_grub_uki_entries(boot_dir: &Dir) -> Result<()> {
8885
let mut str = String::new();
89-
let boot_dir =
90-
Dir::open_ambient_dir("/sysroot/boot", ambient_authority()).context("Opening boot dir")?;
9186
let mut menuentries = get_sorted_grub_uki_boot_entries(&boot_dir, &mut str)
9287
.context("Getting UKI boot entries")?;
9388

@@ -103,9 +98,7 @@ fn rollback_grub_uki_entries() -> Result<()> {
10398
write!(buffer, "{entry}")?;
10499
}
105100

106-
let entries_dir =
107-
cap_std::fs::Dir::open_ambient_dir(&user_cfg_path, cap_std::ambient_authority())
108-
.with_context(|| format!("Opening {user_cfg_path:?}"))?;
101+
let entries_dir = boot_dir.open_dir("grub2").context("Opening grub dir")?;
109102

110103
entries_dir
111104
.atomic_write(USER_CFG_STAGED, buffer)
@@ -199,18 +192,20 @@ pub(crate) async fn composefs_rollback() -> Result<()> {
199192
};
200193

201194
match &rollback_entry.bootloader {
202-
Bootloader::Grub => match rollback_entry.boot_type {
203-
BootType::Bls => {
204-
let boot_dir = Dir::open_ambient_dir("/sysroot/boot", ambient_authority())
205-
.context("Opening boot dir")?;
206-
207-
rollback_composefs_entries(&boot_dir, rollback_entry.bootloader.clone())?;
195+
Bootloader::Grub => {
196+
let boot_dir = Dir::open_ambient_dir("/sysroot/boot", ambient_authority())
197+
.context("Opening boot dir")?;
198+
199+
match rollback_entry.boot_type {
200+
BootType::Bls => {
201+
rollback_composefs_entries(&boot_dir, rollback_entry.bootloader.clone())?;
202+
}
203+
204+
BootType::Uki => {
205+
rollback_grub_uki_entries(&boot_dir)?;
206+
}
208207
}
209-
210-
BootType::Uki => {
211-
rollback_grub_uki_entries()?;
212-
}
213-
},
208+
}
214209

215210
Bootloader::Systemd => {
216211
let parent = get_sysroot_parent_dev()?;

0 commit comments

Comments
 (0)