testfilemonitor.c 30.8 KB
Newer Older
1 2 3
#include "config.h"

#include <errno.h>
4 5 6
#include <stdlib.h>
#include <gio/gio.h>

7 8 9 10 11
/* These tests were written for the inotify implementation.
 * Other implementations may require slight adjustments in
 * the tests, e.g. the length of timeouts
 */

12 13 14 15 16 17
typedef enum {
  NONE      = 0,
  INOTIFY   = (1 << 1),
  KQUEUE    = (1 << 2)
} Environment;

18 19 20 21 22 23
typedef struct
{
  gint event_type;
  gchar *file;
  gchar *other_file;
  gint step;
24 25 26 27 28 29 30

  /* Since different file monitor implementation has different capabilities,
   * we cannot expect all implementations to report all kind of events without
   * any loss. This 'optional' field is a bit mask used to mark events which
   * may be lost under specific platforms.
   */
  Environment optional;
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
} RecordedEvent;

static void
free_recorded_event (RecordedEvent *event)
{
  g_free (event->file);
  g_free (event->other_file);
  g_free (event);
}

typedef struct
{
  GFile *file;
  GFileMonitor *monitor;
  GMainLoop *loop;
  gint step;
  GList *events;
48
  GFileOutputStream *output_stream;
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
} TestData;

#if 0
static void
output_event (RecordedEvent *event)
{
  if (event->step >= 0)
    g_print (">>>> step %d\n", event->step);
  else
    {
      GTypeClass *class;

      class = g_type_class_ref (g_type_from_name ("GFileMonitorEvent"));
      g_print ("%s file=%s other_file=%s\n",
               g_enum_get_value (G_ENUM_CLASS (class), event->event_type)->value_nick,
               event->file,
               event->other_file);
      g_type_class_unref (class);
    }
}

static void
output_events (GList *list)
{
  GList *l;

  g_print (">>>output events\n");
  for (l = list; l; l = l->next)
    output_event ((RecordedEvent *)l->data);
}
#endif

/* a placeholder for temp file names we don't want to compare */
static const gchar DONT_CARE[] = "";

84 85
static Environment
get_environment (GFileMonitor *monitor)
86
{
87 88 89 90 91
  if (g_str_equal (G_OBJECT_TYPE_NAME (monitor), "GInotifyFileMonitor"))
    return INOTIFY;
  if (g_str_equal (G_OBJECT_TYPE_NAME (monitor), "GKqueueFileMonitor"))
    return KQUEUE;
  return NONE;
92 93 94 95 96
}

static void
check_expected_events (RecordedEvent *expected,
                       gsize          n_expected,
97 98
                       GList         *recorded,
                       Environment    env)
