[tracker/binary-log-2: 47/50] Added check that verifies last entry of the journal



commit 30a510ef84a1e1c6c5a071c6295118aa6b21a296
Author: Philip Van Hoof <philip codeminded be>
Date:   Fri Jan 8 16:48:17 2010 +0100

    Added check that verifies last entry of the journal

 src/libtracker-db/tracker-db-journal.c |   13 +++++++
 src/libtracker-db/tracker-db-journal.h |    2 +
 src/libtracker-db/tracker-db-manager.c |   58 ++++++++++++++++---------------
 3 files changed, 45 insertions(+), 28 deletions(-)
---
diff --git a/src/libtracker-db/tracker-db-journal.c b/src/libtracker-db/tracker-db-journal.c
index 34f2345..05454c8 100644
--- a/src/libtracker-db/tracker-db-journal.c
+++ b/src/libtracker-db/tracker-db-journal.c
@@ -658,6 +658,19 @@ tracker_db_journal_reader_get_type (void)
 }
 
 gboolean
+tracker_db_journal_reader_verify_last (GError **error)
+{
+	guint32 entry_size_check;
+
+	g_return_val_if_fail (reader.file != NULL, FALSE);
+
+	entry_size_check = read_uint32 (reader.end - 4);
+	reader.current = reader.end - entry_size_check;
+
+	return tracker_db_journal_reader_next (error);
+}
+
+gboolean
 tracker_db_journal_reader_next (GError **error)
 {
 	g_return_val_if_fail (reader.file != NULL, FALSE);
diff --git a/src/libtracker-db/tracker-db-journal.h b/src/libtracker-db/tracker-db-journal.h
index 3326de9..6187568 100644
--- a/src/libtracker-db/tracker-db-journal.h
+++ b/src/libtracker-db/tracker-db-journal.h
@@ -72,6 +72,7 @@ gboolean     tracker_db_journal_rollback_transaction         (void);
 gboolean     tracker_db_journal_commit_transaction           (void);
 
 gboolean     tracker_db_journal_fsync                        (void);
+gboolean     tracker_db_journal_truncate                     (gsize new_size);
 
 /*
  * Reader API
@@ -92,6 +93,7 @@ gboolean     tracker_db_journal_reader_get_statement_id      (guint32      *s_id
                                                               guint32      *p_id,
                                                               guint32      *o_id);
 gsize        tracker_db_journal_reader_get_size_of_correct   (void);
+gboolean     tracker_db_journal_reader_verify_last           (GError **error);
 
 G_END_DECLS
 
diff --git a/src/libtracker-db/tracker-db-manager.c b/src/libtracker-db/tracker-db-manager.c
index 8ace824..41933c3 100644
--- a/src/libtracker-db/tracker-db-manager.c
+++ b/src/libtracker-db/tracker-db-manager.c
@@ -1107,6 +1107,7 @@ tracker_db_manager_init (TrackerDBManagerFlags  flags,
 	guint               i;
 	gchar              *in_use_filename;
 	int                 in_use_file;
+	gboolean            loaded = FALSE;
 
 	/* First set defaults for return values */
 	if (first_time) {
@@ -1261,23 +1262,21 @@ tracker_db_manager_init (TrackerDBManagerFlags  flags,
 		/* Load databases */
 		g_message ("Loading databases files...");
 
-		for (i = 1; i < G_N_ELEMENTS (dbs); i++) {
-			dbs[i].iface = db_interface_create (i);
-			dbs[i].mtime = tracker_file_get_mtime (dbs[i].abs_filename);
-		}
-
 	} else {
+		gboolean must_recreate;
+
 		/* Make sure we initialize all other modules we depend on */
 		tracker_ontology_init ();
 
 		/* Load databases */
 		g_message ("Loading databases files...");
 
-		if (g_file_test (in_use_filename, G_FILE_TEST_EXISTS)) {
-			gboolean must_recreate;
-			gsize size = 0;
+		tracker_db_journal_reader_init (NULL);
+		must_recreate = !tracker_db_journal_reader_verify_last (NULL);
+		tracker_db_journal_reader_shutdown ();
 
-			must_recreate = FALSE;
+		if (!must_recreate && g_file_test (in_use_filename, G_FILE_TEST_EXISTS)) {
+			gsize size = 0;
 
 			g_message ("Didn't shut down cleanly last time, doing integrity checks");
 
@@ -1293,7 +1292,7 @@ tracker_db_manager_init (TrackerDBManagerFlags  flags,
 				/* Size is 1 when using echo > file.db, none of our databases
 				 * are only one byte in size even initually. */
 
-				if (size == 0 || size == 1) {
+				if (size <= 1) {
 					must_recreate = TRUE;
 					continue;
 				}
@@ -1301,6 +1300,8 @@ tracker_db_manager_init (TrackerDBManagerFlags  flags,
 				dbs[i].iface = db_interface_create (i);
 				dbs[i].mtime = tracker_file_get_mtime (dbs[i].abs_filename);
 
+				loaded = TRUE;
+
 				stmt = tracker_db_interface_create_statement (dbs[i].iface,
 				                                              "PRAGMA integrity_check(1)");
 
@@ -1316,30 +1317,31 @@ tracker_db_manager_init (TrackerDBManagerFlags  flags,
 					g_object_unref (cursor);
 				}
 			}
+		}
 
-			if (must_recreate) {
+		if (must_recreate) {
 
-				if (first_time) {
-					*first_time = TRUE;
-				}
+			if (first_time) {
+				*first_time = TRUE;
+			}
 
-				for (i = 1; i < G_N_ELEMENTS (dbs); i++) {
-					if (dbs[i].iface)
-						g_object_unref (dbs[i].iface);
+			for (i = 1; i < G_N_ELEMENTS (dbs); i++) {
+				if (dbs[i].iface) {
+					g_object_unref (dbs[i].iface);
+					dbs[i].iface = NULL;
 				}
+			}
 
-				db_recreate_all ();
+			db_recreate_all ();
+			loaded = FALSE;
+		}
 
-				for (i = 1; i < G_N_ELEMENTS (dbs); i++) {
-					dbs[i].iface = db_interface_create (i);
-					dbs[i].mtime = tracker_file_get_mtime (dbs[i].abs_filename);
-				}
-			}
-		} else {
-			for (i = 1; i < G_N_ELEMENTS (dbs); i++) {
-				dbs[i].iface = db_interface_create (i);
-				dbs[i].mtime = tracker_file_get_mtime (dbs[i].abs_filename);
-			}
+	}
+
+	if (!loaded) {
+		for (i = 1; i < G_N_ELEMENTS (dbs); i++) {
+			dbs[i].iface = db_interface_create (i);
+			dbs[i].mtime = tracker_file_get_mtime (dbs[i].abs_filename);
 		}
 	}
 



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