From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Jameson Nash <vtjnash@gmail.com>
Date: Sat, 7 Sep 2019 20:45:39 -0400
Subject: fsevents: regression in watching /
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

This case got lost by accident in
https://github.com/libuv/libuv/pull/2082,
preventing the realpath `/` from ever matching.

Fixes: https://github.com/nodejs/node/issues/28917
PR-URL: https://github.com/libuv/libuv/pull/2460
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Saúl Ibarra Corretgé <s@saghul.net>

diff --git a/deps/uv/src/unix/fsevents.c b/deps/uv/src/unix/fsevents.c
index ddacda31fef87eee131fc2ee2ff46cc88be429d9..deeaa63d4730de9aa17ee87923acd96d6507a55d 100644
--- a/deps/uv/src/unix/fsevents.c
+++ b/deps/uv/src/unix/fsevents.c
@@ -263,10 +263,12 @@ static void uv__fsevents_event_cb(ConstFSEventStreamRef streamRef,
       if (len < handle->realpath_len)
         continue;
 
+      /* Make sure that realpath actually named a directory,
+       * (unless watching root, which alone keeps a trailing slash on the realpath)
+       * or that we matched the whole string */
       if (handle->realpath_len != len &&
+          handle->realpath_len > 1 &&
           path[handle->realpath_len] != '/')
-        /* Make sure that realpath actually named a directory,
-         * or that we matched the whole string */
         continue;
 
       if (memcmp(path, handle->realpath, handle->realpath_len) != 0)
diff --git a/deps/uv/test/test-fs-event.c b/deps/uv/test/test-fs-event.c
index 4b8bb1ef03e54407cba8eef85179039632cc3f28..7725c3af94edd5d62bb960912262d38aefa6676e 100644
--- a/deps/uv/test/test-fs-event.c
+++ b/deps/uv/test/test-fs-event.c
@@ -47,6 +47,7 @@ static const char file_prefix[] = "fsevent-";
 static const int fs_event_file_count = 16;
 #if defined(__APPLE__) || defined(_WIN32)
 static const char file_prefix_in_subdir[] = "subdir";
+static int fs_multievent_cb_called;
 #endif
 static uv_timer_t timer;
 static int timer_cb_called;
@@ -280,7 +281,7 @@ static void fs_event_cb_dir_multi_file_in_subdir(uv_fs_event_t* handle,
   if (filename && strcmp(filename, file_prefix_in_subdir) == 0)
     return;
 #endif
-  fs_event_cb_called++;
+  fs_multievent_cb_called++;
   ASSERT(handle == &fs_event);
   ASSERT(status == 0);
   ASSERT(events == UV_CHANGE || events == UV_RENAME);
@@ -298,7 +299,7 @@ static void fs_event_cb_dir_multi_file_in_subdir(uv_fs_event_t* handle,
   if (fs_event_created + fs_event_removed == fs_event_file_count) {
     /* Once we've processed all create events, delete all files */
     ASSERT(0 == uv_timer_start(&timer, fs_event_unlink_files_in_subdir, 1, 0));
-  } else if (fs_event_cb_called == 2 * fs_event_file_count) {
+  } else if (fs_multievent_cb_called == 2 * fs_event_file_count) {
     /* Once we've processed all create and delete events, stop watching */
     uv_close((uv_handle_t*) &timer, close_cb);
     uv_close((uv_handle_t*) handle, close_cb);
@@ -393,6 +394,21 @@ static void timer_cb_watch_twice(uv_timer_t* handle) {
   uv_close((uv_handle_t*) handle, NULL);
 }
 
+static void fs_event_cb_close(uv_fs_event_t* handle,
+                              const char* filename,
+                              int events,
+                              int status) {
+  ASSERT(status == 0);
+
+  ASSERT(fs_event_cb_called < 3);
+  ++fs_event_cb_called;
+
+  if (fs_event_cb_called == 3) {
+    uv_close((uv_handle_t*) handle, close_cb);
+  }
+}
+
+
 TEST_IMPL(fs_event_watch_dir) {
 #if defined(NO_FS_EVENTS)
   RETURN_SKIP(NO_FS_EVENTS);
@@ -434,10 +450,12 @@ TEST_IMPL(fs_event_watch_dir) {
   return 0;
 }
 
+
 TEST_IMPL(fs_event_watch_dir_recursive) {
 #if defined(__APPLE__) || defined(_WIN32)
   uv_loop_t* loop;
   int r;
+  uv_fs_event_t fs_event_root;
 
   /* Setup */
   loop = uv_default_loop();
@@ -451,17 +469,37 @@ TEST_IMPL(fs_event_watch_dir_recursive) {
 
   r = uv_fs_event_init(loop, &fs_event);
   ASSERT(r == 0);
-  r = uv_fs_event_start(&fs_event, fs_event_cb_dir_multi_file_in_subdir, "watch_dir", UV_FS_EVENT_RECURSIVE);
+  r = uv_fs_event_start(&fs_event,
+                        fs_event_cb_dir_multi_file_in_subdir,
+                        "watch_dir",
+                        UV_FS_EVENT_RECURSIVE);
   ASSERT(r == 0);
   r = uv_timer_init(loop, &timer);
   ASSERT(r == 0);
   r = uv_timer_start(&timer, fs_event_create_files_in_subdir, 100, 0);
   ASSERT(r == 0);
 
+#ifndef _WIN32
+  /* Also try to watch the root directory.
+   * This will be noisier, so we're just checking for any couple events to happen. */
+  r = uv_fs_event_init(loop, &fs_event_root);
+  ASSERT(r == 0);
+  r = uv_fs_event_start(&fs_event_root,
+                        fs_event_cb_close,
+                        "/",
+                        UV_FS_EVENT_RECURSIVE);
+  ASSERT(r == 0);
+#else
+  fs_event_cb_called += 3;
+  close_cb_called += 1;
+  (void)fs_event_root;
+#endif
+
   uv_run(loop, UV_RUN_DEFAULT);
 
-  ASSERT(fs_event_cb_called == fs_event_created + fs_event_removed);
-  ASSERT(close_cb_called == 2);
+  ASSERT(fs_multievent_cb_called == fs_event_created + fs_event_removed);
+  ASSERT(fs_event_cb_called == 3);
+  ASSERT(close_cb_called == 3);
 
   /* Cleanup */
   fs_event_unlink_files_in_subdir(NULL);
@@ -870,18 +908,6 @@ TEST_IMPL(fs_event_close_with_pending_event) {
   return 0;
 }
 
-static void fs_event_cb_close(uv_fs_event_t* handle, const char* filename,
-    int events, int status) {
-  ASSERT(status == 0);
-
-  ASSERT(fs_event_cb_called < 3);
-  ++fs_event_cb_called;
-
-  if (fs_event_cb_called == 3) {
-    uv_close((uv_handle_t*) handle, close_cb);
-  }
-}
-
 TEST_IMPL(fs_event_close_in_callback) {
 #if defined(NO_FS_EVENTS)
   RETURN_SKIP(NO_FS_EVENTS);
