[glib/wip/mount-watcher: 24/24] kqueue backend: port to new GLocalFileMonitor API



commit 862580f0c285d982a96ba558b92179a63c1ab190
Author: Ryan Lortie <desrt desrt ca>
Date:   Fri Jan 16 18:51:34 2015 -0500

    kqueue backend: port to new GLocalFileMonitor API
    
    This is the bare minimal effort.  This seems not to crash immediately,
    but it definitely needs some better testing.
    
    The backend is not in good shape.  It could use some serious work.

 gio/kqueue/Makefile.am               |    2 -
 gio/kqueue/gkqueuedirectorymonitor.c |  205 ----------------------------------
 gio/kqueue/gkqueuedirectorymonitor.h |   49 --------
 gio/kqueue/gkqueuefilemonitor.c      |   53 +++------
 gio/kqueue/gkqueuefilemonitor.h      |    4 +-
 gio/kqueue/kqueue-helper.c           |  138 ++++-------------------
 gio/kqueue/kqueue-helper.h           |    3 +-
 gio/kqueue/kqueue-utils.c            |   32 ------
 gio/kqueue/kqueue-utils.h            |    4 -
 9 files changed, 48 insertions(+), 442 deletions(-)
---
diff --git a/gio/kqueue/Makefile.am b/gio/kqueue/Makefile.am
index 77f4cb2..d5657d7 100644
--- a/gio/kqueue/Makefile.am
+++ b/gio/kqueue/Makefile.am
@@ -5,8 +5,6 @@ noinst_LTLIBRARIES += libkqueue.la
 libkqueue_la_SOURCES = \
        gkqueuefilemonitor.c \
        gkqueuefilemonitor.h \
-       gkqueuedirectorymonitor.c \
-       gkqueuedirectorymonitor.h \
        kqueue-helper.c \
        kqueue-helper.h \
        kqueue-thread.c \
diff --git a/gio/kqueue/gkqueuefilemonitor.c b/gio/kqueue/gkqueuefilemonitor.c
index d2d51a9..e518ab7 100644
--- a/gio/kqueue/gkqueuefilemonitor.c
+++ b/gio/kqueue/gkqueuefilemonitor.c
@@ -35,16 +35,13 @@ struct _GKqueueFileMonitor
   GLocalFileMonitor parent_instance;
 
   kqueue_sub *sub;
-  
+
   GFileMonitor *fallback;
   GFile *fbfile;
-
-  gboolean pair_moves;
 };
 
 static gboolean g_kqueue_file_monitor_cancel (GFileMonitor* monitor);
 