99
{
100
  gint i, li;
101 102
  GList *l;

103
  for (i = 0, li = 0, l = recorded; i < n_expected && l != NULL;)
104 105
    {
      RecordedEvent *e1 = &expected[i];
106 107 108
      RecordedEvent *e2 = l->data;
      gboolean mismatch = TRUE;
      gboolean l_extra_step = FALSE;
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
      do
        {
          gboolean ignore_other_file = FALSE;

          if (e1->step != e2->step)
            break;

          /* Kqueue isn't good at detecting file renaming, so
           * G_FILE_MONITOR_WATCH_MOVES is mostly useless there.  */
          if (e1->event_type != e2->event_type && env & KQUEUE)
            {
              /* It is possible for kqueue file monitor to emit 'RENAMED' event,
               * but most of the time it is reported as a 'DELETED' event and
               * a 'CREATED' event. */
              if (e1->event_type == G_FILE_MONITOR_EVENT_RENAMED)
                {
                  RecordedEvent *e2_next;

                  if (l->next == NULL)
                    break;
                  e2_next = l->next->data;

                  if (e2->event_type != G_FILE_MONITOR_EVENT_DELETED)
                    break;
                  if (e2_next->event_type != G_FILE_MONITOR_EVENT_CREATED)
                    break;

                  if (e1->step != e2_next->step)
                    break;

                  if (e1->file != DONT_CARE &&
                      (g_strcmp0 (e1->file, e2->file) != 0 ||
                       e2->other_file != NULL))
                    break;

                  if (e1->other_file != DONT_CARE &&
                      (g_strcmp0 (e1->other_file, e2_next->file) != 0 ||
                       e2_next->other_file != NULL))
                    break;

                  l_extra_step = TRUE;
                  mismatch = FALSE;
                  break;
                }
              /* Kqueue won't report 'MOVED_IN' and 'MOVED_OUT' events. We set
               * 'ignore_other_file' here to let the following code know that
               * 'other_file' may not match. */
              else if (e1->event_type == G_FILE_MONITOR_EVENT_MOVED_IN)
                {
                  if (e2->event_type != G_FILE_MONITOR_EVENT_CREATED)
                    break;
                  ignore_other_file = TRUE;
                }
              else if (e1->event_type == G_FILE_MONITOR_EVENT_MOVED_OUT)
                {
                  if (e2->event_type != G_FILE_MONITOR_EVENT_DELETED)
                    break;
                  ignore_other_file = TRUE;
                }
              else
                break;
            }

          if (e1->file != DONT_CARE &&
              g_strcmp0 (e1->file, e2->file) != 0)
            break;

          if (e1->other_file != DONT_CARE && !ignore_other_file &&
              g_strcmp0 (e1->other_file, e2->other_file) != 0)
            break;

          mismatch = FALSE;
        }
      while (0);

      if (mismatch)
        {
          /* Sometimes the emission of 'CHANGES_DONE_HINT' may be late because
           * it depends on the ability of file monitor implementation to report
           * 'CHANGES_DONE_HINT' itself. If the file monitor implementation
           * doesn't report 'CHANGES_DONE_HINT' itself, it may be emitted by
           * GLocalFileMonitor after a few seconds, which causes the event to
           * mix with results from different steps. Since 'CHANGES_DONE_HINT'
           * is just a hint, we don't require it to be reliable and we simply
           * ignore unexpected 'CHANGES_DONE_HINT' events here. */
          if (e1->event_type != G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT &&
              e2->event_type == G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT)
            {
              g_test_message ("Event CHANGES_DONE_HINT ignored at "
                              "expected index %d, recorded index %d", i, li);
              li++, l = l->next;
              continue;
            }
          /* If an event is marked as optional in the current environment and
           * the event doesn't match, it means the expected event has lost. */
          else if (env & e1->optional)
            {
              g_test_message ("Event %d at expected index %d skipped because "
                              "it is marked as optional", e1->event_type, i);
              i++;
              continue;
            }
          /* Run above checks under g_assert_* again to provide more useful
           * error messages. */
          else
            {
              g_assert_cmpint (e1->step, ==, e2->step);
              g_assert_cmpint (e1->event_type, ==, e2->event_type);

              if (e1->file != DONT_CARE)
                g_assert_cmpstr (e1->file, ==, e2->file);

              if (e1->other_file != DONT_CARE)
                g_assert_cmpstr (e1->other_file, ==, e2->other_file);

              g_assert_not_reached ();
            }
        }

      i++, li++, l = l->next;
      if (l_extra_step)
        li++, l = l->next;
232
    }
233 234 235

  g_assert_cmpint (i, ==, n_expected);
  g_assert_cmpint (li, ==, g_list_length (recorded));
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
}

static void
record_event (TestData    *data,
              gint         event_type,
              const gchar *file,
              const gchar *other_file,
              gint         step)
{
  RecordedEvent *event;

  event = g_new0 (RecordedEvent, 1);
  event->event_type = event_type;
  event->file = g_strdup (file);
  event->other_file = g_strdup (other_file);
  event->step = step;

  data->events = g_list_append (data->events, event);
}

static void
monitor_changed (GFileMonitor      *monitor,
                 GFile             *file,
                 GFile             *other_file,
                 GFileMonitorEvent  event_type,
                 gpointer           user_data)
{
  TestData *data = user_data;
  gchar *basename, *other_base;

  basename = g_file_get_basename (file);
  if (other_file)
    other_base = g_file_get_basename (other_file);
  else
    other_base = NULL;

  record_event (data, event_type, basename, other_base, -1);

  g_free (basename);
  g_free (other_base);
}

static gboolean
atomic_replace_step (gpointer user_data)
{
  TestData *data = user_data;
  GError *error = NULL;

  switch (data->step)
    {
    case 0:
      record_event (data, -1, NULL, NULL, 0);
      g_file_replace_contents (data->file, "step 0", 6, NULL, FALSE, G_FILE_CREATE_NONE, NULL, NULL, &error);
      g_assert_no_error (error);
      break;
    case 1:
      record_event (data, -1, NULL, NULL, 1);
      g_file_replace_contents (data->file, "step 1", 6, NULL, FALSE, G_FILE_CREATE_NONE, NULL, NULL, &error);
      g_assert_no_error (error);
      break;
    case 2:
      record_event (data, -1, NULL, NULL, 2);
298 299 300 301
      g_file_delete (data->file, NULL, NULL);
      break;
    case 3:
      record_event (data, -1, NULL, NULL, 3);
302 303 304 305 306 307 308 309 310 311 312
      g_main_loop_quit (data->loop);
      return G_SOURCE_REMOVE;
    }

  data->step++;

  return G_SOURCE_CONTINUE;
}

