Add myself to the DOAP for GLib
[glib.git] / gio / kqueue / kqueue-helper.c
blobe7d583c8bd623d6cef5c8c8408c1f5316aa4dced
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"
24 #include <sys/types.h>
25 #include <sys/event.h>
26 #include <sys/time.h>
27 #include <sys/socket.h>
28 #include <gio/glocalfile.h>
29 #include <gio/glocalfilemonitor.h>
30 #include <gio/gfile.h>
31 #include <fcntl.h>
32 #include <unistd.h>
33 #include <string.h>
34 #include <errno.h>
35 #include <pthread.h>
36 #include "kqueue-helper.h"
37 #include "kqueue-utils.h"
38 #include "kqueue-thread.h"
39 #include "kqueue-missing.h"
40 #include "kqueue-exclusions.h"
42 static gboolean kh_debug_enabled = FALSE;
43 #define KH_W if (kh_debug_enabled) g_warning
45 static GHashTable *subs_hash_table = NULL;
46 G_LOCK_DEFINE_STATIC (hash_lock);
48 static int kqueue_descriptor = -1;
49 static int kqueue_socket_pair[] = {-1, -1};
50 static pthread_t kqueue_thread;
53 void _kh_file_appeared_cb (kqueue_sub *sub);
55 /**
56 * accessor function for kqueue_descriptor
57 **/
58 int
59 get_kqueue_descriptor()
61 return kqueue_descriptor;
64 /**
65 * convert_kqueue_events_to_gio:
66 * @flags: a set of kqueue filter flags
67 * @done: a pointer to #gboolean indicating that the
68 * conversion has been done (out)
70 * Translates kqueue filter flags into GIO event flags.
72 * Returns: a #GFileMonitorEvent
73 **/
74 static GFileMonitorEvent
75 convert_kqueue_events_to_gio (uint32_t flags, gboolean *done)
77 g_assert (done != NULL);
78 *done = FALSE;
80 /* TODO: The following notifications should be emulated, if possible:
81 * - G_FILE_MONITOR_EVENT_PRE_UNMOUNT
83 if (flags & NOTE_DELETE)
85 *done = TRUE;
86 return G_FILE_MONITOR_EVENT_DELETED;
88 if (flags & NOTE_ATTRIB)
90 *done = TRUE;
91 return G_FILE_MONITOR_EVENT_ATTRIBUTE_CHANGED;
93 if (flags & (NOTE_WRITE | NOTE_EXTEND))
95 *done = TRUE;
96 return G_FILE_MONITOR_EVENT_CHANGED;
98 if (flags & NOTE_RENAME)
100 /* Since there’s apparently no way to get the new name of the file out of
101 * kqueue(), all we can do is say that this one has been deleted. */
102 *done = TRUE;
103 return G_FILE_MONITOR_EVENT_DELETED;
105 if (flags & NOTE_REVOKE)
107 *done = TRUE;
108 return G_FILE_MONITOR_EVENT_UNMOUNTED;
111 /* done is FALSE */
112 return 0;
115 typedef struct {
116 kqueue_sub *sub;
117 GFileMonitorSource *source;
118 } handle_ctx;
121 * handle_created:
122 * @udata: a pointer to user data (#handle_context).
123 * @path: file name of a new file.
124 * @inode: inode number of a new file.
126 * A callback function for the directory diff calculation routine,
127 * produces G_FILE_MONITOR_EVENT_CREATED event for a created file.
129 static void
130 handle_created (void *udata, const char *path, ino_t inode)
132 handle_ctx *ctx = NULL;
134 (void) inode;
135 ctx = (handle_ctx *) udata;
136 g_assert (udata != NULL);
137 g_assert (ctx->sub != NULL);
138 g_assert (ctx->source != NULL);
140 g_file_monitor_source_handle_event (ctx->source, G_FILE_MONITOR_EVENT_CREATED, path,
141 NULL, NULL, g_get_monotonic_time ());
145 * handle_deleted:
146 * @udata: a pointer to user data (#handle_context).
147 * @path: file name of the removed file.
148 * @inode: inode number of the removed file.
150 * A callback function for the directory diff calculation routine,
151 * produces G_FILE_MONITOR_EVENT_DELETED event for a deleted file.
153 static void
154 handle_deleted (void *udata, const char *path, ino_t inode)
156 handle_ctx *ctx = NULL;
158 (void) inode;
159 ctx = (handle_ctx *) udata;
160 g_assert (udata != NULL);
161 g_assert (ctx->sub != NULL);
162 g_assert (ctx->source != NULL);
164 g_file_monitor_source_handle_event (ctx->source, G_FILE_MONITOR_EVENT_DELETED, path,
165 NULL, NULL, g_get_monotonic_time ());
169 * handle_moved:
170 * @udata: a pointer to user data (#handle_context).
171 * @from_path: file name of the source file.
172 * @from_inode: inode number of the source file.
173 * @to_path: file name of the replaced file.
174 * @to_inode: inode number of the replaced file.
176 * A callback function for the directory diff calculation routine,
177 * produces G_FILE_MONITOR_EVENT_RENAMED event on a move.
179 static void
180 handle_moved (void *udata,
181 const char *from_path,
182 ino_t from_inode,
183 const char *to_path,
184 ino_t to_inode)
186 handle_ctx *ctx = NULL;
188 (void) from_inode;
189 (void) to_inode;
191 ctx = (handle_ctx *) udata;
192 g_assert (udata != NULL);
193 g_assert (ctx->sub != NULL);
194 g_assert (ctx->source != NULL);
196 g_file_monitor_source_handle_event (ctx->source, G_FILE_MONITOR_EVENT_RENAMED,
197 from_path, to_path, NULL, g_get_monotonic_time ());
201 * handle_overwritten:
202 * @data: a pointer to user data (#handle_context).
203 * @path: file name of the overwritten file.
204 * @node: inode number of the overwritten file.
206 * A callback function for the directory diff calculation routine,
207 * produces G_FILE_MONITOR_EVENT_DELETED/CREATED event pair when
208 * an overwrite occurs in the directory (see dep-list for details).
210 static void
211 handle_overwritten (void *udata, const char *path, ino_t inode)
213 handle_ctx *ctx = NULL;
215 (void) inode;
216 ctx = (handle_ctx *) udata;
217 g_assert (udata != NULL);
218 g_assert (ctx->sub != NULL);
219 g_assert (ctx->source != NULL);
221 g_file_monitor_source_handle_event (ctx->source, G_FILE_MONITOR_EVENT_DELETED,
222 path, NULL, NULL, g_get_monotonic_time ());
224 g_file_monitor_source_handle_event (ctx->source, G_FILE_MONITOR_EVENT_CREATED,
225 path, NULL, NULL, g_get_monotonic_time ());
228 static const traverse_cbs cbs = {
229 handle_created,
230 handle_deleted,
231 handle_moved,
232 handle_overwritten,
233 handle_moved,
234 NULL, /* many added */
235 NULL, /* many removed */
236 NULL, /* names updated */
240 void
241 _kh_dir_diff (kqueue_sub *sub, GFileMonitorSource *source)
243 dep_list *was;
244 handle_ctx ctx;
246 g_assert (sub != NULL);
247 g_assert (source != NULL);
249 memset (&ctx, 0, sizeof (handle_ctx));
250 ctx.sub = sub;
251 ctx.source = source;
253 was = sub->deps;
254 sub->deps = dl_listing (sub->filename);
256 dl_calculate (was, sub->deps, &cbs, &ctx);
258 dl_free (was);
263 * process_kqueue_notifications:
264 * @gioc: unused.
265 * @cond: unused.
266 * @data: unused.
268 * Processes notifications, coming from the kqueue thread.
270 * Reads notifications from the command file descriptor, emits the
271 * "changed" event on the appropriate monitor.
273 * A typical GIO Channel callback function.
275 * Returns: %TRUE
277 static gboolean
278 process_kqueue_notifications (GIOChannel *gioc,
279 GIOCondition cond,
280 gpointer data)
282 struct kqueue_notification n;
283 kqueue_sub *sub = NULL;
284 GFileMonitorSource *source = NULL;
285 GFileMonitorEvent mask = 0;
287 g_assert (kqueue_socket_pair[0] != -1);
288 if (!_ku_read (kqueue_socket_pair[0], &n, sizeof (struct kqueue_notification)))
290 KH_W ("Failed to read a kqueue notification, error %d", errno);
291 return TRUE;
294 G_LOCK (hash_lock);
295 sub = (kqueue_sub *) g_hash_table_lookup (subs_hash_table, GINT_TO_POINTER (n.fd));
296 G_UNLOCK (hash_lock);
298 if (sub == NULL)
300 KH_W ("Got a notification for a deleted or non-existing subscription %d",
301 n.fd);
302 return TRUE;
305 source = sub->user_data;
306 g_assert (source != NULL);
308 if (n.flags & (NOTE_DELETE | NOTE_REVOKE))
310 if (sub->deps)
312 dl_free (sub->deps);
313 sub->deps = NULL;
315 _km_add_missing (sub);
317 if (!(n.flags & NOTE_REVOKE))
319 /* Note that NOTE_REVOKE is issued by the kqueue thread
320 * on EV_ERROR kevent. In this case, a file descriptor is
321 * already closed from the kqueue thread, no need to close
322 * it manually */
323 _kh_cancel_sub (sub);
327 if (sub->is_dir && n.flags & (NOTE_WRITE | NOTE_EXTEND))
329 _kh_dir_diff (sub, source);
330 n.flags &= ~(NOTE_WRITE | NOTE_EXTEND);
333 if (n.flags)
335 gboolean done = FALSE;
336 mask = convert_kqueue_events_to_gio (n.flags, &done);
337 if (done == TRUE)
338 g_file_monitor_source_handle_event (source, mask, NULL, NULL, NULL, g_get_monotonic_time ());
341 return TRUE;
346 * _kh_startup_impl:
347 * @unused: unused
349 * Kqueue backend startup code. Should be called only once.
351 * Returns: %TRUE on success, %FALSE otherwise.
353 static gpointer
354 _kh_startup_impl (gpointer unused)
356 GIOChannel *channel = NULL;
357 gboolean result = FALSE;
359 kqueue_descriptor = kqueue ();
360 result = (kqueue_descriptor != -1);
361 if (!result)
363 KH_W ("Failed to initialize kqueue\n!");
364 return GINT_TO_POINTER (FALSE);
367 result = socketpair (AF_UNIX, SOCK_STREAM, 0, kqueue_socket_pair);
368 if (result != 0)
370 KH_W ("Failed to create socket pair\n!");
371 return GINT_TO_POINTER (FALSE) ;
374 result = pthread_create (&kqueue_thread,
375 NULL,
376 _kqueue_thread_func,
377 &kqueue_socket_pair[1]);
378 if (result != 0)
380 KH_W ("Failed to run kqueue thread\n!");
381 return GINT_TO_POINTER (FALSE);
384 _km_init (_kh_file_appeared_cb);
386 channel = g_io_channel_unix_new (kqueue_socket_pair[0]);
387 g_io_add_watch (channel, G_IO_IN, process_kqueue_notifications, NULL);
389 subs_hash_table = g_hash_table_new (g_direct_hash, g_direct_equal);
391 KH_W ("started gio kqueue backend\n");
392 return GINT_TO_POINTER (TRUE);
397 * _kh_startup:
398 * Kqueue backend initialization.
400 * Returns: %TRUE on success, %FALSE otherwise.
402 gboolean
403 _kh_startup (void)
405 static GOnce init_once = G_ONCE_INIT;
406 g_once (&init_once, _kh_startup_impl, NULL);
407 return GPOINTER_TO_INT (init_once.retval);
412 * _kh_start_watching:
413 * @sub: a #kqueue_sub
415 * Starts watching on a subscription.
417 * Returns: %TRUE on success, %FALSE otherwise.
419 gboolean
420 _kh_start_watching (kqueue_sub *sub)
422 g_assert (kqueue_socket_pair[0] != -1);
423 g_assert (sub != NULL);
424 g_assert (sub->filename != NULL);
426 /* kqueue requires a file descriptor to monitor. Sad but true */
427 #if defined (O_EVTONLY)
428 sub->fd = open (sub->filename, O_EVTONLY);
429 #else
430 sub->fd = open (sub->filename, O_RDONLY);
431 #endif
433 if (sub->fd == -1)
435 KH_W ("failed to open file %s (error %d)", sub->filename, errno);
436 return FALSE;
439 _ku_file_information (sub->fd, &sub->is_dir, NULL);
440 if (sub->is_dir)
442 /* I know, it is very bad to make such decisions in this way and here.
443 * We already do have an user_data at the #kqueue_sub, and it may point to
444 * GKqueueFileMonitor or GKqueueDirectoryMonitor. For a directory case,
445 * we need to scan in contents for the further diffs. Ideally this process
446 * should be delegated to the GKqueueDirectoryMonitor, but for now I will
447 * do it in a dirty way right here. */
448 if (sub->deps)
449 dl_free (sub->deps);
451 sub->deps = dl_listing (sub->filename);
454 G_LOCK (hash_lock);
455 g_hash_table_insert (subs_hash_table, GINT_TO_POINTER (sub->fd), sub);
456 G_UNLOCK (hash_lock);
458 _kqueue_thread_push_fd (sub->fd);
460 /* Bump the kqueue thread. It will pick up a new sub entry to monitor */
461 if (!_ku_write (kqueue_socket_pair[0], "A", 1))
462 KH_W ("Failed to bump the kqueue thread (add fd, error %d)", errno);
463 return TRUE;
468 * _kh_add_sub:
469 * @sub: a #kqueue_sub
471 * Adds a subscription for monitoring.
473 * This funciton tries to start watching a subscription with
474 * _kh_start_watching(). On failure, i.e. when a file does not exist yet,
475 * the subscription will be added to a list of missing files to continue
476 * watching when the file will appear.
478 * Returns: %TRUE
480 gboolean
481 _kh_add_sub (kqueue_sub *sub)
483 g_assert (sub != NULL);
485 if (!_kh_start_watching (sub))
486 _km_add_missing (sub);
488 return TRUE;
493 * _kh_cancel_sub:
494 * @sub a #kqueue_sub
496 * Stops monitoring on a subscription.
498 * Returns: %TRUE
500 gboolean
501 _kh_cancel_sub (kqueue_sub *sub)
503 gboolean removed = FALSE;
504 g_assert (kqueue_socket_pair[0] != -1);
505 g_assert (sub != NULL);
507 _km_remove (sub);
509 G_LOCK (hash_lock);
510 removed = g_hash_table_remove (subs_hash_table, GINT_TO_POINTER (sub->fd));
511 G_UNLOCK (hash_lock);
513 if (removed)
515 /* fd will be closed in the kqueue thread */
516 _kqueue_thread_remove_fd (sub->fd);
518 /* Bump the kqueue thread. It will pick up a new sub entry to remove*/
519 if (!_ku_write (kqueue_socket_pair[0], "R", 1))
520 KH_W ("Failed to bump the kqueue thread (remove fd, error %d)", errno);
523 return TRUE;
528 * _kh_file_appeared_cb:
529 * @sub: a #kqueue_sub
531 * A callback function for kqueue-missing subsystem.
533 * Signals that a missing file has finally appeared in the filesystem.
534 * Emits %G_FILE_MONITOR_EVENT_CREATED.
536 void
537 _kh_file_appeared_cb (kqueue_sub *sub)
539 GFile* child;
541 g_assert (sub != NULL);
542 g_assert (sub->filename);
544 if (!g_file_test (sub->filename, G_FILE_TEST_EXISTS))
545 return;
547 child = g_file_new_for_path (sub->filename);
549 g_file_monitor_emit_event (G_FILE_MONITOR (sub->user_data),
550 child,
551 NULL,
552 G_FILE_MONITOR_EVENT_CREATED);
554 g_object_unref (child);