[gnome-software/gnome-42: 3/4] flatpak: Make sure the XbSilo is non-NULL when passing it to other functions




commit 09f11d9c6a983a65526dc98051ce1f3779ae36f4
Author: Milan Crha <mcrha redhat com>
Date:   Tue Jun 7 12:06:59 2022 +0200

    flatpak: Make sure the XbSilo is non-NULL when passing it to other functions
    
    It can happen the GsFlatpak::XbSilo is NULL even after refreshing
    the GsFlatpak, thus make sure the functions properly end with an error,
    rather than expecting the XbSilo is always non-NULL.
    
    Closes https://gitlab.gnome.org/GNOME/gnome-software/-/issues/1792

 plugins/flatpak/gs-flatpak.c | 67 ++++++++++++++++++++++++++++++--------------
 1 file changed, 46 insertions(+), 21 deletions(-)
---
diff --git a/plugins/flatpak/gs-flatpak.c b/plugins/flatpak/gs-flatpak.c
index be4a1eda8..e60e61348 100644
--- a/plugins/flatpak/gs-flatpak.c
+++ b/plugins/flatpak/gs-flatpak.c
@@ -1132,6 +1132,41 @@ gs_flatpak_rescan_app_data (GsFlatpak *self,
        return TRUE;
 }
 