/* this is the output we expect from the above steps */
static RecordedEvent atomic_replace_output[] = {
313 314 315 316 317 318 319 320 321
  { -1, NULL, NULL, 0, NONE },
  { G_FILE_MONITOR_EVENT_CREATED, "atomic_replace_file", NULL, -1, NONE },
  { G_FILE_MONITOR_EVENT_CHANGED, "atomic_replace_file", NULL, -1, KQUEUE },
  { G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT, "atomic_replace_file", NULL, -1, KQUEUE },
  { -1, NULL, NULL, 1, NONE },
  { G_FILE_MONITOR_EVENT_RENAMED, (gchar*)DONT_CARE, "atomic_replace_file", -1, NONE },
  { -1, NULL, NULL, 2, NONE },
  { G_FILE_MONITOR_EVENT_DELETED, "atomic_replace_file", NULL, -1, NONE },
  { -1, NULL, NULL, 3, NONE }
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
};

static void
test_atomic_replace (void)
{
  GError *error = NULL;
  TestData data;

  data.step = 0;
  data.events = NULL;

  data.file = g_file_new_for_path ("atomic_replace_file");
  g_file_delete (data.file, NULL, NULL);

  data.monitor = g_file_monitor_file (data.file, G_FILE_MONITOR_WATCH_MOVES, NULL, &error);
  g_assert_no_error (error);

  g_file_monitor_set_rate_limit (data.monitor, 200);
  g_signal_connect (data.monitor, "changed", G_CALLBACK (monitor_changed), &data);

  data.loop = g_main_loop_new (NULL, TRUE);

344
  g_timeout_add (500, atomic_replace_step, &data);
345 346 347 348

  g_main_loop_run (data.loop);

  /*output_events (data.events);*/
349 350 351 352
  check_expected_events (atomic_replace_output,
                         G_N_ELEMENTS (atomic_replace_output),
                         data.events,
                         get_environment (data.monitor));
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
  g_list_free_full (data.events, (GDestroyNotify)free_recorded_event);
  g_main_loop_unref (data.loop);
  g_object_unref (data.monitor);
  g_object_unref (data.file);
}

static gboolean
change_step (gpointer user_data)
{
  TestData *data = user_data;
  GOutputStream *stream;
  GError *error = NULL;
  guint32 mode = 0660;

  switch (data->step)
    {
    case 0:
      record_event (data, -1, NULL, NULL, 0);
      g_file_replace_contents (data->file, "step 0", 6, NULL, FALSE, G_FILE_CREATE_NONE, NULL, NULL, &error);
      g_assert_no_error (error);
      break;
    case 1:
      record_event (data, -1, NULL, NULL, 1);
      stream = (GOutputStream *)g_file_append_to (data->file, G_FILE_CREATE_NONE, NULL, &error);
      g_assert_no_error (error);
      g_output_stream_write_all (stream, " step 1", 7, NULL, NULL, &error);
      g_assert_no_error (error);
      g_output_stream_close (stream, NULL, &error);
      g_assert_no_error (error);
383
      g_object_unref (stream);
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
      break;
    case 2:
      record_event (data, -1, NULL, NULL, 2);
      g_file_set_attribute (data->file,
                            G_FILE_ATTRIBUTE_UNIX_MODE,
                            G_FILE_ATTRIBUTE_TYPE_UINT32,
                            &mode,
                            G_FILE_QUERY_INFO_NONE,
                            NULL,
                            &error);
      g_assert_no_error (error);
      break;
    case 3:
      record_event (data, -1, NULL, NULL, 3);
      g_file_delete (data->file, NULL, NULL);
      break;
    case 4:
      record_event (data, -1, NULL, NULL, 4);
      g_main_loop_quit (data->loop);
      return G_SOURCE_REMOVE;
    }

  data->step++;

  return G_SOURCE_CONTINUE;
}

