[gnome-builder] unsaved-files: replace temp files atomically



commit a6ba90fc6a616f0e96de55697dcb5cc598964b26
Author: Christian Hergert <chergert redhat com>
Date:   Sun Sep 17 11:36:47 2017 -0700

    unsaved-files: replace temp files atomically
    
    https://bugzilla.gnome.org/show_bug.cgi?id=787792

 libide/buffers/ide-unsaved-files.c |   35 ++++++++++++++++++++++++-----------
 1 files changed, 24 insertions(+), 11 deletions(-)
---
diff --git a/libide/buffers/ide-unsaved-files.c b/libide/buffers/ide-unsaved-files.c
index 483cf13..e3a6a6c 100644
--- a/libide/buffers/ide-unsaved-files.c
+++ b/libide/buffers/ide-unsaved-files.c
@@ -130,17 +130,30 @@ unsaved_file_save (UnsavedFile  *uf,
                    const gchar  *path,
                    GError      **error)
 {
-  gboolean ret;
+  g_autoptr(GFile) file = NULL;
 
-  g_assert (uf);
-  g_assert (uf->content);
-  g_assert (path);
+  g_assert (uf != NULL);
+  g_assert (uf->content != NULL);
+  g_assert (path != NULL);
 
-  ret = g_file_set_contents (path,
-                             g_bytes_get_data (uf->content, NULL),
-                             g_bytes_get_size (uf->content),
-                             error);
-  return ret;
+  /*
+   * These files can be accessed by third-party programs. So we need to ensure
+   * those programs see either the old version of the file or the new version
+   * of the file. g_file_replace_contents() conveniently provides the atomic
+   * rename() for us.
+   */
+
+  file = g_file_new_for_path (path);
+
+  return g_file_replace_contents (file,
+                                  g_bytes_get_data (uf->content, NULL),
+                                  g_bytes_get_size (uf->content),
+                                  NULL,
+                                  FALSE,
+                                  G_FILE_CREATE_REPLACE_DESTINATION,
+                                  NULL,
+                                  NULL,
+                                  error);
 }
 
 static gchar *


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