[tracker/gio-dir-monitors: 2/3] libtracker-miner: Use "libinotify" not "inotify", gio supports inotify



commit 6c37ca624159e40ba2adf5baaa6329469cf2f8cf
Author: Martyn Russell <martyn lanedo com>
Date:   Tue Mar 23 14:36:34 2010 +0000

    libtracker-miner: Use "libinotify" not "inotify", gio supports inotify

 configure.ac                           |   32 ++--
 src/Makefile.am                        |    6 +-
 src/libtracker-miner/Makefile.am       |    2 +-
 src/libtracker-miner/tracker-monitor.c |  244 ++++++++++++++++----------------
 4 files changed, 145 insertions(+), 139 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index 149106f..db584b9 100644
--- a/configure.ac
+++ b/configure.ac
@@ -716,29 +716,29 @@ fi
 #  libinotify
 ####################################################################
 
-AC_ARG_ENABLE(inotify,
-	      AS_HELP_STRING([--enable-inotify],
-			     [Enables inotify for the filesystem monitoring [[default=no]]]),,
-	      [enable_inotify=no])
+AC_ARG_ENABLE(libinotify,
+	      AS_HELP_STRING([--enable-libinotify],
+			     [Enables libinotify for the filesystem monitoring [[default=yes]]]),,
+	      [enable_libinotify=yes])
 
-if test "x$enable_inotify" != "xno"; then
-  AC_CHECK_HEADERS([sys/inotify.h],[have_inotify=yes],[have_inotify=no])
+if test "x$enable_libinotify" != "xno"; then
+   AC_CHECK_HEADERS([sys/inotify.h],[have_libinotify=yes],[have_libinotify=no])
 
-  AM_CONDITIONAL(HAVE_INOTIFY, test "$have_inotify" = "yes")
+   AM_CONDITIONAL(HAVE_LIBINOTIFY, test "$have_libinotify" = "yes")
 
-  if test "$have_inotify" = "yes" ; then
-    AC_DEFINE(HAVE_INOTIFY, 1, [Have inotify])
-  fi
+   if test "$have_libinotify" = "yes" ; then
+      AC_DEFINE(HAVE_LIBINOTIFY, 1, [Have libinotify])
+   fi
 else
-   have_inotify="no"
+   have_libinotify="no  (disabled)"
 fi
 
-AM_CONDITIONAL(HAVE_INOTIFY, test "$have_inotify" = "yes")
+AM_CONDITIONAL(HAVE_LIBINOTIFY, test "$have_libinotify" = "yes")
 
-if test "$have_inotify" = "yes" ; then
-  file_monitor="inotify"
+if test "$have_libinotify" = "yes" ; then
+  have_file_monitor="libinotify"
 else
-  file_monitor="gio"
+  have_file_monitor="gio"
 fi
 
 ####################################################################
@@ -1820,7 +1820,7 @@ Build Configuration:
 	Enable unit tests:			$have_unit_tests
 	Enable unac accent stripper:	  	$have_unac
 
-	Support for file monitoring:            $file_monitor
+	Support for file monitoring:            $have_file_monitor
 	Support for ioprio:			$have_ioprio
 	Support for HAL:                    	$have_hal
 	Support for DeviceKit-power:           	$have_devkit_power
diff --git a/src/Makefile.am b/src/Makefile.am
index b6a0b2a..bbc1d09 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -1,12 +1,12 @@
 include $(top_srcdir)/Makefile.decl
 
-if HAVE_INOTIFY
-inotify_dir = libinotify
+if HAVE_LIBINOTIFY
+libinotify_dir = libinotify
 endif
 
 SUBDIRS = 					\
 	libstemmer				\
-	$(inotify_dir)				\
+	$(libinotify_dir)			\
 	libtracker-common 			\
 	libtracker-client			\
 	libtracker-fts				\
