From 0e4948722c6c6c91bf86efdde6f940dbea3f280e Mon Sep 17 00:00:00 2001 From: Alexander Larsson Date: Fri, 25 May 2018 16:40:19 +0200 Subject: [PATCH] Installation: Add flatpak_installation_create_transaction () This is the libflatpak entry point for doing transactions. --- common/flatpak-installation.c | 36 ++++++++++++++++++++++++++++++++++ common/flatpak-installation.h | 4 ++++ common/flatpak-transaction.c | 4 ++++ common/flatpak-utils-private.h | 19 ++++++++++++++++++ 4 files changed, 63 insertions(+) diff --git a/common/flatpak-installation.c b/common/flatpak-installation.c index 87d295ba..3e0decfd 100644 --- a/common/flatpak-installation.c +++ b/common/flatpak-installation.c @@ -30,6 +30,7 @@ #include "flatpak-utils-private.h" #include "flatpak-installation.h" #include "flatpak-installed-ref-private.h" +#include "flatpak-transaction-private.h" #include "flatpak-related-ref-private.h" #include "flatpak-remote-private.h" #include "flatpak-remote-ref-private.h" @@ -1623,6 +1624,41 @@ flatpak_installation_install_ref_file (FlatpakInstallation *self, return flatpak_remote_ref_new (coll_ref, NULL, remote, NULL); } + +/** + * flatpak_installation_create_transaction: + * @self: a #FlatpakInstallation + * @cancellable: (nullable): a #GCancellable + * @error: return location for a #GError + * + * Creates a new #FlatpakTransaction object that can be used to do installation + * and updates of multiple refs, as well as their dependencies, in a single + * operation. Set the options you want on the transaction and add the + * refs you want to install/update, then start the transaction with + * flatpak_transaction_run (). + * + * Returns: (transfer full): a #FlatpakTransaction, or %NULL on failure. + */ +FlatpakTransaction * +flatpak_installation_create_transaction (FlatpakInstallation *self, + GCancellable *cancellable, + GError **error) +{ + g_autoptr(FlatpakDir) dir = NULL; + g_autoptr(FlatpakDir) dir_clone = NULL; + + dir = flatpak_installation_get_dir (self, error); + if (dir == NULL) + return NULL; + + /* Pull, prune, etc are not threadsafe, so we work on a copy */ + dir_clone = flatpak_dir_clone (dir); + if (!flatpak_dir_ensure_repo (dir_clone, cancellable, error)) + return NULL; + + return flatpak_transaction_new (dir); +} + /** * flatpak_installation_install_full: * @self: a #FlatpakInstallation diff --git a/common/flatpak-installation.h b/common/flatpak-installation.h index 691f181e..973e13f9 100644 --- a/common/flatpak-installation.h +++ b/common/flatpak-installation.h @@ -30,6 +30,7 @@ typedef struct _FlatpakInstallation FlatpakInstallation; #include #include #include +#include #define FLATPAK_TYPE_INSTALLATION flatpak_installation_get_type () #define FLATPAK_INSTALLATION(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), FLATPAK_TYPE_INSTALLATION, FlatpakInstallation)) @@ -230,6 +231,9 @@ FLATPAK_EXTERN char * flatpak_installation_load_app_overrides (Flat const char *app_id, GCancellable *cancellable, GError **error); +FLATPAK_EXTERN FlatpakTransaction *flatpak_installation_create_transaction (FlatpakInstallation *self, + GCancellable *cancellable, + GError **error); FLATPAK_EXTERN FlatpakInstalledRef * flatpak_installation_install (FlatpakInstallation *self, const char *remote_name, FlatpakRefKind kind, diff --git a/common/flatpak-transaction.c b/common/flatpak-transaction.c index c3e23d77..eb2fa6b0 100644 --- a/common/flatpak-transaction.c +++ b/common/flatpak-transaction.c @@ -1018,12 +1018,16 @@ flatpak_transaction_run (FlatpakTransaction *self, GList *l; gboolean succeeded = TRUE; gboolean needs_prune = FALSE; + g_autoptr(GMainContextPopDefault) main_context = NULL; int i; if (!self->no_pull && !flatpak_transaction_update_metadata (self, cancellable, error)) return FALSE; + /* Work around ostree-pull spinning the default main context for the sync calls */ + main_context = flatpak_main_context_new_default (); + self->ops = g_list_reverse (self->ops); for (l = self->ops; l != NULL; l = l->next) diff --git a/common/flatpak-utils-private.h b/common/flatpak-utils-private.h index 44365821..c10e369c 100644 --- a/common/flatpak-utils-private.h +++ b/common/flatpak-utils-private.h @@ -496,6 +496,25 @@ flatpak_temp_dir_destroy (void *p) typedef GFile FlatpakTempDir; G_DEFINE_AUTOPTR_CLEANUP_FUNC (FlatpakTempDir, flatpak_temp_dir_destroy) +typedef GMainContext GMainContextPopDefault; +static inline void +flatpak_main_context_pop_default_destroy (void *p) +{ + GMainContext *main_context = p; + + if (main_context) + g_main_context_pop_thread_default (main_context); +} + +static inline GMainContextPopDefault * +flatpak_main_context_new_default (void) +{ + GMainContext *main_context = g_main_context_new (); + g_main_context_push_thread_default (main_context); + return main_context; +} + +G_DEFINE_AUTOPTR_CLEANUP_FUNC (GMainContextPopDefault, flatpak_main_context_pop_default_destroy) typedef OstreeRepo FlatpakRepoTransaction;