Skip to content

Commit 5c92af0

Browse files
committed
composefs: Extract read_origin() helper for deployment origin files
Needed for handling GC later. Assisted-by: OpenCode (Claude claude-opus-4-6) Signed-off-by: Colin Walters <walters@verbum.org>
1 parent 081ac21 commit 5c92af0

File tree

2 files changed

+24
-15
lines changed

2 files changed

+24
-15
lines changed

crates/lib/src/bootc_composefs/state.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,27 @@ use crate::{
4444
utils::path_relative_to,
4545
};
4646

47+
/// Read and parse the `.origin` INI file for a deployment.
48+
///
49+
/// Returns `None` if the state directory or origin file doesn't exist
50+
/// (e.g. the deployment was partially deleted).
51+
#[context("Reading origin for deployment {deployment_id}")]
52+
pub(crate) fn read_origin(sysroot: &Dir, deployment_id: &str) -> Result<Option<tini::Ini>> {
53+
let depl_state_path = std::path::PathBuf::from(STATE_DIR_RELATIVE).join(deployment_id);
54+
55+
let Some(state_dir) = sysroot.open_dir_optional(&depl_state_path)? else {
56+
return Ok(None);
57+
};
58+
59+
let origin_filename = format!("{deployment_id}.origin");
60+
let Some(origin_contents) = state_dir.read_to_string_optional(&origin_filename)? else {
61+
return Ok(None);
62+
};
63+
64+
let ini = tini::Ini::from_string(&origin_contents).context("Failed to parse origin file")?;
65+
Ok(Some(ini))
66+
}
67+
4768
pub(crate) fn get_booted_bls(boot_dir: &Dir) -> Result<BLSConfig> {
4869
let cmdline = Cmdline::from_proc()?;
4970
let booted = cmdline

crates/lib/src/bootc_composefs/status.rs

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::{
1111
boot::BootType,
1212
repo::get_imgref,
1313
selinux::are_selinux_policies_compatible,
14-
state::get_composefs_usr_overlay_status,
14+
state::{get_composefs_usr_overlay_status, read_origin},
1515
utils::{compute_store_boot_digest_for_uki, get_uki_cmdline},
1616
},
1717
composefs_consts::{
@@ -699,11 +699,6 @@ async fn composefs_deployment_status_from(
699699
// This is our source of truth
700700
let bootloader_entry_verity = list_bootloader_entries(storage)?;
701701

702-
let state_dir = storage
703-
.physical_root
704-
.open_dir(STATE_DIR_RELATIVE)
705-
.with_context(|| format!("Opening {STATE_DIR_RELATIVE}"))?;
706-
707702
let host_spec = HostSpec {
708703
image: None,
709704
boot_order: BootOrder::Default,
@@ -732,15 +727,8 @@ async fn composefs_deployment_status_from(
732727
let mut extra_deployment_boot_entries: Vec<BootEntry> = Vec::new();
733728

734729
for verity_digest in bootloader_entry_verity {
735-
// read the origin file
736-
let config = state_dir
737-
.open_dir(&verity_digest)
738-
.with_context(|| format!("Failed to open {verity_digest}"))?
739-
.read_to_string(format!("{verity_digest}.origin"))
740-
.with_context(|| format!("Reading file {verity_digest}.origin"))?;
741-
742-
let ini = tini::Ini::from_string(&config)
743-
.with_context(|| format!("Failed to parse file {verity_digest}.origin as ini"))?;
730+
let ini = read_origin(&storage.physical_root, &verity_digest)?
731+
.ok_or_else(|| anyhow::anyhow!("No origin file for deployment {verity_digest}"))?;
744732

745733
let mut boot_entry =
746734
boot_entry_from_composefs_deployment(storage, ini, &verity_digest).await?;

0 commit comments

Comments
 (0)