glocalfilemonitor.c 27.2 KB
Newer Older
1
/* GIO - GLib Input, Output and Streaming Library
2
 *
3 4 5 6 7
 * Copyright (C) 2006-2007 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
8
 * version 2.1 of the License, or (at your option) any later version.
9 10 11 12 13 14 15
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General
16
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
17 18 19 20
 *
 * Author: Alexander Larsson <alexl@redhat.com>
 */

21
#include "config.h"
22

23
#include "gioenumtypes.h"
24
#include "glocalfilemonitor.h"
Alexander Larsson's avatar
Alexander Larsson committed
25
#include "giomodule-priv.h"
26
#include "gioerror.h"
27
#include "glibintl.h"
28 29
#include "glocalfile.h"
#include "glib-private.h"
30 31 32

#include <string.h>

33 34
#define DEFAULT_RATE_LIMIT                           800 * G_TIME_SPAN_MILLISECOND
#define VIRTUAL_CHANGES_DONE_DELAY                     2 * G_TIME_SPAN_SECOND
35

36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
/* GFileMonitorSource is a GSource responsible for emitting the changed
 * signals in the owner-context of the GFileMonitor.
 *
 * It contains functionality for cross-thread queuing of events.  It
 * also handles merging of CHANGED events and emission of CHANGES_DONE
 * events.
 *
 * We use the "priv" pointer in the external struct to store it.
 */
struct _GFileMonitorSource {
  GSource       source;

