Fix a failing testcase
[platform/upstream/glib.git] / gio / kqueue / gkqueuefilemonitor.c
1 /*******************************************************************************
2   Copyright (c) 2011, 2012 Dmitry Matveev <me@dmitrymatveev.co.uk>
3
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:
10
11   The above copyright notice and this permission notice shall be included in
12   all copies or substantial portions of the Software.
13
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 *******************************************************************************/
22
23 #include "config.h"
24
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>
31
32
33 struct _GKqueueFileMonitor
34 {
35   GLocalFileMonitor parent_instance;
36
37   kqueue_sub *sub;
38   
39   GFileMonitor *fallback;
40   GFile *fbfile;
41
42   gboolean pair_moves;
43 };
44
45 static gboolean g_kqueue_file_monitor_cancel (GFileMonitor* monitor);
46
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,
50                g_define_type_id,
51                "kqueue",
52                20))
53
54
55 static void
56 _fallback_callback (GFileMonitor      *unused,
57                     GFile             *first,
58                     GFile             *second,
59                     GFileMonitorEvent  event,
60                     gpointer           udata)
61 {
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);
66   (void) unused;
67
68   if (event == G_FILE_MONITOR_EVENT_CHANGED)
69   {
70     _kh_dir_diff (kq_mon->sub, mon);
71   }
72   else
73     g_file_monitor_emit_event (mon, first, second, event);
74 }
75
76
77 static void
78 g_kqueue_file_monitor_finalize (GObject *object)
79 {
80   GKqueueFileMonitor *kqueue_monitor = G_KQUEUE_FILE_MONITOR (object);
81
82   if (kqueue_monitor->sub)
83     {
84       _kh_cancel_sub (kqueue_monitor->sub);
85       _kh_sub_free (kqueue_monitor->sub);
86       kqueue_monitor->sub = NULL;
87     }
88
89   if (kqueue_monitor->fallback)
90     g_object_unref (kqueue_monitor->fallback);
91
92   if (kqueue_monitor->fbfile)
93     g_object_unref (kqueue_monitor->fbfile);
94
95   if (G_OBJECT_CLASS (g_kqueue_file_monitor_parent_class)->finalize)
96     (*G_OBJECT_CLASS (g_kqueue_file_monitor_parent_class)->finalize) (object);
97 }
98
99 static GObject*
100 g_kqueue_file_monitor_constructor (GType                 type,
101                                    guint                 n_construct_properties,
102                                    GObjectConstructParam *construct_properties)
103 {
104   GObject *obj;
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; 
111
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);
117
118   kqueue_monitor = G_KQUEUE_FILE_MONITOR (obj);
119
120   ret_kh_startup = _kh_startup ();
121   g_assert (ret_kh_startup);
122
123   kqueue_monitor->pair_moves = G_LOCAL_FILE_MONITOR (obj)->flags & G_FILE_MONITOR_SEND_MOVED
124                                ? TRUE : FALSE;
125
126   kqueue_monitor->sub = NULL;
127   kqueue_monitor->fallback = NULL;
128   kqueue_monitor->fbfile = NULL;
129
130   path = G_LOCAL_FILE_MONITOR (obj)->filename;
131
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. */
139
140   sub = _kh_sub_new (path,
141                      kqueue_monitor->pair_moves,
142                      kqueue_monitor);
143
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;
149
150   if (!_ke_is_excluded (path))
151     _kh_add_sub (sub);
152   else
153     {
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,
158                         "changed",
159                         G_CALLBACK (_fallback_callback),
160                         kqueue_monitor);
161     }
162
163   return obj;
164 }
165
166 static gboolean
167 g_kqueue_file_monitor_is_supported (void)
168 {
169   return _kh_startup ();
170 }
171
172 static void
173 g_kqueue_file_monitor_class_init (GKqueueFileMonitorClass *klass)
174 {
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);
178
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;
182
183   local_file_monitor_class->is_supported = g_kqueue_file_monitor_is_supported;
184 }
185
186 static void
187 g_kqueue_file_monitor_init (GKqueueFileMonitor *monitor)
188 {
189 }
190
191 static gboolean
192 g_kqueue_file_monitor_cancel (GFileMonitor *monitor)
193 {
194   GKqueueFileMonitor *kqueue_monitor = G_KQUEUE_FILE_MONITOR (monitor);
195
196   if (kqueue_monitor->sub)
197     {
198       _kh_cancel_sub (kqueue_monitor->sub);
199       _kh_sub_free (kqueue_monitor->sub);
200       kqueue_monitor->sub = NULL;
201     }
202   else if (kqueue_monitor->fallback)
203     g_file_monitor_cancel (kqueue_monitor->fallback);
204
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);
207
208   return TRUE;
209 }