[kdbus] KDBUS_ITEM_PAYLOAD_OFF items are (once again) relative to msg header
[platform/upstream/glib.git] / gio / kqueue / gkqueuedirectorymonitor.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 "gkqueuedirectorymonitor.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 _GKqueueDirectoryMonitor
34 {
35   GLocalDirectoryMonitor parent_instance;
36   kqueue_sub *sub;
37
38   GFileMonitor *fallback;
39   GFile *fbfile;
40  
41   gboolean pair_moves;
42 };
43
44 static gboolean g_kqueue_directory_monitor_cancel (GFileMonitor *monitor);
45
46 #define g_kqueue_directory_monitor_get_type _g_kqueue_directory_monitor_get_type
47 G_DEFINE_TYPE_WITH_CODE (GKqueueDirectoryMonitor, g_kqueue_directory_monitor, G_TYPE_LOCAL_DIRECTORY_MONITOR,
48        g_io_extension_point_implement (G_LOCAL_DIRECTORY_MONITOR_EXTENSION_POINT_NAME,
49                g_define_type_id,
50                "kqueue",
51                20))
52
53
54 static void
55 _fallback_callback (GFileMonitor      *unused,
56                     GFile             *first,
57                     GFile             *second,
58                     GFileMonitorEvent  event,
59                     gpointer           udata)
60 {
61   GKqueueDirectoryMonitor *kq_mon = G_KQUEUE_DIRECTORY_MONITOR (udata); 
62   GFileMonitor *mon = G_FILE_MONITOR (kq_mon);
63   g_assert (kq_mon != NULL);
64   g_assert (mon != NULL);
65   (void) unused;
66
67   if (event == G_FILE_MONITOR_EVENT_CHANGED)
68   {
69     _kh_dir_diff (kq_mon->sub, mon);
70   }
71   else
72     g_file_monitor_emit_event (mon, first, second, event);
73 }
74
75
76 static void
77 g_kqueue_directory_monitor_finalize (GObject *object)
78 {
79   GKqueueDirectoryMonitor *kqueue_monitor = G_KQUEUE_DIRECTORY_MONITOR (object);
80   
81   if (kqueue_monitor->sub)
82     {
83       _kh_cancel_sub (kqueue_monitor->sub);
84       _kh_sub_free (kqueue_monitor->sub);
85       kqueue_monitor->sub = NULL;
86     }
87
88   if (kqueue_monitor->fallback)
89     g_object_unref (kqueue_monitor->fallback);
90
91   if (kqueue_monitor->fbfile)
92     g_object_unref (kqueue_monitor->fbfile);
93
94   if (G_OBJECT_CLASS (g_kqueue_directory_monitor_parent_class)->finalize)
95     (*G_OBJECT_CLASS (g_kqueue_directory_monitor_parent_class)->finalize) (object);
96 }
97
98 static GObject*
99 g_kqueue_directory_monitor_constructor (GType                 type,
100                                         guint                 n_construct_properties,
101                                         GObjectConstructParam *construct_properties)
102 {
103   GObject *obj;
104   GKqueueDirectoryMonitorClass *klass;
105   GObjectClass *parent_class;
106   GKqueueDirectoryMonitor *kqueue_monitor;
107   kqueue_sub *sub = NULL;
108   gboolean ret_kh_startup;
109   const gchar *path = NULL;
110
111   klass = G_KQUEUE_DIRECTORY_MONITOR_CLASS (g_type_class_peek (G_TYPE_KQUEUE_DIRECTORY_MONITOR));
112   parent_class = G_OBJECT_CLASS (g_type_class_peek_parent (klass));
113   obj = parent_class->constructor (type,
114                                    n_construct_properties,
115                                    construct_properties);
116
117   kqueue_monitor = G_KQUEUE_DIRECTORY_MONITOR (obj);
118
119   ret_kh_startup = _kh_startup ();
120   g_assert (ret_kh_startup);
121
122   kqueue_monitor->pair_moves = (G_LOCAL_DIRECTORY_MONITOR (obj)->flags & G_FILE_MONITOR_SEND_MOVED)
123                                ? TRUE : FALSE;
124
125   kqueue_monitor->sub = NULL;
126   kqueue_monitor->fallback = NULL;
127   kqueue_monitor->fbfile = NULL;
128
129   path = G_LOCAL_DIRECTORY_MONITOR (obj)->dirname;
130
131   /* For a directory monitor, create a subscription object anyway.
132    * It will be used for directory diff calculation routines. */
133
134   sub = _kh_sub_new (path,
135                      kqueue_monitor->pair_moves,
136                      kqueue_monitor);
137
138   /* FIXME: what to do about errors here? we can't return NULL or another
139    * kind of error and an assertion is probably too hard (same issue as in
140    * the inotify backend) */
141   g_assert (sub != NULL);
142   kqueue_monitor->sub = sub;
143
144   if (!_ke_is_excluded (path))
145     _kh_add_sub (sub);
146   else
147     {
148       GFile *file = g_file_new_for_path (path);
149       kqueue_monitor->fbfile = file;
150       kqueue_monitor->fallback = _g_poll_file_monitor_new (file);
151       g_signal_connect (kqueue_monitor->fallback,
152                         "changed",
153                         G_CALLBACK (_fallback_callback),
154                         kqueue_monitor);
155     }
156
157   return obj;
158 }
159
160 static gboolean
161 g_kqueue_directory_monitor_is_supported (void)
162 {
163   return _kh_startup ();
164 }
165
166 static void
167 g_kqueue_directory_monitor_class_init (GKqueueDirectoryMonitorClass *klass)
168 {
169   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
170   GFileMonitorClass *directory_monitor_class = G_FILE_MONITOR_CLASS (klass);
171   GLocalDirectoryMonitorClass *local_directory_monitor_class = G_LOCAL_DIRECTORY_MONITOR_CLASS (klass);
172
173   gobject_class->finalize = g_kqueue_directory_monitor_finalize;
174   gobject_class->constructor = g_kqueue_directory_monitor_constructor;
175   directory_monitor_class->cancel = g_kqueue_directory_monitor_cancel;
176
177   local_directory_monitor_class->mount_notify = TRUE; /* TODO: ??? */
178   local_directory_monitor_class->is_supported = g_kqueue_directory_monitor_is_supported;
179 }
180
181 static void
182 g_kqueue_directory_monitor_init (GKqueueDirectoryMonitor *monitor)
183 {
184 }
185
186 static gboolean
187 g_kqueue_directory_monitor_cancel (GFileMonitor *monitor)
188 {
189   GKqueueDirectoryMonitor *kqueue_monitor = G_KQUEUE_DIRECTORY_MONITOR (monitor);
190
191   if (kqueue_monitor->sub)
192     {
193       _kh_cancel_sub (kqueue_monitor->sub);
194       _kh_sub_free (kqueue_monitor->sub);
195       kqueue_monitor->sub = NULL;
196     }
197   else if (kqueue_monitor->fallback)
198     g_file_monitor_cancel (kqueue_monitor->fallback);
199
200
201   if (G_FILE_MONITOR_CLASS (g_kqueue_directory_monitor_parent_class)->cancel)
202     (*G_FILE_MONITOR_CLASS (g_kqueue_directory_monitor_parent_class)->cancel) (monitor);
203
204   return TRUE;
205 }