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 *******************************************************************************/
24 #include <sys/types.h>
25 #include <sys/event.h>
27 #include <sys/socket.h>
28 #include <gio/glocalfile.h>
29 #include <gio/glocalfilemonitor.h>
30 #include <gio/gfile.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
);
56 * accessor function for kqueue_descriptor
59 get_kqueue_descriptor()
61 return kqueue_descriptor
;
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
74 static GFileMonitorEvent
75 convert_kqueue_events_to_gio (uint32_t flags
, gboolean
*done
)
77 g_assert (done
!= NULL
);
80 /* TODO: The following notifications should be emulated, if possible:
81 * - G_FILE_MONITOR_EVENT_PRE_UNMOUNT
83 if (flags
& NOTE_DELETE
)
86 return G_FILE_MONITOR_EVENT_DELETED
;
88 if (flags
& NOTE_ATTRIB
)
91 return G_FILE_MONITOR_EVENT_ATTRIBUTE_CHANGED
;
93 if (flags
& (NOTE_WRITE
| NOTE_EXTEND
))
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. */
103 return G_FILE_MONITOR_EVENT_DELETED
;
105 if (flags
& NOTE_REVOKE
)
108 return G_FILE_MONITOR_EVENT_UNMOUNTED
;
117 GFileMonitorSource
*source
;
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.
130 handle_created (void *udata
, const char *path
, ino_t inode
)
132 handle_ctx
*ctx
= NULL
;
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 ());
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.
154 handle_deleted (void *udata
, const char *path
, ino_t inode
)
156 handle_ctx
*ctx
= NULL
;
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 ());
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.
180 handle_moved (void *udata
,
181 const char *from_path
,
186 handle_ctx
*ctx
= NULL
;
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).
211 handle_overwritten (void *udata
, const char *path
, ino_t inode
)
213 handle_ctx
*ctx
= NULL
;
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
= {
234 NULL
, /* many added */
235 NULL
, /* many removed */
236 NULL
, /* names updated */
241 _kh_dir_diff (kqueue_sub
*sub
, GFileMonitorSource
*source
)
246 g_assert (sub
!= NULL
);
247 g_assert (source
!= NULL
);
249 memset (&ctx
, 0, sizeof (handle_ctx
));
254 sub
->deps
= dl_listing (sub
->filename
);
256 dl_calculate (was
, sub
->deps
, &cbs
, &ctx
);
263 * process_kqueue_notifications:
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.
278 process_kqueue_notifications (GIOChannel
*gioc
,
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
);
295 sub
= (kqueue_sub
*) g_hash_table_lookup (subs_hash_table
, GINT_TO_POINTER (n
.fd
));
296 G_UNLOCK (hash_lock
);
300 KH_W ("Got a notification for a deleted or non-existing subscription %d",
305 source
= sub
->user_data
;
306 g_assert (source
!= NULL
);
308 if (n
.flags
& (NOTE_DELETE
| NOTE_REVOKE
))
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
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
);
335 gboolean done
= FALSE
;
336 mask
= convert_kqueue_events_to_gio (n
.flags
, &done
);
338 g_file_monitor_source_handle_event (source
, mask
, NULL
, NULL
, NULL
, g_get_monotonic_time ());
349 * Kqueue backend startup code. Should be called only once.
351 * Returns: %TRUE on success, %FALSE otherwise.
354 _kh_startup_impl (gpointer unused
)
356 GIOChannel
*channel
= NULL
;
357 gboolean result
= FALSE
;
359 kqueue_descriptor
= kqueue ();
360 result
= (kqueue_descriptor
!= -1);
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
);
370 KH_W ("Failed to create socket pair\n!");
371 return GINT_TO_POINTER (FALSE
) ;
374 result
= pthread_create (&kqueue_thread
,
377 &kqueue_socket_pair
[1]);
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
);
398 * Kqueue backend initialization.
400 * Returns: %TRUE on success, %FALSE otherwise.
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.
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
);
430 sub
->fd
= open (sub
->filename
, O_RDONLY
);
435 KH_W ("failed to open file %s (error %d)", sub
->filename
, errno
);
439 _ku_file_information (sub
->fd
, &sub
->is_dir
, NULL
);
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. */
451 sub
->deps
= dl_listing (sub
->filename
);
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
);
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.
481 _kh_add_sub (kqueue_sub
*sub
)
483 g_assert (sub
!= NULL
);
485 if (!_kh_start_watching (sub
))
486 _km_add_missing (sub
);
496 * Stops monitoring on a subscription.
501 _kh_cancel_sub (kqueue_sub
*sub
)
503 gboolean removed
= FALSE
;
504 g_assert (kqueue_socket_pair
[0] != -1);
505 g_assert (sub
!= NULL
);
510 removed
= g_hash_table_remove (subs_hash_table
, GINT_TO_POINTER (sub
->fd
));
511 G_UNLOCK (hash_lock
);
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
);
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.
537 _kh_file_appeared_cb (kqueue_sub
*sub
)
541 g_assert (sub
!= NULL
);
542 g_assert (sub
->filename
);
544 if (!g_file_test (sub
->filename
, G_FILE_TEST_EXISTS
))
547 child
= g_file_new_for_path (sub
->filename
);
549 g_file_monitor_emit_event (G_FILE_MONITOR (sub
->user_data
),
552 G_FILE_MONITOR_EVENT_CREATED
);
554 g_object_unref (child
);