[tracker/tracker-0.8] Fixes GB#617812: Tracker not disabled when started on battery power



commit f27579c768e2e86c431dcbfac59dfdc51554b5a4
Author: Aleksander Morgado <aleksander lanedo com>
Date:   Tue May 18 11:29:50 2010 +0200

    Fixes GB#617812: Tracker not disabled when started on battery power
    
    	* FS miner reads battery-related conf and acts based on the configured
    	parameters.
    
    	* The first index done is detected based on a new .firstindex stamp
    	file created in $cachedir/tracker/ when the FS miner finishes a full
    	index (only if the file is not already there). When a re-index is
    	forced or database files removed, that file is also removed so that
    	the miner detects it and a new first full index is done.
    
    	* Miner-FS is now linked to libtracker-db, so that it can use the new
    	API methods to check if the first index was done, and to set the first
    	index as done.

 src/libtracker-db/tracker-db-manager.c |   86 ++++++++++++++++++++++++++
 src/libtracker-db/tracker-db-manager.h |    3 +
 src/miners/fs/Makefile.am              |    1 +
 src/miners/fs/tracker-miner-files.c    |  104 ++++++++++++++++++++++++++++---
 4 files changed, 184 insertions(+), 10 deletions(-)
---
diff --git a/src/libtracker-db/tracker-db-manager.c b/src/libtracker-db/tracker-db-manager.c
index 8579cc8..bbd743e 100644
--- a/src/libtracker-db/tracker-db-manager.c
+++ b/src/libtracker-db/tracker-db-manager.c
@@ -29,6 +29,7 @@
 #include <sys/types.h>
 #include <stdio.h>
 #include <fcntl.h>
+#include <errno.h>
 
 #include <glib/gstdio.h>
 
@@ -56,6 +57,9 @@
 
 #define IN_USE_FILENAME               ".meta.isrunning"
 