  GMutex        lock;
  gpointer      instance;
  GFileMonitorFlags flags;
  gchar        *dirname;
  gchar        *basename;
  gchar        *filename;
  GSequence    *pending_changes; /* sorted by ready time */
  GHashTable   *pending_changes_table;
  GQueue        event_queue;
  gint64        rate_limit;
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
/* PendingChange is a struct to keep track of a file that needs to have
 * (at least) a CHANGES_DONE_HINT event sent for it in the near future.
 *
 * If 'dirty' is TRUE then a CHANGED event also needs to be sent.
 *
 * last_emission is the last time a CHANGED event was emitted.  It is
 * used to calculate the time to send the next event.
 */
typedef struct {
  gchar    *child;
  guint64   last_emission : 63;
  guint64   dirty         :  1;
} PendingChange;

/* QueuedEvent is a signal that will be sent immediately, as soon as the
 * source gets a chance to dispatch.  The existence of any queued event
 * implies that the source is ready now.
 */
typedef struct
{
  GFileMonitorEvent event_type;
  GFile *child;
  GFile *other;
} QueuedEvent;

static gint64
pending_change_get_ready_time (const PendingChange *change,
                               GFileMonitorSource  *fms)
{
  if (change->dirty)
    return change->last_emission + fms->rate_limit;
  else
    return change->last_emission + VIRTUAL_CHANGES_DONE_DELAY;
}

static int
pending_change_compare_ready_time (gconstpointer a_p,
                                   gconstpointer b_p,
                                   gpointer      user_data)
{
  GFileMonitorSource *fms = user_data;
  const PendingChange *a = a_p;
  const PendingChange *b = b_p;
  gint64 ready_time_a;
  gint64 ready_time_b;

  ready_time_a = pending_change_get_ready_time (a, fms);
  ready_time_b = pending_change_get_ready_time (b, fms);

  if (ready_time_a < ready_time_b)
    return -1;
  else
    return ready_time_a > ready_time_b;
}
114 115

static void
116
pending_change_free (gpointer data)
117
{
118 119 120 121 122
  PendingChange *change = data;

  g_free (change->child);

  g_slice_free (PendingChange, change);
123 124 125
}

static void
126
queued_event_free (QueuedEvent *event)
127
{
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
  g_object_unref (event->child);
  if (event->other)
    g_object_unref (event->other);

  g_slice_free (QueuedEvent, event);
}

static gint64
g_file_monitor_source_get_ready_time (GFileMonitorSource *fms)
{
  GSequenceIter *iter;

  if (fms->event_queue.length)
    return 0;

  iter = g_sequence_get_begin_iter (fms->pending_changes);
  if (g_sequence_iter_is_end (iter))
    return -1;

  return pending_change_get_ready_time (g_sequence_get (iter), fms);
}
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
static void
g_file_monitor_source_update_ready_time (GFileMonitorSource *fms)
{
  g_source_set_ready_time ((GSource *) fms, g_file_monitor_source_get_ready_time (fms));
}

static GSequenceIter *
g_file_monitor_source_find_pending_change (GFileMonitorSource *fms,
                                           const gchar        *child)
{
  return g_hash_table_lookup (fms->pending_changes_table, child);
}

static void
g_file_monitor_source_add_pending_change (GFileMonitorSource *fms,
                                          const gchar        *child,
                                          gint64              now)
{
  PendingChange *change;
  GSequenceIter *iter;

  change = g_slice_new (PendingChange);
  change->child = g_strdup (child);
  change->last_emission = now;
  change->dirty = FALSE;

  iter = g_sequence_insert_sorted (fms->pending_changes, change, pending_change_compare_ready_time, fms);
  g_hash_table_insert (fms->pending_changes_table, change->child, iter);
}

180
static gboolean
181 182 183 184 185 186 187
g_file_monitor_source_set_pending_change_dirty (GFileMonitorSource *fms,
                                                GSequenceIter      *iter)
{
  PendingChange *change;

  change = g_sequence_get (iter);

188 189 190
  /* if it was already dirty then this change is 'uninteresting' */
  if (change->dirty)
    return FALSE;
191

192 193 194 195 196
  change->dirty = TRUE;

  g_sequence_sort_changed (iter, pending_change_compare_ready_time, fms);

  return TRUE;
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
}

static gboolean
g_file_monitor_source_get_pending_change_dirty (GFileMonitorSource *fms,
                                                GSequenceIter      *iter)
{
  PendingChange *change;

  change = g_sequence_get (iter);

  return change->dirty;
}

static void
g_file_monitor_source_remove_pending_change (GFileMonitorSource *fms,
                                             GSequenceIter      *iter,
                                             const gchar        *child)
{
  /* must remove the hash entry first -- its key is owned by the data
   * which will be freed when removing the sequence iter
   */
  g_hash_table_remove (fms->pending_changes_table, child);
  g_sequence_remove (iter);
}

static void
g_file_monitor_source_queue_event (GFileMonitorSource *fms,
                                   GFileMonitorEvent   event_type,
                                   const gchar        *child,
                                   GFile              *other)
{
  QueuedEvent *event;

  event = g_slice_new (QueuedEvent);
  event->event_type = event_type;
232
  if (child != NULL && fms->dirname != NULL)
233
    event->child = g_local_file_new_from_dirname_and_basename (fms->dirname, child);
234 235 236 237 238 239
  else if (child != NULL)
    {
      gchar *dirname = g_path_get_dirname (fms->filename);
      event->child = g_local_file_new_from_dirname_and_basename (dirname, child);
      g_free (dirname);
    }
240 241 242 243 244 245 246 247 248 249 250
  else if (fms->dirname)
    event->child = _g_local_file_new (fms->dirname);
  else if (fms->filename)
    event->child = _g_local_file_new (fms->filename);
  event->other = other;
  if (other)
    g_object_ref (other);

  g_queue_push_tail (&fms->event_queue, event);
}

251
static gboolean
252 253 254 255 256
g_file_monitor_source_file_changed (GFileMonitorSource *fms,
                                    const gchar        *child,
                                    gint64              now)
{
  GSequenceIter *pending;
257
  gboolean interesting;
258 259 260 261 262 263 264 265 266 267

  pending = g_file_monitor_source_find_pending_change (fms, child);

  /* If there is no pending change, emit one and create a record,
   * else: just mark the existing record as dirty.
   */
  if (!pending)
    {
      g_file_monitor_source_queue_event (fms, G_FILE_MONITOR_EVENT_CHANGED, child, NULL);
      g_file_monitor_source_add_pending_change (fms, child, now);
268
      interesting = TRUE;
269 270
    }
  else
271
    interesting = g_file_monitor_source_set_pending_change_dirty (fms, pending);
272 273

  g_file_monitor_source_update_ready_time (fms);
274 275

  return interesting;
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
}

static void
g_file_monitor_source_file_changes_done (GFileMonitorSource *fms,
                                         const gchar        *child)
{
  GSequenceIter *pending;

  pending = g_file_monitor_source_find_pending_change (fms, child);
  if (pending)
    {
      /* If it is dirty, make sure we push out the last CHANGED event */
      if (g_file_monitor_source_get_pending_change_dirty (fms, pending))
        g_file_monitor_source_queue_event (fms, G_FILE_MONITOR_EVENT_CHANGED, child, NULL);

      g_file_monitor_source_queue_event (fms, G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT, child, NULL);
      g_file_monitor_source_remove_pending_change (fms, pending, child);
    }
}

static void
g_file_monitor_source_file_created (GFileMonitorSource *fms,
                                    const gchar        *child,
                                    gint64              event_time)
{
  /* Unlikely, but if we have pending changes for this filename, make
   * sure we flush those out first, before creating the new ones.
   */
  g_file_monitor_source_file_changes_done (fms, child);

  /* Emit CREATE and add a pending changes record */
  g_file_monitor_source_queue_event (fms, G_FILE_MONITOR_EVENT_CREATED, child, NULL);
  g_file_monitor_source_add_pending_change (fms, child, event_time);
}

static void
g_file_monitor_source_send_event (GFileMonitorSource *fms,
                                  GFileMonitorEvent   event_type,
                                  const gchar        *child,
                                  GFile              *other)
{
  /* always flush any pending changes before we queue a new event */
  g_file_monitor_source_file_changes_done (fms, child);
  g_file_monitor_source_queue_event (fms, event_type, child, other);
}

static void
g_file_monitor_source_send_synthetic_created (GFileMonitorSource *fms,
                                              const gchar        *child)
{
  g_file_monitor_source_file_changes_done (fms, child);
  g_file_monitor_source_queue_event (fms, G_FILE_MONITOR_EVENT_CREATED, child, NULL);
  g_file_monitor_source_queue_event (fms, G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT, child, NULL);
}

static gboolean
is_basename (const gchar *name)
{
  if (name[0] == '.' && ((name[1] == '.' && name[2] == '\0') || name[1] == '\0'))
    return FALSE;

  return !strchr (name, '/');
}

340
gboolean
341 342 343 344 345 346 347
g_file_monitor_source_handle_event (GFileMonitorSource *fms,
                                    GFileMonitorEvent   event_type,
                                    const gchar        *child,
                                    const gchar        *rename_to,
                                    GFile              *other,
                                    gint64              event_time)
{
348 349
  gboolean interesting = TRUE;

350 351 352
  g_assert (!child || is_basename (child));
  g_assert (!rename_to || is_basename (rename_to));

353 354
  if (fms->basename && (!child || !g_str_equal (child, fms->basename))
                    && (!rename_to || !g_str_equal (rename_to, fms->basename)))
355
    return TRUE;
356 357 358 359 360 361 362

  g_mutex_lock (&fms->lock);

  /* monitor is already gone -- don't bother */
  if (!fms->instance)
    {
      g_mutex_unlock (&fms->lock);
363
      return TRUE;
364 365 366 367 368 369 370
    }

  switch (event_type)
    {
    case G_FILE_MONITOR_EVENT_CREATED:
      g_assert (!other && !rename_to);
      g_file_monitor_source_file_created (fms, child, event_time);
371
      break;
372

373 374
    case G_FILE_MONITOR_EVENT_CHANGED:
      g_assert (!other && !rename_to);
375
      interesting = g_file_monitor_source_file_changed (fms, child, event_time);
376
      break;
377

378 379 380
    case G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT:
      g_assert (!other && !rename_to);
      g_file_monitor_source_file_changes_done (fms, child);
381
      break;
382 383 384 385 386 387 388 389 390 391 392 393 394

    case G_FILE_MONITOR_EVENT_MOVED_IN:
      g_assert (!rename_to);
      if (fms->flags & G_FILE_MONITOR_WATCH_MOVES)
        g_file_monitor_source_send_event (fms, G_FILE_MONITOR_EVENT_MOVED_IN, child, other);
      else
        g_file_monitor_source_send_synthetic_created (fms, child);
      break;

    case G_FILE_MONITOR_EVENT_MOVED_OUT:
      g_assert (!rename_to);
      if (fms->flags & G_FILE_MONITOR_WATCH_MOVES)
        g_file_monitor_source_send_event (fms, G_FILE_MONITOR_EVENT_MOVED_OUT, child, other);
395
      else if (other && (fms->flags & G_FILE_MONITOR_SEND_MOVED))
396 397 398 399 400 401 402
        g_file_monitor_source_send_event (fms, G_FILE_MONITOR_EVENT_MOVED, child, other);
      else
        g_file_monitor_source_send_event (fms, G_FILE_MONITOR_EVENT_DELETED, child, NULL);
      break;

    case G_FILE_MONITOR_EVENT_RENAMED:
      g_assert (!other && rename_to);
403
      if (fms->flags & (G_FILE_MONITOR_WATCH_MOVES | G_FILE_MONITOR_SEND_MOVED))
404 405
        {
          GFile *other;
406 407 408
          const gchar *dirname;
          gchar *allocated_dirname = NULL;
          GFileMonitorEvent event;
409

410
          event = (fms->flags & G_FILE_MONITOR_WATCH_MOVES) ? G_FILE_MONITOR_EVENT_RENAMED : G_FILE_MONITOR_EVENT_MOVED;
411

412 413 414 415 416 417 418 419 420
          if (fms->dirname != NULL)
            dirname = fms->dirname;
          else
            {
              allocated_dirname = g_path_get_dirname (fms->filename);
              dirname = allocated_dirname;
            }

          other = g_local_file_new_from_dirname_and_basename (dirname, rename_to);
421
          g_file_monitor_source_file_changes_done (fms, rename_to);
422 423
          g_file_monitor_source_send_event (fms, event, child, other);

424
          g_object_unref (other);
425
          g_free (allocated_dirname);
426 427 428 429
        }
      else
        {
          g_file_monitor_source_send_event (fms, G_FILE_MONITOR_EVENT_DELETED, child, NULL);
430
          g_file_monitor_source_send_synthetic_created (fms, rename_to);
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
        }
      break;

    case G_FILE_MONITOR_EVENT_DELETED:
    case G_FILE_MONITOR_EVENT_ATTRIBUTE_CHANGED:
    case G_FILE_MONITOR_EVENT_PRE_UNMOUNT:
    case G_FILE_MONITOR_EVENT_UNMOUNTED:
      g_assert (!other && !rename_to);
      g_file_monitor_source_send_event (fms, event_type, child, NULL);
      break;

    case G_FILE_MONITOR_EVENT_MOVED:
      /* was never available in this API */
    default:
      g_assert_not_reached ();
446
    }
447 448 449 450

  g_file_monitor_source_update_ready_time (fms);

  g_mutex_unlock (&fms->lock);
451 452

  return interesting;
453 454
}

455 456
static gint64
g_file_monitor_source_get_rate_limit (GFileMonitorSource *fms)
457
{
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
  gint64 rate_limit;

  g_mutex_lock (&fms->lock);
  rate_limit = fms->rate_limit;
  g_mutex_unlock (&fms->lock);

  return rate_limit;
}

static gboolean
g_file_monitor_source_set_rate_limit (GFileMonitorSource *fms,
                                      gint64              rate_limit)
{
  gboolean changed;

  g_mutex_lock (&fms->lock);

  if (rate_limit != fms->rate_limit)
    {
      fms->rate_limit = rate_limit;

      g_sequence_sort (fms->pending_changes, pending_change_compare_ready_time, fms);
      g_file_monitor_source_update_ready_time (fms);
481 482

      changed = TRUE;
483 484 485
    }
  else
    changed = FALSE;
486

487
  g_mutex_unlock (&fms->lock);
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
  return changed;
}

static gboolean
g_file_monitor_source_dispatch (GSource     *source,
                                GSourceFunc  callback,
                                gpointer     user_data)
{
  GFileMonitorSource *fms = (GFileMonitorSource *) source;
  QueuedEvent *event;
  GQueue event_queue;
  gint64 now;

  /* make sure the monitor still exists */
  if (!fms->instance)
    return FALSE;

  now = g_source_get_time (source);

  /* Acquire the lock once and grab all events in one go, handling the
   * queued events first.  This avoids strange possibilities in cases of
   * long delays, such as CHANGED events coming before CREATED events.
   *
   * We do this by converting the applicable pending changes into queued
   * events (after the ones already queued) and then stealing the entire
   * event queue in one go.
   */
  g_mutex_lock (&fms->lock);

  /* Create events for any pending changes that are due to fire */
519
  while (!g_sequence_is_empty (fms->pending_changes))
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
    {
      GSequenceIter *iter = g_sequence_get_begin_iter (fms->pending_changes);
      PendingChange *pending = g_sequence_get (iter);

      /* We've gotten to a pending change that's not ready.  Stop. */
      if (pending_change_get_ready_time (pending, fms) > now)
        break;

      if (pending->dirty)
        {
          /* It's time to send another CHANGED and update the record */
          g_file_monitor_source_queue_event (fms, G_FILE_MONITOR_EVENT_CHANGED, pending->child, NULL);
          pending->last_emission = now;
          pending->dirty = FALSE;

          g_sequence_sort_changed (iter, pending_change_compare_ready_time, fms);
        }
      else
        {
          /* It's time to send CHANGES_DONE and remove the pending record */
          g_file_monitor_source_queue_event (fms, G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT, pending->child, NULL);
          g_file_monitor_source_remove_pending_change (fms, iter, pending->child);
        }
    }

  /* Steal the queue */
  memcpy (&event_queue, &fms->event_queue, sizeof event_queue);
  memset (&fms->event_queue, 0, sizeof fms->event_queue);

  g_file_monitor_source_update_ready_time (fms);

  g_mutex_unlock (&fms->lock);

  /* We now have our list of events to deliver */
  while ((event = g_queue_pop_head (&event_queue)))
    {
      /* an event handler could destroy 'instance', so check each time */
      if (fms->instance)
        g_file_monitor_emit_event (fms->instance, event->child, event->other, event->event_type);

      queued_event_free (event);
    }

  return TRUE;
}

static void
g_file_monitor_source_dispose (GFileMonitorSource *fms)
{
  g_mutex_lock (&fms->lock);

  if (fms->instance)
    {
      GHashTableIter iter;
      gpointer seqiter;
      QueuedEvent *event;

      g_hash_table_iter_init (&iter, fms->pending_changes_table);
      while (g_hash_table_iter_next (&iter, NULL, &seqiter))
        {
          g_hash_table_iter_remove (&iter);
          g_sequence_remove (seqiter);
        }

      while ((event = g_queue_pop_head (&fms->event_queue)))
        queued_event_free (event);

587
      g_assert (g_sequence_is_empty (fms->pending_changes));
588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606
      g_assert (g_hash_table_size (fms->pending_changes_table) == 0);
      g_assert (fms->event_queue.length == 0);
      fms->instance = NULL;

      g_file_monitor_source_update_ready_time (fms);
    }

  g_mutex_unlock (&fms->lock);

  g_source_destroy ((GSource *) fms);
}

static void
g_file_monitor_source_finalize (GSource *source)
{
  GFileMonitorSource *fms = (GFileMonitorSource *) source;

  /* should already have been cleared in dispose of the monitor */
  g_assert (fms->instance == NULL);
607
  g_assert (g_sequence_is_empty (fms->pending_changes));
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 647 648 649 650
  g_assert (g_hash_table_size (fms->pending_changes_table) == 0);
  g_assert (fms->event_queue.length == 0);

  g_hash_table_unref (fms->pending_changes_table);
  g_sequence_free (fms->pending_changes);

  g_free (fms->dirname);
  g_free (fms->basename);
  g_free (fms->filename);

  g_mutex_clear (&fms->lock);
}

static guint
str_hash0 (gconstpointer str)
{
  return str ? g_str_hash (str) : 0;
}

static gboolean
str_equal0 (gconstpointer a,
            gconstpointer b)
{
  return g_strcmp0 (a, b) == 0;
}

static GFileMonitorSource *
g_file_monitor_source_new (gpointer           instance,
                           const gchar       *filename,
                           gboolean           is_directory,
                           GFileMonitorFlags  flags)
{
  static GSourceFuncs source_funcs = {
    NULL, NULL,
    g_file_monitor_source_dispatch,
    g_file_monitor_source_finalize
  };
  GFileMonitorSource *fms;
  GSource *source;

  source = g_source_new (&source_funcs, sizeof (GFileMonitorSource));
  fms = (GFileMonitorSource *) source;

651 652
  g_source_set_name (source, "GFileMonitorSource");

653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764
  g_mutex_init (&fms->lock);
  fms->instance = instance;
  fms->pending_changes = g_sequence_new (pending_change_free);
  fms->pending_changes_table = g_hash_table_new (str_hash0, str_equal0);
  fms->rate_limit = DEFAULT_RATE_LIMIT;
  fms->flags = flags;

  if (is_directory)
    {
      fms->dirname = g_strdup (filename);
      fms->basename = NULL;
      fms->filename = NULL;
    }
  else if (flags & G_FILE_MONITOR_WATCH_HARD_LINKS)
    {
      fms->dirname = NULL;
      fms->basename = NULL;
      fms->filename = g_strdup (filename);
    }
  else
    {
      fms->dirname = g_path_get_dirname (filename);
      fms->basename = g_path_get_basename (filename);
      fms->filename = NULL;
    }

  return fms;
}

G_DEFINE_ABSTRACT_TYPE (GLocalFileMonitor, g_local_file_monitor, G_TYPE_FILE_MONITOR)

enum {
  PROP_0,
  PROP_RATE_LIMIT,
};

static void
g_local_file_monitor_get_property (GObject *object, guint prop_id,
                                   GValue *value, GParamSpec *pspec)
{
  GLocalFileMonitor *monitor = G_LOCAL_FILE_MONITOR (object);
  gint64 rate_limit;

  g_assert (prop_id == PROP_RATE_LIMIT);

  rate_limit = g_file_monitor_source_get_rate_limit (monitor->source);
  rate_limit /= G_TIME_SPAN_MILLISECOND;

  g_value_set_int (value, rate_limit);
}

static void
g_local_file_monitor_set_property (GObject *object, guint prop_id,
                                   const GValue *value, GParamSpec *pspec)
{
  GLocalFileMonitor *monitor = G_LOCAL_FILE_MONITOR (object);
  gint64 rate_limit;

  g_assert (prop_id == PROP_RATE_LIMIT);

  rate_limit = g_value_get_int (value);
  rate_limit *= G_TIME_SPAN_MILLISECOND;

  if (g_file_monitor_source_set_rate_limit (monitor->source, rate_limit))
    g_object_notify (object, "rate-limit");
}

#ifndef G_OS_WIN32
static void
g_local_file_monitor_mounts_changed (GUnixMountMonitor *mount_monitor,
                                     gpointer           user_data)
{
  GLocalFileMonitor *local_monitor = user_data;
  GUnixMountEntry *mount;
  gboolean is_mounted;
  GFile *file;

  /* Emulate unmount detection */
  mount = g_unix_mount_at (local_monitor->source->dirname, NULL);

  is_mounted = mount != NULL;

  if (mount)
    g_unix_mount_free (mount);

  if (local_monitor->was_mounted != is_mounted)
    {
      if (local_monitor->was_mounted && !is_mounted)
        {
          file = g_file_new_for_path (local_monitor->source->dirname);
          g_file_monitor_emit_event (G_FILE_MONITOR (local_monitor), file, NULL, G_FILE_MONITOR_EVENT_UNMOUNTED);
          g_object_unref (file);
        }
      local_monitor->was_mounted = is_mounted;
    }
}
#endif

static void
g_local_file_monitor_start (GLocalFileMonitor *local_monitor,
                            const gchar       *filename,
                            gboolean           is_directory,
                            GFileMonitorFlags  flags,
                            GMainContext      *context)
{
  GLocalFileMonitorClass *class = G_LOCAL_FILE_MONITOR_GET_CLASS (local_monitor);
  GFileMonitorSource *source;

  g_return_if_fail (G_IS_LOCAL_FILE_MONITOR (local_monitor));

  g_assert (!local_monitor->source);

765 766 767
  source = g_file_monitor_source_new (local_monitor, filename, is_directory, flags);
  local_monitor->source = source; /* owns the ref */

768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805
  if (is_directory && !class->mount_notify && (flags & G_FILE_MONITOR_WATCH_MOUNTS))
    {
#ifdef G_OS_WIN32
      /*claim everything was mounted */
      local_monitor->was_mounted = TRUE;
#else
      GUnixMountEntry *mount;

      /* Emulate unmount detection */

      mount = g_unix_mount_at (local_monitor->source->dirname, NULL);

      local_monitor->was_mounted = mount != NULL;

      if (mount)
        g_unix_mount_free (mount);

      local_monitor->mount_monitor = g_unix_mount_monitor_get ();
      g_signal_connect_object (local_monitor->mount_monitor, "mounts-changed",
                               G_CALLBACK (g_local_file_monitor_mounts_changed), local_monitor, 0);
#endif
    }

  G_LOCAL_FILE_MONITOR_GET_CLASS (local_monitor)->start (local_monitor,
                                                         source->dirname, source->basename, source->filename,
                                                         source);

  g_source_attach ((GSource *) source, context);
}

static void
g_local_file_monitor_dispose (GObject *object)
{
  GLocalFileMonitor *local_monitor = G_LOCAL_FILE_MONITOR (object);

  g_file_monitor_source_dispose (local_monitor->source);

  G_OBJECT_CLASS (g_local_file_monitor_parent_class)->dispose (object);
806 807 808 809 810 811
}

static void
g_local_file_monitor_finalize (GObject *object)
{
  GLocalFileMonitor *local_monitor = G_LOCAL_FILE_MONITOR (object);
812

813
  g_source_unref ((GSource *) local_monitor->source);
814

815
  G_OBJECT_CLASS (g_local_file_monitor_parent_class)->finalize (object);
816 817
}

818 819 820 821 822 823
static void
g_local_file_monitor_init (GLocalFileMonitor* local_monitor)
{
}

static void g_local_file_monitor_class_init (GLocalFileMonitorClass *class)
824
{
825
  GObjectClass *gobject_class = G_OBJECT_CLASS (class);
826

827
  gobject_class->get_property = g_local_file_monitor_get_property;
828
  gobject_class->set_property = g_local_file_monitor_set_property;
829
  gobject_class->dispose = g_local_file_monitor_dispose;
830 831
  gobject_class->finalize = g_local_file_monitor_finalize;

832 833 834 835 836
  g_object_class_override_property (gobject_class, PROP_RATE_LIMIT, "rate-limit");
}

static GLocalFileMonitor *
g_local_file_monitor_new (gboolean   is_remote_fs,
837
                          gboolean   is_directory,
838 839
                          GError   **error)
{
840
  GType type = G_TYPE_INVALID;
841

842 843 844 845 846
  if (is_remote_fs)
    type = _g_io_module_get_default_type (G_NFS_FILE_MONITOR_EXTENSION_POINT_NAME,
                                          "GIO_USE_FILE_MONITOR",
                                          G_STRUCT_OFFSET (GLocalFileMonitorClass, is_supported));

847 848
  /* Fallback rather to poll file monitor for remote files, see gfile.c. */
  if (type == G_TYPE_INVALID && (!is_remote_fs || is_directory))
849 850 851
    type = _g_io_module_get_default_type (G_LOCAL_FILE_MONITOR_EXTENSION_POINT_NAME,
                                          "GIO_USE_FILE_MONITOR",
                                          G_STRUCT_OFFSET (GLocalFileMonitorClass, is_supported));
852

853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870
  if (type == G_TYPE_INVALID)
    {
      g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
                           _("Unable to find default local file monitor type"));
      return NULL;
    }

