[gnome-software/gnome-3-24] snap: Cancel snapd transaction when install/remove cancelled



commit a0ae57fb2febee8fccd8f31206cc9b3f2cde3c7f
Author: Robert Ancell <robert ancell canonical com>
Date:   Fri Oct 27 14:29:29 2017 +1300

    snap: Cancel snapd transaction when install/remove cancelled

 plugins/snap/gs-snapd.c |   67 +++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 59 insertions(+), 8 deletions(-)
---
diff --git a/plugins/snap/gs-snapd.c b/plugins/snap/gs-snapd.c
index 924b8ef..8057ac3 100644
--- a/plugins/snap/gs-snapd.c
+++ b/plugins/snap/gs-snapd.c
@@ -576,9 +576,9 @@ gs_snapd_get_interfaces (const gchar *macaroon, gchar **discharges, GCancellable
 }
 
 static JsonObject *
-get_changes (const gchar *macaroon, gchar **discharges,
-            const gchar *change_id,
-            GCancellable *cancellable, GError **error)
+get_change (const gchar *macaroon, gchar **discharges,
+           const gchar *change_id,
+           GCancellable *cancellable, GError **error)
 {
        g_autofree gchar *path = NULL;
        guint status_code;
@@ -621,6 +621,52 @@ get_changes (const gchar *macaroon, gchar **discharges,
        return json_object_ref (result);
 }
 
+static JsonObject *
+abort_change (const gchar *macaroon, gchar **discharges,
+             const gchar *change_id,
+             GCancellable *cancellable, GError **error)
+{
+       g_autofree gchar *path = NULL;
+       guint status_code;
+       g_autofree gchar *reason_phrase = NULL;
+       g_autofree gchar *response_type = NULL;
+       g_autofree gchar *response = NULL;
+       g_autoptr(JsonParser) parser = NULL;
+       JsonObject *root, *result;
+
+       path = g_strdup_printf ("/v2/changes/%s", change_id);
+       if (!send_request ("POST", path, "{\"action\": \"abort\"}",
+                          macaroon, discharges,
+                          &status_code, &reason_phrase,
+                          &response_type, &response, NULL,
+                          cancellable, error))
+               return NULL;
+
+       if (status_code != SOUP_STATUS_OK) {
+               g_set_error (error,
+                            GS_PLUGIN_ERROR,
+                            GS_PLUGIN_ERROR_FAILED,
+                            "snapd returned status code %u: %s",
+                            status_code, reason_phrase);
+               return NULL;
+       }
+
+       parser = parse_result (response, response_type, error);
+       if (parser == NULL)
+               return NULL;
+       root = json_node_get_object (json_parser_get_root (parser));
+       result = json_object_get_object_member (root, "result");
+       if (result == NULL) {
+               g_set_error (error,
+                            GS_PLUGIN_ERROR,
+                            GS_PLUGIN_ERROR_FAILED,
+                            "snapd returned no result");
+               return NULL;
+       }
+
+       return json_object_ref (result);
+}
+
 static gboolean
 send_package_action (const gchar *macaroon,
                     gchar **discharges,
@@ -647,7 +693,7 @@ send_package_action (const gchar *macaroon,
                           macaroon, discharges,
                           &status_code, &reason_phrase,
                           &response_type, &response, NULL,
-                          cancellable, error))
+                          NULL, error))
                return FALSE;
 
        if (status_code == SOUP_STATUS_UNAUTHORIZED) {
@@ -680,10 +726,15 @@ send_package_action (const gchar *macaroon,
                change_id = json_object_get_string_member (root, "change");
 
                while (TRUE) {
-                       /* Wait for a little bit before polling */
-                       g_usleep (100 * 1000);
-
-                       result = get_changes (macaroon, discharges, change_id, cancellable, error);
+                       if (g_cancellable_is_cancelled (cancellable)) {
+                               result = abort_change (macaroon, discharges, change_id, NULL, error);
+                        }
+                       else {
+                               /* Wait for a little bit before polling */
+                               g_usleep (100 * 1000);
+
+                               result = get_change (macaroon, discharges, change_id, NULL, error);
+                       }
                        if (result == NULL)
                                return FALSE;
 


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