/* this is the output we expect from the above steps */
static RecordedEvent change_output[] = {
413 414 415 416 417 418 419 420 421 422 423 424
  { -1, NULL, NULL, 0, NONE },
  { G_FILE_MONITOR_EVENT_CREATED, "change_file", NULL, -1, NONE },
  { G_FILE_MONITOR_EVENT_CHANGED, "change_file", NULL, -1, KQUEUE },
  { G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT, "change_file", NULL, -1, KQUEUE },
  { -1, NULL, NULL, 1, NONE },
  { G_FILE_MONITOR_EVENT_CHANGED, "change_file", NULL, -1, NONE },
  { G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT, "change_file", NULL, -1, NONE },
  { -1, NULL, NULL, 2, NONE },
  { G_FILE_MONITOR_EVENT_ATTRIBUTE_CHANGED, "change_file", NULL, -1, NONE },
  { -1, NULL, NULL, 3, NONE },
  { G_FILE_MONITOR_EVENT_DELETED, "change_file", NULL, -1, NONE },
  { -1, NULL, NULL, 4, NONE }
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
};

static void
test_file_changes (void)
{
  GError *error = NULL;
  TestData data;

  data.step = 0;
  data.events = NULL;

  data.file = g_file_new_for_path ("change_file");
  g_file_delete (data.file, NULL, NULL);

  data.monitor = g_file_monitor_file (data.file, G_FILE_MONITOR_WATCH_MOVES, NULL, &error);
  g_assert_no_error (error);

  g_file_monitor_set_rate_limit (data.monitor, 200);
  g_signal_connect (data.monitor, "changed", G_CALLBACK (monitor_changed), &data);

  data.loop = g_main_loop_new (NULL, TRUE);

  g_timeout_add (500, change_step, &data);

  g_main_loop_run (data.loop);

  /*output_events (data.events);*/
452 453 454 455
  check_expected_events (change_output,
                         G_N_ELEMENTS (change_output),
                         data.events,
                         get_environment (data.monitor));
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

  g_list_free_full (data.events, (GDestroyNotify)free_recorded_event);
  g_main_loop_unref (data.loop);
  g_object_unref (data.monitor);
  g_object_unref (data.file);
}

static gboolean
dir_step (gpointer user_data)
{
  TestData *data = user_data;
  GFile *parent, *file, *file2;
  GError *error = NULL;

  switch (data->step)
    {
    case 1:
      record_event (data, -1, NULL, NULL, 1);
      parent = g_file_get_parent (data->file);
      file = g_file_get_child (parent, "dir_test_file");
      g_file_replace_contents (file, "step 1", 6, NULL, FALSE, G_FILE_CREATE_NONE, NULL, NULL, &error);
      g_assert_no_error (error);
      g_object_unref (file);
      g_object_unref (parent);
      break;
    case 2:
      record_event (data, -1, NULL, NULL, 2);
      parent = g_file_get_parent (data->file);
      file = g_file_get_child (parent, "dir_test_file");
      file2 = g_file_get_child (data->file, "dir_test_file");
      g_file_move (file, file2, G_FILE_COPY_NONE, NULL, NULL, NULL, &error);
      g_assert_no_error (error);
      g_object_unref (file);
      g_object_unref (file2);
      g_object_unref (parent);
      break;
    case 3:
      record_event (data, -1, NULL, NULL, 3);
      file = g_file_get_child (data->file, "dir_test_file");
      file2 = g_file_get_child (data->file, "dir_test_file2");
      g_file_move (file, file2, G_FILE_COPY_NONE, NULL, NULL, NULL, &error);
      g_assert_no_error (error);
      g_object_unref (file);
      g_object_unref (file2);
      break;
    case 4:
      record_event (data, -1, NULL, NULL, 4);
      parent = g_file_get_parent (data->file);
      file = g_file_get_child (data->file, "dir_test_file2");
      file2 = g_file_get_child (parent, "dir_test_file2");
      g_file_move (file, file2, G_FILE_COPY_NONE, NULL, NULL, NULL, &error);
      g_assert_no_error (error);
      g_file_delete (file2, NULL, NULL);
      g_object_unref (file);
      g_object_unref (file2);
      g_object_unref (parent);
      break;
    case 5:
      record_event (data, -1, NULL, NULL, 5);
      g_file_delete (data->file, NULL, NULL);
      break;
    case 6:
      record_event (data, -1, NULL, NULL, 6);
      g_main_loop_quit (data->loop);
      return G_SOURCE_REMOVE;
    }

  data->step++;

  return G_SOURCE_CONTINUE;
}

/* this is the output we expect from the above steps */
static RecordedEvent dir_output[] = {
530 531 532 533 534 535 536 537 538 539
  { -1, NULL, NULL, 1, NONE },
  { -1, NULL, NULL, 2, NONE },
  { G_FILE_MONITOR_EVENT_MOVED_IN, "dir_test_file", NULL, -1, NONE },
  { -1, NULL, NULL, 3, NONE },
  { G_FILE_MONITOR_EVENT_RENAMED, "dir_test_file", "dir_test_file2", -1, NONE },
  { -1, NULL, NULL, 4, NONE },
  { G_FILE_MONITOR_EVENT_MOVED_OUT, "dir_test_file2", NULL, -1, NONE },
  { -1, NULL, NULL, 5, NONE },
  { G_FILE_MONITOR_EVENT_DELETED, "dir_monitor_test", NULL, -1, NONE },
  { -1, NULL, NULL, 6, NONE }
540 541 542 543 544 545 546 547 548 549 550 551
};