  return g_object_new (type, NULL);
}

GFileMonitor *
g_local_file_monitor_new_for_path (const gchar        *pathname,
                                   gboolean            is_directory,
                                   GFileMonitorFlags   flags,
                                   GError            **error)
{
  GLocalFileMonitor *monitor;
  gboolean is_remote_fs;
871

872 873
  is_remote_fs = g_local_file_is_remote (pathname);

874
  monitor = g_local_file_monitor_new (is_remote_fs, is_directory, error);
875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894

  if (monitor)
    g_local_file_monitor_start (monitor, pathname, is_directory, flags, g_main_context_get_thread_default ());

  return G_FILE_MONITOR (monitor);
}

GFileMonitor *
g_local_file_monitor_new_in_worker (const gchar           *pathname,
                                    gboolean               is_directory,
                                    GFileMonitorFlags      flags,
                                    GFileMonitorCallback   callback,
                                    gpointer               user_data,
                                    GError               **error)
{
  GLocalFileMonitor *monitor;
  gboolean is_remote_fs;

  is_remote_fs = g_local_file_is_remote (pathname);

895
  monitor = g_local_file_monitor_new (is_remote_fs, is_directory, error);
896 897 898 899 900 901 902 903

  if (monitor)
    {
      if (callback)
        g_signal_connect (monitor, "changed", G_CALLBACK (callback), user_data);

      g_local_file_monitor_start (monitor, pathname, is_directory, flags, GLIB_PRIVATE_CALL(g_get_worker_context) ());
    }
904

905
  return G_FILE_MONITOR (monitor);
906
}