[tracker] libtracker-data: Use temporary directory for restore recovery



commit da74c14f810b48ba3f819dd170fb8e801e7d9373
Author: JÃrg Billeter <j bitron ch>
Date:   Mon Jun 27 11:57:48 2011 +0200

    libtracker-data: Use temporary directory for restore recovery
    
    This is now handled in tracker-data-backup, which allows us to shutdown
    TrackerDataManager before moving files to the temporary directory.

 src/libtracker-data/tracker-data-backup.c |  177 ++++++++++++++++++++-
 src/libtracker-data/tracker-db-manager.c  |  250 -----------------------------
 src/libtracker-data/tracker-db-manager.h  |    3 -
 3 files changed, 174 insertions(+), 256 deletions(-)
---
diff --git a/src/libtracker-data/tracker-data-backup.c b/src/libtracker-data/tracker-data-backup.c
index 1a7b843..e7f6503 100644
--- a/src/libtracker-data/tracker-data-backup.c
+++ b/src/libtracker-data/tracker-data-backup.c
@@ -19,6 +19,7 @@
  */
 #include "config.h"
 
+#include <errno.h>
 #include <string.h>
 
 #include <glib.h>
@@ -209,6 +210,175 @@ process_context_child_watch_cb (GPid     pid,
 	process_context_destroy (context, error);
 }
 
