[tracker] libtracker-db: Moved last-crawl.txt machinery to here from miner-fs



commit 37fd6971bd45dae718149dfed0a016e7a0c7a40d
Author: Martyn Russell <martyn lanedo com>
Date:   Wed Jun 9 18:50:13 2010 +0100

    libtracker-db: Moved last-crawl.txt machinery to here from miner-fs
    
    * This was previously called crawling-timestamp.txt. It has been renamed
    last-crawl.txt now.
    * Fixes GB#620353, remove crawling interval timestamp on re-index
    * Now libtracker-db removes this file too when a reindex is requested

 src/libtracker-db/tracker-db-manager.c |  130 ++++++++++++++++++++++++++------
 src/libtracker-db/tracker-db-manager.h |    6 +-
 src/miners/fs/tracker-main.c           |   35 +--------
 src/miners/fs/tracker-miner-files.c    |   36 +--------
 4 files changed, 119 insertions(+), 88 deletions(-)
---
diff --git a/src/libtracker-db/tracker-db-manager.c b/src/libtracker-db/tracker-db-manager.c
index 28cb787..7201ab9 100644
--- a/src/libtracker-db/tracker-db-manager.c
+++ b/src/libtracker-db/tracker-db-manager.c
@@ -58,8 +58,9 @@
 
 #define IN_USE_FILENAME               ".meta.isrunning"
 
-/* Stamp filename to check for first index */
+/* Stamp files to know crawling/indexing state */
 #define FIRST_INDEX_FILENAME          "first-index.txt"
+#define LAST_CRAWL_FILENAME           "last-crawl.txt"
 
 typedef enum {
 	TRACKER_DB_LOCATION_DATA_DIR,
@@ -416,8 +417,9 @@ db_manager_remove_all (gboolean rm_journal)
 
 	g_message ("Removing all database files");
 
-	/* Remove stamp file */
+	/* Remove stamp files */
 	tracker_db_manager_set_first_index_done (FALSE);
+	tracker_db_manager_set_last_crawl_done (FALSE);
 
 	/* NOTE: We don't have to be initialized for this so we
 	 * calculate the absolute directories here.
@@ -1290,7 +1292,7 @@ tracker_db_manager_has_enough_space  (void)
 
 
 inline static gchar *
-get_first_index_stamp_path (void)
+get_first_index_filename (void)
 {
 	return g_build_filename (g_get_user_cache_dir (),
 	                         "tracker",
@@ -1309,11 +1311,11 @@ gboolean
 tracker_db_manager_get_first_index_done (void)
 {
 	gboolean exists;
-	gchar *stamp;
+	gchar *filename;
 
-	stamp = get_first_index_stamp_path ();
-	exists = g_file_test (stamp, G_FILE_TEST_EXISTS);
-	g_free (stamp);
+	filename = get_first_index_filename ();
+	exists = g_file_test (filename, G_FILE_TEST_EXISTS);
+	g_free (filename);
 
 	return exists;
 }
@@ -1329,40 +1331,122 @@ void
 tracker_db_manager_set_first_index_done (gboolean done)
 {
 	gboolean already_exists;
-	gchar *stamp;
+	gchar *filename;
+
+	filename = get_first_index_filename ();
+	already_exists = g_file_test (filename, G_FILE_TEST_EXISTS);
+
+	if (done && !already_exists) {
+		GError *error = NULL;
+
+		/* If done, create stamp file if not already there */
+		if (!g_file_set_contents (filename, PACKAGE_VERSION, -1, &error)) {
+			g_warning ("  Could not create file:'%s' failed, %s",
+			           filename,
+			           error->message);
+			g_error_free (error);
+		} else {
+			g_message ("  First index file:'%s' created",
+			           filename);
+		}
+	} else if (!done && already_exists) {
+		/* If NOT done, remove stamp file */
+		if (g_remove (filename)) {
+			g_warning ("  Could not remove file:'%s', %s", 
+			           filename,
+			           g_strerror (errno));
+		} else {
+			g_message ("  First index file:'%s' removed",
+			           filename);
+		}
+	}
+
+	g_free (filename);
+}
+
+inline static gchar *
+get_last_crawl_filename (void)
+{
+	return g_build_filename (g_get_user_cache_dir (),
+	                         "tracker",
+	                         LAST_CRAWL_FILENAME,
+	                         NULL);
+}
+
+/**
+ * tracker_db_manager_get_last_crawl_done:
+ *
+ * Check when last crawl was performed.
+ *
+ * Returns: time_t() value when last crawl occurred, otherwise 0.
+ **/
+guint64
+tracker_db_manager_get_last_crawl_done (void)
+{
+	gchar *filename;
+	gchar *content;
+	guint64 then;
+
+	filename = get_last_crawl_filename ();
+
+	if (!g_file_get_contents (filename, &content, NULL, NULL)) {
+		g_message ("  No previous timestamp, crawling forced");
+		return 0;
+	}
+
+	then = g_ascii_strtoull (content, NULL, 10);
+	g_free (content);
+
+	return then;
+}
 