+/* Returns with a read lock held on @self->silo_lock on success.
+   The *locker should be NULL when being called. */
+static gboolean
+ensure_flatpak_silo_with_locker (GsFlatpak            *self,
+                                GRWLockReaderLocker **locker,
+                                gboolean              interactive,
+                                GCancellable         *cancellable,
+                                GError              **error)
+{
+       /* should not hold the lock when called */
+       g_return_val_if_fail (*locker == NULL, FALSE);
+
+       /* ensure valid */
+       if (!gs_flatpak_rescan_app_data (self, interactive, cancellable, error))
+               return FALSE;
+
+       *locker = g_rw_lock_reader_locker_new (&self->silo_lock);
+
+       while (self->silo == NULL) {
+               g_clear_pointer (locker, g_rw_lock_reader_locker_free);
+
+               if (!gs_flatpak_rescan_appstream_store (self, interactive, cancellable, error)) {
+                       gs_flatpak_internal_data_changed (self);
+                       return FALSE;
+               }
+
+               /* At this point either rescan_appstream_store() returned an error or it successfully
+                * initialised self->silo. There is the possibility that another thread will invalidate
+                * the silo before we regain the lock. If so, we’ll have to rescan again. */
+               *locker = g_rw_lock_reader_locker_new (&self->silo_lock);
+       }
+
+       return TRUE;
+}
+
 gboolean
 gs_flatpak_setup (GsFlatpak *self, GCancellable *cancellable, GError **error)
 {
@@ -3197,7 +3232,8 @@ gs_flatpak_refine_app_unlocked (GsFlatpak *self,
        if (gs_app_get_bundle_kind (app) != AS_BUNDLE_KIND_FLATPAK)
                return TRUE;
 
-       locker = g_rw_lock_reader_locker_new (&self->silo_lock);
+       if (!ensure_flatpak_silo_with_locker (self, &locker, interactive, cancellable, error))
+               return FALSE;
 
        /* always do AppStream properties */
        if (!gs_flatpak_refine_appstream (self, app, self->silo, flags, interactive, cancellable, error))
@@ -3390,12 +3426,9 @@ gs_flatpak_refine_wildcard (GsFlatpak *self, GsApp *app,
        if (id == NULL)
                return TRUE;
 
-       /* ensure valid */
-       if (!gs_flatpak_rescan_app_data (self, interactive, cancellable, error))
+       if (!ensure_flatpak_silo_with_locker (self, &locker, interactive, cancellable, error))
                return FALSE;
 
-       locker = g_rw_lock_reader_locker_new (&self->silo_lock);
-
        /* find all apps when matching any prefixes */
        xpath = g_strdup_printf ("components/component/id[text()='%s']/..", id);
        components = xb_silo_query (self->silo, xpath, 0, &error_local);
@@ -3948,10 +3981,9 @@ gs_flatpak_search (GsFlatpak *self,
        GHashTableIter iter;
        gpointer key, value;
 
-       if (!gs_flatpak_rescan_app_data (self, interactive, cancellable, error))
+       if (!ensure_flatpak_silo_with_locker (self, &locker, interactive, cancellable, error))
                return FALSE;
 
-       locker = g_rw_lock_reader_locker_new (&self->silo_lock);
        if (!gs_appstream_search (self->plugin, self->silo, values, list_tmp,
                                  cancellable, error))
                return FALSE;
@@ -4017,10 +4049,9 @@ gs_flatpak_add_category_apps (GsFlatpak *self,
 {
        g_autoptr(GRWLockReaderLocker) locker = NULL;
 
-       if (!gs_flatpak_rescan_app_data (self, interactive, cancellable, error))
+       if (!ensure_flatpak_silo_with_locker (self, &locker, interactive, cancellable, error))
                return FALSE;
 
-       locker = g_rw_lock_reader_locker_new (&self->silo_lock);
        return gs_appstream_add_category_apps (self->plugin, self->silo,
                                               category, list,
                                               cancellable, error);
@@ -4035,10 +4066,9 @@ gs_flatpak_add_categories (GsFlatpak *self,
 {
        g_autoptr(GRWLockReaderLocker) locker = NULL;
 
-       if (!gs_flatpak_rescan_app_data (self, interactive, cancellable, error))
+       if (!ensure_flatpak_silo_with_locker (self, &locker, interactive, cancellable, error))
                return FALSE;
 
-       locker = g_rw_lock_reader_locker_new (&self->silo_lock);
        return gs_appstream_add_categories (self->silo,
                                            list, cancellable, error);
 }
@@ -4053,10 +4083,9 @@ gs_flatpak_add_popular (GsFlatpak *self,
        g_autoptr(GsAppList) list_tmp = gs_app_list_new ();
        g_autoptr(GRWLockReaderLocker) locker = NULL;
 
-       if (!gs_flatpak_rescan_app_data (self, interactive, cancellable, error))
+       if (!ensure_flatpak_silo_with_locker (self, &locker, interactive, cancellable, error))
                return FALSE;
 
-       locker = g_rw_lock_reader_locker_new (&self->silo_lock);
        if (!gs_appstream_add_popular (self->silo, list_tmp,
                                       cancellable, error))
                return FALSE;
@@ -4076,10 +4105,9 @@ gs_flatpak_add_featured (GsFlatpak *self,
        g_autoptr(GsAppList) list_tmp = gs_app_list_new ();
        g_autoptr(GRWLockReaderLocker) locker = NULL;
 
-       if (!gs_flatpak_rescan_app_data (self, interactive, cancellable, error))
+       if (!ensure_flatpak_silo_with_locker (self, &locker, interactive, cancellable, error))
                return FALSE;
 
-       locker = g_rw_lock_reader_locker_new (&self->silo_lock);
        if (!gs_appstream_add_featured (self->silo, list_tmp,
                                        cancellable, error))
                return FALSE;
@@ -4100,10 +4128,9 @@ gs_flatpak_add_alternates (GsFlatpak *self,
        g_autoptr(GsAppList) list_tmp = gs_app_list_new ();
        g_autoptr(GRWLockReaderLocker) locker = NULL;
 
-       if (!gs_flatpak_rescan_app_data (self, interactive, cancellable, error))
+       if (!ensure_flatpak_silo_with_locker (self, &locker, interactive, cancellable, error))
                return FALSE;
 
-       locker = g_rw_lock_reader_locker_new (&self->silo_lock);
        if (!gs_appstream_add_alternates (self->silo, app, list_tmp,
                                          cancellable, error))
                return FALSE;
@@ -4124,10 +4151,9 @@ gs_flatpak_add_recent (GsFlatpak *self,
        g_autoptr(GsAppList) list_tmp = gs_app_list_new ();
        g_autoptr(GRWLockReaderLocker) locker = NULL;
 
-       if (!gs_flatpak_rescan_app_data (self, interactive, cancellable, error))
+       if (!ensure_flatpak_silo_with_locker (self, &locker, interactive, cancellable, error))
                return FALSE;
 
-       locker = g_rw_lock_reader_locker_new (&self->silo_lock);
        if (!gs_appstream_add_recent (self->plugin, self->silo, list_tmp, age,
                                      cancellable, error))
                return FALSE;
@@ -4149,10 +4175,9 @@ gs_flatpak_url_to_app (GsFlatpak *self,
        g_autoptr(GsAppList) list_tmp = gs_app_list_new ();
        g_autoptr(GRWLockReaderLocker) locker = NULL;
 
-       if (!gs_flatpak_rescan_app_data (self, interactive, cancellable, error))
+       if (!ensure_flatpak_silo_with_locker (self, &locker, interactive, cancellable, error))
                return FALSE;
 
-       locker = g_rw_lock_reader_locker_new (&self->silo_lock);
        if (!gs_appstream_url_to_app (self->plugin, self->silo, list_tmp, url, cancellable, error))
                return FALSE;
 


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