[gnome-software/jrocha/fix-install-queue: 3/3] Fix updating the installation queue on disk



commit a28de2d0ece7b509933c09091cddc219f1f2836b
Author: Joaquim Rocha <jrocha endlessm com>
Date:   Fri Feb 16 22:04:30 2018 +0100

    Fix updating the installation queue on disk
    
    When an app is removed from the installation queue, it was not updating
    the corresponding file on disk. So if an app was uninstalled, the next
    time GNOME Software loaded the pending apps, the list would still
    contain that previously uninstalled application.
    
    This patch ensures that when an app is removed from the pending apps
    queue in memory, the queue is also saved on disk.

 lib/gs-plugin-loader.c | 84 +++++++++++++++++++++++++++-----------------------
 1 file changed, 46 insertions(+), 38 deletions(-)
---
diff --git a/lib/gs-plugin-loader.c b/lib/gs-plugin-loader.c
index c335fd0a..ed6ad779 100644
--- a/lib/gs-plugin-loader.c
+++ b/lib/gs-plugin-loader.c
@@ -1731,6 +1731,42 @@ remove_pending_app (GsPluginLoader *plugin_loader,
        return FALSE;
 }
 
+static void
+save_install_queue (GsPluginLoader *plugin_loader)
+{
+       GsPluginLoaderPrivate *priv = gs_plugin_loader_get_instance_private (plugin_loader);
+       GPtrArray *pending_apps;
+       gboolean ret;
+       g_autoptr(GError) error = NULL;
+       g_autoptr(GString) s = NULL;
+       g_autofree gchar *file = NULL;
+
+       s = g_string_new ("");
+       pending_apps = priv->pending_apps;
+       g_mutex_lock (&priv->pending_apps_mutex);
+       for (guint i = 0; i < pending_apps->len; ++i) {
+               const gchar *id = g_ptr_array_index (pending_apps, i);
+               g_string_append (s, id);
+               g_string_append_c (s, '\n');
+       }
+       g_mutex_unlock (&priv->pending_apps_mutex);
+
+       /* save file */
+       file = g_build_filename (g_get_user_data_dir (),
+                                "gnome-software",
+                                "install-queue",
+                                NULL);
+       if (!gs_mkdir_parent (file, &error)) {
+               g_warning ("failed to create dir for %s: %s",
+                          file, error->message);
+               return;
+       }
+       g_debug ("saving install queue to %s", file);
+       ret = g_file_set_contents (file, s->str, (gssize) s->len, &error);
+       if (!ret)
+               g_warning ("failed to save install queue: %s", error->message);
+}
+
 static void
 gs_plugin_loader_pending_apps_remove (GsPluginLoader *plugin_loader,
                                      GsPluginLoaderHelper *helper)
@@ -1738,11 +1774,15 @@ gs_plugin_loader_pending_apps_remove (GsPluginLoader *plugin_loader,
        GsAppList *list = gs_plugin_job_get_list (helper->plugin_job);
        GsPluginLoaderPrivate *priv = gs_plugin_loader_get_instance_private (plugin_loader);
        g_autoptr(GMutexLocker) locker = g_mutex_locker_new (&priv->pending_apps_mutex);
+       gboolean save_queue = FALSE;
 
        g_assert (gs_app_list_length (list) > 0);
        for (guint i = 0; i < gs_app_list_length (list); i++) {
                GsApp *app = gs_app_list_index (list, i);
-               remove_pending_app (plugin_loader, gs_app_get_unique_id (app));
+               if (gs_app_get_state (app) != AS_APP_STATE_QUEUED_FOR_INSTALL) {
+                       save_queue = TRUE;
+                       remove_pending_app (plugin_loader, gs_app_get_unique_id (app));
+               }
 
                /* check the app is not still in an action helper */
                switch (gs_app_get_state (app)) {
@@ -1758,6 +1798,10 @@ gs_plugin_loader_pending_apps_remove (GsPluginLoader *plugin_loader,
                }
 
        }
+       if (save_queue) {
+               g_clear_pointer (&locker, g_mutex_locker_free);
+               save_install_queue (plugin_loader);
+       }
        g_idle_add (emit_pending_apps_idle, g_object_ref (plugin_loader));
 }
 
@@ -1811,42 +1855,6 @@ load_install_queue (GsPluginLoader *plugin_loader, GError **error)
        return TRUE;
 }
 
-static void
-save_install_queue (GsPluginLoader *plugin_loader)
-{
-       GsPluginLoaderPrivate *priv = gs_plugin_loader_get_instance_private (plugin_loader);
-       GPtrArray *pending_apps;
-       gboolean ret;
-       g_autoptr(GError) error = NULL;
-       g_autoptr(GString) s = NULL;
-       g_autofree gchar *file = NULL;
-
-       s = g_string_new ("");
-       pending_apps = priv->pending_apps;
-       g_mutex_lock (&priv->pending_apps_mutex);
-       for (guint i = 0; i < pending_apps->len; ++i) {
-               const gchar *id = g_ptr_array_index (pending_apps, i);
-               g_string_append (s, id);
-               g_string_append_c (s, '\n');
-       }
-       g_mutex_unlock (&priv->pending_apps_mutex);
-
-       /* save file */
-       file = g_build_filename (g_get_user_data_dir (),
-                                "gnome-software",
-                                "install-queue",
-                                NULL);
-       if (!gs_mkdir_parent (file, &error)) {
-               g_warning ("failed to create dir for %s: %s",
-                          file, error->message);
-               return;
-       }
-       g_debug ("saving install queue to %s", file);
-       ret = g_file_set_contents (file, s->str, (gssize) s->len, &error);
-       if (!ret)
-               g_warning ("failed to save install queue: %s", error->message);
-}
-
 static void
 add_app_to_install_queue (GsPluginLoader *plugin_loader, GsApp *app)
 {
@@ -1938,7 +1946,7 @@ gs_plugin_loader_get_pending (GsPluginLoader *plugin_loader)
        g_mutex_lock (&priv->pending_apps_mutex);
        for (i = 0; i < priv->pending_apps->len; i++) {
                const gchar *id = g_ptr_array_index (priv->pending_apps, i);
-               GsApp *app = gs_app_list_lookup (priv->global_cache, id);
+               GsApp *app = gs_plugin_loader_app_create (plugin_loader, id);
                if (app != NULL)
                        gs_app_list_add (array, app);
        }


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