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



commit df567cce717f9654e4792e0ed2fd18278fa205b9
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        | 27 ++++++++++++++++++++++++---
 plugins/flatpak/gs-plugin-flatpak.c | 28 ++++++++++++++++++++++++++++
 2 files changed, 52 insertions(+), 3 deletions(-)
---
diff --git a/plugins/flatpak/gs-flatpak.c b/plugins/flatpak/gs-flatpak.c
index ce6a6615d..832025caa 100644
--- a/plugins/flatpak/gs-flatpak.c
+++ b/plugins/flatpak/gs-flatpak.c
@@ -2125,13 +2125,14 @@ gs_flatpak_refine_app_state (GsFlatpak *self,
 }
 
 static GsApp *
-gs_flatpak_create_runtime (GsFlatpak *self, GsApp *parent, const gchar *runtime)
+gs_flatpak_create_runtime (GsFlatpak *self, GsApp *parent, const gchar *runtime, 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);
@@ -2146,6 +2147,24 @@ gs_flatpak_create_runtime (GsFlatpak *self, GsApp *parent, const gchar *runtime)
        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 (self->installation,
+                                                                  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) {
@@ -2183,6 +2202,7 @@ gs_flatpak_set_app_metadata (GsFlatpak *self,
                             GsApp *app,
                             const gchar *data,
                             gsize length,
+                            GCancellable *cancellable,
                             GError **error)
 {
        gboolean secure = TRUE;
@@ -2236,7 +2256,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);
+       app_runtime = gs_flatpak_create_runtime (self, app, runtime, cancellable);
        if (app_runtime != NULL) {
                gs_plugin_refine_item_scope (self, app_runtime);
                gs_app_set_runtime (app, app_runtime);
@@ -2343,7 +2363,7 @@ gs_plugin_refine_item_metadata (GsFlatpak *self,
        }
 
        /* parse key file */
-       if (!gs_flatpak_set_app_metadata (self, app, str, len, error))
+       if (!gs_flatpak_set_app_metadata (self, app, str, len, cancellable, error))
                return FALSE;
        return TRUE;
 }
@@ -3252,6 +3272,7 @@ gs_flatpak_file_to_app_bundle (GsFlatpak *self,
        if (!gs_flatpak_set_app_metadata (self, app,
                                          g_bytes_get_data (metadata, NULL),
                                          g_bytes_get_size (metadata),
+                                         cancellable,
                                          error))
                return NULL;
 
diff --git a/plugins/flatpak/gs-plugin-flatpak.c b/plugins/flatpak/gs-plugin-flatpak.c
index 364bc27d1..356ab5d3b 100644
--- a/plugins/flatpak/gs-plugin-flatpak.c
+++ b/plugins/flatpak/gs-plugin-flatpak.c
@@ -1041,6 +1041,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),
+                                               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]