diff --git a/src/libtracker-miner/Makefile.am b/src/libtracker-miner/Makefile.am
index f884370..c02e006 100644
--- a/src/libtracker-miner/Makefile.am
+++ b/src/libtracker-miner/Makefile.am
@@ -80,7 +80,7 @@ libtracker_miner_ TRACKER_API_VERSION@_la_LIBADD = 	\
 	$(GLIB2_LIBS)					\
 	$(GDKPIXBUF_LIBS)
 
-if HAVE_INOTIFY
+if HAVE_LIBINOTIFY
 libtracker_miner_ TRACKER_API_VERSION@_la_LIBADD +=	\
 	$(top_builddir)/src/libinotify/libinotify.la
 endif
diff --git a/src/libtracker-miner/tracker-monitor.c b/src/libtracker-miner/tracker-monitor.c
index 555ea15..711f20f 100644
--- a/src/libtracker-miner/tracker-monitor.c
+++ b/src/libtracker-miner/tracker-monitor.c
@@ -22,12 +22,12 @@
 #include <stdlib.h>
 #include <string.h>
 
-#ifdef HAVE_INOTIFY
+#ifdef HAVE_LIBINOTIFY
 #  include <sys/inotify.h>
 #  include <libinotify/libinotify.h>
 #else
 #  include <gio/gio.h>
-#endif /* HAVE_INOTIFY */
+#endif /* HAVE_LIBINOTIFY */
 
 #include <libtracker-common/tracker-keyfile-object.h>
 
@@ -76,17 +76,17 @@ struct TrackerMonitorPrivate {
 	GHashTable    *event_pairs;
 	guint          event_pairs_timeout_id;
 
-#ifdef HAVE_INOTIFY
+#ifdef HAVE_LIBINOTIFY
 	GHashTable    *cached_events;
 	guint          cached_events_timeout_id;
-#endif /* HAVE_INOTIFY */
+#endif /* HAVE_LIBINOTIFY */
 };
 
-#ifdef HAVE_INOTIFY
+#ifdef HAVE_LIBINOTIFY
 typedef INotifyHandle DirMonitor;
 #else
 typedef GFileMonitor DirMonitor;
-#endif /* HAVE_INOTIFY */
+#endif /* HAVE_LIBINOTIFY */
 
 typedef struct {
 	GFile    *file;
@@ -119,15 +119,15 @@ static void           tracker_monitor_get_property (GObject        *object,
                                                     guint           prop_id,
                                                     GValue         *value,
                                                     GParamSpec     *pspec);
+
+#ifndef HAVE_LIBINOTIFY
 static guint          get_inotify_limit            (void);
+#endif /* !HAVE_LIBINOTIFY */
 
 static DirMonitor *   directory_monitor_new        (TrackerMonitor *monitor,
 						    GFile          *file);
 static void           directory_monitor_cancel     (DirMonitor     *dir_monitor);
 
-static gboolean       monitor_move                 (TrackerMonitor *monitor,
-                                                    GFile          *old_file,
-                                                    GFile          *new_file);
 
 static void           event_data_free              (gpointer        data);
 
@@ -227,9 +227,11 @@ static void
 tracker_monitor_init (TrackerMonitor *object)
 {
 	TrackerMonitorPrivate *priv;
+#ifndef HAVE_LIBINOTIFY
 	GFile                 *file;
 	GFileMonitor          *monitor;
 	const gchar           *name;
+#endif /* HAVE_LIBINOTIFY */
 
 	object->private = TRACKER_MONITOR_GET_PRIVATE (object);
 
@@ -245,7 +247,7 @@ tracker_monitor_init (TrackerMonitor *object)
 		                       (GDestroyNotify) g_object_unref,
 		                       (GDestroyNotify) directory_monitor_cancel);
 
-#ifdef HAVE_INOTIFY
+#ifdef HAVE_LIBINOTIFY
 	/* We have a hash table with cookies so we can pair up move
 	 * events.
 	 */
@@ -264,7 +266,7 @@ tracker_monitor_init (TrackerMonitor *object)
 		                       (GEqualFunc) g_file_equal,
 		                       g_object_unref,
 		                       event_data_free);
