[gimp/gimp-2-10] app: do no overwite XCF when an error occurred at saving time.



commit c9da44bed3d8a537c14bb9a8b879e143b39dc980
Author: Jehan <jehan girinstud io>
Date:   Mon Nov 26 14:10:37 2018 +0100

    app: do no overwite XCF when an error occurred at saving time.
    
    We can cancel a file overwrite at the last second when closing the
    stream by setting a cancelled cancellable. Current code was simply not
    closing the stream, but this was not enough as overwriting was happening
    anyway (probably when finalizing).
    This will allow much safe saving process since we would not be
    overwriting a previously sane XCF file when an error occurred (either in
    our code or a memory error, or whatnot).
    See also discussion in #2565.
    
    (cherry picked from commit 076b53511a46e2a798033c03249e940dc3e95f4b)

 app/xcf/xcf.c | 25 +++++++++++++++++++------
 1 file changed, 19 insertions(+), 6 deletions(-)
---
diff --git a/app/xcf/xcf.c b/app/xcf/xcf.c
index 01fce11efb..254da0841a 100644
--- a/app/xcf/xcf.c
+++ b/app/xcf/xcf.c
@@ -342,10 +342,11 @@ xcf_save_stream (Gimp           *gimp,
                  GimpProgress   *progress,
                  GError        **error)
 {
-  XcfInfo      info     = { 0, };
-  const gchar *filename;
-  gboolean     success  = FALSE;
-  GError      *my_error = NULL;
+  XcfInfo       info     = { 0, };
+  const gchar  *filename;
+  gboolean      success  = FALSE;
+  GError       *my_error = NULL;
+  GCancellable *cancellable;
 
   g_return_val_if_fail (GIMP_IS_GIMP (gimp), FALSE);
   g_return_val_if_fail (GIMP_IS_IMAGE (image), FALSE);
@@ -384,13 +385,25 @@ xcf_save_stream (Gimp           *gimp,
 
   success = xcf_save_image (&info, image, &my_error);
 
+  cancellable = g_cancellable_new ();
   if (success)
     {
       if (progress)
         gimp_progress_set_text (progress, _("Closing '%s'"), filename);
-
-      success = g_output_stream_close (info.output, NULL, &my_error);
     }
+  else
+    {
+      /* When closing the stream, the image will be actually saved,
+       * unless we properly cancel it with a GCancellable.
+       * Not closing the stream is not an option either, as this will
+       * happen anyway when finalizing the output.
+       * So let's make sure now that we don't overwrite the XCF file
+       * when an error occurred.
+       */
+      g_cancellable_cancel (cancellable);
+    }
+  success = g_output_stream_close (info.output, cancellable, &my_error);
+  g_object_unref (cancellable);
 
   if (! success && my_error)
     g_propagate_prefixed_error (error, my_error,


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