From 90c546c5dc6c12d2e8234ed1446ebd3142e424c3 Mon Sep 17 00:00:00 2001 From: Jonathan Lebon Date: Fri, 29 Jan 2021 17:58:04 -0500 Subject: [PATCH] core: Factor out function to download pkgs I want to be able to use this function without an `RpmOstreeContext`. Prep for future patch. --- src/libpriv/rpmostree-core.cxx | 67 +++++++++++++++++++--------------- src/libpriv/rpmostree-core.h | 4 ++ 2 files changed, 41 insertions(+), 30 deletions(-) diff --git a/src/libpriv/rpmostree-core.cxx b/src/libpriv/rpmostree-core.cxx index 90e9cd21..79718ead 100644 --- a/src/libpriv/rpmostree-core.cxx +++ b/src/libpriv/rpmostree-core.cxx @@ -2443,14 +2443,14 @@ rpmostree_context_get_state_sha512 (RpmOstreeContext *self, } static GHashTable * -gather_source_to_packages (RpmOstreeContext *self) +gather_source_to_packages (GPtrArray *packages) { g_autoptr(GHashTable) source_to_packages = g_hash_table_new_full (NULL, NULL, NULL, (GDestroyNotify)g_ptr_array_unref); - for (guint i = 0; i < self->pkgs_to_download->len; i++) + for (guint i = 0; i < packages->len; i++) { - auto pkg = static_cast(self->pkgs_to_download->pdata[i]); + auto pkg = static_cast(packages->pdata[i]); DnfRepo *src = dnf_package_get_repo (pkg); GPtrArray *source_packages; @@ -2468,6 +2468,39 @@ gather_source_to_packages (RpmOstreeContext *self) return util::move_nullify (source_to_packages); } +gboolean +rpmostree_download_packages (GPtrArray *packages, + GCancellable *cancellable, + GError **error) +{ + guint progress_sigid; + g_autoptr(GHashTable) source_to_packages = gather_source_to_packages (packages); + GLNX_HASH_TABLE_FOREACH_KV (source_to_packages, DnfRepo*, src, GPtrArray*, src_packages) + { + g_autofree char *target_dir = NULL; + glnx_unref_object DnfState *hifstate = dnf_state_new (); + + progress_sigid = g_signal_connect (hifstate, "percentage-changed", + G_CALLBACK (on_hifstate_percentage_changed), + NULL); + g_auto(RpmOstreeProgress) progress = { 0, }; + rpmostree_output_progress_percent_begin (&progress, "Downloading from '%s'", + dnf_repo_get_id (src)); + + target_dir = g_build_filename (dnf_repo_get_location (src), "/packages/", NULL); + if (!glnx_shutil_mkdir_p_at (AT_FDCWD, target_dir, 0755, cancellable, error)) + return FALSE; + + if (!dnf_repo_download_packages (src, src_packages, target_dir, + hifstate, error)) + return FALSE; + + g_signal_handler_disconnect (hifstate, progress_sigid); + } + + return TRUE; +} + gboolean rpmostree_context_download (RpmOstreeContext *self, GCancellable *cancellable, @@ -2485,33 +2518,7 @@ rpmostree_context_download (RpmOstreeContext *self, else return TRUE; - { guint progress_sigid; - g_autoptr(GHashTable) source_to_packages = gather_source_to_packages (self); - GLNX_HASH_TABLE_FOREACH_KV (source_to_packages, DnfRepo*, src, GPtrArray*, src_packages) - { - g_autofree char *target_dir = NULL; - glnx_unref_object DnfState *hifstate = dnf_state_new (); - - progress_sigid = g_signal_connect (hifstate, "percentage-changed", - G_CALLBACK (on_hifstate_percentage_changed), - NULL); - g_auto(RpmOstreeProgress) progress = { 0, }; - rpmostree_output_progress_percent_begin (&progress, "Downloading from '%s'", - dnf_repo_get_id (src)); - - target_dir = g_build_filename (dnf_repo_get_location (src), "/packages/", NULL); - if (!glnx_shutil_mkdir_p_at (AT_FDCWD, target_dir, 0755, cancellable, error)) - return FALSE; - - if (!dnf_repo_download_packages (src, src_packages, target_dir, - hifstate, error)) - return FALSE; - - g_signal_handler_disconnect (hifstate, progress_sigid); - } - } - - return TRUE; + return rpmostree_download_packages (self->pkgs_to_download, cancellable, error); } /* Returns: (transfer none): The rojig package */ diff --git a/src/libpriv/rpmostree-core.h b/src/libpriv/rpmostree-core.h index c0dde196..fb347622 100644 --- a/src/libpriv/rpmostree-core.h +++ b/src/libpriv/rpmostree-core.h @@ -186,6 +186,10 @@ rpmostree_context_set_vlockmap (RpmOstreeContext *self, GHashTable *map, gboolean strict); +gboolean rpmostree_download_packages (GPtrArray *packages, + GCancellable *cancellable, + GError **error); + gboolean rpmostree_context_download (RpmOstreeContext *self, GCancellable *cancellable, GError **error);