-#else /* HAVE_INOTIFY */
+#else /* HAVE_LIBINOTIFY */
 	priv->event_pairs =
 		g_hash_table_new_full (g_file_hash,
 				       (GEqualFunc) g_file_equal,
@@ -351,7 +353,7 @@ tracker_monitor_init (TrackerMonitor *object)
 	g_file_monitor_cancel (monitor);
 	g_object_unref (monitor);
 	g_object_unref (file);
-#endif /* !HAVE_INOTIFY */
+#endif /* !HAVE_LIBINOTIFY */
 }
 
 static void
@@ -367,7 +369,7 @@ tracker_monitor_finalize (GObject *object)
 	}
 #endif /* PAUSE_ON_IO */
 
-#ifdef HAVE_INOTIFY
+#ifdef HAVE_LIBINOTIFY
 	if (priv->cached_events_timeout_id) {
 		g_source_remove (priv->cached_events_timeout_id);
 	}
@@ -377,7 +379,7 @@ tracker_monitor_finalize (GObject *object)
 	}
 
 	g_hash_table_unref (priv->cached_events);
-#endif /* HAVE_INOTIFY */
+#endif /* HAVE_LIBINOTIFY */
 
 	g_hash_table_unref (priv->event_pairs);
 	g_hash_table_unref (priv->monitors);
@@ -436,6 +438,8 @@ tracker_monitor_get_property (GObject      *object,
 	}
 }
 
+#ifndef HAVE_LIBINOTIFY
+
 static guint
 get_inotify_limit (void)
 {
@@ -465,6 +469,8 @@ get_inotify_limit (void)
 	return limit;
 }
 
+#endif /* !HAVE_LIBINOTIFY */
+
 #ifdef PAUSE_ON_IO
 
 static gboolean
@@ -538,7 +544,7 @@ event_data_free (gpointer data)
 	g_slice_free (EventData, data);
 }
 
-#ifdef HAVE_INOTIFY
+#ifdef HAVE_LIBINOTIFY
 
 static void
 event_data_update (EventData *event)
@@ -550,6 +556,99 @@ event_data_update (EventData *event)
 	event->last_time = now;
 }
 
