Add some more cases to the app-id unit tests
[glib.git] / gio / kqueue / gkqueuefilemonitor.c
blob78b749637d631e9c3a2e646c626d0555c0126ba2
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
20 THE SOFTWARE.
21 *******************************************************************************/
23 #include "config.h"
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;
37 kqueue_sub *sub;
39 GFileMonitor *fallback;
40 GFile *fbfile;
43 static gboolean g_kqueue_file_monitor_cancel (GFileMonitor* monitor);
45 G_DEFINE_TYPE_WITH_CODE (GKqueueFileMonitor, g_kqueue_file_monitor, G_TYPE_LOCAL_FILE_MONITOR,
46 g_io_extension_point_implement (G_LOCAL_FILE_MONITOR_EXTENSION_POINT_NAME,
47 g_define_type_id,
48 "kqueue",
49 20))
52 static void
53 _fallback_callback (GFileMonitor *unused,
54 GFile *first,
55 GFile *second,
56 GFileMonitorEvent event,
57 gpointer udata)
59 GKqueueFileMonitor *kq_mon = G_KQUEUE_FILE_MONITOR (udata);
60 GFileMonitor *mon = G_FILE_MONITOR (kq_mon);
61 g_assert (kq_mon != NULL);
62 g_assert (mon != NULL);
63 (void) unused;
65 if (event == G_FILE_MONITOR_EVENT_CHANGED)
67 GLocalFileMonitor *local_monitor = G_LOCAL_FILE_MONITOR (kq_mon);
69 _kh_dir_diff (kq_mon->sub, local_monitor->source);
71 else
72 g_file_monitor_emit_event (mon, first, second, event);
76 static void
77 g_kqueue_file_monitor_finalize (GObject *object)
79 GKqueueFileMonitor *kqueue_monitor = G_KQUEUE_FILE_MONITOR (object);
81 if (kqueue_monitor->sub)
83 _kh_cancel_sub (kqueue_monitor->sub);
84 _kh_sub_free (kqueue_monitor->sub);
85 kqueue_monitor->sub = NULL;
88 if (kqueue_monitor->fallback)
89 g_object_unref (kqueue_monitor->fallback);
91 if (kqueue_monitor->fbfile)
92 g_object_unref (kqueue_monitor->fbfile);
94 if (G_OBJECT_CLASS (g_kqueue_file_monitor_parent_class)->finalize)
95 (*G_OBJECT_CLASS (g_kqueue_file_monitor_parent_class)->finalize) (object);
98 static void
99 g_kqueue_file_monitor_start (GLocalFileMonitor *local_monitor,
100 const gchar *dirname,
101 const gchar *basename,
102 const gchar *filename,
103 GFileMonitorSource *source)
105 GKqueueFileMonitor *kqueue_monitor = G_KQUEUE_FILE_MONITOR (local_monitor);
106 GObject *obj;
107 GKqueueFileMonitorClass *klass;
108 GObjectClass *parent_class;
109 kqueue_sub *sub = NULL;
110 gboolean ret_kh_startup = FALSE;
111 const gchar *path = NULL;
114 ret_kh_startup = _kh_startup ();
115 g_assert (ret_kh_startup);
117 path = filename;
118 if (!path)
119 path = dirname;
121 /* For a directory monitor, create a subscription object anyway.
122 * It will be used for directory diff calculation routines.
123 * Wait, directory diff in a GKqueueFileMonitor?
124 * Yes, it is. When a file monitor is started on an non-existent
125 * file, GIO uses a GKqueueFileMonitor object for that. If a directory
126 * will be created under that path, GKqueueFileMonitor will have to
127 * handle the directory notifications. */
129 sub = _kh_sub_new (path, TRUE, source);
131 /* FIXME: what to do about errors here? we can't return NULL or another
132 * kind of error and an assertion is probably too hard (same issue as in
133 * the inotify backend) */
134 g_assert (sub != NULL);
135 kqueue_monitor->sub = sub;
137 if (!_ke_is_excluded (path))
138 _kh_add_sub (sub);
139 else
141 GFile *file = g_file_new_for_path (path);
142 kqueue_monitor->fbfile = file;
143 kqueue_monitor->fallback = _g_poll_file_monitor_new (file);
144 g_signal_connect (kqueue_monitor->fallback,
145 "changed",
146 G_CALLBACK (_fallback_callback),
147 kqueue_monitor);
151 static gboolean
152 g_kqueue_file_monitor_is_supported (void)
154 return _kh_startup ();
157 static void
158 g_kqueue_file_monitor_class_init (GKqueueFileMonitorClass *klass)
160 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
161 GFileMonitorClass *file_monitor_class = G_FILE_MONITOR_CLASS (klass);
162 GLocalFileMonitorClass *local_file_monitor_class = G_LOCAL_FILE_MONITOR_CLASS (klass);
164 gobject_class->finalize = g_kqueue_file_monitor_finalize;
165 file_monitor_class->cancel = g_kqueue_file_monitor_cancel;
167 local_file_monitor_class->is_supported = g_kqueue_file_monitor_is_supported;
168 local_file_monitor_class->start = g_kqueue_file_monitor_start;
169 local_file_monitor_class->mount_notify = TRUE; /* TODO: ??? */
172 static void
173 g_kqueue_file_monitor_init (GKqueueFileMonitor *monitor)
177 static gboolean
178 g_kqueue_file_monitor_cancel (GFileMonitor *monitor)
180 GKqueueFileMonitor *kqueue_monitor = G_KQUEUE_FILE_MONITOR (monitor);
182 if (kqueue_monitor->sub)
184 _kh_cancel_sub (kqueue_monitor->sub);
185 _kh_sub_free (kqueue_monitor->sub);
186 kqueue_monitor->sub = NULL;
188 else if (kqueue_monitor->fallback)
190 g_signal_handlers_disconnect_by_func (kqueue_monitor->fallback, _fallback_callback, kqueue_monitor);
191 g_file_monitor_cancel (kqueue_monitor->fallback);
194 if (G_FILE_MONITOR_CLASS (g_kqueue_file_monitor_parent_class)->cancel)
195 (*G_FILE_MONITOR_CLASS (g_kqueue_file_monitor_parent_class)->cancel) (monitor);
197 return TRUE;