[gnome-boxes] Simplify and robustify Util::keyfile_save



commit 8f6c839175ddd90070462538ffa6495807499561
Author: Christophe Fergeau <cfergeau redhat com>
Date:   Fri Sep 7 11:54:17 2012 +0200

    Simplify and robustify Util::keyfile_save
    
    The previous implementation was deleting the keyfile and then
    trying to write to it. This means we can endup with an empty/partial
    keyfile if an error occurs during the writing. g_file_set_contents
    will write to a temporary file and then atomatically replace the
    existing file which is much better. This also makes this function
    shorter.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=683558

 src/util.vala |   14 +++-----------
 1 files changed, 3 insertions(+), 11 deletions(-)
---
diff --git a/src/util.vala b/src/util.vala
index c0f13af..5f210a3 100644
--- a/src/util.vala
+++ b/src/util.vala
@@ -102,18 +102,10 @@ namespace Boxes {
 
     public bool keyfile_save (KeyFile key_file, string file_name, bool overwrite = false) {
         try {
-            var file = File.new_for_path (file_name);
-
-            if (file.query_exists ())
-                if (!overwrite)
-                    return false;
-                else
-                    file.delete ();
-
-            var stream = new DataOutputStream (file.create (FileCreateFlags.REPLACE_DESTINATION));
-            stream.put_string (key_file.to_data (null));
+            if (!overwrite && FileUtils.test (file_name, FileTest.EXISTS))
+                return false;
 
-            return true;
+            return FileUtils.set_contents(file_name, key_file.to_data (null));
         } catch (GLib.Error error) {
             warning (error.message);
             return false;



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