+static gboolean
+libinotify_monitor_move (TrackerMonitor *monitor,
+                         GFile          *old_file,
+                         GFile          *new_file)
+{
+	GHashTableIter iter;
+	GHashTable *new_monitors;
+	gchar *old_prefix;
+	gpointer iter_file, iter_file_monitor;
+	guint items_moved = 0;
+
+	/* So this is tricky. What we have to do is:
+	 *
+	 * 1) Add all monitors for the new_file directory hierarchy
+	 * 2) Then remove the monitors for old_file
+	 *
+	 * This order is necessary because inotify can reuse watch
+	 * descriptors, and libinotify will remove handles
+	 * asynchronously on IN_IGNORE, so the opposite sequence
+	 * may possibly remove valid, just added, monitors.
+	 */
+	new_monitors = g_hash_table_new_full (g_file_hash,
+	                                      (GEqualFunc) g_file_equal,
+	                                      (GDestroyNotify) g_object_unref,
+	                                      NULL);
+	old_prefix = g_file_get_path (old_file);
+
+	/* Find out which subdirectories should have a file monitor added */
+	g_hash_table_iter_init (&iter, monitor->private->monitors);
+	while (g_hash_table_iter_next (&iter, &iter_file, &iter_file_monitor)) {
+		GFile *f;
+		gchar *old_path, *new_path;
+		gchar *new_prefix;
+		gchar *p;
+
+		if (!g_file_has_prefix (iter_file, old_file) &&
+		    !g_file_equal (iter_file, old_file)) {
+			continue;
+		}
+
+		old_path = g_file_get_path (iter_file);
+		p = strstr (old_path, old_prefix);
+
+		if (!p || strcmp (p, old_prefix) == 0) {
+			g_free (old_path);
+			continue;
+		}
+
+		/* Move to end of prefix */
+		p += strlen (old_prefix) + 1;
+
+		/* Check this is not the end of the string */
+		if (*p == '\0') {
+			g_free (old_path);
+			continue;
+		}
+
+		new_prefix = g_file_get_path (new_file);
+		new_path = g_build_path (G_DIR_SEPARATOR_S, new_prefix, p, NULL);
+		g_free (new_prefix);
+
+		f = g_file_new_for_path (new_path);
+		g_free (new_path);
+
+		if (!g_hash_table_lookup (new_monitors, f)) {
+			g_hash_table_insert (new_monitors, f, GINT_TO_POINTER (1));
+		} else {
+			g_object_unref (f);
+		}
+
+		g_free (old_path);
+		items_moved++;
+	}
+
+	/* Add a new monitor for the top level directory */
+	tracker_monitor_add (monitor, new_file);
+
+	/* Add a new monitor for all subdirectories */
+	g_hash_table_iter_init (&iter, new_monitors);
+	while (g_hash_table_iter_next (&iter, &iter_file, NULL)) {
+		tracker_monitor_add (monitor, iter_file);
+		g_hash_table_iter_remove (&iter);
+	}
+
+	/* Remove the monitor for the old top level directory hierarchy */
+	tracker_monitor_remove_recursively (monitor, old_file);
+
+	g_hash_table_unref (new_monitors);
+	g_free (old_prefix);
+
+	return items_moved > 0;
+}
+
 static gchar *
 libinotify_monitor_event_to_string (guint32 event_type)
 {
@@ -1129,7 +1228,7 @@ libinotify_monitor_event_cb (INotifyHandle *handle,
 			               TRUE);
 
 			if (is_directory) {
-				monitor_move (monitor, file, other_file);
+				libinotify_monitor_move (monitor, file, other_file);
 			}
 
 			g_hash_table_remove (monitor->private->event_pairs,
@@ -1194,7 +1293,7 @@ libinotify_monitor_event_cb (INotifyHandle *handle,
 			               is_source_indexed);
 
 			if (is_directory) {
-				monitor_move (monitor, other_file, file);
+				libinotify_monitor_move (monitor, other_file, file);
 			}
 
 			g_hash_table_remove (monitor->private->event_pairs,
@@ -1276,14 +1375,14 @@ directory_monitor_new (TrackerMonitor *monitor,
 }
 
 static void
-directory_monitor_cancel (gpointer data)
+directory_monitor_cancel (DirMonitor *dir_monitor)
 {
-	if (data) {
-		inotify_monitor_remove (data);
+	if (dir_monitor) {
+		inotify_monitor_remove (dir_monitor);
 	}
 }
 
-#else /* HAVE_INOTIFY */
+#else /* HAVE_LIBINOTIFY */
 
 static const gchar *
 monitor_event_to_string (GFileMonitorEvent event_type)
@@ -1306,7 +1405,7 @@ monitor_event_to_string (GFileMonitorEvent event_type)
 #if GLIB_CHECK_VERSION (2, 23, 6)
 	case G_FILE_MONITOR_EVENT_MOVED:
 		return "G_FILE_MONITOR_EVENT_MOVED";
-#endif
+#endif /* GLIB_CHECK_VERSION */
 	}
 
 	return "unknown";
@@ -1485,11 +1584,11 @@ monitor_event_cb (GFileMonitor	    *file_monitor,
 			       TRUE);
 
 		if (is_directory) {
-			monitor_move (monitor, file, other_file);
+			libinotify_monitor_move (monitor, file, other_file);
 		}
 
 		break;
-#endif
+#endif /* GLIB_CHECK_VERSION */
 
 	case G_FILE_MONITOR_EVENT_PRE_UNMOUNT:
 	case G_FILE_MONITOR_EVENT_UNMOUNTED:
@@ -1527,7 +1626,7 @@ directory_monitor_new (TrackerMonitor *monitor,
 	file_monitor = g_file_monitor_directory (file,
 #if GLIB_CHECK_VERSION (2, 23, 6)
 						 G_FILE_MONITOR_SEND_MOVED |
-#endif
+#endif /* GLIB_CHECK_VERSION */
 						 G_FILE_MONITOR_WATCH_MOUNTS,
 						 NULL,
 						 &error);
@@ -1561,100 +1660,7 @@ directory_monitor_cancel (DirMonitor *monitor)
 	}
 }
 
-#endif /* HAVE_INOTIFY */
-
-static gboolean
-monitor_move (TrackerMonitor *monitor,
-              GFile          *old_file,
-              GFile          *new_file)
-{
-	GHashTableIter iter;
-	GHashTable *new_monitors;
-	gchar *old_prefix;
-	gpointer iter_file, iter_file_monitor;
-	guint items_moved = 0;
-
-	/* So this is tricky. What we have to do is:
-	 *
-	 * 1) Add all monitors for the new_file directory hierarchy
-	 * 2) Then remove the monitors for old_file
-	 *
-	 * This order is necessary because inotify can reuse watch
-	 * descriptors, and libinotify will remove handles
-	 * asynchronously on IN_IGNORE, so the opposite sequence
-	 * may possibly remove valid, just added, monitors.
-	 */
-	new_monitors = g_hash_table_new_full (g_file_hash,
-	                                      (GEqualFunc) g_file_equal,
-	                                      (GDestroyNotify) g_object_unref,
-	                                      NULL);
-	old_prefix = g_file_get_path (old_file);
-
-	/* Find out which subdirectories should have a file monitor added */
-	g_hash_table_iter_init (&iter, monitor->private->monitors);
-	while (g_hash_table_iter_next (&iter, &iter_file, &iter_file_monitor)) {
-		GFile *f;
-		gchar *old_path, *new_path;
-		gchar *new_prefix;
-		gchar *p;
-
-		if (!g_file_has_prefix (iter_file, old_file) &&
-		    !g_file_equal (iter_file, old_file)) {
-			continue;
-		}
-
-		old_path = g_file_get_path (iter_file);
-		p = strstr (old_path, old_prefix);
-
-		if (!p || strcmp (p, old_prefix) == 0) {
-			g_free (old_path);
-			continue;
-		}
-
-		/* Move to end of prefix */
-		p += strlen (old_prefix) + 1;
-
-		/* Check this is not the end of the string */
-		if (*p == '\0') {
-			g_free (old_path);
-			continue;
-		}
-
-		new_prefix = g_file_get_path (new_file);
-		new_path = g_build_path (G_DIR_SEPARATOR_S, new_prefix, p, NULL);
-		g_free (new_prefix);
-
-		f = g_file_new_for_path (new_path);
-		g_free (new_path);
-
-		if (!g_hash_table_lookup (new_monitors, f)) {
-			g_hash_table_insert (new_monitors, f, GINT_TO_POINTER (1));
-		} else {
-			g_object_unref (f);
-		}
-
-		g_free (old_path);
-		items_moved++;
-	}
-
-	/* Add a new monitor for the top level directory */
-	tracker_monitor_add (monitor, new_file);
-
-	/* Add a new monitor for all subdirectories */
-	g_hash_table_iter_init (&iter, new_monitors);
-	while (g_hash_table_iter_next (&iter, &iter_file, NULL)) {
-		tracker_monitor_add (monitor, iter_file);
-		g_hash_table_iter_remove (&iter);
-	}
-
-	/* Remove the monitor for the old top level directory hierarchy */
-	tracker_monitor_remove_recursively (monitor, old_file);
-
-	g_hash_table_unref (new_monitors);
-	g_free (old_prefix);
-
-	return items_moved > 0;
-}
+#endif /* HAVE_LIBINOTIFY */
 
 TrackerMonitor *
 tracker_monitor_new (void)



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