1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
4 * Copyright (C) 2007 Sebastian Dröge.
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 of the License, or (at your option) any later version.
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.
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/>.
19 * Authors: Alexander Larsson <alexl@redhat.com>
20 * John McCutchan <john@johnmccutchan.com>
21 * Sebastian Dröge <slomo@circular-chaos.org>
26 #include "ginotifyfilemonitor.h"
27 #include <gio/giomodule.h>
30 #include "inotify-helper.h"
32 struct _GInotifyFileMonitor
34 GLocalFileMonitor parent_instance;
41 static gboolean g_inotify_file_monitor_cancel (GFileMonitor* monitor);
43 #define g_inotify_file_monitor_get_type _g_inotify_file_monitor_get_type
44 G_DEFINE_TYPE_WITH_CODE (GInotifyFileMonitor, g_inotify_file_monitor, G_TYPE_LOCAL_FILE_MONITOR,
45 g_io_extension_point_implement (G_LOCAL_FILE_MONITOR_EXTENSION_POINT_NAME,
51 g_inotify_file_monitor_finalize (GObject *object)
53 GInotifyFileMonitor *inotify_monitor = G_INOTIFY_FILE_MONITOR (object);
54 inotify_sub *sub = inotify_monitor->sub;
60 inotify_monitor->sub = NULL;
63 g_free (inotify_monitor->filename);
64 g_free (inotify_monitor->dirname);
66 G_OBJECT_CLASS (g_inotify_file_monitor_parent_class)->finalize (object);
70 g_inotify_file_monitor_start (GLocalFileMonitor *local_monitor)
72 GInotifyFileMonitor *inotify_monitor = G_INOTIFY_FILE_MONITOR (local_monitor);
73 const gchar *filename = NULL;
74 gboolean watch_hardlinks;
75 inotify_sub *sub = NULL;
77 gboolean ret_ih_startup; /* return value of _ih_startup, for asserting */
79 filename = local_monitor->filename;
80 g_assert (filename != NULL);
82 inotify_monitor->filename = g_path_get_basename (filename);
83 inotify_monitor->dirname = g_path_get_dirname (filename);
85 /* Will never fail as is_supported() should be called before instantiating
87 /* assert on return value */
88 ret_ih_startup = _ih_startup();
89 g_assert (ret_ih_startup);
91 pair_moves = local_monitor->flags & G_FILE_MONITOR_SEND_MOVED;
92 watch_hardlinks = local_monitor->flags & G_FILE_MONITOR_WATCH_HARD_LINKS;
94 sub = _ih_sub_new (inotify_monitor->dirname,
95 inotify_monitor->filename,
100 /* FIXME: what to do about errors here? we can't return NULL or another
101 * kind of error and an assertion is probably too hard */
102 g_assert (sub != NULL);
104 /* _ih_sub_add allways returns TRUE, see gio/inotify/inotify-helper.c line 109
105 * g_assert (_ih_sub_add (sub)); */
108 inotify_monitor->sub = sub;
112 g_inotify_file_monitor_is_supported (void)
114 return _ih_startup ();
118 g_inotify_file_monitor_class_init (GInotifyFileMonitorClass* klass)
120 GObjectClass* gobject_class = G_OBJECT_CLASS (klass);
121 GFileMonitorClass *file_monitor_class = G_FILE_MONITOR_CLASS (klass);
122 GLocalFileMonitorClass *local_file_monitor_class = G_LOCAL_FILE_MONITOR_CLASS (klass);
124 gobject_class->finalize = g_inotify_file_monitor_finalize;
125 file_monitor_class->cancel = g_inotify_file_monitor_cancel;
127 local_file_monitor_class->is_supported = g_inotify_file_monitor_is_supported;
128 local_file_monitor_class->start = g_inotify_file_monitor_start;
132 g_inotify_file_monitor_init (GInotifyFileMonitor* monitor)
137 g_inotify_file_monitor_cancel (GFileMonitor* monitor)
139 GInotifyFileMonitor *inotify_monitor = G_INOTIFY_FILE_MONITOR (monitor);
140 inotify_sub *sub = inotify_monitor->sub;
144 _ih_sub_cancel (sub);
146 inotify_monitor->sub = NULL;
149 if (G_FILE_MONITOR_CLASS (g_inotify_file_monitor_parent_class)->cancel)
150 (*G_FILE_MONITOR_CLASS (g_inotify_file_monitor_parent_class)->cancel) (monitor);