From 6913c9e235e1841f75c26f84d571bfa03b4061b7 Mon Sep 17 00:00:00 2001 From: Rafael Fonseca Date: Fri, 1 Nov 2019 17:19:51 +0100 Subject: [PATCH] deployment: add ostree_deployment_get_version API Signed-off-by: Rafael Fonseca --- src/libostree/ostree-deployment.c | 34 +++++++++++++++++++++++ src/libostree/ostree-deployment.h | 2 ++ src/libostree/ostree-sysroot-deploy.c | 20 ++------------ src/libostree/ostree-sysroot.c | 40 ++++++--------------------- 4 files changed, 47 insertions(+), 49 deletions(-) diff --git a/src/libostree/ostree-deployment.c b/src/libostree/ostree-deployment.c index 6532a973dc..98927c5f9d 100644 --- a/src/libostree/ostree-deployment.c +++ b/src/libostree/ostree-deployment.c @@ -63,6 +63,40 @@ ostree_deployment_get_bootserial (OstreeDeployment *self) return self->bootserial; } +/* + * ostree_deployment_get_version: + * @self: Deployment + * + * Returns: (transfer full): The deployment's version + */ +char * +ostree_deployment_get_version (OstreeDeployment *self, + OstreeRepo *repo, + GError **error) +{ + gchar *version = NULL; + + if (!repo) + return NULL; + + /* Try extracting a version for this deployment. */ + const gchar *csum = ostree_deployment_get_csum (self); + g_autoptr(GVariant) variant = NULL; + g_autoptr(GVariant) metadata = NULL; + + /* XXX Copying ot_admin_checksum_version() + bits from + * ot-admin-builtin-status.c. Maybe this should be + * public API in libostree? */ + if (ostree_repo_load_variant (repo, OSTREE_OBJECT_TYPE_COMMIT, csum, + &variant, error)) + { + metadata = g_variant_get_child_value (variant, 0); + g_variant_lookup (metadata, OSTREE_COMMIT_META_KEY_VERSION, "s", &version); + } + + return version; +} + /** * ostree_deployment_get_bootconfig: * @self: Deployment diff --git a/src/libostree/ostree-deployment.h b/src/libostree/ostree-deployment.h index 756e39d2b4..62f109ad7a 100644 --- a/src/libostree/ostree-deployment.h +++ b/src/libostree/ostree-deployment.h @@ -72,6 +72,8 @@ _OSTREE_PUBLIC OstreeBootconfigParser *ostree_deployment_get_bootconfig (OstreeDeployment *self); _OSTREE_PUBLIC GKeyFile *ostree_deployment_get_origin (OstreeDeployment *self); +_OSTREE_PUBLIC +char *ostree_deployment_get_version (OstreeDeployment *self, OstreeRepo *repo, GError **error); _OSTREE_PUBLIC gboolean ostree_deployment_is_staged (OstreeDeployment *self); diff --git a/src/libostree/ostree-sysroot-deploy.c b/src/libostree/ostree-sysroot-deploy.c index e5a6d4b01b..d021dadbb8 100644 --- a/src/libostree/ostree-sysroot-deploy.c +++ b/src/libostree/ostree-sysroot-deploy.c @@ -1712,24 +1712,8 @@ install_deployment_kernel (OstreeSysroot *sysroot, if (val == NULL) return glnx_throw (error, "No PRETTY_NAME or ID in /etc/os-release"); - g_autofree char *deployment_version = NULL; - if (repo) - { - /* Try extracting a version for this deployment. */ - const char *csum = ostree_deployment_get_csum (deployment); - g_autoptr(GVariant) variant = NULL; - g_autoptr(GVariant) metadata = NULL; - - /* XXX Copying ot_admin_checksum_version() + bits from - * ot-admin-builtin-status.c. Maybe this should be - * public API in libostree? */ - if (ostree_repo_load_variant (repo, OSTREE_OBJECT_TYPE_COMMIT, csum, - &variant, NULL)) - { - metadata = g_variant_get_child_value (variant, 0); - g_variant_lookup (metadata, OSTREE_COMMIT_META_KEY_VERSION, "s", &deployment_version); - } - } + g_autofree char *deployment_version = + ostree_deployment_get_version (deployment, repo, error); /* XXX The SYSLINUX bootloader backend actually parses the title string * (specifically, it looks for the substring "(ostree"), so further diff --git a/src/libostree/ostree-sysroot.c b/src/libostree/ostree-sysroot.c index 02c04b94ad..12585d1f42 100644 --- a/src/libostree/ostree-sysroot.c +++ b/src/libostree/ostree-sysroot.c @@ -1553,32 +1553,6 @@ ostree_sysroot_init_osname (OstreeSysroot *self, return TRUE; } -static gchar * -get_deployment_version (OstreeSysroot *sysroot, - OstreeDeployment *deployment, - GError **error) -{ - gchar *deployment_version = NULL; - - /* Try extracting a version for this deployment. */ - const gchar *csum = ostree_deployment_get_csum (deployment); - g_autoptr(GVariant) variant = NULL; - g_autoptr(GVariant) metadata = NULL; - - OstreeRepo *repo = ostree_sysroot_repo (sysroot); - /* XXX Copying ot_admin_checksum_version() + bits from - * ot-admin-builtin-status.c. Maybe this should be - * public API in libostree? */ - if (ostree_repo_load_variant (repo, OSTREE_OBJECT_TYPE_COMMIT, csum, - &variant, error)) - { - metadata = g_variant_get_child_value (variant, 0); - g_variant_lookup (metadata, OSTREE_COMMIT_META_KEY_VERSION, "s", &deployment_version); - } - - return deployment_version; -} - /** * ostree_sysroot_simple_write_deployment: * @sysroot: Sysroot @@ -1682,14 +1656,18 @@ ostree_sysroot_simple_write_deployment (OstreeSysroot *sysroot, gboolean is_previous_version = FALSE; if (!retained_previous_version) { + OstreeRepo *repo = ostree_sysroot_repo (sysroot); g_autofree char *deployment_version = - get_deployment_version (sysroot, deployment, error); + ostree_deployment_get_version (deployment, repo, error); - if (!last_version) - last_version = g_steal_pointer (&deployment_version); + if (deployment_version && *deployment_version) + { + if (!last_version) + last_version = g_steal_pointer (&deployment_version); - is_previous_version = (osname_matches && passed_crossover && last_version && - (g_strcmp0 (deployment_version, last_version) != 0)); + is_previous_version = (osname_matches && passed_crossover && last_version && + (g_strcmp0 (deployment_version, last_version) != 0)); + } } /* Retain deployment if: