From 9bf8a8503afa5cc7acacc8203490330292da7f85 Mon Sep 17 00:00:00 2001 From: Colin Walters Date: Thu, 25 May 2017 19:38:52 -0400 Subject: [PATCH] lib/sysroot: Add non-failable ostree_sysroot_repo() Having a failable accessor is annoying, since it's really common to reference both. Instead, open the repo once when we load the sysroot, and provide a non-failable accessor. This is also prep for `ostree_repo_open_at()`, which collapses the separation between `ostree_repo_new()` and `ostree_repo_open()`. Closes: #886 Approved by: jlebon --- apidoc/ostree-sections.txt | 1 + src/libostree/libostree.sym | 6 ++-- src/libostree/ostree-sysroot-cleanup.c | 6 +--- src/libostree/ostree-sysroot-deploy.c | 5 +--- src/libostree/ostree-sysroot-private.h | 1 + src/libostree/ostree-sysroot.c | 40 ++++++++++++++++++++++++-- src/libostree/ostree-sysroot.h | 3 ++ 7 files changed, 47 insertions(+), 15 deletions(-) diff --git a/apidoc/ostree-sections.txt b/apidoc/ostree-sections.txt index adc2dfd7..56c5c94e 100644 --- a/apidoc/ostree-sections.txt +++ b/apidoc/ostree-sections.txt @@ -486,6 +486,7 @@ ostree_sysroot_get_deployment_dirpath ostree_sysroot_get_deployment_origin_path ostree_sysroot_cleanup ostree_sysroot_prepare_cleanup +ostree_sysroot_repo ostree_sysroot_get_repo ostree_sysroot_init_osname ostree_sysroot_deployment_set_kargs diff --git a/src/libostree/libostree.sym b/src/libostree/libostree.sym index e2a7c3d9..612eb8ac 100644 --- a/src/libostree/libostree.sym +++ b/src/libostree/libostree.sym @@ -402,12 +402,10 @@ global: * NOTE NOTE NOTE */ -/* -LIBOSTREE_2017.$NEWVERSION { +LIBOSTREE_2017.7 { global: - someostree_symbol_deleteme; + ostree_sysroot_repo; } LIBOSTREE_2017.6; -*/ /* Stub section for the stable release *after* this development one; don't * edit this other than to update the last number. This is just a copy/paste diff --git a/src/libostree/ostree-sysroot-cleanup.c b/src/libostree/ostree-sysroot-cleanup.c index 499f20eb..c0b8bf07 100644 --- a/src/libostree/ostree-sysroot-cleanup.c +++ b/src/libostree/ostree-sysroot-cleanup.c @@ -501,8 +501,6 @@ _ostree_sysroot_cleanup_internal (OstreeSysroot *self, GCancellable *cancellable, GError **error) { - glnx_unref_object OstreeRepo *repo = NULL; - g_return_val_if_fail (OSTREE_IS_SYSROOT (self), FALSE); g_return_val_if_fail (self->loaded, FALSE); @@ -512,9 +510,7 @@ _ostree_sysroot_cleanup_internal (OstreeSysroot *self, if (!cleanup_old_deployments (self, cancellable, error)) return FALSE; - if (!ostree_sysroot_get_repo (self, &repo, cancellable, error)) - return FALSE; - + OstreeRepo *repo = ostree_sysroot_repo (self); if (!generate_deployment_refs (self, repo, self->bootversion, self->subbootversion, diff --git a/src/libostree/ostree-sysroot-deploy.c b/src/libostree/ostree-sysroot-deploy.c index 6df4fff2..2b45e6da 100644 --- a/src/libostree/ostree-sysroot-deploy.c +++ b/src/libostree/ostree-sysroot-deploy.c @@ -1956,10 +1956,7 @@ ostree_sysroot_deploy_tree (OstreeSysroot *self, if (!glnx_opendirat (self->sysroot_fd, osdeploypath, TRUE, &os_deploy_dfd, error)) return FALSE; - glnx_unref_object OstreeRepo *repo = NULL; - if (!ostree_sysroot_get_repo (self, &repo, cancellable, error)) - return FALSE; - + OstreeRepo *repo = ostree_sysroot_repo (self); glnx_unref_object OstreeDeployment *merge_deployment = NULL; if (provided_merge_deployment != NULL) merge_deployment = g_object_ref (provided_merge_deployment); diff --git a/src/libostree/ostree-sysroot-private.h b/src/libostree/ostree-sysroot-private.h index 26cfd363..14ee5cad 100644 --- a/src/libostree/ostree-sysroot-private.h +++ b/src/libostree/ostree-sysroot-private.h @@ -57,6 +57,7 @@ struct OstreeSysroot { /* Only access through ostree_sysroot_get_repo() */ OstreeRepo *repo; + gboolean repo_opened; OstreeSysrootDebugFlags debug_flags; }; diff --git a/src/libostree/ostree-sysroot.c b/src/libostree/ostree-sysroot.c index ac24b0af..86aa8ce3 100644 --- a/src/libostree/ostree-sysroot.c +++ b/src/libostree/ostree-sysroot.c @@ -729,6 +729,18 @@ ostree_sysroot_load (OstreeSysroot *self, return ostree_sysroot_load_if_changed (self, NULL, cancellable, error); } +static gboolean +ensure_repo_opened (OstreeSysroot *self, + GError **error) +{ + if (self->repo_opened) + return TRUE; + if (!ostree_repo_open (self->repo, NULL, error)) + return FALSE; + self->repo_opened = TRUE; + return TRUE; +} + gboolean ostree_sysroot_load_if_changed (OstreeSysroot *self, gboolean *out_changed, @@ -738,6 +750,13 @@ ostree_sysroot_load_if_changed (OstreeSysroot *self, if (!ensure_sysroot_fd (self, error)) return FALSE; + /* Here we also lazily initialize the repository. We didn't do this + * previous to v2017.6, but we do now to support the error-free + * ostree_sysroot_repo() API. + */ + if (!ensure_repo_opened (self, error)) + return FALSE; + int bootversion = 0; if (!read_current_bootversion (self, &bootversion, cancellable, error)) return FALSE; @@ -918,8 +937,7 @@ ostree_sysroot_get_repo (OstreeSysroot *self, GCancellable *cancellable, GError **error) { - /* ostree_repo_open() is idempotent. */ - if (!ostree_repo_open (self->repo, cancellable, error)) + if (!ensure_repo_opened (self, error)) return FALSE; if (out_repo != NULL) @@ -927,6 +945,24 @@ ostree_sysroot_get_repo (OstreeSysroot *self, return TRUE; } +/** + * ostree_sysroot_repo: + * @self: Sysroot + * + * This function is a variant of ostree_sysroot_get_repo() that cannot fail, and + * returns a cached repository. Can only be called after ostree_sysroot_load() + * has been invoked successfully. + * + * Returns: (transfer none): The OSTree repository in sysroot @self. + */ +OstreeRepo * +ostree_sysroot_repo (OstreeSysroot *self) +{ + g_return_val_if_fail (self->loaded, NULL); + g_assert (self->repo); + return self->repo; +} + /** * ostree_sysroot_query_bootloader: * @sysroot: Sysroot diff --git a/src/libostree/ostree-sysroot.h b/src/libostree/ostree-sysroot.h index 09614b55..e5969e9e 100644 --- a/src/libostree/ostree-sysroot.h +++ b/src/libostree/ostree-sysroot.h @@ -126,6 +126,9 @@ gboolean ostree_sysroot_write_origin_file (OstreeSysroot *sysroot, GCancellable *cancellable, GError **error); +_OSTREE_PUBLIC +OstreeRepo * ostree_sysroot_repo (OstreeSysroot *self); + _OSTREE_PUBLIC gboolean ostree_sysroot_get_repo (OstreeSysroot *self, OstreeRepo **out_repo,