[gtksourceview/wip/loader-saver] FileSaver: manually cancel output stream on error



commit 866a3304a1edb74984caeb589d97797903561d40
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Thu Dec 26 15:04:32 2013 +0100

    FileSaver: manually cancel output stream on error

 gtksourceview/gtksourcefilesaver.c |   87 +++++++++++------------------------
 1 files changed, 28 insertions(+), 59 deletions(-)
---
diff --git a/gtksourceview/gtksourcefilesaver.c b/gtksourceview/gtksourcefilesaver.c
index ba98d29..a159abd 100644
--- a/gtksourceview/gtksourcefilesaver.c
+++ b/gtksourceview/gtksourcefilesaver.c
@@ -69,6 +69,12 @@ struct _GtkSourceFileSaverPrivate
 
        GTask *task;
 
+       /* This field is used when cancelling the output stream: an error occurs
+        * and is stored in this field, the output stream is cancelled
+        * asynchronously, and then the error is reported to the task.
+        */
+       GError *error;
+
        goffset total_size;
        GFileProgressCallback progress_cb;
        gpointer progress_cb_data;
@@ -77,8 +83,6 @@ struct _GtkSourceFileSaverPrivate
        gssize chunk_bytes_read;
        gssize chunk_bytes_written;
 
-       GCancellable *cancellable;
-
        /* The output_stream contains the required converter(s) for the encoding
         * and the compression type. The input_stream is the
         * GtkSourceBufferInputStream (thus in UTF-8, without compression).
@@ -329,7 +333,7 @@ gtk_source_file_saver_new (GtkTextBuffer            *buffer,
  * the save should be aborted (there could be a conversion error). The patch
  * explicitly closes the output stream in all these cases with a GCancellable in
  * the cancelled state, causing the output stream to close, but not move the
- * file. This makes use of an implementation detail in the local  file stream
+ * file. This makes use of an implementation detail in the local file stream
  * and should be properly fixed by adding the appropriate API in GIO. Until
  * then, at least we prevent data corruption for now.
  *
@@ -342,31 +346,27 @@ gtk_source_file_saver_new (GtkTextBuffer            *buffer,
  *              errors while writing (glib/gio)
  * https://bugzilla.gnome.org/show_bug.cgi?id=602412
  */
-#if 0
 static void
-cancel_output_stream_ready_cb (GOutputStream      *stream,
+cancel_output_stream_ready_cb (GOutputStream      *output_stream,
                               GAsyncResult       *result,
                               GtkSourceFileSaver *saver)
 {
-       GError *error = NULL;
+       g_output_stream_close_finish (output_stream, result, NULL);
 
-       g_output_stream_close_finish (stream, result, NULL);
-
-       /* check cancelled state manually */
-       if (g_cancellable_is_cancelled (saver->priv->cancellable) || async->error == NULL)
+       if (saver->priv->error != NULL)
        {
-               async_data_free (async);
-               return;
+               GError *error = saver->priv->error;
+               saver->priv->error = NULL;
+               g_task_return_error (saver->priv->task, error);
+       }
+       else
+       {
+               g_task_return_boolean (saver->priv->task, FALSE);
        }
-
-       error = async->error;
-       async->error = NULL;
-
-       async_failed (async, error);
 }
 
 static void
-cancel_output_stream (AsyncData *async)
+cancel_output_stream (GtkSourceFileSaver *saver)
 {
        GCancellable *cancellable;
 
@@ -377,28 +377,15 @@ cancel_output_stream (AsyncData *async)
        cancellable = g_cancellable_new ();
        g_cancellable_cancel (cancellable);
 
-       g_output_stream_close_async (async->saver->priv->output_stream,
-                                    G_PRIORITY_HIGH,
+       g_output_stream_close_async (saver->priv->output_stream,
+                                    g_task_get_priority (saver->priv->task),
                                     cancellable,
-                                    (GAsyncReadyCallback)cancel_output_stream_ready_cb,
-                                    async);
+                                    (GAsyncReadyCallback) cancel_output_stream_ready_cb,
+                                    saver);
 
        g_object_unref (cancellable);
 }
 
-static void
-cancel_output_stream_and_fail (AsyncData *async,
-                               GError    *error)
-{
-       DEBUG ({
-              g_print ("Cancel output stream and fail\n");
-       });
-
-       g_propagate_error (&async->error, error);
-       cancel_output_stream (async);
-}
-#endif
-
 /*
  * END NOTE
  */
@@ -414,15 +401,6 @@ close_output_stream_cb (GOutputStream      *output_stream,
               g_print ("%s\n", G_STRFUNC);
        });
 
-#if 0
-       /* check cancelled state manually */
-       if (g_cancellable_is_cancelled (async->cancellable))
-       {
-               async_data_free (async);
-               return;
-       }
-#endif
-
        g_output_stream_close_finish (output_stream, result, &error);
 
        if (error != NULL)
@@ -457,11 +435,8 @@ write_complete (GtkSourceFileSaver *saver)
                       g_print ("Closing input stream error: %s\n", error->message);
                });
 
-#if 0
-               cancel_output_stream_and_fail (async, error);
-#endif
-
-               g_task_return_error (saver->priv->task, error);
+               saver->priv->error = error;
+               cancel_output_stream (saver);
                return;
        }
 
@@ -500,11 +475,8 @@ write_file_chunk_cb (GOutputStream      *output_stream,
                       g_print ("Write error: %s\n", error->message);
                });
 
-#if 0
-               cancel_output_stream_and_fail (saver, error);
-#endif
-
-               g_task_return_error (saver->priv->task, error);
+               saver->priv->error = error;
+               cancel_output_stream (saver);
                return;
        }
 
@@ -574,11 +546,8 @@ read_file_chunk (GtkSourceFileSaver *saver)
 
        if (error != NULL)
        {
-#if 0
-               cancel_output_stream_and_fail (saver, error);
-#endif
-
-               g_task_return_error (saver->priv->task, error);
+               saver->priv->error = error;
+               cancel_output_stream (saver);
                return;
        }
 


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