static void
test_dir_monitor (void)
{
  GError *error = NULL;
  TestData data;

  data.step = 0;
  data.events = NULL;

  data.file = g_file_new_for_path ("dir_monitor_test");
552
  g_file_delete (data.file, NULL, NULL);
553 554 555 556 557 558 559 560 561 562 563 564 565 566 567
  g_file_make_directory (data.file, NULL, &error);

  data.monitor = g_file_monitor_directory (data.file, G_FILE_MONITOR_WATCH_MOVES, NULL, &error);
  g_assert_no_error (error);

  g_file_monitor_set_rate_limit (data.monitor, 200);
  g_signal_connect (data.monitor, "changed", G_CALLBACK (monitor_changed), &data);

  data.loop = g_main_loop_new (NULL, TRUE);

  g_timeout_add (500, dir_step, &data);

  g_main_loop_run (data.loop);

  /*output_events (data.events);*/
568 569 570 571
  check_expected_events (dir_output,
                         G_N_ELEMENTS (dir_output),
                         data.events,
                         get_environment (data.monitor));
572 573 574 575 576 577 578

  g_list_free_full (data.events, (GDestroyNotify)free_recorded_event);
  g_main_loop_unref (data.loop);
  g_object_unref (data.monitor);
  g_object_unref (data.file);
}

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
static gboolean
nodir_step (gpointer user_data)
{
  TestData *data = user_data;
  GFile *parent;
  GError *error = NULL;

  switch (data->step)
    {
    case 0:
      record_event (data, -1, NULL, NULL, 0);
      parent = g_file_get_parent (data->file);
      g_file_make_directory (parent, NULL, &error);
      g_assert_no_error (error);
      g_object_unref (parent);
      break;
    case 1:
      record_event (data, -1, NULL, NULL, 1);
      g_file_replace_contents (data->file, "step 1", 6, NULL, FALSE, G_FILE_CREATE_NONE, NULL, NULL, &error);
      g_assert_no_error (error);
      break;
    case 2:
      record_event (data, -1, NULL, NULL, 2);
      g_file_delete (data->file, NULL, &error);
      g_assert_no_error (error);
      break;
    case 3:
      record_event (data, -1, NULL, NULL, 3);
      parent = g_file_get_parent (data->file);
      g_file_delete (parent, NULL, &error);
      g_assert_no_error (error);
      g_object_unref (parent);
      break;
    case 4:
      record_event (data, -1, NULL, NULL, 4);
      g_main_loop_quit (data->loop);
      return G_SOURCE_REMOVE;
    }

  data->step++;

  return G_SOURCE_CONTINUE;
}

static RecordedEvent nodir_output[] = {
624 625 626 627 628 629 630 631 632 633 634
  { -1, NULL, NULL, 0, NONE },
  { G_FILE_MONITOR_EVENT_CREATED, "nosuchfile", NULL, -1, KQUEUE },
  { G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT, "nosuchfile", NULL, -1, KQUEUE },
  { -1, NULL, NULL, 1, NONE },
  { G_FILE_MONITOR_EVENT_CREATED, "nosuchfile", NULL, -1, NONE },
  { G_FILE_MONITOR_EVENT_CHANGED, "nosuchfile", NULL, -1, KQUEUE },
  { G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT, "nosuchfile", NULL, -1, KQUEUE },
  { -1, NULL, NULL, 2, NONE },
  { G_FILE_MONITOR_EVENT_DELETED, "nosuchfile", NULL, -1, NONE },
  { -1, NULL, NULL, 3, NONE },
  { -1, NULL, NULL, 4, NONE }
635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662
};