-	stamp = get_first_index_stamp_path ();
+/**
+ * tracker_db_manager_set_last_crawl_done:
+ *
+ * Set the status of the first full index of files. Should be set to
+ *  %FALSE if the index was never done or if a reindex is needed. When
+ *  the index is completed, should be set to %TRUE.
+ **/
+void
+tracker_db_manager_set_last_crawl_done (gboolean done)
+{
+	gboolean already_exists;
+	gchar *filename;
 
-	already_exists = g_file_test (stamp, G_FILE_TEST_EXISTS);
+	filename = get_last_crawl_filename ();
+	already_exists = g_file_test (filename, G_FILE_TEST_EXISTS);
 
 	if (done && !already_exists) {
 		GError *error = NULL;
+		gchar *content;
+
+		content = g_strdup_printf ("%" G_GUINT64_FORMAT, (guint64) time (NULL));
 
 		/* If done, create stamp file if not already there */
-		if (!g_file_set_contents (stamp, PACKAGE_VERSION, -1, &error)) {
-			g_warning ("  Creating first-index stamp in "
-			           "'%s' failed: '%s'",
-			           stamp,
+		if (!g_file_set_contents (filename, content, -1, &error)) {
+			g_warning ("  Could not create file:'%s' failed, %s",
+			           filename,
 			           error->message);
 			g_error_free (error);
 		} else {
-			g_message ("  First-index stamp created in '%s'",
-			           stamp);
+			g_message ("  Last crawl file:'%s' created",
+			           filename);
 		}
+
+		g_free (content);
 	} else if (!done && already_exists) {
 		/* If NOT done, remove stamp file */
-		if (g_remove (stamp)) {
-			g_warning ("  Removing first-index stamp from '%s' "
-			           "failed: '%s'",
-			           stamp,
+		if (g_remove (filename)) {
+			g_warning ("  Could not remove file:'%s', %s", 
+			           filename,
 			           g_strerror (errno));
 		} else {
-			g_message ("  First-index stamp removed from '%s'",
-			           stamp);
+			g_message ("  Last crawl file:'%s' removed",
+			           filename);
 		}
 	}
 
-	g_free (stamp);
+	g_free (filename);
 }
 
 /**
diff --git a/src/libtracker-db/tracker-db-manager.h b/src/libtracker-db/tracker-db-manager.h
index 2859f4b..e4dce00 100644
--- a/src/libtracker-db/tracker-db-manager.h
+++ b/src/libtracker-db/tracker-db-manager.h
@@ -68,9 +68,11 @@ TrackerDBManagerFlags
 gboolean            tracker_db_manager_interrupt_thread       (GThread *thread);
 void                tracker_db_manager_interrupt_thread_reset (GThread *thread);
 
-gboolean            tracker_db_manager_get_first_index_done (void);
-void                tracker_db_manager_set_first_index_done (gboolean done);
+gboolean            tracker_db_manager_get_first_index_done   (void);
+guint64             tracker_db_manager_get_last_crawl_done    (void);
 
+void                tracker_db_manager_set_first_index_done   (gboolean done);
+void                tracker_db_manager_set_last_crawl_done    (gboolean done);
 
 G_END_DECLS
 
diff --git a/src/miners/fs/tracker-main.c b/src/miners/fs/tracker-main.c
index 688052c..0ed4307 100644
--- a/src/miners/fs/tracker-main.c
+++ b/src/miners/fs/tracker-main.c
@@ -74,7 +74,6 @@ static gint initial_sleep = -1;
 static gchar *eligible;
 static gchar *add_file;
 static gboolean version;
-static gchar *crawl_timestamp_file;
 
 static GOptionEntry entries[] = {
 	{ "verbosity", 'v', 0,
@@ -200,13 +199,6 @@ should_crawl (TrackerConfig *config)
 {
 	gint crawling_interval;
 
-	if (G_UNLIKELY (!crawl_timestamp_file)) {
-		crawl_timestamp_file = g_build_filename (g_get_user_cache_dir (),
-		                                         "tracker",
-		                                         "crawling-timestamp.txt",
-		                                         NULL);
-	}
-
 	crawling_interval = tracker_config_get_crawling_interval (config);
 
 	g_message ("Checking whether to perform mtime checks during crawling:");
@@ -219,18 +211,15 @@ should_crawl (TrackerConfig *config)
 		return TRUE;
 	} else {
 		guint64 then, now;
-		gchar *content;
+		
+		then = tracker_db_manager_get_last_crawl_done ();
 
-		if (!g_file_get_contents (crawl_timestamp_file, &content, NULL, NULL)) {
-			g_message ("  No previous timestamp, crawling forced");
+		if (then < 1) {
 			return TRUE;
 		}
 
 		now = (guint64) time (NULL);
 
-		then = g_ascii_strtoull (content, NULL, 10);
-		g_free (content);
-
 		if (now < then + (crawling_interval * SECONDS_PER_DAY)) {
 			g_message ("  Postponed");
 			return FALSE;
@@ -242,22 +231,6 @@ should_crawl (TrackerConfig *config)
 }
 
 static void
-save_crawling_time (void)
-{
-	GError *error = NULL;
-	gchar *content;
-
-	content = g_strdup_printf ("%" G_GUINT64_FORMAT, (guint64) time (NULL));
-
-	g_file_set_contents (crawl_timestamp_file, content, -1, &error);
-
-	if (error) {
-		g_critical ("Could not save crawling timestamp: %s", error->message);
-		g_error_free (error);
-	}
-}
-
-static void
 miner_handle_next (void)
 {
 	if (finished_miners) {
@@ -309,7 +282,7 @@ miner_finished_cb (TrackerMinerFS *fs,
 
 	if (TRACKER_IS_MINER_FILES (fs) &&
 	    tracker_miner_fs_get_initial_crawling (fs)) {
-		save_crawling_time ();
+		tracker_db_manager_set_last_crawl_done (TRUE);
 	}
 
 	miner_handle_next ();
diff --git a/src/miners/fs/tracker-miner-files.c b/src/miners/fs/tracker-miner-files.c
index f6b96ca..ef7ee80 100644
--- a/src/miners/fs/tracker-miner-files.c
+++ b/src/miners/fs/tracker-miner-files.c
@@ -97,8 +97,6 @@ enum {
 	PROP_CONFIG
 };
 
-static gchar *crawl_timestamp_file = NULL;
-
 static void        miner_files_set_property             (GObject              *object,
                                                          guint                 param_id,
                                                          const GValue         *value,
@@ -1779,13 +1777,6 @@ should_check_mtime (TrackerConfig *config)
 {
 	gint crawling_interval;
 
-	if (G_UNLIKELY (!crawl_timestamp_file)) {
-		crawl_timestamp_file = g_build_filename (g_get_user_cache_dir (),
-		                                         "tracker",
-		                                         "crawling-timestamp.txt",
-		                                         NULL);
-	}
-
 	crawling_interval = tracker_config_get_crawling_interval (config);
 
 	g_message ("Checking whether to perform mtime checks during crawling:");
@@ -1798,18 +1789,15 @@ should_check_mtime (TrackerConfig *config)
 		return TRUE;
 	} else {
 		guint64 then, now;
-		gchar *content;
 
-		if (!g_file_get_contents (crawl_timestamp_file, &content, NULL, NULL)) {
+		then = tracker_db_manager_get_last_crawl_done ();
+		if (then < 1) {
 			g_message ("  No previous timestamp, crawling forced");
 			return TRUE;
 		}
-
+	
 		now = (guint64) time (NULL);
 
-		then = g_ascii_strtoull (content, NULL, 10);
-		g_free (content);
-
 		if (now < then + (crawling_interval * SECONDS_PER_DAY)) {
 			g_message ("  Postponed");
 			return FALSE;
@@ -1821,25 +1809,9 @@ should_check_mtime (TrackerConfig *config)
 }
 
 static void
-save_crawling_time (void)
-{
-	GError *error = NULL;
-	gchar *content;
-
-	content = g_strdup_printf ("%" G_GUINT64_FORMAT, (guint64) time (NULL));
-
-	g_file_set_contents (crawl_timestamp_file, content, -1, &error);
-
-	if (error) {
-		g_critical ("Could not save crawling timestamp: %s", error->message);
-		g_error_free (error);
-	}
-}
-
-static void
 miner_files_finished (TrackerMinerFS *fs)
 {
-        save_crawling_time ();
+        tracker_db_manager_set_last_crawl_done (TRUE);
 }
 
 TrackerMiner *



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