+/* Stamp filename to check for first index */
+#define FIRST_INDEX_STAMP_FILENAME    ".firstindex"
+
 typedef enum {
 	TRACKER_DB_LOCATION_DATA_DIR,
 	TRACKER_DB_LOCATION_USER_DATA_DIR,
@@ -401,6 +405,9 @@ db_manager_remove_all (gboolean rm_journal)
 
 	g_message ("Removing all database files");
 
+	/* Remove stamp file */
+	tracker_db_manager_set_first_index_done (FALSE);
+
 	/* NOTE: We don't have to be initialized for this so we
 	 * calculate the absolute directories here.
 	 */
@@ -781,6 +788,9 @@ tracker_db_manager_init (TrackerDBManagerFlags  flags,
 			return FALSE;
 		}
 
+		/* Clear the first-index stamp file */
+		tracker_db_manager_set_first_index_done (FALSE);
+
 		db_recreate_all ();
 
 		/* Load databases */
@@ -1199,3 +1209,79 @@ tracker_db_manager_has_enough_space  (void)
 {
 	return tracker_file_system_has_enough_space (data_dir, TRACKER_DB_MIN_REQUIRED_SPACE, FALSE);
 }
+
+static gchar *
+get_first_index_stamp_path (void)
+{
+	return g_build_filename (g_get_user_cache_dir (),
+	                         "tracker",
+	                         FIRST_INDEX_STAMP_FILENAME,
+	                         NULL);
+}
+
+/**
+ * tracker_db_manager_get_first_index_done:
+ *
+ * Check if first full index of files was already done.
+ *
+ * Returns: %TRUE if a first full index have been done, %FALSE otherwise.
+ **/
+gboolean
+tracker_db_manager_get_first_index_done (void)
+{
+	gboolean exists;
+	gchar *stamp;
+
+	stamp = get_first_index_stamp_path();
+	exists = g_file_test (stamp, G_FILE_TEST_EXISTS);
+	g_free (stamp);
+
+	return exists;
+}
+
+/**
+ * tracker_db_manager_set_first_index_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_first_index_done (gboolean done)
+{
+	gboolean already_exists;
+	gchar *stamp;
+
+	stamp = get_first_index_stamp_path ();
+
+	already_exists = g_file_test (stamp, 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 (stamp, "", -1, &error)) {
+			g_warning ("  Creating first-index stamp in "
+			           "'%s' failed: '%s'",
+			           stamp,
+			           error->message);
+			g_error_free (error);
+		} else {
+			g_message ("  First-index stamp created in '%s'",
+			           stamp);
+		}
+	} 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,
+			           g_strerror (errno));
+		} else {
+			g_message ("  First-index stamp removed from '%s'",
+			           stamp);
+		}
+	}
+
+	g_free (stamp);
+}
diff --git a/src/libtracker-db/tracker-db-manager.h b/src/libtracker-db/tracker-db-manager.h
index 520daba..10c5032 100644
--- a/src/libtracker-db/tracker-db-manager.h
+++ b/src/libtracker-db/tracker-db-manager.h
@@ -65,6 +65,9 @@ gboolean            tracker_db_manager_has_enough_space  (void);
 TrackerDBManagerFlags
                     tracker_db_manager_get_flags         (void);
 
+gboolean            tracker_db_manager_get_first_index_done (void);
+void                tracker_db_manager_set_first_index_done (gboolean done);
+
 G_END_DECLS
 
 #endif /* __LIBTRACKER_DB_MANAGER_H__ */
diff --git a/src/miners/fs/Makefile.am b/src/miners/fs/Makefile.am
index 8022b2a..a786e6f 100644
--- a/src/miners/fs/Makefile.am
+++ b/src/miners/fs/Makefile.am
@@ -34,6 +34,7 @@ tracker_miner_fs_SOURCES =						\
 tracker_miner_fs_LDADD =						\
 	$(top_builddir)/src/libtracker-client/libtracker-client- TRACKER_API_VERSION@.la \
 	$(top_builddir)/src/libtracker-miner/libtracker-miner- TRACKER_API_VERSION@.la	\
+	$(top_builddir)/src/libtracker-db/libtracker-db.la 		\
 	$(top_builddir)/src/libtracker-common/libtracker-common.la 	\
 	$(DBUS_LIBS)							\
 	$(GMODULE_LIBS)							\
diff --git a/src/miners/fs/tracker-miner-files.c b/src/miners/fs/tracker-miner-files.c
index 7fbdc9c..24eb60a 100644
--- a/src/miners/fs/tracker-miner-files.c
+++ b/src/miners/fs/tracker-miner-files.c
@@ -36,6 +36,8 @@
 #include <libtracker-common/tracker-type-utils.h>
 #include <libtracker-common/tracker-utils.h>
 
+#include <libtracker-db/tracker-db.h>
+
 #include "tracker-miner-files.h"
 #include "tracker-config.h"
 #include "tracker-extract-client.h"
@@ -60,7 +62,6 @@ struct ProcessFileData {
 struct TrackerMinerFilesPrivate {
 	TrackerConfig *config;
 	TrackerStorage *storage;
-	TrackerPower *power;
 
 	GVolumeMonitor *volume_monitor;
 
@@ -72,6 +73,11 @@ struct TrackerMinerFilesPrivate {
 
 	guint low_battery_pause_cookie;
 
+#if defined(HAVE_UPOWER) || defined(HAVE_HAL)
+	TrackerPower *power;
+	gulong finished_handler;
+#endif /* defined(HAVE_UPOWER) || defined(HAVE_HAL) */
+
 	DBusGProxy *extractor_proxy;
 
 	GQuark quark_mount_point_uuid;
@@ -118,7 +124,9 @@ static void        check_battery_status                 (TrackerMinerFiles    *f
 static void        battery_status_cb                    (GObject              *object,
                                                          GParamSpec           *pspec,
                                                          gpointer              user_data);
-
+static void        index_on_battery_cb                  (GObject    *object,
+                                                         GParamSpec *pspec,
+                                                         gpointer    user_data);
 static void        init_mount_points                    (TrackerMinerFiles    *miner);
 static void        disk_space_check_start               (TrackerMinerFiles    *mf);
 static void        disk_space_check_stop                (TrackerMinerFiles    *mf);
@@ -155,6 +163,13 @@ static gboolean    miner_files_ignore_next_update_file  (TrackerMinerFS       *f
 static void      extractor_get_embedded_metadata_cancel (GCancellable    *cancellable,
                                                          ProcessFileData *data);
 
+static void        miner_finished_cb                    (TrackerMinerFS *fs,
+                                                         gdouble         seconds_elapsed,
+                                                         guint           total_directories_found,
+                                                         guint           total_directories_ignored,
+                                                         guint           total_files_found,
+                                                         guint           total_files_ignored,
+                                                         gpointer        user_data);
 
 G_DEFINE_TYPE (TrackerMinerFiles, tracker_miner_files, TRACKER_TYPE_MINER_FS)
 
@@ -215,6 +230,11 @@ tracker_miner_files_init (TrackerMinerFiles *mf)
 	g_signal_connect (priv->power, "notify::on-battery",
 	                  G_CALLBACK (battery_status_cb),
 	                  mf);
+
+	priv->finished_handler = g_signal_connect_after (mf, "finished",
+							 G_CALLBACK (miner_finished_cb),
+							 NULL);
+
 #endif /* defined(HAVE_DEVKIT_POWER) || defined(HAVE_HAL) */
 
 	priv->volume_monitor = g_volume_monitor_get ();
@@ -483,7 +503,7 @@ miner_files_constructed (GObject *object)
 		g_object_unref (file);
 	}
 
-	/* Add optical media */
+	/* We want to get notified when config changes */
 
 	g_signal_connect (mf->private->config, "notify::low-disk-space-limit",
 	                  G_CALLBACK (low_disk_space_limit_cb),
@@ -501,6 +521,17 @@ miner_files_constructed (GObject *object)
 	                  G_CALLBACK (ignore_directories_cb),
 	                  mf);
 
+#if defined(HAVE_UPOWER) || defined(HAVE_HAL)
+
+	g_signal_connect (mf->private->config, "notify::index-on-battery",
+	                  G_CALLBACK (index_on_battery_cb),
+	                  mf);
+	g_signal_connect (mf->private->config, "notify::index-on-battery-first-time",
+	                  G_CALLBACK (index_on_battery_cb),
+	                  mf);
+
+#endif /* defined(HAVE_UPOWER) || defined(HAVE_HAL) */
+
 	g_slist_foreach (mounts, (GFunc) g_free, NULL);
 	g_slist_free (mounts);
 
@@ -537,7 +568,7 @@ set_up_mount_point (TrackerMinerFiles *miner,
 {
 	GString *queries;
 
-	g_debug ("Setting mount point '%s' state in database (URN '%s')", 
+	g_debug ("Setting mount point '%s' state in database (URN '%s')",
 	         mount_point,
 	         removable_device_urn);
 
@@ -815,7 +846,7 @@ mount_point_added_cb (TrackerStorage *storage,
 
 	g_message ("Added mount point '%s'", mount_point);
 
-	should_crawl = TRUE;	
+	should_crawl = TRUE;
 
 	if (removable && !tracker_config_get_index_removable_devices (priv->config)) {
 		g_message ("  Not crawling, removable devices disabled in config");
@@ -887,14 +918,28 @@ check_battery_status (TrackerMinerFiles *mf)
 		g_message ("Running on AC power");
 		should_pause = FALSE;
 		should_throttle = FALSE;
+	} else if (on_low_battery) {
+		g_message ("Running on LOW Battery, pausing");
+		should_pause = TRUE;
+		should_throttle = TRUE;
 	} else {
-		g_message ("Running on battery");
-
 		should_throttle = TRUE;
 
-		if (on_low_battery) {
-			g_message ("  Battery is LOW, pausing");
-			should_pause = TRUE;
+		/* Check if miner should be paused based on configuration */
+		if (!tracker_config_get_index_on_battery (mf->private->config)) {
+			if (!tracker_config_get_index_on_battery_first_time (mf->private->config)) {
+				g_message ("Running on battery, but not enabled, pausing");
+				should_pause = TRUE;
+			} else if (tracker_db_manager_get_first_index_done()) {
+				g_message ("Running on battery and first-time index "
+				           "already done, pausing");
+				should_pause = TRUE;
+			} else {
+				g_message ("Running on battery, but first-time index not "
+				           "already finished, keeping on");
+			}
+		} else {
+			g_message ("Running on battery");
 		}
 	}
 
@@ -919,6 +964,7 @@ check_battery_status (TrackerMinerFiles *mf)
 	set_up_throttle (mf, should_throttle);
 }
 
+/* Called when battery status change is detected */
 static void
 battery_status_cb (GObject    *object,
                    GParamSpec *pspec,
@@ -929,6 +975,44 @@ battery_status_cb (GObject    *object,
 	check_battery_status (mf);
 }
 
+/* Called when battery-related configuration change is detected */
+static void
+index_on_battery_cb (GObject    *object,
+                     GParamSpec *pspec,
+                     gpointer    user_data)
+{
+	TrackerMinerFiles *mf = user_data;
+
+	check_battery_status (mf);
+}
+
+/* Called when mining has finished the first time */
+static void
+miner_finished_cb (TrackerMinerFS *fs,
+                   gdouble         seconds_elapsed,
+                   guint           total_directories_found,
+                   guint           total_directories_ignored,
+                   guint           total_files_found,
+                   guint           total_files_ignored,
+                   gpointer        user_data)
+{
+	TrackerMinerFiles *mf = TRACKER_MINER_FILES (fs);
+
+	/* Create stamp file if not already there */
+	if (!tracker_db_manager_get_first_index_done ()) {
+		tracker_db_manager_set_first_index_done (TRUE);
+	}
+
+	/* And remove the signal handler so that it's not
+	 *  called again */
+	if (mf->private->finished_handler) {
+		g_signal_handler_disconnect (fs, mf->private->finished_handler);
+		mf->private->finished_handler = 0;
+	}
+
+	check_battery_status (mf);
+}
+
 #endif /* defined(HAVE_DEVKIT_POWER) || defined(HAVE_HAL) */
 
 static void



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