Skip to content

Commit

Permalink
Merge pull request ostreedev#635 from cgwalters/fix-signed-parsing
Browse files Browse the repository at this point in the history
prepareroot: Also accept composefs enabled=signed
  • Loading branch information
jeckersb authored May 30, 2024
2 parents d621755 + ab3e9df commit 4730a72
Showing 1 changed file with 57 additions and 2 deletions.
59 changes: 57 additions & 2 deletions lib/src/ostree_prepareroot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,47 @@ impl Tristate {
}
}

#[derive(Debug, PartialEq, Eq)]
enum ComposefsState {
Signed,
Tristate(Tristate),
}

impl Default for ComposefsState {
fn default() -> Self {
Self::Tristate(Tristate::default())
}
}

impl FromStr for ComposefsState {
type Err = anyhow::Error;

fn from_str(s: &str) -> Result<Self> {
let r = match s {
"signed" => Self::Signed,
o => Self::Tristate(Tristate::from_str(o)?),
};
Ok(r)
}
}

impl ComposefsState {
pub(crate) fn maybe_enabled(&self) -> bool {
match self {
ComposefsState::Signed => true,
ComposefsState::Tristate(t) => t.maybe_enabled(),
}
}
}

/// Query whether the config uses an overlayfs model (composefs or plain overlayfs).
pub fn overlayfs_enabled_in_config(config: &glib::KeyFile) -> Result<bool> {
let root_transient = config
.optional_bool("root", "transient")?
.unwrap_or_default();
let composefs = config
.optional_string("composefs", "enabled")?
.map(|s| Tristate::from_str(s.as_str()))
.map(|s| ComposefsState::from_str(s.as_str()))
.transpose()
.log_err_default()
.unwrap_or_default();
Expand All @@ -112,6 +145,27 @@ fn test_tristate() {
}
}

#[test]
fn test_composefs_state() {
assert_eq!(
ComposefsState::from_str("signed").unwrap(),
ComposefsState::Signed
);
for v in ["yes", "true", "1"] {
assert_eq!(
ComposefsState::from_str(v).unwrap(),
ComposefsState::Tristate(Tristate::Enabled)
);
}
assert_eq!(Tristate::from_str("maybe").unwrap(), Tristate::Maybe);
for v in ["no", "false", "0"] {
assert_eq!(
ComposefsState::from_str(v).unwrap(),
ComposefsState::Tristate(Tristate::Disabled)
);
}
}

#[test]
fn test_overlayfs_enabled() {
let d0 = indoc::indoc! { r#"
Expand All @@ -136,7 +190,8 @@ enabled = false
let e0 = format!("{d0}\n[root]\ntransient = true");
let e1 = format!("{d1}\n[composefs]\nenabled = true\n[other]\nsomekey = someval");
let e2 = format!("{d1}\n[composefs]\nenabled = yes");
for v in [e0, e1, e2] {
let e3 = format!("{d1}\n[composefs]\nenabled = signed");
for v in [e0, e1, e2, e3] {
let kf = glib::KeyFile::new();
kf.load_from_data(&v, glib::KeyFileFlags::empty()).unwrap();
assert_eq!(overlayfs_enabled_in_config(&kf).unwrap(), true);
Expand Down

0 comments on commit 4730a72

Please sign in to comment.