-#define g_kqueue_file_monitor_get_type _g_kqueue_file_monitor_get_type
 G_DEFINE_TYPE_WITH_CODE (GKqueueFileMonitor, g_kqueue_file_monitor, G_TYPE_LOCAL_FILE_MONITOR,
        g_io_extension_point_implement (G_LOCAL_FILE_MONITOR_EXTENSION_POINT_NAME,
                g_define_type_id,
@@ -59,16 +56,18 @@ _fallback_callback (GFileMonitor      *unused,
                     GFileMonitorEvent  event,
                     gpointer           udata)
 {
-  GKqueueFileMonitor *kq_mon = G_KQUEUE_FILE_MONITOR (udata); 
+  GKqueueFileMonitor *kq_mon = G_KQUEUE_FILE_MONITOR (udata);
   GFileMonitor *mon = G_FILE_MONITOR (kq_mon);
   g_assert (kq_mon != NULL);
   g_assert (mon != NULL);
   (void) unused;
 
   if (event == G_FILE_MONITOR_EVENT_CHANGED)
-  {
-    _kh_dir_diff (kq_mon->sub, mon);
-  }
+    {
+      GLocalFileMonitor *local_monitor = G_LOCAL_FILE_MONITOR (kq_mon);
+
+      _kh_dir_diff (kq_mon->sub, local_monitor->source);
+    }
   else
     g_file_monitor_emit_event (mon, first, second, event);
 }
@@ -96,38 +95,28 @@ g_kqueue_file_monitor_finalize (GObject *object)
     (*G_OBJECT_CLASS (g_kqueue_file_monitor_parent_class)->finalize) (object);
 }
 
-static GObject*
-g_kqueue_file_monitor_constructor (GType                 type,
-                                   guint                 n_construct_properties,
-                                   GObjectConstructParam *construct_properties)
+static void
+g_kqueue_file_monitor_start (GLocalFileMonitor *local_monitor,
+                             const gchar *dirname,
+                             const gchar *basename,
+                             const gchar *filename,
+                             GFileMonitorSource *source)
 {
+  GKqueueFileMonitor *kqueue_monitor = G_KQUEUE_FILE_MONITOR (local_monitor);
   GObject *obj;
   GKqueueFileMonitorClass *klass;
   GObjectClass *parent_class;
-  GKqueueFileMonitor *kqueue_monitor;
   kqueue_sub *sub = NULL;
   gboolean ret_kh_startup = FALSE;
   const gchar *path = NULL; 
 
-  klass = G_KQUEUE_FILE_MONITOR_CLASS (g_type_class_peek (G_TYPE_KQUEUE_FILE_MONITOR));
-  parent_class = G_OBJECT_CLASS (g_type_class_peek_parent (klass));
-  obj = parent_class->constructor (type,
-                                   n_construct_properties,
-                                   construct_properties);
-
-  kqueue_monitor = G_KQUEUE_FILE_MONITOR (obj);
 
   ret_kh_startup = _kh_startup ();
   g_assert (ret_kh_startup);
 
-  kqueue_monitor->pair_moves = G_LOCAL_FILE_MONITOR (obj)->flags & G_FILE_MONITOR_SEND_MOVED
-                               ? TRUE : FALSE;
-
-  kqueue_monitor->sub = NULL;
-  kqueue_monitor->fallback = NULL;
-  kqueue_monitor->fbfile = NULL;
-
-  path = G_LOCAL_FILE_MONITOR (obj)->filename;
+  path = filename;
+  if (!path)
+    path = dirname;
 
   /* For a directory monitor, create a subscription object anyway.
    * It will be used for directory diff calculation routines. 
@@ -137,9 +126,7 @@ g_kqueue_file_monitor_constructor (GType                 type,
    * will be created under that path, GKqueueFileMonitor will have to
    * handle the directory notifications. */
 
-  sub = _kh_sub_new (path,
-                     kqueue_monitor->pair_moves,
-                     kqueue_monitor);
+  sub = _kh_sub_new (path, TRUE, source);
 
   /* FIXME: what to do about errors here? we can't return NULL or another
    * kind of error and an assertion is probably too hard (same issue as in
@@ -159,8 +146,6 @@ g_kqueue_file_monitor_constructor (GType                 type,
                         G_CALLBACK (_fallback_callback),
                         kqueue_monitor);
     }
-
-  return obj;
 }
 
 static gboolean
@@ -177,10 +162,10 @@ g_kqueue_file_monitor_class_init (GKqueueFileMonitorClass *klass)
   GLocalFileMonitorClass *local_file_monitor_class = G_LOCAL_FILE_MONITOR_CLASS (klass);
 
   gobject_class->finalize = g_kqueue_file_monitor_finalize;
-  gobject_class->constructor = g_kqueue_file_monitor_constructor;
   file_monitor_class->cancel = g_kqueue_file_monitor_cancel;
 
   local_file_monitor_class->is_supported = g_kqueue_file_monitor_is_supported;
+  local_file_monitor_class->start = g_kqueue_file_monitor_start;
 }
 
 static void
diff --git a/gio/kqueue/gkqueuefilemonitor.h b/gio/kqueue/gkqueuefilemonitor.h
index 3c47377..32752f1 100644
--- a/gio/kqueue/gkqueuefilemonitor.h
+++ b/gio/kqueue/gkqueuefilemonitor.h
@@ -31,7 +31,7 @@
 
 G_BEGIN_DECLS
 
-#define G_TYPE_KQUEUE_FILE_MONITOR        (_g_kqueue_file_monitor_get_type ())
+#define G_TYPE_KQUEUE_FILE_MONITOR        (g_kqueue_file_monitor_get_type ())
 #define G_KQUEUE_FILE_MONITOR(o)          (G_TYPE_CHECK_INSTANCE_CAST ((o), G_TYPE_KQUEUE_FILE_MONITOR, 
GKqueueFileMonitor))
 #define G_KQUEUE_FILE_MONITOR_CLASS(k)    (G_TYPE_CHECK_CLASS_CAST ((k), G_TYPE_KQUEUE_FILE_MONITOR, 
GKqueueFileMonitorClass))
 #define G_IS_KQUEUE_FILE_MONITOR(o)       (G_TYPE_CHECK_INSTANCE_TYPE ((o), G_TYPE_KQUEUE_FILE_MONITOR))
@@ -44,7 +44,7 @@ struct _GKqueueFileMonitorClass {
   GLocalFileMonitorClass parent_class;
 };
 
-GType _g_kqueue_file_monitor_get_type (void);
+GType g_kqueue_file_monitor_get_type (void);
 
 G_END_DECLS
 
diff --git a/gio/kqueue/kqueue-helper.c b/gio/kqueue/kqueue-helper.c
index 1a95b83..fab2a76 100644
--- a/gio/kqueue/kqueue-helper.c
+++ b/gio/kqueue/kqueue-helper.c
@@ -26,6 +26,7 @@
 #include <sys/time.h>
 #include <sys/socket.h>
 #include <gio/glocalfile.h>
+#include <gio/glocalfilemonitor.h>
 #include <gio/gfile.h>
 #include <fcntl.h>
 #include <unistd.h>
@@ -38,8 +39,6 @@
 #include "kqueue-missing.h"
 #include "kqueue-exclusions.h"
 
-#include "gkqueuedirectorymonitor.h"
-
 static gboolean kh_debug_enabled = FALSE;
 #define KH_W if (kh_debug_enabled) g_warning
 
@@ -113,7 +112,7 @@ convert_kqueue_events_to_gio (uint32_t flags, gboolean *done)
 
 typedef struct {
   kqueue_sub *sub;
-  GFileMonitor *monitor;  
+  GFileMonitorSource *source;
 } handle_ctx;
 
 /**
@@ -129,29 +128,15 @@ static void
 handle_created (void *udata, const char *path, ino_t inode)
 {
   handle_ctx *ctx = NULL;
-  GFile *file = NULL;
-  gchar *fpath = NULL;
 
   (void) inode;
   ctx = (handle_ctx *) udata;
   g_assert (udata != NULL);
   g_assert (ctx->sub != NULL);
-  g_assert (ctx->monitor != NULL);
-
-  fpath = _ku_path_concat (ctx->sub->filename, path);
-  if (fpath == NULL)
-    {
-      KH_W ("Failed to allocate a string for a new event");
-      return;
-    }
+  g_assert (ctx->source != NULL);
 
-  file = g_file_new_for_path (fpath);
-  g_file_monitor_emit_event (ctx->monitor,
-                             file,
-                             NULL,
-                             G_FILE_MONITOR_EVENT_CREATED);
-  g_free (fpath);
-  g_object_unref (file);
+  g_file_monitor_source_handle_event (ctx->source, G_FILE_MONITOR_EVENT_CREATED, path,
+                                      NULL, NULL, g_get_monotonic_time ());
 }
 
 /**
@@ -167,29 +152,15 @@ static void
 handle_deleted (void *udata, const char *path, ino_t inode)
 {
   handle_ctx *ctx = NULL;
-  GFile *file = NULL;
-  gchar *fpath = NULL;
 
   (void) inode;
   ctx = (handle_ctx *) udata;
   g_assert (udata != NULL);
   g_assert (ctx->sub != NULL);
-  g_assert (ctx->monitor != NULL);
-
-  fpath = _ku_path_concat (ctx->sub->filename, path);
-  if (fpath == NULL)
-    {
-      KH_W ("Failed to allocate a string for a new event");
-      return;
-    }
+  g_assert (ctx->source != NULL);
 
-  file = g_file_new_for_path (fpath);
-  g_file_monitor_emit_event (ctx->monitor,
-                             file,
-                             NULL,
-                             G_FILE_MONITOR_EVENT_DELETED);
-  g_free (fpath);
-  g_object_unref (file);
+  g_file_monitor_source_handle_event (ctx->source, G_FILE_MONITOR_EVENT_DELETED, path,
+                                      NULL, NULL, g_get_monotonic_time ());
 }
 
 /**
@@ -201,7 +172,7 @@ handle_deleted (void *udata, const char *path, ino_t inode)
  * @to_inode: inode number of the replaced file.
  *
  * A callback function for the directory diff calculation routine,
- * produces G_FILE_MONITOR_EVENT_MOVED event on a move.
+ * produces G_FILE_MONITOR_EVENT_RENAMED event on a move.
  **/
 static void
 handle_moved (void       *udata,
@@ -211,10 +182,6 @@ handle_moved (void       *udata,
               ino_t       to_inode)
 {
   handle_ctx *ctx = NULL;
-  GFile *file = NULL;
-  GFile *other = NULL;
-  gchar *path = NULL;
-  gchar *npath = NULL;
 
   (void) from_inode;
   (void) to_inode;
@@ -222,47 +189,12 @@ handle_moved (void       *udata,
   ctx = (handle_ctx *) udata;
   g_assert (udata != NULL);
   g_assert (ctx->sub != NULL);
-  g_assert (ctx->monitor != NULL);
-
-
-  path = _ku_path_concat (ctx->sub->filename, from_path);
-  npath = _ku_path_concat (ctx->sub->filename, to_path);
-  if (path == NULL || npath == NULL)
-    {
-      KH_W ("Failed to allocate strings for event");
-      return;
-    }
+  g_assert (ctx->source != NULL);
 
-  file = g_file_new_for_path (path);
-  other = g_file_new_for_path (npath);
-
-  if (ctx->sub->pair_moves)
-    {
-      g_file_monitor_emit_event (ctx->monitor,
-                                 file,
-                                 other,
-                                 G_FILE_MONITOR_EVENT_MOVED);
-    }
-  else
-    {
-      g_file_monitor_emit_event (ctx->monitor,
-                                 file,
-                                 NULL,
-                                 G_FILE_MONITOR_EVENT_DELETED);
-      g_file_monitor_emit_event (ctx->monitor,
-                                 other,
-                                 NULL,
-                                 G_FILE_MONITOR_EVENT_CREATED);
-    }
-
-  g_free (path);
-  g_free (npath);
-
-  g_object_unref (file);
-  g_object_unref (other);
+  g_file_monitor_source_handle_event (ctx->source, G_FILE_MONITOR_EVENT_RENAMED,
+                                      from_path, to_path, NULL, g_get_monotonic_time ());
 }
 
-
 /**
  * handle_overwritten:
  * @data: a pointer to user data (#handle_context).
@@ -277,34 +209,18 @@ static void
 handle_overwritten (void *udata, const char *path, ino_t inode)
 {
   handle_ctx *ctx = NULL;
-  GFile *file = NULL;
-  gchar *fpath = NULL;
 
   (void) inode;
   ctx = (handle_ctx *) udata;
   g_assert (udata != NULL);
   g_assert (ctx->sub != NULL);
-  g_assert (ctx->monitor != NULL);
+  g_assert (ctx->source != NULL);
 
-  fpath = _ku_path_concat (ctx->sub->filename, path);
-  if (fpath == NULL)
-    {
-      KH_W ("Failed to allocate a string for a new event");
-      return;
-    }
+  g_file_monitor_source_handle_event (ctx->source, G_FILE_MONITOR_EVENT_DELETED,
+                                      path, NULL, NULL, g_get_monotonic_time ());
 
-  file = g_file_new_for_path (fpath);
-  g_file_monitor_emit_event (ctx->monitor,
-                             file,
-                             NULL,
-                             G_FILE_MONITOR_EVENT_DELETED);
-  g_file_monitor_emit_event (ctx->monitor,
-                             file,
-                             NULL,
-                             G_FILE_MONITOR_EVENT_CREATED);
-
-  g_free (fpath);
-  g_object_unref (file);
+  g_file_monitor_source_handle_event (ctx->source, G_FILE_MONITOR_EVENT_CREATED,
+                                      path, NULL, NULL, g_get_monotonic_time ());
 }
 
 static const traverse_cbs cbs = {
@@ -320,17 +236,17 @@ static const traverse_cbs cbs = {
 
 
 void
-_kh_dir_diff (kqueue_sub *sub, GFileMonitor *monitor)
+_kh_dir_diff (kqueue_sub *sub, GFileMonitorSource *source)
 {
   dep_list *was;
   handle_ctx ctx;
 
   g_assert (sub != NULL);
-  g_assert (monitor != NULL);
+  g_assert (source != NULL);
 
   memset (&ctx, 0, sizeof (handle_ctx));
   ctx.sub = sub;
-  ctx.monitor = monitor;
+  ctx.source = source;
 
   was = sub->deps;
   sub->deps = dl_listing (sub->filename);
@@ -363,7 +279,7 @@ process_kqueue_notifications (GIOChannel   *gioc,
 {
   struct kqueue_notification n;
   kqueue_sub *sub = NULL;
-  GFileMonitor *monitor = NULL;
+  GFileMonitorSource *source = NULL;
   GFileMonitorEvent mask = 0;
   
   g_assert (kqueue_socket_pair[0] != -1);
@@ -384,8 +300,8 @@ process_kqueue_notifications (GIOChannel   *gioc,
       return TRUE;
     }
 
-  monitor = G_FILE_MONITOR (sub->user_data);
-  g_assert (monitor != NULL);
+  source = sub->user_data;
+  g_assert (source != NULL);
 
   if (n.flags & (NOTE_DELETE | NOTE_REVOKE))
     {
@@ -408,7 +324,7 @@ process_kqueue_notifications (GIOChannel   *gioc,
 
   if (sub->is_dir && n.flags & (NOTE_WRITE | NOTE_EXTEND))
     {
-      _kh_dir_diff (sub, monitor);  
+      _kh_dir_diff (sub, source);
       n.flags &= ~(NOTE_WRITE | NOTE_EXTEND);
     }
 
@@ -417,11 +333,7 @@ process_kqueue_notifications (GIOChannel   *gioc,
       gboolean done = FALSE;
       mask = convert_kqueue_events_to_gio (n.flags, &done);
       if (done == TRUE)
-        {
-          GFile *file = g_file_new_for_path (sub->filename);
-          g_file_monitor_emit_event (monitor, file, NULL, mask);
-          g_object_unref (file);
-        }
+        g_file_monitor_source_handle_event (source, mask, NULL, NULL, NULL, g_get_monotonic_time ());
     }
 
   return TRUE;
diff --git a/gio/kqueue/kqueue-helper.h b/gio/kqueue/kqueue-helper.h
index fc33a8d..b12a28f 100644
--- a/gio/kqueue/kqueue-helper.h
+++ b/gio/kqueue/kqueue-helper.h
@@ -24,6 +24,7 @@
 #define __KQUEUE_HELPER_H
 
 #include "kqueue-sub.h"
+#include <gio/glocalfilemonitor.h>
 #include <gio/gfilemonitor.h>
 
 gboolean _kh_startup        (void);
@@ -32,6 +33,6 @@ gboolean _kh_cancel_sub     (kqueue_sub *sub);
 
 gboolean _kh_start_watching (kqueue_sub *sub);
 
-void     _kh_dir_diff       (kqueue_sub *sub, GFileMonitor *monitor);
+void     _kh_dir_diff       (kqueue_sub *sub, GFileMonitorSource *source);
 
 #endif /* __KQUEUE_HELPER_H */
diff --git a/gio/kqueue/kqueue-utils.c b/gio/kqueue/kqueue-utils.c
index 00b5c26..bba6522 100644
--- a/gio/kqueue/kqueue-utils.c
+++ b/gio/kqueue/kqueue-utils.c
@@ -208,35 +208,3 @@ _ku_file_information (int fd, int *is_dir, ino_t *inode)
   if (inode != NULL)
       *inode = st.st_ino;
 }
-
-/**
- * Create a file path using its name and a path to its directory.
- *
- * @param[in] dir  A path to a file directory. May end with a '/'.
- * @param[in] file File name.
- * @return A concatenated path. Should be freed with free().
- **/
-gchar*
-_ku_path_concat (const gchar *dir, const gchar *file)
-{
-  int dir_len = strlen (dir);
-  int file_len = strlen (file);
-
-  char *path = g_malloc (dir_len + file_len + 2);
-  if (path == NULL)
-    {
-      KU_W ("Failed to allocate memory path for concatenation");
-      return NULL;
-    }
-
-  strcpy (path, dir);
-
-  if (dir[dir_len - 1] != '/') {
-      ++dir_len;
-      path[dir_len - 1] = '/';
-  }
-
-  strcpy (path + dir_len, file);
-  return path;
-}
-
diff --git a/gio/kqueue/kqueue-utils.h b/gio/kqueue/kqueue-utils.h
index 2c4f2c3..4e37f4a 100644
--- a/gio/kqueue/kqueue-utils.h
+++ b/gio/kqueue/kqueue-utils.h
@@ -50,8 +50,4 @@ gboolean _ku_write            (int fd, gconstpointer data, gsize size);
 
 void     _ku_file_information (int fd, int *is_dir, ino_t *inode);
 
-gchar*    _ku_path_concat     (const gchar *dir, const gchar *file);
-
-
-
 #endif /* __KQUEUE_UTILS_H */


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