+/* delete all regular files from the directory */
+static void
+dir_remove_files (const gchar *path)
+{
+	GDir *dir;
+	const gchar *name;
+
+	dir = g_dir_open (path, 0, NULL);
+	if (dir == NULL) {
+		return;
+	}
+
+	while ((name = g_dir_read_name (dir)) != NULL) {
+		gchar *filename;
+
+		filename = g_build_filename (path, name, NULL);
+
+		if (g_file_test (filename, G_FILE_TEST_IS_REGULAR)) {
+			g_debug ("Removing '%s'", filename);
+			if (g_unlink (filename) == -1) {
+				g_warning ("Unable to remove '%s': %s", filename, g_strerror (errno));
+			}
+		}
+
+		g_free (filename);
+	}
+
+	g_dir_close (dir);
+}
+
+/* move all regular files from the source directory to the destination directory */
+static void
+dir_move_files (const gchar *src_path, const gchar *dest_path)
+{
+	GDir *src_dir;
+	const gchar *src_name;
+
+	src_dir = g_dir_open (src_path, 0, NULL);
+	if (src_dir == NULL) {
+		return;
+	}
+
+	while ((src_name = g_dir_read_name (src_dir)) != NULL) {
+		gchar *src_filename, *dest_filename;
+
+		src_filename = g_build_filename (src_path, src_name, NULL);
+
+		if (g_file_test (src_filename, G_FILE_TEST_IS_REGULAR)) {
+			dest_filename = g_build_filename (dest_path, src_name, NULL);
+
+			g_debug ("Renaming '%s' to '%s'", src_filename, dest_filename);
+			if (g_rename (src_filename, dest_filename) == -1) {
+				g_warning ("Unable to rename '%s' to '%s': %s", src_filename, dest_filename, g_strerror (errno));
+			}
+
+			g_free (dest_filename);
+		}
+
+		g_free (src_filename);
+	}
+
+	g_dir_close (src_dir);
+}
+
+static void
+dir_move_to_temp (const gchar *path)
+{
+	gchar *temp_dir;
+
+	temp_dir = g_build_filename (path, "tmp", NULL);
+	g_mkdir (temp_dir, 0777);
+
+	/* ensure that no obsolete temporary files are around */
+	dir_remove_files (temp_dir);
+	dir_move_files (path, temp_dir);
+
+	g_free (temp_dir);
+}
+
+static void
+dir_move_from_temp (const gchar *path)
+{
+	gchar *temp_dir;
+
+	temp_dir = g_build_filename (path, "tmp", NULL);
+
+	/* ensure that no obsolete files are around */
+	dir_remove_files (path);
+	dir_move_files (temp_dir, path);
+
+	g_rmdir (temp_dir);
+
+	g_free (temp_dir);
+}
+
+static void
+move_to_temp (void)
+{
+	gchar *data_dir, *cache_dir;
+
+	g_message ("Moving all database files to temporary location");
+
+	data_dir = g_build_filename (g_get_user_data_dir (),
+	                             "tracker",
+	                             "data",
+	                             NULL);
+
+	cache_dir = g_build_filename (g_get_user_cache_dir (),
+	                              "tracker",
+	                              NULL);
+
+	dir_move_to_temp (data_dir);
+	dir_move_to_temp (cache_dir);
+
+	g_free (cache_dir);
+	g_free (data_dir);
+}
+
+static void
+remove_temp (void)
+{
+	gchar *tmp_data_dir, *tmp_cache_dir;
+
+	g_message ("Removing all database files from temporary location");
+
+	tmp_data_dir = g_build_filename (g_get_user_data_dir (),
+	                                 "tracker",
+	                                 "data",
+	                                 "tmp",
+	                                 NULL);
+
+	tmp_cache_dir = g_build_filename (g_get_user_cache_dir (),
+	                                  "tracker",
+	                                  "tmp",
+	                                  NULL);
+
+	dir_remove_files (tmp_data_dir);
+	dir_remove_files (tmp_cache_dir);
+
+	g_rmdir (tmp_data_dir);
+	g_rmdir (tmp_cache_dir);
+
+	g_free (tmp_cache_dir);
+	g_free (tmp_data_dir);
+}
+
+static void
+restore_from_temp (void)
+{
+	gchar *data_dir, *cache_dir;
+
+	g_message ("Restoring all database files from temporary location");
+
+	data_dir = g_build_filename (g_get_user_data_dir (),
+	                             "tracker",
+	                             "data",
+	                             NULL);
+
+	cache_dir = g_build_filename (g_get_user_cache_dir (),
+	                              "tracker",
+	                              NULL);
+
+	dir_move_from_temp (data_dir);
+	dir_move_from_temp (cache_dir);
+
+	g_free (cache_dir);
+	g_free (data_dir);
+}
+
 void
 tracker_data_backup_save (GFile *destination,
                           TrackerDataBackupFinished callback,
@@ -339,9 +509,10 @@ tracker_data_backup_restore (GFile                *journal,
 
 		flags = tracker_db_manager_get_flags (&select_cache_size, &update_cache_size);
 
-		tracker_db_manager_move_to_temp ();
 		tracker_data_manager_shutdown ();
 
+		move_to_temp ();
+
 		argv = g_new0 (char*, 6);
 
 		argv[0] = g_strdup ("tar");
@@ -394,9 +565,9 @@ tracker_data_backup_restore (GFile                *journal,
 		}
 
 		if (info->error) {
-			tracker_db_manager_restore_from_temp ();
+			restore_from_temp ();
 		} else {
-			tracker_db_manager_remove_temp ();
+			remove_temp ();
 		}
 
 		tracker_db_journal_shutdown (&n_error);
diff --git a/src/libtracker-data/tracker-db-manager.c b/src/libtracker-data/tracker-db-manager.c
index 9d5441a..5fc16b4 100644
--- a/src/libtracker-data/tracker-db-manager.c
+++ b/src/libtracker-data/tracker-db-manager.c
@@ -1282,256 +1282,6 @@ tracker_db_manager_remove_all (gboolean rm_journal)
 	db_manager_remove_all (rm_journal);
 }
 
-
-void
-tracker_db_manager_move_to_temp (void)
-{
-	guint i;
-	gchar *cpath, *filename;
-	gchar *fullpath;
-	gchar *directory, *rotate_to = NULL;
-	const gchar *dirs[3] = { NULL, NULL, NULL };
-	gsize chunk_size = 0;
-	gboolean do_rotate = FALSE;
-
-	g_return_if_fail (initialized != FALSE);
-
-	g_message ("Moving all database files");
-
-	for (i = 1; i < G_N_ELEMENTS (dbs); i++) {
-		filename = g_strdup_printf ("%s.tmp", dbs[i].abs_filename);
-		g_message ("  Renaming database:'%s' -> '%s'",
-		           dbs[i].abs_filename, filename);
-		if (g_rename (dbs[i].abs_filename, filename) == -1) {
-			g_critical ("%s", g_strerror (errno));
-		}
-		g_free (filename);
-	}
-
-	cpath = g_strdup (tracker_db_journal_get_filename ());
-
-	directory = g_path_get_dirname (cpath);
-
-	tracker_db_journal_get_rotating (&do_rotate, &chunk_size, &rotate_to);
-
-	dirs[0] = directory;
-	dirs[1] = do_rotate ? rotate_to : NULL;
-
-	for (i = 0; dirs[i] != NULL; i++) {
-		GDir *journal_dir;
-		const gchar *f_name;
-
-		journal_dir = g_dir_open (dirs[i], 0, NULL);
-		f_name = g_dir_read_name (journal_dir);
-
-		while (f_name) {
-			if (f_name) {
-				if (!g_str_has_prefix (f_name, TRACKER_DB_JOURNAL_FILENAME ".")) {
-					f_name = g_dir_read_name (journal_dir);
-					continue;
-				}
-			}
-
-			fullpath = g_build_filename (dirs[i], f_name, NULL);
-			filename = g_strdup_printf ("%s.tmp", fullpath);
-			if (g_rename (fullpath, filename) == -1) {
-				g_critical ("%s", g_strerror (errno));
-			}
-			g_free (filename);
-			g_free (fullpath);
-			f_name = g_dir_read_name (journal_dir);
-		}
-
-		g_dir_close (journal_dir);
-	}
-
-	fullpath = g_build_filename (directory, TRACKER_DB_JOURNAL_ONTOLOGY_FILENAME, NULL);
-	filename = g_strdup_printf ("%s.tmp", fullpath);
-	if (g_rename (fullpath, filename) == -1) {
-		g_critical ("%s", g_strerror (errno));
-	}
-	g_free (filename);
-	g_free (fullpath);
-
-	g_free (rotate_to);
-	g_free (directory);
-
-	filename = g_strdup_printf ("%s.tmp", cpath);
-	g_message ("  Renaming journal:'%s' -> '%s'",
-	           cpath, filename);
-	if (g_rename (cpath, filename) == -1) {
-		g_critical ("%s", g_strerror (errno));
-	}
-	g_free (cpath);
-	g_free (filename);
-}
-
-
-void
-tracker_db_manager_restore_from_temp (void)
-{
-	guint i;
-	gchar *cpath, *filename;
-	gchar *fullpath;
-	gchar *directory, *rotate_to = NULL;
-	const gchar *dirs[3] = { NULL, NULL, NULL };
-	gsize chunk_size = 0;
-	gboolean do_rotate = FALSE;
-
-	g_return_if_fail (locations_initialized != FALSE);
-
-	g_message ("Moving all database files");
-
-	for (i = 1; i < G_N_ELEMENTS (dbs); i++) {
-		filename = g_strdup_printf ("%s.tmp", dbs[i].abs_filename);
-		g_message ("  Renaming database:'%s' -> '%s'",
-		           dbs[i].abs_filename, filename);
-		if (g_rename (dbs[i].abs_filename, filename) == -1) {
-			g_critical ("%s", g_strerror (errno));
-		}
-		g_free (filename);
-	}
-
-	cpath = g_strdup (tracker_db_journal_get_filename ());
-	filename = g_strdup_printf ("%s.tmp", cpath);
-	g_message ("  Renaming journal:'%s' -> '%s'",
-	           filename, cpath);
-	if (g_rename (filename, cpath) == -1) {
-		g_critical ("%s", g_strerror (errno));
-	}
-	g_free (filename);
-
-	directory = g_path_get_dirname (cpath);
-	tracker_db_journal_get_rotating (&do_rotate, &chunk_size, &rotate_to);
-
-	fullpath = g_build_filename (directory, TRACKER_DB_JOURNAL_ONTOLOGY_FILENAME, NULL);
-	filename = g_strdup_printf ("%s.tmp", fullpath);
-	if (g_rename (filename, fullpath) == -1) {
-		g_critical ("%s", g_strerror (errno));
-	}
-	g_free (filename);
-	g_free (fullpath);
-
-	dirs[0] = directory;
-	dirs[1] = do_rotate ? rotate_to : NULL;
-
-	for (i = 0; dirs[i] != NULL; i++) {
-		GDir *journal_dir;
-		const gchar *f_name;
-
-		journal_dir = g_dir_open (dirs[i], 0, NULL);
-		f_name = g_dir_read_name (journal_dir);
-
-		while (f_name) {
-			gchar *ptr;
-
-			if (f_name) {
-				if (!g_str_has_suffix (f_name, ".tmp")) {
-					f_name = g_dir_read_name (journal_dir);
-					continue;
-				}
-			}
-
-			fullpath = g_build_filename (dirs[i], f_name, NULL);
-			filename = g_strdup (fullpath);
-			ptr = strstr (filename, ".tmp");
-			if (ptr) {
-				*ptr = '\0';
-				if (g_rename (fullpath, filename) == -1) {
-					g_critical ("%s", g_strerror (errno));
-				}
-			}
-			g_free (filename);
-			g_free (fullpath);
-			f_name = g_dir_read_name (journal_dir);
-		}
-		g_dir_close (journal_dir);
-	}
-
-	g_free (rotate_to);
-	g_free (directory);
-	g_free (cpath);
-}
-
-void
-tracker_db_manager_remove_temp (void)
-{
-	guint i;
-	gchar *cpath, *filename;
-	gchar *directory, *rotate_to = NULL;
-	gsize chunk_size = 0;
-	gboolean do_rotate = FALSE;
-	const gchar *dirs[3] = { NULL, NULL, NULL };
-
-	g_return_if_fail (locations_initialized != FALSE);
-
-	g_message ("Removing all temp database files");
-
-	for (i = 1; i < G_N_ELEMENTS (dbs); i++) {
-		filename = g_strdup_printf ("%s.tmp", dbs[i].abs_filename);
-		g_message ("  Removing temp database:'%s'",
-		           filename);
-		if (g_unlink (filename) == -1) {
-			g_message ("%s", g_strerror (errno));
-		}
-		g_free (filename);
-	}
-
-	cpath = g_strdup (tracker_db_journal_get_filename ());
-	filename = g_strdup_printf ("%s.tmp", cpath);
-	g_message ("  Removing temp journal:'%s'",
-	           filename);
-	if (g_unlink (filename) == -1) {
-		g_message ("%s", g_strerror (errno));
-	}
-	g_free (filename);
-
-	directory = g_path_get_dirname (cpath);
-	tracker_db_journal_get_rotating (&do_rotate, &chunk_size, &rotate_to);
-	g_free (cpath);
-
-	cpath = g_build_filename (directory, TRACKER_DB_JOURNAL_ONTOLOGY_FILENAME, NULL);
-	filename = g_strdup_printf ("%s.tmp", cpath);
-	if (g_unlink (filename) == -1) {
-		g_message ("%s", g_strerror (errno));
-	}
-	g_free (filename);
-	g_free (cpath);
-
-	dirs[0] = directory;
-	dirs[1] = do_rotate ? rotate_to : NULL;
-
-	for (i = 0; dirs[i] != NULL; i++) {
-		GDir *journal_dir;
-		const gchar *f_name;
-
-		journal_dir = g_dir_open (dirs[i], 0, NULL);
-		f_name = g_dir_read_name (journal_dir);
-
-		while (f_name) {
-			if (f_name) {
-				if (!g_str_has_suffix (f_name, ".tmp")) {
-					f_name = g_dir_read_name (journal_dir);
-					continue;
-				}
-			}
-
-			filename = g_build_filename (dirs[i], f_name, NULL);
-			if (g_unlink (filename) == -1) {
-				g_message ("%s", g_strerror (errno));
-			}
-			g_free (filename);
-
-			f_name = g_dir_read_name (journal_dir);
-		}
-
-		g_dir_close (journal_dir);
-	}
-
-	g_free (rotate_to);
-	g_free (directory);
-}
-
 void
 tracker_db_manager_optimize (void)
 {
diff --git a/src/libtracker-data/tracker-db-manager.h b/src/libtracker-data/tracker-db-manager.h
index 468a204..af20ced 100644
--- a/src/libtracker-data/tracker-db-manager.h
+++ b/src/libtracker-data/tracker-db-manager.h
@@ -64,9 +64,6 @@ void                tracker_db_manager_remove_all             (gboolean
 void                tracker_db_manager_optimize               (void);
 const gchar *       tracker_db_manager_get_file               (TrackerDB              db);
 TrackerDBInterface *tracker_db_manager_get_db_interface       (void);
-void                tracker_db_manager_remove_temp            (void);
-void                tracker_db_manager_move_to_temp           (void);
-void                tracker_db_manager_restore_from_temp      (void);
 void                tracker_db_manager_init_locations         (void);
 gboolean            tracker_db_manager_has_enough_space       (void);
 void                tracker_db_manager_create_version_file    (void);



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