(root)/
glib-2.79.0/
gio/
inotify/
ginotifyfilemonitor.c
       1  /* GIO - GLib Input, Output and Streaming Library
       2   * 
       3   * Copyright (C) 2006-2007 Red Hat, Inc.
       4   * Copyright (C) 2007 Sebastian Dröge.
       5   *
       6   * This library is free software; you can redistribute it and/or
       7   * modify it under the terms of the GNU Lesser General Public
       8   * License as published by the Free Software Foundation; either
       9   * version 2.1 of the License, or (at your option) any later version.
      10   *
      11   * This library is distributed in the hope that it will be useful,
      12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
      13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      14   * Lesser General Public License for more details.
      15   *
      16   * You should have received a copy of the GNU Lesser General
      17   * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
      18   *
      19   * Authors: Alexander Larsson <alexl@redhat.com>
      20   *          John McCutchan <john@johnmccutchan.com> 
      21   *          Sebastian Dröge <slomo@circular-chaos.org>
      22   *          Ryan Lortie <desrt@desrt.ca>
      23   */
      24  
      25  #include "config.h"
      26  
      27  #include "ginotifyfilemonitor.h"
      28  #include <gio/giomodule.h>
      29  
      30  #define USE_INOTIFY 1
      31  #include "inotify-helper.h"
      32  
      33  struct _GInotifyFileMonitor
      34  {
      35    GLocalFileMonitor parent_instance;
      36  
      37    inotify_sub *sub;
      38  };
      39  
      40  G_DEFINE_TYPE_WITH_CODE (GInotifyFileMonitor, g_inotify_file_monitor, G_TYPE_LOCAL_FILE_MONITOR,
      41                           g_io_extension_point_implement (G_LOCAL_FILE_MONITOR_EXTENSION_POINT_NAME,
      42                                                           g_define_type_id, "inotify", 20))
      43  
      44  static gboolean
      45  g_inotify_file_monitor_is_supported (void)
      46  {
      47    return _ih_startup ();
      48  }
      49  
      50  static void
      51  g_inotify_file_monitor_start (GLocalFileMonitor  *local_monitor,
      52                                const gchar        *dirname,
      53                                const gchar        *basename,
      54                                const gchar        *filename,
      55                                GFileMonitorSource *source)
      56  {
      57    GInotifyFileMonitor *inotify_monitor = G_INOTIFY_FILE_MONITOR (local_monitor);
      58    gboolean success G_GNUC_UNUSED  /* when compiling with G_DISABLE_ASSERT */;
      59  
      60    /* should already have been called, from is_supported() */
      61    success = _ih_startup ();
      62    g_assert (success);
      63  
      64    inotify_monitor->sub = _ih_sub_new (dirname, basename, filename, source);
      65    _ih_sub_add (inotify_monitor->sub);
      66  }
      67  
      68  static gboolean
      69  g_inotify_file_monitor_cancel (GFileMonitor *monitor)
      70  {
      71    GInotifyFileMonitor *inotify_monitor = G_INOTIFY_FILE_MONITOR (monitor);
      72  
      73    if (inotify_monitor->sub)
      74      {
      75        _ih_sub_cancel (inotify_monitor->sub);
      76        _ih_sub_free (inotify_monitor->sub);
      77        inotify_monitor->sub = NULL;
      78      }
      79  
      80    return TRUE;
      81  }
      82  
      83  static void
      84  g_inotify_file_monitor_finalize (GObject *object)
      85  {
      86  #ifndef G_DISABLE_ASSERT
      87    GInotifyFileMonitor *inotify_monitor = G_INOTIFY_FILE_MONITOR (object);
      88  #endif
      89  
      90    /* must surely have been cancelled already */
      91    g_assert (!inotify_monitor->sub);
      92  
      93    G_OBJECT_CLASS (g_inotify_file_monitor_parent_class)->finalize (object);
      94  }
      95  
      96  static void
      97  g_inotify_file_monitor_init (GInotifyFileMonitor* monitor)
      98  {
      99  }
     100  
     101  static void
     102  g_inotify_file_monitor_class_init (GInotifyFileMonitorClass* klass)
     103  {
     104    GObjectClass* gobject_class = G_OBJECT_CLASS (klass);
     105    GFileMonitorClass *file_monitor_class = G_FILE_MONITOR_CLASS (klass);
     106    GLocalFileMonitorClass *local_file_monitor_class = G_LOCAL_FILE_MONITOR_CLASS (klass);
     107  
     108    local_file_monitor_class->is_supported = g_inotify_file_monitor_is_supported;
     109    local_file_monitor_class->start = g_inotify_file_monitor_start;
     110    local_file_monitor_class->mount_notify = TRUE;
     111    file_monitor_class->cancel = g_inotify_file_monitor_cancel;
     112  
     113    gobject_class->finalize = g_inotify_file_monitor_finalize;
     114  }