[tracker/writeback-miner-interaction: 2/3] writeback: guarantee atomic updates on the file



commit 188a66833e3391936fce9e4b67d1ecf0b63cf244
Author: Carlos Garnacho <carlosg gnome org>
Date:   Tue Jun 7 17:32:04 2011 +0200

    writeback: guarantee atomic updates on the file
    
    We do this by copying the file to a temporary location, modifying
    the temporary file, and moving it onto the original location.
    
    This generally should be done by the libraries we use in
    tracker-writeback, but we don't get any guarantees that it'll be
    like that...

 src/tracker-writeback/tracker-writeback-file.c |   44 +++++++++++++++++++++---
 1 files changed, 39 insertions(+), 5 deletions(-)
---
diff --git a/src/tracker-writeback/tracker-writeback-file.c b/src/tracker-writeback/tracker-writeback-file.c
index 9ed5a97..60175b5 100644
--- a/src/tracker-writeback/tracker-writeback-file.c
+++ b/src/tracker-writeback/tracker-writeback-file.c
@@ -59,6 +59,27 @@ file_unlock_cb (gpointer user_data)
 	return FALSE;
 }
 
+static GFile *
+get_tmp_file (GFile *file)
+{
+	GFile *tmp_file, *parent;
+	gchar *tmp_name, *name;
+
+	/* Create a temporary, hidden file
+	 * within the same directory */
+	parent = g_file_get_parent (file);
+	name = g_file_get_basename (file);
+
+	tmp_name = g_strdup_printf ("._tracker_%s", name);
+	tmp_file = g_file_get_child (parent, tmp_name);
+
+	g_object_unref (parent);
+	g_free (tmp_name);
+	g_free (name);
+
+	return tmp_file;
+}
+
 static gboolean
 tracker_writeback_file_update_metadata (TrackerWriteback        *writeback,
                                         GPtrArray               *values,
@@ -66,7 +87,7 @@ tracker_writeback_file_update_metadata (TrackerWriteback        *writeback,
 {
 	TrackerWritebackFileClass *writeback_file_class;
 	gboolean retval;
-	GFile *file;
+	GFile *file, *tmp_file;
 	GFileInfo *file_info;
 	const gchar *urls[2] = { NULL, NULL };
 	GStrv row;
@@ -98,9 +119,16 @@ tracker_writeback_file_update_metadata (TrackerWriteback        *writeback,
 	                               NULL, NULL);
 
 	if (!file_info) {
-		if (file) {
-			g_object_unref (file);
-		}
+		g_object_unref (file);
+		return FALSE;
+	}
+
+	/* Copy to a temporary file so we can perform an atomic write on move */
+	tmp_file = get_tmp_file (file);
+	if (!g_file_copy (file, tmp_file, 0,
+			  NULL, NULL, NULL, NULL)) {
+		g_object_unref (file);
+		g_object_unref (tmp_file);
 		return FALSE;
 	}
 
@@ -158,7 +186,7 @@ tracker_writeback_file_update_metadata (TrackerWriteback        *writeback,
 		 */
 
 		retval = (writeback_file_class->update_file_metadata) (TRACKER_WRITEBACK_FILE (writeback),
-		                                                       file, values, connection);
+		                                                       tmp_file, values, connection);
 
 		/*
 		 * This timeout value was 3s before, which could have been in
@@ -178,6 +206,12 @@ tracker_writeback_file_update_metadata (TrackerWriteback        *writeback,
 		g_timeout_add_seconds (1, file_unlock_cb, g_object_ref (file));
 	}
 
+	/* Move back the modified file to the original location */
+	g_file_move (tmp_file, file,
+		     G_FILE_COPY_OVERWRITE,
+		     NULL, NULL, NULL, NULL);
+
+	g_object_unref (tmp_file);
 	g_object_unref (file);
 
 	return retval;



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