1 /*******************************************************************************
2 Copyright (c) 2011, 2012 Dmitry Matveev <me@dmitrymatveev.co.uk>
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 *******************************************************************************/
25 #include "gkqueuefilemonitor.h"
26 #include "kqueue-helper.h"
27 #include "kqueue-exclusions.h"
28 #include <gio/gpollfilemonitor.h>
29 #include <gio/gfile.h>
30 #include <gio/giomodule.h>
33 struct _GKqueueFileMonitor
35 GLocalFileMonitor parent_instance;
39 GFileMonitor *fallback;
45 static gboolean g_kqueue_file_monitor_cancel (GFileMonitor* monitor);
47 #define g_kqueue_file_monitor_get_type _g_kqueue_file_monitor_get_type
48 G_DEFINE_TYPE_WITH_CODE (GKqueueFileMonitor, g_kqueue_file_monitor, G_TYPE_LOCAL_FILE_MONITOR,
49 g_io_extension_point_implement (G_LOCAL_FILE_MONITOR_EXTENSION_POINT_NAME,
56 _fallback_callback (GFileMonitor *unused,
59 GFileMonitorEvent event,
62 GKqueueFileMonitor *kq_mon = G_KQUEUE_FILE_MONITOR (udata);
63 GFileMonitor *mon = G_FILE_MONITOR (kq_mon);
64 g_assert (kq_mon != NULL);
65 g_assert (mon != NULL);
68 if (event == G_FILE_MONITOR_EVENT_CHANGED)
70 _kh_dir_diff (kq_mon->sub, mon);
73 g_file_monitor_emit_event (mon, first, second, event);
78 g_kqueue_file_monitor_finalize (GObject *object)
80 GKqueueFileMonitor *kqueue_monitor = G_KQUEUE_FILE_MONITOR (object);
82 if (kqueue_monitor->sub)
84 _kh_cancel_sub (kqueue_monitor->sub);
85 _kh_sub_free (kqueue_monitor->sub);
86 kqueue_monitor->sub = NULL;
89 if (kqueue_monitor->fallback)
90 g_object_unref (kqueue_monitor->fallback);
92 if (kqueue_monitor->fbfile)
93 g_object_unref (kqueue_monitor->fbfile);
95 if (G_OBJECT_CLASS (g_kqueue_file_monitor_parent_class)->finalize)
96 (*G_OBJECT_CLASS (g_kqueue_file_monitor_parent_class)->finalize) (object);
100 g_kqueue_file_monitor_constructor (GType type,
101 guint n_construct_properties,
102 GObjectConstructParam *construct_properties)
105 GKqueueFileMonitorClass *klass;
106 GObjectClass *parent_class;
107 GKqueueFileMonitor *kqueue_monitor;
108 kqueue_sub *sub = NULL;
109 gboolean ret_kh_startup = FALSE;
110 const gchar *path = NULL;
112 klass = G_KQUEUE_FILE_MONITOR_CLASS (g_type_class_peek (G_TYPE_KQUEUE_FILE_MONITOR));
113 parent_class = G_OBJECT_CLASS (g_type_class_peek_parent (klass));
114 obj = parent_class->constructor (type,
115 n_construct_properties,
116 construct_properties);
118 kqueue_monitor = G_KQUEUE_FILE_MONITOR (obj);
120 ret_kh_startup = _kh_startup ();
121 g_assert (ret_kh_startup);
123 kqueue_monitor->pair_moves = G_LOCAL_FILE_MONITOR (obj)->flags & G_FILE_MONITOR_SEND_MOVED
126 kqueue_monitor->sub = NULL;
127 kqueue_monitor->fallback = NULL;
128 kqueue_monitor->fbfile = NULL;
130 path = G_LOCAL_FILE_MONITOR (obj)->filename;
132 /* For a directory monitor, create a subscription object anyway.
133 * It will be used for directory diff calculation routines.
134 * Wait, directory diff in a GKqueueFileMonitor?
135 * Yes, it is. When a file monitor is started on an non-existent
136 * file, GIO uses a GKqueueFileMonitor object for that. If a directory
137 * will be created under that path, GKqueueFileMonitor will have to
138 * handle the directory notifications. */
140 sub = _kh_sub_new (path,
141 kqueue_monitor->pair_moves,
144 /* FIXME: what to do about errors here? we can't return NULL or another
145 * kind of error and an assertion is probably too hard (same issue as in
146 * the inotify backend) */
147 g_assert (sub != NULL);
148 kqueue_monitor->sub = sub;
150 if (!_ke_is_excluded (path))
154 GFile *file = g_file_new_for_path (path);
155 kqueue_monitor->fbfile = file;
156 kqueue_monitor->fallback = _g_poll_file_monitor_new (file);
157 g_signal_connect (kqueue_monitor->fallback,
159 G_CALLBACK (_fallback_callback),
167 g_kqueue_file_monitor_is_supported (void)
169 return _kh_startup ();
173 g_kqueue_file_monitor_class_init (GKqueueFileMonitorClass *klass)
175 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
176 GFileMonitorClass *file_monitor_class = G_FILE_MONITOR_CLASS (klass);
177 GLocalFileMonitorClass *local_file_monitor_class = G_LOCAL_FILE_MONITOR_CLASS (klass);
179 gobject_class->finalize = g_kqueue_file_monitor_finalize;
180 gobject_class->constructor = g_kqueue_file_monitor_constructor;
181 file_monitor_class->cancel = g_kqueue_file_monitor_cancel;
183 local_file_monitor_class->is_supported = g_kqueue_file_monitor_is_supported;
187 g_kqueue_file_monitor_init (GKqueueFileMonitor *monitor)
192 g_kqueue_file_monitor_cancel (GFileMonitor *monitor)
194 GKqueueFileMonitor *kqueue_monitor = G_KQUEUE_FILE_MONITOR (monitor);
196 if (kqueue_monitor->sub)
198 _kh_cancel_sub (kqueue_monitor->sub);
199 _kh_sub_free (kqueue_monitor->sub);
200 kqueue_monitor->sub = NULL;
202 else if (kqueue_monitor->fallback)
203 g_file_monitor_cancel (kqueue_monitor->fallback);
205 if (G_FILE_MONITOR_CLASS (g_kqueue_file_monitor_parent_class)->cancel)
206 (*G_FILE_MONITOR_CLASS (g_kqueue_file_monitor_parent_class)->cancel) (monitor);