Blame gio/kqueue/gkqueuefilemonitor.c

Packit ae235b
/*******************************************************************************
Packit ae235b
  Copyright (c) 2011, 2012 Dmitry Matveev <me@dmitrymatveev.co.uk>
Packit ae235b
Packit ae235b
  Permission is hereby granted, free of charge, to any person obtaining a copy
Packit ae235b
  of this software and associated documentation files (the "Software"), to deal
Packit ae235b
  in the Software without restriction, including without limitation the rights
Packit ae235b
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
Packit ae235b
  copies of the Software, and to permit persons to whom the Software is
Packit ae235b
  furnished to do so, subject to the following conditions:
Packit ae235b
Packit ae235b
  The above copyright notice and this permission notice shall be included in
Packit ae235b
  all copies or substantial portions of the Software.
Packit ae235b
Packit ae235b
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Packit ae235b
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Packit ae235b
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Packit ae235b
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Packit ae235b
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Packit ae235b
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
Packit ae235b
  THE SOFTWARE.
Packit ae235b
*******************************************************************************/
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
Packit ae235b
#include "gkqueuefilemonitor.h"
Packit ae235b
#include "kqueue-helper.h"
Packit ae235b
#include "kqueue-exclusions.h"
Packit ae235b
#include <gio/gpollfilemonitor.h>
Packit ae235b
#include <gio/gfile.h>
Packit ae235b
#include <gio/giomodule.h>
Packit ae235b
Packit ae235b
Packit ae235b
struct _GKqueueFileMonitor
Packit ae235b
{
Packit ae235b
  GLocalFileMonitor parent_instance;
Packit ae235b
Packit ae235b
  kqueue_sub *sub;
Packit ae235b
Packit ae235b
  GFileMonitor *fallback;
Packit ae235b
  GFile *fbfile;
Packit ae235b
};
Packit ae235b
Packit ae235b
static gboolean g_kqueue_file_monitor_cancel (GFileMonitor* monitor);
Packit ae235b
Packit ae235b
G_DEFINE_TYPE_WITH_CODE (GKqueueFileMonitor, g_kqueue_file_monitor, G_TYPE_LOCAL_FILE_MONITOR,
Packit ae235b
       g_io_extension_point_implement (G_LOCAL_FILE_MONITOR_EXTENSION_POINT_NAME,
Packit ae235b
               g_define_type_id,
Packit ae235b
               "kqueue",
Packit ae235b
               20))
Packit ae235b
Packit ae235b
Packit ae235b
static void
Packit ae235b
_fallback_callback (GFileMonitor      *unused,
Packit ae235b
                    GFile             *first,
Packit ae235b
                    GFile             *second,
Packit ae235b
                    GFileMonitorEvent  event,
Packit ae235b
                    gpointer           udata)
