1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646
|
/*******************************************************************************
Copyright (c) 2011, 2012 Dmitry Matveev <me@dmitrymatveev.co.uk>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*******************************************************************************/
#include "config.h"
#include <sys/types.h>
#include <sys/event.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <gio/glocalfile.h>
#include <gio/gfile.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <pthread.h>
#include "kqueue-helper.h"
#include "kqueue-utils.h"
#include "kqueue-thread.h"
#include "kqueue-missing.h"
#include "kqueue-exclusions.h"
#include "gkqueuedirectorymonitor.h"
static gboolean kh_debug_enabled = FALSE;
#define KH_W if (kh_debug_enabled) g_warning
static GHashTable *subs_hash_table = NULL;
G_LOCK_DEFINE_STATIC (hash_lock);
static int kqueue_descriptor = -1;
static int kqueue_socket_pair[] = {-1, -1};
static pthread_t kqueue_thread;
void _kh_file_appeared_cb (kqueue_sub *sub);
/**
* accessor function for kqueue_descriptor
**/
int
get_kqueue_descriptor()
{
return kqueue_descriptor;
}
/**
* convert_kqueue_events_to_gio:
* @flags: a set of kqueue filter flags
* @done: a pointer to #gboolean indicating that the
* conversion has been done (out)
*
* Translates kqueue filter flags into GIO event flags.
*
* Returns: a #GFileMonitorEvent
**/
static GFileMonitorEvent
convert_kqueue_events_to_gio (uint32_t flags, gboolean *done)
{
g_assert (done != NULL);
*done = FALSE;
/* TODO: The following notifications should be emulated, if possible:
* - G_FILE_MONITOR_EVENT_PRE_UNMOUNT
*/
if (flags & NOTE_DELETE)
{
*done = TRUE;
return G_FILE_MONITOR_EVENT_DELETED;
}
if (flags & NOTE_ATTRIB)
{
*done = TRUE;
return G_FILE_MONITOR_EVENT_ATTRIBUTE_CHANGED;
}
if (flags & (NOTE_WRITE | NOTE_EXTEND))
{
*done = TRUE;
return G_FILE_MONITOR_EVENT_CHANGED;
}
if (flags & NOTE_RENAME)
{
*done = TRUE;
return G_FILE_MONITOR_EVENT_MOVED;
}
if (flags & NOTE_REVOKE)
{
*done = TRUE;
return G_FILE_MONITOR_EVENT_UNMOUNTED;
}
/* done is FALSE */
return 0;
}
typedef struct {
kqueue_sub *sub;
GFileMonitor *monitor;
} handle_ctx;
/**
* handle_created:
* @udata: a pointer to user data (#handle_context).
* @path: file name of a new file.
* @inode: inode number of a new file.
*
* A callback function for the directory diff calculation routine,
* produces G_FILE_MONITOR_EVENT_CREATED event for a created file.
**/
static void
handle_created (void *udata, const char *path, ino_t inode)
{
handle_ctx *ctx = NULL;
GFile *file = NULL;
gchar *fpath = NULL;
(void) inode;
ctx = (handle_ctx *) udata;
g_assert (udata != NULL);
g_assert (ctx->sub != NULL);
g_assert (ctx->monitor != NULL);
fpath = _ku_path_concat (ctx->sub->filename, path);
if (fpath == NULL)
{
KH_W ("Failed to allocate a string for a new event");
return;
}
file = g_file_new_for_path (fpath);
g_file_monitor_emit_event (ctx->monitor,
file,
NULL,
G_FILE_MONITOR_EVENT_CREATED);
g_free (fpath);
g_object_unref (file);
}
/**
* handle_deleted:
* @udata: a pointer to user data (#handle_context).
* @path: file name of the removed file.
* @inode: inode number of the removed file.
*
* A callback function for the directory diff calculation routine,
* produces G_FILE_MONITOR_EVENT_DELETED event for a deleted file.
**/
static void
handle_deleted (void *udata, const char *path, ino_t inode)
{
handle_ctx *ctx = NULL;
GFile *file = NULL;
gchar *fpath = NULL;
(void) inode;
ctx = (handle_ctx *) udata;
g_assert (udata != NULL);
g_assert (ctx->sub != NULL);
g_assert (ctx->monitor != NULL);
fpath = _ku_path_concat (ctx->sub->filename, path);
if (fpath == NULL)
{
KH_W ("Failed to allocate a string for a new event");
return;
}
file = g_file_new_for_path (fpath);
g_file_monitor_emit_event (ctx->monitor,
file,
NULL,
G_FILE_MONITOR_EVENT_DELETED);
g_free (fpath);
g_object_unref (file);
}
/**
* handle_moved:
* @udata: a pointer to user data (#handle_context).
* @from_path: file name of the source file.
* @from_inode: inode number of the source file.
* @to_path: file name of the replaced file.
* @to_inode: inode number of the replaced file.
*
* A callback function for the directory diff calculation routine,
* produces G_FILE_MONITOR_EVENT_MOVED event on a move.
**/
static void
handle_moved (void *udata,
const char *from_path,
ino_t from_inode,
const char *to_path,
ino_t to_inode)
{
handle_ctx *ctx = NULL;
GFile *file = NULL;
GFile *other = NULL;
gchar *path = NULL;
gchar *npath = NULL;
(void) from_inode;
(void) to_inode;
ctx = (handle_ctx *) udata;
g_assert (udata != NULL);
g_assert (ctx->sub != NULL);
g_assert (ctx->monitor != NULL);
path = _ku_path_concat (ctx->sub->filename, from_path);
npath = _ku_path_concat (ctx->sub->filename, to_path);
if (path == NULL || npath == NULL)
{
KH_W ("Failed to allocate strings for event");
return;
}
file = g_file_new_for_path (path);
other = g_file_new_for_path (npath);
if (ctx->sub->pair_moves)
{
g_file_monitor_emit_event (ctx->monitor,
file,
other,
G_FILE_MONITOR_EVENT_MOVED);
}
else
{
g_file_monitor_emit_event (ctx->monitor,
file,
NULL,
G_FILE_MONITOR_EVENT_DELETED);
g_file_monitor_emit_event (ctx->monitor,
other,
NULL,
G_FILE_MONITOR_EVENT_CREATED);
}
g_free (path);
g_free (npath);
g_object_unref (file);
g_object_unref (other);
}
/**
* handle_overwritten:
* @data: a pointer to user data (#handle_context).
* @path: file name of the overwritten file.
* @node: inode number of the overwritten file.
*
* A callback function for the directory diff calculation routine,
* produces G_FILE_MONITOR_EVENT_DELETED/CREATED event pair when
* an overwrite occurs in the directory (see dep-list for details).
**/
static void
handle_overwritten (void *udata, const char *path, ino_t inode)
{
handle_ctx *ctx = NULL;
GFile *file = NULL;
gchar *fpath = NULL;
(void) inode;
ctx = (handle_ctx *) udata;
g_assert (udata != NULL);
g_assert (ctx->sub != NULL);
g_assert (ctx->monitor != NULL);
fpath = _ku_path_concat (ctx->sub->filename, path);
if (fpath == NULL)
{
KH_W ("Failed to allocate a string for a new event");
return;
}
file = g_file_new_for_path (fpath);
g_file_monitor_emit_event (ctx->monitor,
file,
NULL,
G_FILE_MONITOR_EVENT_DELETED);
g_file_monitor_emit_event (ctx->monitor,
file,
NULL,
G_FILE_MONITOR_EVENT_CREATED);
g_free (fpath);
g_object_unref (file);
}
static const traverse_cbs cbs = {
handle_created,
handle_deleted,
handle_moved,
handle_overwritten,
handle_moved,
NULL, /* many added */
NULL, /* many removed */
NULL, /* names updated */
};
void
_kh_dir_diff (kqueue_sub *sub, GFileMonitor *monitor)
{
dep_list *was;
handle_ctx ctx;
g_assert (sub != NULL);
g_assert (monitor != NULL);
memset (&ctx, 0, sizeof (handle_ctx));
ctx.sub = sub;
ctx.monitor = monitor;
was = sub->deps;
sub->deps = dl_listing (sub->filename);
dl_calculate (was, sub->deps, &cbs, &ctx);
dl_free (was);
}
/**
* process_kqueue_notifications:
* @gioc: unused.
* @cond: unused.
* @data: unused.
*
* Processes notifications, coming from the kqueue thread.
*
* Reads notifications from the command file descriptor, emits the
* "changed" event on the appropriate monitor.
*
* A typical GIO Channel callback function.
*
* Returns: %TRUE
**/
static gboolean
process_kqueue_notifications (GIOChannel *gioc,
GIOCondition cond,
gpointer data)
{
struct kqueue_notification n;
kqueue_sub *sub = NULL;
GFileMonitor *monitor = NULL;
GFileMonitorEvent mask = 0;
g_assert (kqueue_socket_pair[0] != -1);
if (!_ku_read (kqueue_socket_pair[0], &n, sizeof (struct kqueue_notification)))
{
KH_W ("Failed to read a kqueue notification, error %d", errno);
return TRUE;
}
G_LOCK (hash_lock);
sub = (kqueue_sub *) g_hash_table_lookup (subs_hash_table, GINT_TO_POINTER (n.fd));
G_UNLOCK (hash_lock);
if (sub == NULL)
{
KH_W ("Got a notification for a deleted or non-existing subscription %d",
n.fd);
return TRUE;
}
monitor = G_FILE_MONITOR (sub->user_data);
g_assert (monitor != NULL);
if (n.flags & (NOTE_DELETE | NOTE_REVOKE))
{
if (sub->deps)
{
dl_free (sub->deps);
sub->deps = NULL;
}
_km_add_missing (sub);
if (!(n.flags & NOTE_REVOKE))
{
/* Note that NOTE_REVOKE is issued by the kqueue thread
* on EV_ERROR kevent. In this case, a file descriptor is
* already closed from the kqueue thread, no need to close
* it manually */
_kh_cancel_sub (sub);
}
}
if (sub->is_dir && n.flags & (NOTE_WRITE | NOTE_EXTEND))
{
_kh_dir_diff (sub, monitor);
n.flags &= ~(NOTE_WRITE | NOTE_EXTEND);
}
if (n.flags)
{
gboolean done = FALSE;
mask = convert_kqueue_events_to_gio (n.flags, &done);
if (done == TRUE)
{
GFile *file = g_file_new_for_path (sub->filename);
g_file_monitor_emit_event (monitor, file, NULL, mask);
g_object_unref (file);
}
}
return TRUE;
}
/**
* _kh_startup_impl:
* @unused: unused
*
* Kqueue backend startup code. Should be called only once.
*
* Returns: %TRUE on success, %FALSE otherwise.
**/
static gpointer
_kh_startup_impl (gpointer unused)
{
GIOChannel *channel = NULL;
gboolean result = FALSE;
kqueue_descriptor = kqueue ();
result = (kqueue_descriptor != -1);
if (!result)
{
KH_W ("Failed to initialize kqueue\n!");
return GINT_TO_POINTER (FALSE);
}
result = socketpair (AF_UNIX, SOCK_STREAM, 0, kqueue_socket_pair);
if (result != 0)
{
KH_W ("Failed to create socket pair\n!");
return GINT_TO_POINTER (FALSE) ;
}
result = pthread_create (&kqueue_thread,
NULL,
_kqueue_thread_func,
&kqueue_socket_pair[1]);
if (result != 0)
{
KH_W ("Failed to run kqueue thread\n!");
return GINT_TO_POINTER (FALSE);
}
_km_init (_kh_file_appeared_cb);
channel = g_io_channel_unix_new (kqueue_socket_pair[0]);
g_io_add_watch (channel, G_IO_IN, process_kqueue_notifications, NULL);
subs_hash_table = g_hash_table_new (g_direct_hash, g_direct_equal);
KH_W ("started gio kqueue backend\n");
return GINT_TO_POINTER (TRUE);
}
/**
* _kh_startup:
* Kqueue backend initialization.
*
* Returns: %TRUE on success, %FALSE otherwise.
**/
gboolean
_kh_startup (void)
{
static GOnce init_once = G_ONCE_INIT;
g_once (&init_once, _kh_startup_impl, NULL);
return GPOINTER_TO_INT (init_once.retval);
}
/**
* _kh_start_watching:
* @sub: a #kqueue_sub
*
* Starts watching on a subscription.
*
* Returns: %TRUE on success, %FALSE otherwise.
**/
gboolean
_kh_start_watching (kqueue_sub *sub)
{
g_assert (kqueue_socket_pair[0] != -1);
g_assert (sub != NULL);
g_assert (sub->filename != NULL);
/* kqueue requires a file descriptor to monitor. Sad but true */
#if defined (O_EVTONLY)
sub->fd = open (sub->filename, O_EVTONLY);
#else
sub->fd = open (sub->filename, O_RDONLY);
#endif
if (sub->fd == -1)
{
KH_W ("failed to open file %s (error %d)", sub->filename, errno);
return FALSE;
}
_ku_file_information (sub->fd, &sub->is_dir, NULL);
if (sub->is_dir)
{
/* I know, it is very bad to make such decisions in this way and here.
* We already do have an user_data at the #kqueue_sub, and it may point to
* GKqueueFileMonitor or GKqueueDirectoryMonitor. For a directory case,
* we need to scan in contents for the further diffs. Ideally this process
* should be delegated to the GKqueueDirectoryMonitor, but for now I will
* do it in a dirty way right here. */
if (sub->deps)
dl_free (sub->deps);
sub->deps = dl_listing (sub->filename);
}
G_LOCK (hash_lock);
g_hash_table_insert (subs_hash_table, GINT_TO_POINTER (sub->fd), sub);
G_UNLOCK (hash_lock);
_kqueue_thread_push_fd (sub->fd);
/* Bump the kqueue thread. It will pick up a new sub entry to monitor */
if (!_ku_write (kqueue_socket_pair[0], "A", 1))
KH_W ("Failed to bump the kqueue thread (add fd, error %d)", errno);
return TRUE;
}
/**
* _kh_add_sub:
* @sub: a #kqueue_sub
*
* Adds a subscription for monitoring.
*
* This funciton tries to start watching a subscription with
* _kh_start_watching(). On failure, i.e. when a file does not exist yet,
* the subscription will be added to a list of missing files to continue
* watching when the file will appear.
*
* Returns: %TRUE
**/
gboolean
_kh_add_sub (kqueue_sub *sub)
{
g_assert (sub != NULL);
if (!_kh_start_watching (sub))
_km_add_missing (sub);
return TRUE;
}
/**
* _kh_cancel_sub:
* @sub a #kqueue_sub
*
* Stops monitoring on a subscription.
*
* Returns: %TRUE
**/
gboolean
_kh_cancel_sub (kqueue_sub *sub)
{
gboolean missing = FALSE;
g_assert (kqueue_socket_pair[0] != -1);
g_assert (sub != NULL);
G_LOCK (hash_lock);
missing = !g_hash_table_remove (subs_hash_table, GINT_TO_POINTER (sub->fd));
G_UNLOCK (hash_lock);
if (missing)
{
/* If there were no fd for this subscription, file is still
* missing. */
KH_W ("Removing subscription from missing");
_km_remove (sub);
}
else
{
/* fd will be closed in the kqueue thread */
_kqueue_thread_remove_fd (sub->fd);
/* Bump the kqueue thread. It will pick up a new sub entry to remove*/
if (!_ku_write (kqueue_socket_pair[0], "R", 1))
KH_W ("Failed to bump the kqueue thread (remove fd, error %d)", errno);
}
return TRUE;
}
/**
* _kh_file_appeared_cb:
* @sub: a #kqueue_sub
*
* A callback function for kqueue-missing subsystem.
*
* Signals that a missing file has finally appeared in the filesystem.
* Emits %G_FILE_MONITOR_EVENT_CREATED.
**/
void
_kh_file_appeared_cb (kqueue_sub *sub)
{
GFile* child;
g_assert (sub != NULL);
g_assert (sub->filename);
if (!g_file_test (sub->filename, G_FILE_TEST_EXISTS))
return;
child = g_file_new_for_path (sub->filename);
g_file_monitor_emit_event (G_FILE_MONITOR (sub->user_data),
child,
NULL,
G_FILE_MONITOR_EVENT_CREATED);
g_object_unref (child);
}
|