static void
test_dir_non_existent (void)
{
  TestData data;
  GError *error = NULL;

  data.step = 0;
  data.events = NULL;

  data.file = g_file_new_for_path ("nosuchdir/nosuchfile");
  data.monitor = g_file_monitor_file (data.file, G_FILE_MONITOR_WATCH_MOVES, NULL, &error);
  g_assert_no_error (error);

  g_file_monitor_set_rate_limit (data.monitor, 200);
  g_signal_connect (data.monitor, "changed", G_CALLBACK (monitor_changed), &data);

  data.loop = g_main_loop_new (NULL, TRUE);

  /* we need a long timeout here, since the inotify implementation only scans
   * for missing files every 4 seconds.
   */
  g_timeout_add (5000, nodir_step, &data);

  g_main_loop_run (data.loop);

  /*output_events (data.events);*/
663 664 665 666
  check_expected_events (nodir_output,
                         G_N_ELEMENTS (nodir_output),
                         data.events,
                         get_environment (data.monitor));
667 668 669 670 671 672 673

  g_list_free_full (data.events, (GDestroyNotify)free_recorded_event);
  g_main_loop_unref (data.loop);
  g_object_unref (data.monitor);
  g_object_unref (data.file);
}

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
static gboolean
cross_dir_step (gpointer user_data)
{
  TestData *data = user_data;
  GFile *file, *file2;
  GError *error = NULL;

  switch (data[0].step)
    {
    case 0:
      record_event (&data[0], -1, NULL, NULL, 0);
      record_event (&data[1], -1, NULL, NULL, 0);
      file = g_file_get_child (data[1].file, "a");
      g_file_replace_contents (file, "step 0", 6, NULL, FALSE, G_FILE_CREATE_NONE, NULL, NULL, &error);
      g_assert_no_error (error);
      g_object_unref (file);
      break;
    case 1:
      record_event (&data[0], -1, NULL, NULL, 1);
      record_event (&data[1], -1, NULL, NULL, 1);
      file = g_file_get_child (data[1].file, "a");
      file2 = g_file_get_child (data[0].file, "a");
      g_file_move (file, file2, 0, NULL, NULL, NULL, &error);
      g_assert_no_error (error);
      g_object_unref (file);
      g_object_unref (file2);
      break;
    case 2:
      record_event (&data[0], -1, NULL, NULL, 2);
      record_event (&data[1], -1, NULL, NULL, 2);
      file2 = g_file_get_child (data[0].file, "a");
      g_file_delete (file2, NULL, NULL);
      g_file_delete (data[0].file, NULL, NULL);
      g_file_delete (data[1].file, NULL, NULL);
      g_object_unref (file2);
      break;
    case 3:
      record_event (&data[0], -1, NULL, NULL, 3);
      record_event (&data[1], -1, NULL, NULL, 3);
      g_main_loop_quit (data->loop);
      return G_SOURCE_REMOVE;
    }

  data->step++;

  return G_SOURCE_CONTINUE;
}

static RecordedEvent cross_dir_a_output[] = {
723 724 725 726 727 728 729 730
  { -1, NULL, NULL, 0, NONE },
  { -1, NULL, NULL, 1, NONE },
  { G_FILE_MONITOR_EVENT_CREATED, "a", NULL, -1, NONE },
  { G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT, "a", NULL, -1, KQUEUE },
  { -1, NULL, NULL, 2, NONE },
  { G_FILE_MONITOR_EVENT_DELETED, "a", NULL, -1, NONE },
  { G_FILE_MONITOR_EVENT_DELETED, "cross_dir_a", NULL, -1, NONE },
  { -1, NULL, NULL, 3, NONE },
731 732 733
};