Packit ae235b
{
Packit ae235b
  GKqueueFileMonitor *kq_mon = G_KQUEUE_FILE_MONITOR (udata);
Packit ae235b
  GFileMonitor *mon = G_FILE_MONITOR (kq_mon);
Packit ae235b
  g_assert (kq_mon != NULL);
Packit ae235b
  g_assert (mon != NULL);
Packit ae235b
  (void) unused;
Packit ae235b
Packit ae235b
  if (event == G_FILE_MONITOR_EVENT_CHANGED)
Packit ae235b
    {
Packit ae235b
      GLocalFileMonitor *local_monitor = G_LOCAL_FILE_MONITOR (kq_mon);
Packit ae235b
Packit ae235b
      _kh_dir_diff (kq_mon->sub, local_monitor->source);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    g_file_monitor_emit_event (mon, first, second, event);
Packit ae235b
}
Packit ae235b
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_kqueue_file_monitor_finalize (GObject *object)
Packit ae235b
{
Packit ae235b
  GKqueueFileMonitor *kqueue_monitor = G_KQUEUE_FILE_MONITOR (object);
Packit ae235b
Packit ae235b
  if (kqueue_monitor->sub)
Packit ae235b
    {
Packit ae235b
      _kh_cancel_sub (kqueue_monitor->sub);
Packit ae235b
      _kh_sub_free (kqueue_monitor->sub);
Packit ae235b
      kqueue_monitor->sub = NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (kqueue_monitor->fallback)
Packit ae235b
    g_object_unref (kqueue_monitor->fallback);
Packit ae235b
Packit ae235b
  if (kqueue_monitor->fbfile)
Packit ae235b
    g_object_unref (kqueue_monitor->fbfile);
Packit ae235b
Packit ae235b
  if (G_OBJECT_CLASS (g_kqueue_file_monitor_parent_class)->finalize)
Packit ae235b
    (*G_OBJECT_CLASS (g_kqueue_file_monitor_parent_class)->finalize) (object);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_kqueue_file_monitor_start (GLocalFileMonitor *local_monitor,
Packit ae235b
                             const gchar *dirname,
Packit ae235b
                             const gchar *basename,
Packit ae235b
                             const gchar *filename,
Packit ae235b
                             GFileMonitorSource *source)
Packit ae235b
{
Packit ae235b
  GKqueueFileMonitor *kqueue_monitor = G_KQUEUE_FILE_MONITOR (local_monitor);
Packit ae235b
  GObject *obj;
Packit ae235b
  GKqueueFileMonitorClass *klass;
Packit ae235b
  GObjectClass *parent_class;
Packit ae235b
  kqueue_sub *sub = NULL;
Packit ae235b
  gboolean ret_kh_startup = FALSE;
Packit ae235b
  const gchar *path = NULL; 
Packit ae235b
Packit ae235b
Packit ae235b
  ret_kh_startup = _kh_startup ();
Packit ae235b
  g_assert (ret_kh_startup);
Packit ae235b
Packit ae235b
  path = filename;
Packit ae235b
  if (!path)
Packit ae235b
    path = dirname;
Packit ae235b
Packit ae235b
  /* For a directory monitor, create a subscription object anyway.
Packit ae235b
   * It will be used for directory diff calculation routines. 
Packit ae235b
   * Wait, directory diff in a GKqueueFileMonitor?
Packit ae235b
   * Yes, it is. When a file monitor is started on an non-existent
Packit ae235b
   * file, GIO uses a GKqueueFileMonitor object for that. If a directory
Packit ae235b
   * will be created under that path, GKqueueFileMonitor will have to
Packit ae235b
   * handle the directory notifications. */
Packit ae235b
Packit ae235b
  sub = _kh_sub_new (path, TRUE, source);
Packit ae235b
Packit ae235b
  /* FIXME: what to do about errors here? we can't return NULL or another
Packit ae235b
   * kind of error and an assertion is probably too hard (same issue as in
Packit ae235b
   * the inotify backend) */
Packit ae235b
  g_assert (sub != NULL);
Packit ae235b
  kqueue_monitor->sub = sub;
Packit ae235b
Packit ae235b
  if (!_ke_is_excluded (path))
Packit ae235b
    _kh_add_sub (sub);
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      GFile *file = g_file_new_for_path (path);
Packit ae235b
      kqueue_monitor->fbfile = file;
Packit ae235b
      kqueue_monitor->fallback = _g_poll_file_monitor_new (file);
Packit ae235b
      g_signal_connect (kqueue_monitor->fallback,
Packit ae235b
                        "changed",
Packit ae235b
                        G_CALLBACK (_fallback_callback),
Packit ae235b
                        kqueue_monitor);
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_kqueue_file_monitor_is_supported (void)
Packit ae235b
{
Packit ae235b
  return _kh_startup ();
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_kqueue_file_monitor_class_init (GKqueueFileMonitorClass *klass)
Packit ae235b
{
Packit ae235b
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
Packit ae235b
  GFileMonitorClass *file_monitor_class = G_FILE_MONITOR_CLASS (klass);
Packit ae235b
  GLocalFileMonitorClass *local_file_monitor_class = G_LOCAL_FILE_MONITOR_CLASS (klass);
Packit ae235b
Packit ae235b
  gobject_class->finalize = g_kqueue_file_monitor_finalize;
Packit ae235b
  file_monitor_class->cancel = g_kqueue_file_monitor_cancel;
Packit ae235b
Packit ae235b
  local_file_monitor_class->is_supported = g_kqueue_file_monitor_is_supported;
Packit ae235b
  local_file_monitor_class->start = g_kqueue_file_monitor_start;
Packit ae235b
  local_file_monitor_class->mount_notify = TRUE; /* TODO: ??? */
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_kqueue_file_monitor_init (GKqueueFileMonitor *monitor)
Packit ae235b
{
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_kqueue_file_monitor_cancel (GFileMonitor *monitor)
Packit ae235b
{
Packit ae235b
  GKqueueFileMonitor *kqueue_monitor = G_KQUEUE_FILE_MONITOR (monitor);
Packit ae235b
Packit ae235b
  if (kqueue_monitor->sub)
Packit ae235b
    {
Packit ae235b
      _kh_cancel_sub (kqueue_monitor->sub);
Packit ae235b
      _kh_sub_free (kqueue_monitor->sub);
Packit ae235b
      kqueue_monitor->sub = NULL;
Packit ae235b
    }
Packit ae235b
  else if (kqueue_monitor->fallback)
Packit ae235b
    {
Packit ae235b
      g_signal_handlers_disconnect_by_func (kqueue_monitor->fallback, _fallback_callback, kqueue_monitor);
Packit ae235b
      g_file_monitor_cancel (kqueue_monitor->fallback);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (G_FILE_MONITOR_CLASS (g_kqueue_file_monitor_parent_class)->cancel)
Packit ae235b
    (*G_FILE_MONITOR_CLASS (g_kqueue_file_monitor_parent_class)->cancel) (monitor);
Packit ae235b
Packit ae235b
  return TRUE;
Packit ae235b
}