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




commit a01bcec33e0b8d0028bb220b88815d67d87c5965
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.c        | 28 +++++++++++++++++++++++++---
 plugins/flatpak/gs-plugin-flatpak.c | 28 ++++++++++++++++++++++++++++
 2 files changed, 53 insertions(+), 3 deletions(-)
---
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;
 
diff --git a/plugins/flatpak/gs-plugin-flatpak.c b/plugins/flatpak/gs-plugin-flatpak.c
index 15e60404d..bc966ad64 100644
--- a/plugins/flatpak/gs-plugin-flatpak.c
+++ b/plugins/flatpak/gs-plugin-flatpak.c
@@ -1320,6 +1320,34 @@ gs_plugin_app_install (GsPlugin *plugin,
                                gs_flatpak_error_convert (error);
                                return FALSE;
                        }
+               } else {
+                       GsApp *runtime;
+
+                       runtime = gs_app_get_runtime (app);
+                       if (runtime != NULL && gs_app_get_origin (runtime) != NULL) {
+                               g_autoptr(FlatpakInstalledRef) runtime_ref = NULL;
+
+                               runtime_ref = flatpak_installation_get_installed_ref 
(gs_flatpak_get_installation (flatpak, interactive),
+                                               gs_flatpak_app_get_ref_kind (runtime),
+                                               gs_flatpak_app_get_ref_name (runtime),
+                                               gs_flatpak_app_get_ref_arch (runtime),
+                                               gs_app_get_branch (runtime),
+                                               cancellable,
+                                               NULL);
+                               if (runtime_ref == NULL) {
+                                       g_clear_pointer (&ref, g_free);
+                                       ref = gs_flatpak_app_get_ref_display (runtime);
+                                       if (!flatpak_transaction_add_install (transaction, gs_app_get_origin 
(runtime), ref, NULL, &error_local)) {
+                                               if (g_error_matches (error_local, FLATPAK_ERROR, 
FLATPAK_ERROR_ALREADY_INSTALLED)) {
+                                                       g_clear_error (&error_local);
+                                               } else {
+                                                       g_propagate_error (error, g_steal_pointer 
(&error_local));
+                                                       gs_flatpak_error_convert (error);
+                                                       return FALSE;
+                                               }
+                                       }
+                               }
+                       }
                }
        }
 


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