rust/ostree-host: Add SysrootExt trait

This currently just adds a reimplementation of a new API that's
already in C upstream, but the plan is to add more Rust-only APIs in
the future here.
This commit is contained in:
Colin Walters 2021-03-19 12:58:03 +00:00
parent da9b64cd44
commit 41357a730a
6 changed files with 30 additions and 17 deletions

View File

@ -1,7 +1,7 @@
[package]
name = "ostree-host"
version = "0.1.0"
authors = ["Colin Walters <walters@verbum.org>", "Jonathan Lebon <jonathan@jlebon.com>"]
authors = ["Colin Walters <walters@verbum.org>"]
edition = "2018"
#rust = "1.48"

View File

@ -6,7 +6,7 @@
use glib::translate::*;
use std::ptr;
/// Extension functions which fix incorectly bound APIs
/// Extension functions which fix incorrectly bound APIs.
pub trait RepoBindingExt {
fn x_resolve_ref_optional(&self, refspec: &str) -> Result<Option<glib::GString>, glib::Error>;
}

View File

@ -5,7 +5,14 @@
#![deny(unused_must_use)]
mod binding_fixes;
pub use binding_fixes::*;
mod sysroot_ext;
pub use sysroot_ext::*;
/// This prelude currently just adds extension traits to various OSTree objects,
/// and like all preludes is intended to be quite "safe" to import and will avoid
/// likely naming clashes.
pub mod prelude {
pub use super::binding_fixes::*;
pub use super::sysroot_ext::*;
}

View File

@ -0,0 +1,17 @@
//! Extension traits that add new APIs.
// SPDX-License-Identifier: Apache-2.0 OR MIT
/// Extension APIs for `ostree::Sysroot`
pub trait SysrootExt {
/// Require a booted deployment; reimplements https://github.com/ostreedev/ostree/pull/2301
fn require_booted_deployment(&self) -> Result<ostree::Deployment, glib::Error>;
}
impl SysrootExt for ostree::Sysroot {
fn require_booted_deployment(&self) -> Result<ostree::Deployment, glib::Error> {
self.get_booted_deployment().ok_or_else(|| {
glib::Error::new(gio::IOErrorEnum::Failed, "Not booted into an OSTree system")
})
}
}

View File

@ -5,6 +5,7 @@ use crate::live;
use crate::{cxxrsutil::*, variant_utils};
use anyhow::{anyhow, Result};
use gio::DBusProxyExt;
use ostree_host::prelude::*;
use structopt::StructOpt;
#[derive(Debug, StructOpt)]
@ -23,12 +24,6 @@ struct Opts {
allow_replacement: bool,
}
fn get_required_booted_deployment(sysroot: &ostree::Sysroot) -> Result<ostree::Deployment> {
sysroot
.get_booted_deployment()
.ok_or_else(|| anyhow!("Not booted into an OSTree system"))
}
fn get_args_variant(sysroot: &ostree::Sysroot, opts: &Opts) -> Result<glib::Variant> {
let r = glib::VariantDict::new(None);
@ -38,7 +33,7 @@ fn get_args_variant(sysroot: &ostree::Sysroot, opts: &Opts) -> Result<glib::Vari
}
r.insert(live::OPT_TARGET, &target.as_str());
} else if opts.reset {
let booted = get_required_booted_deployment(sysroot)?;
let booted = sysroot.require_booted_deployment()?;
// Unwrap safety: This can't return NULL
let csum = booted.get_csum().expect("csum");
r.insert(live::OPT_TARGET, &csum.as_str());
@ -82,7 +77,7 @@ fn finish(sysroot: &ostree::Sysroot) -> Result<()> {
let cancellable = gio::NONE_CANCELLABLE;
sysroot.load_if_changed(cancellable)?;
let repo = &sysroot.get_repo(cancellable)?;
let booted = &get_required_booted_deployment(sysroot)?;
let booted = &sysroot.require_booted_deployment()?;
let booted_commit = booted.get_csum().expect("csum");
let booted_commit = booted_commit.as_str();

View File

@ -333,12 +333,6 @@ fn rerun_tmpfiles() -> Result<()> {
})
}
fn get_required_booted_deployment(sysroot: &ostree::Sysroot) -> Result<ostree::Deployment> {
sysroot
.get_booted_deployment()
.ok_or_else(|| anyhow!("Not booted into an OSTree system"))
}
/// Implementation of `rpm-ostree ex apply-live`.
pub(crate) fn transaction_apply_live(
mut sysroot: Pin<&mut crate::ffi::OstreeSysroot>,
@ -351,7 +345,7 @@ pub(crate) fn transaction_apply_live(
let allow_replacement = variant_dict_lookup_bool(options, OPT_REPLACE).unwrap_or_default();
let repo = &sysroot.repo().expect("repo");
let booted = get_required_booted_deployment(sysroot)?;
let booted = sysroot.require_booted_deployment()?;
let osname = booted.get_osname().expect("osname");
let booted_commit = booted.get_csum().expect("csum");
let booted_commit = booted_commit.as_str();