[gnome-software/wip/mcrha/flatpak-runtime-origin] flatpak: Prefer runtime from the same origin as the application




commit 55d86cb4fbad18d01eedfb2c5870a9fc62df1eb3
Author: Milan Crha <mcrha redhat com>
Date:   Mon Jun 21 21:28:58 2021 +0200

    flatpak: Prefer runtime from the same origin as the application
    
    When installing an application, install also the runtime, preferably
    from the same origin as the application. This avoids pulling the runtime
    from a different origin, when there are two or more remotes providing
    the same runtime.

 plugins/flatpak/gs-flatpak-transaction.c | 11 ++++++++++-
 plugins/flatpak/gs-flatpak.c             | 28 +++++++++++++++++++++++++---
 2 files changed, 35 insertions(+), 4 deletions(-)
---
diff --git a/plugins/flatpak/gs-flatpak-transaction.c b/plugins/flatpak/gs-flatpak-transaction.c
index 7377f6d48..2e6bf1fe6 100644
--- a/plugins/flatpak/gs-flatpak-transaction.c
+++ b/plugins/flatpak/gs-flatpak-transaction.c
@@ -638,7 +638,16 @@ _transaction_choose_remote_for_ref (FlatpakTransaction *transaction,
                                    const char *runtime_ref,
                                    const char * const *remotes)
 {
-       //FIXME: do something smarter
+       GsApp *app = gs_flatpak_transaction_get_app_by_ref (transaction, for_ref);
+       if (app != NULL && gs_app_get_origin (app) != NULL) {
+               const gchar *app_origin = gs_app_get_origin (app);
+               for (guint i = 0; remotes[i] != NULL; i++) {
+                       /* prefer the same origin as the to-be-installed app */
+                       if (g_strcmp0 (app_origin, remotes[i]) == 0)
+                               return i;
+               }
+       }
+       /* Fallback to use the first provided remote */
        return 0;
 }
 
diff --git a/plugins/flatpak/gs-flatpak.c b/plugins/flatpak/gs-flatpak.c
index 3b3b641fa..0dcd211f1 100644
--- a/plugins/flatpak/gs-flatpak.c
+++ b/plugins/flatpak/gs-flatpak.c
@@ -2287,13 +2287,15 @@ static GsApp *
 gs_flatpak_create_runtime (GsFlatpak   *self,
                            GsApp       *parent,
                            const gchar *runtime,
-                           gboolean     interactive)
+                           gboolean     interactive,
+                           GCancellable *cancellable)
 {
        g_autofree gchar *source = NULL;
        g_auto(GStrv) split = NULL;
        g_autoptr(GsApp) app_cache = NULL;
        g_autoptr(GsApp) app = NULL;
        g_autoptr(GError) local_error = NULL;
+       const gchar *origin;
 
        /* get the name/arch/branch */
        split = g_strsplit (runtime, "/", -1);
@@ -2308,6 +2310,24 @@ gs_flatpak_create_runtime (GsFlatpak   *self,
        gs_app_set_kind (app, AS_COMPONENT_KIND_RUNTIME);
        gs_app_set_branch (app, split[2]);
 
+       origin = gs_app_get_origin (parent);
+       if (origin != NULL) {
+               g_autoptr(FlatpakRemoteRef) xref = NULL;
+
+               xref = flatpak_installation_fetch_remote_ref_sync (gs_flatpak_get_installation (self, 
interactive),
+                                                                  origin,
+                                                                  FLATPAK_REF_KIND_RUNTIME,
+                                                                  gs_app_get_id (app),
+                                                                  gs_flatpak_app_get_ref_arch (parent),
+                                                                  gs_app_get_branch (app),
+                                                                  cancellable,
+                                                                  NULL);
+
+               /* Prefer runtime from the same origin as the parent application */
+               if (xref)
+                       gs_app_set_origin (app, origin);
+       }
+
        /* search in the cache */
        app_cache = gs_plugin_cache_lookup (self->plugin, gs_app_get_unique_id (app));
        if (app_cache != NULL) {
@@ -2346,6 +2366,7 @@ gs_flatpak_set_app_metadata (GsFlatpak *self,
                             const gchar *data,
                             gsize length,
                             gboolean interactive,
+                            GCancellable *cancellable,
                             GError **error)
 {
        gboolean secure = TRUE;
@@ -2399,7 +2420,7 @@ gs_flatpak_set_app_metadata (GsFlatpak *self,
                gs_app_add_kudo (app, GS_APP_KUDO_SANDBOXED_SECURE);
 
        /* create runtime */
-       app_runtime = gs_flatpak_create_runtime (self, app, runtime, interactive);
+       app_runtime = gs_flatpak_create_runtime (self, app, runtime, interactive, cancellable);
        if (app_runtime != NULL) {
                gs_plugin_refine_item_scope (self, app_runtime);
                gs_app_set_runtime (app, app_runtime);
@@ -2505,7 +2526,7 @@ gs_plugin_refine_item_metadata (GsFlatpak *self,
        }
 
        /* parse key file */
-       if (!gs_flatpak_set_app_metadata (self, app, str, len, interactive, error))
+       if (!gs_flatpak_set_app_metadata (self, app, str, len, interactive, cancellable, error))
                return FALSE;
        return TRUE;
 }
@@ -3477,6 +3498,7 @@ gs_flatpak_file_to_app_bundle (GsFlatpak *self,
                                          g_bytes_get_data (metadata, NULL),
                                          g_bytes_get_size (metadata),
                                          interactive,
+                                         cancellable,
                                          error))
                return NULL;
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]