[tracker/tracker-0.6] Get the actual mount point for a given path to get volume id in db



commit f3c8b83705ebba63ce25de9f7e6d3b8c2a4c6a83
Author: Martyn Russell <martyn imendio com>
Date:   Mon May 11 12:48:42 2009 +0100

    Get the actual mount point for a given path to get volume id in db
    
    Before we did this, we were using clever SQL statements to get the
    ID in the Volumes table based on a given path and the known volume's
    mount point in the database. This of course doesn't give a real idea
    of the mount point because you can't rely on clever string
    manipulation, only the file system really knows which removable device
    the file is on. So now we get the real device and use some extra
    caching to improve performance here too so we can get the UDI by path.
    The UDI is then looked up in the database for the volume ID.
    
    NOTE: This is related to and improves on NB#109892 and NB#112632.
---
 data/db/sqlite-stored-procs.sql                |    1 -
 src/libtracker-common/tracker-hal.c            |   70 +++++++++++++++++++++++-
 src/libtracker-common/tracker-hal.h            |   34 ++++++------
 src/libtracker-data/tracker-data-update.c      |   37 ++++++++-----
 src/libtracker-data/tracker-data-update.h      |    4 +-
 src/tracker-indexer/tracker-indexer.c          |   13 ++++-
 src/tracker-indexer/tracker-indexer.h          |    3 +
 src/tracker-indexer/tracker-removable-device.c |   18 ++++++-
 8 files changed, 139 insertions(+), 41 deletions(-)

diff --git a/data/db/sqlite-stored-procs.sql b/data/db/sqlite-stored-procs.sql
index d7ca389..2d50330 100644
--- a/data/db/sqlite-stored-procs.sql
+++ b/data/db/sqlite-stored-procs.sql
@@ -129,7 +129,6 @@ GetHitDetails                  SELECT ROWID, HitCount, HitArraySize FROM HitInde
  */
 
 GetVolumeID                    SELECT VolumeID FROM Volumes WHERE UDI = ?;
-GetVolumeByPath                SELECT VolumeID FROM Volumes WHERE Enabled = 1 AND (? = MountPath OR ? LIKE (MountPath || '/%')) ORDER BY length(MountPath) DESC LIMIT 1;
 GetVolumesToClean              SELECT MountPath, VolumeID FROM Volumes WHERE DisabledDate < date('now', '-3 day');
 InsertVolume                   INSERT INTO Volumes (MountPath, UDI, Enabled, DisabledDate) VALUES (?, ?, 1, date('now'));
 EnableVolume                   UPDATE Volumes SET MountPath = ?, Enabled = 1 WHERE UDI = ?;
diff --git a/src/libtracker-common/tracker-hal.c b/src/libtracker-common/tracker-hal.c
index ff32af1..df7a35e 100644
--- a/src/libtracker-common/tracker-hal.c
+++ b/src/libtracker-common/tracker-hal.c
@@ -30,6 +30,8 @@
 
 #include <dbus/dbus-glib-lowlevel.h>
 
+#include <gio/gio.h>
+
 #include "tracker-log.h"
 #include "tracker-hal.h"
 #include "tracker-utils.h"
@@ -52,6 +54,7 @@ typedef struct {
 	GHashTable    *all_devices;
 	GHashTable    *mounted_devices;
 	GHashTable    *removable_devices;
+	GHashTable    *removable_mount_points;
 	GHashTable    *batteries;
 
 	gchar	      *ac_adapter_udi;
@@ -191,6 +194,12 @@ tracker_hal_init (TrackerHal *hal)
 							 g_str_equal,
 							 (GDestroyNotify) g_free,
 							 (GDestroyNotify) g_free);