static RecordedEvent cross_dir_b_output[] = {
734 735 736 737 738 739 740 741 742
  { -1, NULL, NULL, 0, NONE },
  { G_FILE_MONITOR_EVENT_CREATED, "a", NULL, -1, NONE },
  { G_FILE_MONITOR_EVENT_CHANGED, "a", NULL, -1, KQUEUE },
  { G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT, "a", NULL, -1, KQUEUE },
  { -1, NULL, NULL, 1, NONE },
  { G_FILE_MONITOR_EVENT_MOVED_OUT, "a", "a", -1, NONE },
  { -1, NULL, NULL, 2, NONE },
  { G_FILE_MONITOR_EVENT_DELETED, "cross_dir_b", NULL, -1, NONE },
  { -1, NULL, NULL, 3, NONE },
743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788
};
static void
test_cross_dir_moves (void)
{
  GError *error = NULL;
  TestData data[2];

  data[0].step = 0;
  data[0].events = NULL;

  data[0].file = g_file_new_for_path ("cross_dir_a");
  g_file_delete (data[0].file, NULL, NULL);
  g_file_make_directory (data[0].file, NULL, &error);

  data[0].monitor = g_file_monitor_directory (data[0].file, 0, NULL, &error);
  g_assert_no_error (error);

  g_file_monitor_set_rate_limit (data[0].monitor, 200);
  g_signal_connect (data[0].monitor, "changed", G_CALLBACK (monitor_changed), &data[0]);

  data[1].step = 0;
  data[1].events = NULL;

  data[1].file = g_file_new_for_path ("cross_dir_b");
  g_file_delete (data[1].file, NULL, NULL);
  g_file_make_directory (data[1].file, NULL, &error);

  data[1].monitor = g_file_monitor_directory (data[1].file, G_FILE_MONITOR_WATCH_MOVES, NULL, &error);
  g_assert_no_error (error);

  g_file_monitor_set_rate_limit (data[1].monitor, 200);
  g_signal_connect (data[1].monitor, "changed", G_CALLBACK (monitor_changed), &data[1]);

  data[0].loop = g_main_loop_new (NULL, TRUE);

  g_timeout_add (500, cross_dir_step, data);

  g_main_loop_run (data[0].loop);

#if 0
  g_print ("monitor a:\n");
  output_events (data[0].events);
  g_print ("monitor b:\n");
  output_events (data[1].events);
#endif

789 790 791 792 793 794 795 796
  check_expected_events (cross_dir_a_output,
                         G_N_ELEMENTS (cross_dir_a_output),
                         data[0].events,
                         get_environment (data[0].monitor));
  check_expected_events (cross_dir_b_output,
                         G_N_ELEMENTS (cross_dir_b_output),
                         data[1].events,
                         get_environment (data[1].monitor));
797 798 799 800 801 802 803 804 805 806 807

  g_list_free_full (data[0].events, (GDestroyNotify)free_recorded_event);
  g_main_loop_unref (data[0].loop);
  g_object_unref (data[0].monitor);
  g_object_unref (data[0].file);

  g_list_free_full (data[1].events, (GDestroyNotify)free_recorded_event);
  g_object_unref (data[1].monitor);
  g_object_unref (data[1].file);
}

808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892
static gboolean
file_hard_links_step (gpointer user_data)
{
  gboolean retval = G_SOURCE_CONTINUE;
  TestData *data = user_data;
  GError *error = NULL;

  gchar *filename = g_file_get_path (data->file);
  gchar *hard_link_name = g_strdup_printf ("%s2", filename);
  GFile *hard_link_file = g_file_new_for_path (hard_link_name);

  switch (data->step)
    {
    case 0:
      record_event (data, -1, NULL, NULL, 0);
      g_output_stream_write_all (G_OUTPUT_STREAM (data->output_stream),
                                 "hello, step 0", 13, NULL, NULL, &error);
      g_assert_no_error (error);
      g_output_stream_close (G_OUTPUT_STREAM (data->output_stream), NULL, &error);
      g_assert_no_error (error);
      break;
    case 1:
      record_event (data, -1, NULL, NULL, 1);
      g_file_replace_contents (data->file, "step 1", 6, NULL, FALSE,
                               G_FILE_CREATE_NONE, NULL, NULL, &error);
      g_assert_no_error (error);
      break;
    case 2:
      record_event (data, -1, NULL, NULL, 2);
#ifdef HAVE_LINK
      if (link (filename, hard_link_name) < 0)
        {
          g_error ("link(%s, %s) failed: %s", filename, hard_link_name, g_strerror (errno));
        }
#endif  /* HAVE_LINK */
      break;
    case 3:
      record_event (data, -1, NULL, NULL, 3);
#ifdef HAVE_LINK
      {
        GOutputStream *hard_link_stream = NULL;

        /* Deliberately don’t do an atomic swap on the hard-linked file. */
        hard_link_stream = G_OUTPUT_STREAM (g_file_append_to (hard_link_file,
                                                              G_FILE_CREATE_NONE,
                                                              NULL, &error));
        g_assert_no_error (error);
        g_output_stream_write_all (hard_link_stream, " step 3", 7, NULL, NULL, &error);
        g_assert_no_error (error);
        g_output_stream_close (hard_link_stream, NULL, &error);
        g_assert_no_error (error);
        g_object_unref (hard_link_stream);
      }
#endif  /* HAVE_LINK */
      break;
    case 4:
      record_event (data, -1, NULL, NULL, 4);
      g_file_delete (data->file, NULL, &error);
      g_assert_no_error (error);
      break;
    case 5:
      record_event (data, -1, NULL, NULL, 5);
#ifdef HAVE_LINK
      g_file_delete (hard_link_file, NULL, &error);
      g_assert_no_error (error);
#endif  /* HAVE_LINK */
      break;
    case 6:
      record_event (data, -1, NULL, NULL, 6);
      g_main_loop_quit (data->loop);
      retval = G_SOURCE_REMOVE;
      break;
    }

  if (retval != G_SOURCE_REMOVE)
    data->step++;

  g_object_unref (hard_link_file);
  g_free (hard_link_name);
  g_free (filename);

  return retval;
}

