Skip to content

Commit 95af34c

Browse files
committed
update: pass sysroot as parameter
Remove duplicated sysroot as `openat::Dir::open("/")` and pass as parameter
1 parent ccfcccb commit 95af34c

File tree

2 files changed

+13
-12
lines changed

2 files changed

+13
-12
lines changed

src/blockdev.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use anyhow::{bail, Context, Result};
66
use bootc_blockdev::PartitionTable;
77
use fn_error_context::context;
88

9-
/// Get parent devices and save
9+
/// Init parent devices and save
1010
pub fn get_parent_devices(devices: Option<Vec<String>>) -> &'static [String] {
1111
static PARENT_DEVICES: OnceLock<Vec<String>> = OnceLock::new();
1212
PARENT_DEVICES.get_or_init(|| devices.unwrap())

src/bootupd.rs

+12-11
Original file line numberDiff line numberDiff line change
@@ -215,16 +215,15 @@ fn ensure_writable_boot() -> Result<()> {
215215
}
216216

217217
/// daemon implementation of component update
218-
pub(crate) fn update(name: &str) -> Result<ComponentUpdateResult> {
218+
pub(crate) fn update(name: &str, sysroot: &openat::Dir) -> Result<ComponentUpdateResult> {
219219
let mut state = SavedState::load_from_disk("/")?.unwrap_or_default();
220220
let component = component::new_from_name(name)?;
221221
let inst = if let Some(inst) = state.installed.get(name) {
222222
inst.clone()
223223
} else {
224224
anyhow::bail!("Component {} is not installed", name);
225225
};
226-
let sysroot = openat::Dir::open("/")?;
227-
let update = component.query_update(&sysroot)?;
226+
let update = component.query_update(sysroot)?;
228227
let update = match update.as_ref() {
229228
Some(p) if inst.meta.can_upgrade_to(p) => p,
230229
_ => return Ok(ComponentUpdateResult::AtLatestVersion),
@@ -235,6 +234,7 @@ pub(crate) fn update(name: &str) -> Result<ComponentUpdateResult> {
235234
let mut pending_container = state.pending.take().unwrap_or_default();
236235
let interrupted = pending_container.get(component.name()).cloned();
237236
pending_container.insert(component.name().into(), update.clone());
237+
let sysroot = sysroot.try_clone()?;
238238
let mut state_guard =
239239
SavedState::acquire_write_lock(sysroot).context("Failed to acquire write lock")?;
240240
state_guard
@@ -256,8 +256,7 @@ pub(crate) fn update(name: &str) -> Result<ComponentUpdateResult> {
256256
}
257257

258258
/// daemon implementation of component adoption
259-
pub(crate) fn adopt_and_update(name: &str) -> Result<ContentMetadata> {
260-
let sysroot = openat::Dir::open("/")?;
259+
pub(crate) fn adopt_and_update(name: &str, sysroot: &openat::Dir) -> Result<ContentMetadata> {
261260
let mut state = SavedState::load_from_disk("/")?.unwrap_or_default();
262261
let component = component::new_from_name(name)?;
263262
if state.installed.contains_key(name) {
@@ -269,6 +268,7 @@ pub(crate) fn adopt_and_update(name: &str) -> Result<ContentMetadata> {
269268
let Some(update) = component.query_update(&sysroot)? else {
270269
anyhow::bail!("Component {} has no available update", name);
271270
};
271+
let sysroot = sysroot.try_clone()?;
272272
let mut state_guard =
273273
SavedState::acquire_write_lock(sysroot).context("Failed to acquire write lock")?;
274274

@@ -408,7 +408,7 @@ pub(crate) fn print_status(status: &Status) -> Result<()> {
408408
Ok(())
409409
}
410410

411-
pub(crate) fn client_run_update() -> Result<()> {
411+
pub(crate) fn client_run_update(sysroot: &openat::Dir) -> Result<()> {
412412
crate::try_fail_point!("update");
413413
let status: Status = status()?;
414414
if status.components.is_empty() && status.adoptable.is_empty() {
@@ -421,7 +421,7 @@ pub(crate) fn client_run_update() -> Result<()> {
421421
ComponentUpdatable::Upgradable => {}
422422
_ => continue,
423423
};
424-
match update(name)? {
424+
match update(name, sysroot)? {
425425
ComponentUpdateResult::AtLatestVersion => {
426426
// Shouldn't happen unless we raced with another client
427427
eprintln!(
@@ -449,7 +449,7 @@ pub(crate) fn client_run_update() -> Result<()> {
449449
}
450450
for (name, adoptable) in status.adoptable.iter() {
451451
if adoptable.confident {
452-
let r: ContentMetadata = adopt_and_update(name)?;
452+
let r: ContentMetadata = adopt_and_update(name, sysroot)?;
453453
println!("Adopted and updated: {}: {}", name, r.version);
454454
updated = true;
455455
} else {
@@ -462,13 +462,13 @@ pub(crate) fn client_run_update() -> Result<()> {
462462
Ok(())
463463
}
464464

465-
pub(crate) fn client_run_adopt_and_update() -> Result<()> {
465+
pub(crate) fn client_run_adopt_and_update(sysroot: &openat::Dir) -> Result<()> {
466466
let status: Status = status()?;
467467
if status.adoptable.is_empty() {
468468
println!("No components are adoptable.");
469469
} else {
470470
for (name, _) in status.adoptable.iter() {
471-
let r: ContentMetadata = adopt_and_update(name)?;
471+
let r: ContentMetadata = adopt_and_update(name, sysroot)?;
472472
println!("Adopted and updated: {}: {}", name, r.version);
473473
}
474474
}
@@ -656,7 +656,8 @@ mod tests {
656656
fn test_failpoint_update() {
657657
let guard = fail::FailScenario::setup();
658658
fail::cfg("update", "return").unwrap();
659-
let r = client_run_update();
659+
let sysroot = openat::Dir::open("/").expect("open /");
660+
let r = client_run_update(&sysroot);
660661
assert_eq!(r.is_err(), true);
661662
guard.teardown();
662663
}

0 commit comments

Comments
 (0)