+
+	priv->removable_mount_points = g_hash_table_new_full (g_str_hash,
+							      g_str_equal,
+							      (GDestroyNotify) g_free,
+							      (GDestroyNotify) g_free);
+
 	priv->batteries = g_hash_table_new_full (g_str_hash,
 						 g_str_equal,
 						 (GDestroyNotify) g_free,
@@ -264,6 +273,10 @@ tracker_hal_finalize (GObject *object)
 
 	priv = GET_PRIV (object);
 
+	if (priv->removable_mount_points) {
+		g_hash_table_unref (priv->removable_mount_points);
+	}
+
 	if (priv->removable_devices) {
 		g_hash_table_unref (priv->removable_devices);
 	}
@@ -532,6 +545,9 @@ hal_mount_point_add (TrackerHal  *hal,
 		g_hash_table_insert (priv->removable_devices,
 				     g_strdup (udi),
 				     g_strdup (mount_point));
+		g_hash_table_insert (priv->removable_mount_points,
+				     g_strdup (mount_point),
+				     g_strdup (udi));
 	}
 
 	g_signal_emit (hal, signals[MOUNT_POINT_ADDED], 0, udi, mount_point, NULL);
@@ -543,6 +559,7 @@ hal_mount_point_remove (TrackerHal  *hal,
 {
 	TrackerHalPriv *priv;
 	const gchar    *mount_point;
+	gboolean        was_removable;
 
 	priv = GET_PRIV (hal);
 
@@ -552,16 +569,18 @@ hal_mount_point_remove (TrackerHal  *hal,
 		return;
 	}
 
+	was_removable = g_hash_table_remove (priv->removable_devices, udi);
+	g_hash_table_remove (priv->removable_mount_points, mount_point);
+
 	g_message ("HAL device:'%s' with mount point:'%s' (uuid:'%s'), removable:%s NO LONGER being tracked",
 		   (const gchar*) g_hash_table_lookup (priv->all_devices, udi),
 		   mount_point,
 		   udi,
-		   g_hash_table_remove (priv->removable_devices, udi) ? "yes" : "no");
+		   was_removable ? "yes" : "no");
 	
 	g_signal_emit (hal, signals[MOUNT_POINT_REMOVED], 0, udi, mount_point, NULL);
 
 	g_hash_table_remove (priv->mounted_devices, udi);
-	g_hash_table_remove (priv->removable_devices, udi);
 }
 
 static const gchar *
@@ -1286,4 +1305,51 @@ tracker_hal_udi_get_is_mounted (TrackerHal  *hal,
 
 }
 
+/**
+ * tracker_hal_udi_get_for_path:
+ * @hal: A #TrackerHal
+ * @file: a file
+ *
+ * Returns the UDI of the removable device for @file
+ *
+ * Returns: Returns the UDI of the removable device for @file
+ **/
+const gchar *
+tracker_hal_udi_get_for_path (TrackerHal  *hal,
+			      const gchar *path)
+{
+	TrackerHalPriv *priv;
+	GFile          *file, *file_for_mount_point;
+	GMount         *mount;
+	GError         *error = NULL;
+	gchar          *path_for_mount_point;
+	const gchar    *udi;
+
+	g_return_val_if_fail (TRACKER_IS_HAL (hal), NULL);
+	g_return_val_if_fail (path != NULL, NULL);
+
+	/* Get the actual mount point root for the given path */
+	file = g_file_new_for_path (path);
+	mount = g_file_find_enclosing_mount (file, NULL, &error);
+	g_object_unref (file);
+
+	if (!mount) {
+		return NULL;
+	}
+
+	file_for_mount_point = g_mount_get_root (mount);
+	path_for_mount_point = g_file_get_path (file_for_mount_point);
+	g_object_unref (file_for_mount_point);
+	g_object_unref (mount);
+
+	/* Now check against all volumes to get the right UDI */
+	priv = GET_PRIV (hal);
+
+	udi = g_hash_table_lookup (priv->removable_mount_points, 
+				   path_for_mount_point);
+	g_free (path_for_mount_point);
+
+	return udi;
+}
+
 #endif /* HAVE_HAL */
diff --git a/src/libtracker-common/tracker-hal.h b/src/libtracker-common/tracker-hal.h
index d88aecc..c08082b 100644
--- a/src/libtracker-common/tracker-hal.h
+++ b/src/libtracker-common/tracker-hal.h
@@ -51,25 +51,23 @@ struct _TrackerHalClass {
 #ifdef HAVE_HAL
 
 GType	     tracker_hal_get_type		     (void) G_GNUC_CONST;
-
 TrackerHal * tracker_hal_new                         (void);
-
-gboolean     tracker_hal_get_battery_in_use          (TrackerHal  *hal);
-gboolean     tracker_hal_get_battery_exists          (TrackerHal  *hal);
-gdouble      tracker_hal_get_battery_percentage      (TrackerHal  *hal);
-
-GList *      tracker_hal_get_mounted_directory_roots (TrackerHal  *hal);
-GList *      tracker_hal_get_removable_device_roots  (TrackerHal  *hal);
-GList *      tracker_hal_get_removable_device_udis   (TrackerHal  *hal);
-
-const gchar *tracker_hal_udi_get_mount_point         (TrackerHal  *hal,
-						      const gchar *udi);
-gboolean     tracker_hal_udi_get_is_mounted          (TrackerHal  *hal,
-						      const gchar *udi);
-gboolean     tracker_hal_path_is_on_removable_device (TrackerHal  *hal,
-						      const gchar *path,
-						      gchar      **mount_point,
-						      gboolean    *available);
+gboolean     tracker_hal_get_battery_in_use          (TrackerHal   *hal);
+gboolean     tracker_hal_get_battery_exists          (TrackerHal   *hal);
+gdouble      tracker_hal_get_battery_percentage      (TrackerHal   *hal);
+GList *      tracker_hal_get_mounted_directory_roots (TrackerHal   *hal);
+GList *      tracker_hal_get_removable_device_roots  (TrackerHal   *hal);
+GList *      tracker_hal_get_removable_device_udis   (TrackerHal   *hal);
+const gchar *tracker_hal_udi_get_mount_point         (TrackerHal   *hal,
+						      const gchar  *udi);
+gboolean     tracker_hal_udi_get_is_mounted          (TrackerHal   *hal,
+						      const gchar  *udi);
+const gchar *tracker_hal_udi_get_for_path            (TrackerHal   *hal,
+						      const gchar  *path);
+gboolean     tracker_hal_path_is_on_removable_device (TrackerHal   *hal,
+						      const gchar  *path,
+						      gchar       **mount_point,
+						      gboolean     *available);
 
 #endif /* HAVE_HAL */
 
diff --git a/src/libtracker-data/tracker-data-update.c b/src/libtracker-data/tracker-data-update.c
index 3f596ed..b686dee 100644
--- a/src/libtracker-data/tracker-data-update.c
+++ b/src/libtracker-data/tracker-data-update.c
@@ -92,6 +92,7 @@ tracker_data_update_get_new_service_id (TrackerDBInterface *iface)
 gboolean
 tracker_data_update_create_service (TrackerService *service,
 				    guint32	    service_id,
+				    const gchar    *udi,
 				    const gchar	   *dirname,
 				    const gchar	   *basename,
 				    GHashTable     *metadata)
@@ -106,17 +107,20 @@ tracker_data_update_create_service (TrackerService *service,
 		return FALSE;
 	}
 
-	/* retrieve VolumeID */
 	iface = tracker_db_manager_get_db_interface (TRACKER_DB_COMMON);
-	result_set = tracker_db_interface_execute_procedure (iface, NULL,
-							     "GetVolumeByPath",
-							     dirname,
-							     dirname,
-							     NULL);
-	if (result_set) {
-		tracker_db_result_set_get (result_set, 0, &volume_id, -1);
-		g_object_unref (result_set);
+	
+	if (udi) {
+		result_set = tracker_db_interface_execute_procedure (iface, NULL,
+								     "GetVolumeID",
+								     udi,
+								     NULL);
+		
+		if (result_set) {
+			tracker_db_result_set_get (result_set, 0, &volume_id, -1);
+			g_object_unref (result_set);
+		}
 	}
+
 	volume_id_str = tracker_guint32_to_string (volume_id);
 
 	iface = tracker_db_manager_get_db_interface_by_type (tracker_service_get_name (service),
@@ -688,7 +692,8 @@ foreach_in_metadata_set_metadata (gpointer   predicate,
 }
 
 void 
-tracker_data_update_replace_service (const gchar *path,
+tracker_data_update_replace_service (const gchar *udi,
+				     const gchar *path,
 				     const gchar *rdf_type,
 				     GHashTable  *metadata)
 {
@@ -703,11 +708,13 @@ tracker_data_update_replace_service (const gchar *path,
 	gboolean             set_metadata = FALSE;
 	guint32              id = 0;
 
+	g_return_if_fail (udi != NULL);
 	g_return_if_fail (path != NULL);
 	g_return_if_fail (metadata != NULL);
 
-	if (!rdf_type)
+	if (!rdf_type) {
 		return;
+	}
 
 	service = tracker_ontology_get_service_by_name (rdf_type);
 
@@ -752,6 +759,7 @@ tracker_data_update_replace_service (const gchar *path,
 		id = tracker_data_update_get_new_service_id (iface);
 
 		if (tracker_data_update_create_service (service, id,
+							udi,
 							dirname, basename,
 							metadata)) {
 			set_metadata = TRUE;
@@ -795,10 +803,9 @@ tracker_data_update_enable_volume (const gchar *udi,
 	iface = tracker_db_manager_get_db_interface (TRACKER_DB_COMMON);
 
 	result_set = tracker_db_interface_execute_procedure (iface, NULL,
-					   "GetVolumeID",
-					   udi,
-					   NULL);
-
+							     "GetVolumeID",
+							     udi,
+							     NULL);
 
 	if (result_set) {
 		tracker_db_result_set_get (result_set, 0, &id, -1);
diff --git a/src/libtracker-data/tracker-data-update.h b/src/libtracker-data/tracker-data-update.h
index 9382a66..90cc12a 100644
--- a/src/libtracker-data/tracker-data-update.h
+++ b/src/libtracker-data/tracker-data-update.h
@@ -37,6 +37,7 @@ guint32  tracker_data_update_get_new_service_id         (TrackerDBInterface  *if
 /* Services  */
 gboolean tracker_data_update_create_service             (TrackerService      *service,
 							 guint32              service_id,
+							 const gchar         *udi,
 							 const gchar         *dirname,
 							 const gchar         *basename,
 							 GHashTable          *metadata);
@@ -51,7 +52,8 @@ gboolean tracker_data_update_move_service               (TrackerService      *se
 							 const gchar         *to);
 
 /* Turtle importing */
-void     tracker_data_update_replace_service            (const gchar         *path,
+void     tracker_data_update_replace_service            (const gchar         *udi,
+							 const gchar         *path,
 							 const gchar         *rdf_type,
 							 GHashTable          *metadata);
 void     tracker_data_update_delete_service_by_path     (const gchar         *path,
diff --git a/src/tracker-indexer/tracker-indexer.c b/src/tracker-indexer/tracker-indexer.c
index ecc63f3..053a324 100644
--- a/src/tracker-indexer/tracker-indexer.c
+++ b/src/tracker-indexer/tracker-indexer.c
@@ -56,7 +56,6 @@
 #include <libtracker-common/tracker-config.h>
 #include <libtracker-common/tracker-dbus.h>
 #include <libtracker-common/tracker-file-utils.h>
-#include <libtracker-common/tracker-hal.h>
 #include <libtracker-common/tracker-language.h>
 #include <libtracker-common/tracker-parser.h>
 #include <libtracker-common/tracker-ontology.h>
@@ -438,7 +437,6 @@ tracker_indexer_transaction_commit (TrackerIndexer *indexer)
 {
 	stop_transaction (indexer);
 	tracker_indexer_set_running (indexer, TRUE);
-
 }
 
 void
@@ -448,6 +446,14 @@ tracker_indexer_transaction_open (TrackerIndexer *indexer)
 	start_transaction (indexer);
 }
 
+TrackerHal *
+tracker_indexer_get_hal (TrackerIndexer *indexer)
+{
+	g_return_val_if_fail (TRACKER_IS_INDEXER (indexer), NULL);
+
+	return indexer->private->hal;
+}
+
 #ifdef HAVE_HAL
 
 static void
@@ -1722,6 +1728,7 @@ item_add_or_update (TrackerIndexer        *indexer,
 		tracker_data_metadata_free (old_metadata_non_emb);
 	} else {
 		GHashTable *data;
+		const gchar *udi;
 
 		g_debug ("Adding item '%s/%s'",
 			 dirname,
@@ -1730,9 +1737,11 @@ item_add_or_update (TrackerIndexer        *indexer,
 		/* Service wasn't previously indexed */
 		id = tracker_data_update_get_new_service_id (indexer->private->common);
 		data = tracker_module_metadata_get_hash_table (metadata);
+		udi = tracker_hal_udi_get_for_path (indexer->private->hal, dirname);
 
 		tracker_data_update_create_service (service,
 						    id,
+						    udi,
 						    dirname,
 						    basename,
 						    data);
diff --git a/src/tracker-indexer/tracker-indexer.h b/src/tracker-indexer/tracker-indexer.h
index 9b2646a..5e6f5e3 100644
--- a/src/tracker-indexer/tracker-indexer.h
+++ b/src/tracker-indexer/tracker-indexer.h
@@ -25,6 +25,8 @@
 #include <glib-object.h>
 #include <dbus/dbus-glib.h>
 
+#include <libtracker-common/tracker-hal.h>
+
 #define TRACKER_DAEMON_SERVICE	     "org.freedesktop.Tracker"
 #define TRACKER_INDEXER_SERVICE      "org.freedesktop.Tracker.Indexer"
 #define TRACKER_INDEXER_PATH	     "/org/freedesktop/Tracker/Indexer"
@@ -92,6 +94,7 @@ void            tracker_indexer_process_modules     (TrackerIndexer         *ind
 						     gchar                 **modules);
 void		tracker_indexer_transaction_commit  (TrackerIndexer         *indexer);
 void		tracker_indexer_transaction_open    (TrackerIndexer         *indexer);
+TrackerHal *    tracker_indexer_get_hal             (TrackerIndexer         *indexer);
 
 /* DBus methods */
 void            tracker_indexer_pause               (TrackerIndexer         *indexer,
diff --git a/src/tracker-indexer/tracker-removable-device.c b/src/tracker-indexer/tracker-removable-device.c
index d1cf0f5..8b5fe17 100644
--- a/src/tracker-indexer/tracker-removable-device.c
+++ b/src/tracker-indexer/tracker-removable-device.c
@@ -104,17 +104,26 @@ commit_turtle_parse_info_storer (TurtleStorerInfo *info,
 		}
 
 		if (info->rdf_type) {
+			TrackerHal *hal;
+			const gchar *udi;
+
 			/* We ignore records that didn't have an <rdf:type> 
 			 * predicate. Those are just wrong anyway.
 			 */
+
 			switch (task) {
 			case REMOVAL:
 				tracker_data_update_delete_service_by_path (path, info->rdf_type);
 				break;
 			case MOVE:
 				data = tracker_module_metadata_get_hash_table (info->metadata);
+
+				hal = tracker_indexer_get_hal (info->indexer);
+				udi = tracker_hal_udi_get_for_path (hal, dest_path);
+
 				tracker_data_update_delete_service_by_path (path, info->rdf_type);
-				tracker_data_update_replace_service (dest_path,
+				tracker_data_update_replace_service (udi,
+								     dest_path,
 								     info->rdf_type,
 								     data);
 				g_hash_table_destroy (data);
@@ -122,7 +131,12 @@ commit_turtle_parse_info_storer (TurtleStorerInfo *info,
 			case REPLACE:
 			default:
 				data = tracker_module_metadata_get_hash_table (info->metadata);
-				tracker_data_update_replace_service (path,
+
+				hal = tracker_indexer_get_hal (info->indexer);
+				udi = tracker_hal_udi_get_for_path (hal, path);
+
+				tracker_data_update_replace_service (udi, 
+								     path,
 								     info->rdf_type,
 								     data);
 				g_hash_table_destroy (data);



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