static RecordedEvent file_hard_links_output[] = {
893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912
  { -1, NULL, NULL, 0, NONE },
  { G_FILE_MONITOR_EVENT_CHANGED, "testfilemonitor.db", NULL, -1, NONE },
  { G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT, "testfilemonitor.db", NULL, -1, NONE },
  { -1, NULL, NULL, 1, NONE },
  { G_FILE_MONITOR_EVENT_RENAMED, (gchar*)DONT_CARE /* .goutputstream-XXXXXX */, "testfilemonitor.db", -1, NONE },
  { -1, NULL, NULL, 2, NONE },
  { -1, NULL, NULL, 3, NONE },
  /* Kqueue is based on file descriptors. You can get events from all hard
   * links by just monitoring one open file descriptor, and it is not possible
   * to know whether it is done on the file name we use to open the file. Since
   * the hard link count of 'testfilemonitor.db' is 2, it is expected to see
   * two 'DELETED' events reported here. You have to call 'unlink' twice on
   * different file names to remove 'testfilemonitor.db' from the file system,
   * and each 'unlink' call generates a 'DELETED' event. */
  { G_FILE_MONITOR_EVENT_CHANGED, "testfilemonitor.db", NULL, -1, INOTIFY },
  { -1, NULL, NULL, 4, NONE },
  { G_FILE_MONITOR_EVENT_DELETED, "testfilemonitor.db", NULL, -1, NONE },
  { -1, NULL, NULL, 5, NONE },
  { G_FILE_MONITOR_EVENT_DELETED, "testfilemonitor.db", NULL, -1, INOTIFY },
  { -1, NULL, NULL, 6, NONE },
913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957
};

static void
test_file_hard_links (void)
{
  GError *error = NULL;
  TestData data;

  g_test_bug ("755721");

#ifdef HAVE_LINK
  g_test_message ("Running with hard link tests");
#else  /* if !HAVE_LINK */
  g_test_message ("Running without hard link tests");
#endif  /* !HAVE_LINK */

  data.step = 0;
  data.events = NULL;

  /* Create a file which exists and is not a directory. */
  data.file = g_file_new_for_path ("testfilemonitor.db");
  data.output_stream = g_file_replace (data.file, NULL, FALSE,
                                       G_FILE_CREATE_NONE, NULL, &error);
  g_assert_no_error (error);

  /* Monitor it. Creating the monitor should not crash (bug #755721). */
  data.monitor = g_file_monitor_file (data.file,
                                      G_FILE_MONITOR_WATCH_MOUNTS |
                                      G_FILE_MONITOR_WATCH_MOVES |
                                      G_FILE_MONITOR_WATCH_HARD_LINKS,
                                      NULL,
                                      &error);
  g_assert_no_error (error);
  g_assert_nonnull (data.monitor);

  /* Change the file a bit. */
  g_file_monitor_set_rate_limit (data.monitor, 200);
  g_signal_connect (data.monitor, "changed", (GCallback) monitor_changed, &data);

  data.loop = g_main_loop_new (NULL, TRUE);
  g_timeout_add (500, file_hard_links_step, &data);
  g_main_loop_run (data.loop);

  /* output_events (data.events); */
  check_expected_events (file_hard_links_output,
958 959 960
                         G_N_ELEMENTS (file_hard_links_output),
                         data.events,
                         get_environment (data.monitor));
961 962 963 964 965 966 967 968

  g_list_free_full (data.events, (GDestroyNotify) free_recorded_event);
  g_main_loop_unref (data.loop);
  g_object_unref (data.monitor);
  g_object_unref (data.file);
  g_object_unref (data.output_stream);
}

969 970 971 972 973
int
main (int argc, char *argv[])
{
  g_test_init (&argc, &argv, NULL);

974 975
  g_test_bug_base ("https://bugzilla.gnome.org/show_bug.cgi?id=");

976
  g_test_add_func ("/monitor/atomic-replace", test_atomic_replace);
977 978
  g_test_add_func ("/monitor/file-changes", test_file_changes);
  g_test_add_func ("/monitor/dir-monitor", test_dir_monitor);
979
  g_test_add_func ("/monitor/dir-not-existent", test_dir_non_existent);
980
  g_test_add_func ("/monitor/cross-dir-moves", test_cross_dir_moves);
981
  g_test_add_func ("/monitor/file/hard-links", test_file_hard_links);
982 983 984

  return g_test_run ();
}