Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

daemon: Move some "deployment variant" generation to Rust #2609

Merged
merged 1 commit into from
Feb 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion rust/src/cxxrsutil.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,20 @@ bind_ostree_obj!(Deployment);
// try to instead create a bind_gio_obj!() macro or so.
#[repr(transparent)]
pub struct FFIGCancellable(gio_sys::GCancellable);

unsafe impl ExternType for FFIGCancellable {
type Id = type_id!(rpmostreecxx::GCancellable);
type Kind = cxx::kind::Trivial;
}
impl_wrap!(FFIGCancellable, gio::Cancellable, gio_sys::GCancellable);

#[repr(transparent)]
pub struct FFIGVariantDict(glib_sys::GVariantDict);
unsafe impl ExternType for FFIGVariantDict {
type Id = type_id!(rpmostreecxx::GVariantDict);
type Kind = cxx::kind::Trivial;
}
impl_wrap!(FFIGVariantDict, glib::VariantDict, glib_sys::GVariantDict);

// An error type helper; separate from the GObject bridging
mod err {
use std::error::Error as StdError;
Expand Down
46 changes: 46 additions & 0 deletions rust/src/daemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,49 @@ pub(crate) fn deployment_generate_id(
deployment.get_deployserial()
)
}

pub(crate) fn deployment_populate_variant(
mut sysroot: Pin<&mut crate::FFIOstreeSysroot>,
mut deployment: Pin<&mut crate::FFIOstreeDeployment>,
mut dict: Pin<&mut crate::FFIGVariantDict>,
) -> CxxResult<()> {
let sysroot = &sysroot.gobj_wrap();
let deployment = &deployment.gobj_wrap();
let dict = dict.gobj_wrap();

let id = deployment_generate_id(deployment.gobj_rewrap());
// First, basic values from ostree
dict.insert("id", &id);

dict.insert("osname", &deployment.get_osname().expect("osname").as_str());
dict.insert("checksum", &deployment.get_csum().expect("csum").as_str());
dict.insert_value(
"serial",
&glib::Variant::from(deployment.get_deployserial() as i32),
);

let booted: bool = sysroot
.get_booted_deployment()
.map(|b| b.equal(deployment))
.unwrap_or_default();
dict.insert("booted", &booted);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This slightly changes the behaviour to always inserting the booted key instead of only if we're booted inside an OSTree. I vaguely remember having a reason to do that in #350, but I can't recall. So... I guess if nothing breaks, let's do it! :)


let live_state =
crate::live::get_live_apply_state(sysroot.gobj_rewrap(), deployment.gobj_rewrap())?;
if !live_state.inprogress.is_empty() {
dict.insert("live-inprogress", &live_state.inprogress.as_str());
}
if !live_state.commit.is_empty() {
dict.insert("live-replaced", &live_state.commit.as_str());
}

/* Staging status */
if deployment.is_staged() {
dict.insert("staged", &true);
if std::path::Path::new("/run/ostree/staged-deployment-locked").exists() {
dict.insert("finalization-locked", &true);
}
}

Ok(())
}
6 changes: 6 additions & 0 deletions rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub mod ffi {
type OstreeRepo = crate::FFIOstreeRepo;
type OstreeDeployment = crate::FFIOstreeDeployment;
type GCancellable = crate::FFIGCancellable;
type GVariantDict = crate::FFIGVariantDict;
}

/// Currently cxx-rs doesn't support mappings; like probably most projects,
Expand Down Expand Up @@ -80,6 +81,11 @@ pub mod ffi {
// daemon.rs
extern "Rust" {
fn deployment_generate_id(deployment: Pin<&mut OstreeDeployment>) -> String;
fn deployment_populate_variant(
mut sysroot: Pin<&mut OstreeSysroot>,
mut deployment: Pin<&mut OstreeDeployment>,
mut dict: Pin<&mut GVariantDict>,
) -> Result<()>;
}

// initramfs.rs
Expand Down
36 changes: 1 addition & 35 deletions src/daemon/rpmostreed-deployment-utils.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -235,42 +235,8 @@ rpmostreed_deployment_generate_variant (OstreeSysroot *sysroot,
GVariantDict dict;
g_variant_dict_init (&dict, NULL);

/* First, basic values from ostree */
auto id = rpmostreecxx::deployment_generate_id (*deployment);
g_variant_dict_insert (&dict, "id", "s", id.c_str());

const gchar *osname = ostree_deployment_get_osname (deployment);
if (osname != NULL)
g_variant_dict_insert (&dict, "osname", "s", osname);
rpmostreecxx::deployment_populate_variant(*sysroot, *deployment, dict);
const gchar *csum = ostree_deployment_get_csum (deployment);
g_variant_dict_insert (&dict, "checksum", "s", csum);
gint serial = ostree_deployment_get_deployserial (deployment);
g_variant_dict_insert (&dict, "serial", "i", serial);

/* Booted status */
const gboolean is_booted = g_strcmp0 (booted_id, id.c_str()) == 0;
if (booted_id != NULL)
g_variant_dict_insert (&dict, "booted", "b", is_booted);
if (is_booted)
{
auto live_state = rpmostreecxx::get_live_apply_state(*sysroot, *deployment);

if (live_state.inprogress.length() > 0)
g_variant_dict_insert (&dict, "live-inprogress", "s", live_state.inprogress.c_str());
if (live_state.commit.length() > 0)
g_variant_dict_insert (&dict, "live-replaced", "s", live_state.commit.c_str());
}

/* Staging status */
if (ostree_deployment_is_staged (deployment))
{
g_variant_dict_insert (&dict, "staged", "b", TRUE);
if (!glnx_fstatat_allow_noent (AT_FDCWD, _OSTREE_SYSROOT_RUNSTATE_STAGED_LOCKED,
NULL, 0, error))
return FALSE;
g_variant_dict_insert (&dict, "finalization-locked", "b", errno == 0);
}

/* Load the commit object */
g_autoptr(GVariant) commit = NULL;
if (!ostree_repo_load_variant (repo,
Expand Down
1 change: 1 addition & 0 deletions src/libpriv/rpmostree-cxxrs-prelude.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ namespace rpmostreecxx {
typedef ::OstreeRepo OstreeRepo;
typedef ::OstreeDeployment OstreeDeployment;
typedef ::GCancellable GCancellable;
typedef ::GVariantDict GVariantDict;
}