fsevents-stop-using-fsevents-to-watch-files.patch 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
  2. From: Jameson Nash <[email protected]>
  3. Date: Sat, 7 Sep 2019 14:55:40 -0400
  4. Subject: fsevents: stop using fsevents to watch files
  5. MIME-Version: 1.0
  6. Content-Type: text/plain; charset=UTF-8
  7. Content-Transfer-Encoding: 8bit
  8. Goes back to just using it to watch folders,
  9. but keeps the other logic changes around.
  10. Refs: https://github.com/libuv/libuv/pull/387
  11. Refs: https://github.com/libuv/libuv/pull/2082
  12. Refs: https://github.com/libuv/libuv/pull/1572
  13. Refs: https://github.com/nodejs/node/issues/29460
  14. Fixes: https://github.com/libuv/libuv/issues/2488
  15. Closes: https://github.com/libuv/libuv/pull/2452
  16. PR-URL: https://github.com/libuv/libuv/pull/2459
  17. Reviewed-By: Ben Noordhuis <[email protected]>
  18. Reviewed-By: Saúl Ibarra Corretgé <[email protected]>
  19. diff --git a/deps/uv/src/unix/kqueue.c b/deps/uv/src/unix/kqueue.c
  20. index c04e7a485cf992beec501144e04ff068c17b9494..ad09f4031318cafe08faed3f0a6373e2bb598672 100644
  21. --- a/deps/uv/src/unix/kqueue.c
  22. +++ b/deps/uv/src/unix/kqueue.c
  23. @@ -454,10 +454,26 @@ int uv_fs_event_start(uv_fs_event_t* handle,
  24. const char* path,
  25. unsigned int flags) {
  26. int fd;
  27. +#if defined(__APPLE__) && MAC_OS_X_VERSION_MAX_ALLOWED >= 1070
  28. + struct stat statbuf;
  29. +#endif
  30. if (uv__is_active(handle))
  31. return UV_EINVAL;
  32. + handle->cb = cb;
  33. + handle->path = uv__strdup(path);
  34. + if (handle->path == NULL)
  35. + return UV_ENOMEM;
  36. +
  37. + /* TODO open asynchronously - but how do we report back errors? */
  38. + fd = open(handle->path, O_RDONLY);
  39. + if (fd == -1) {
  40. + uv__free(handle->path);
  41. + handle->path = NULL;
  42. + return UV__ERR(errno);
  43. + }
  44. +
  45. #if defined(__APPLE__) && MAC_OS_X_VERSION_MAX_ALLOWED >= 1070
  46. /* Nullify field to perform checks later */
  47. handle->cf_cb = NULL;
  48. @@ -465,14 +481,17 @@ int uv_fs_event_start(uv_fs_event_t* handle,
  49. handle->realpath_len = 0;
  50. handle->cf_flags = flags;
  51. + if (fstat(fd, &statbuf))
  52. + goto fallback;
  53. + /* FSEvents works only with directories */
  54. + if (!(statbuf.st_mode & S_IFDIR))
  55. + goto fallback;
  56. +
  57. if (!uv__has_forked_with_cfrunloop) {
  58. int r;
  59. - /* The fallback fd is not used */
  60. + /* The fallback fd is no longer needed */
  61. + uv__close_nocheckstdio(fd);
  62. handle->event_watcher.fd = -1;
  63. - handle->path = uv__strdup(path);
  64. - if (handle->path == NULL)
  65. - return UV_ENOMEM;
  66. - handle->cb = cb;
  67. r = uv__fsevents_init(handle);
  68. if (r == 0) {
  69. uv__handle_start(handle);
  70. @@ -482,20 +501,9 @@ int uv_fs_event_start(uv_fs_event_t* handle,
  71. }
  72. return r;
  73. }
  74. +fallback:
  75. #endif /* #if defined(__APPLE__) && MAC_OS_X_VERSION_MAX_ALLOWED >= 1070 */
  76. - /* TODO open asynchronously - but how do we report back errors? */
  77. - fd = open(path, O_RDONLY);
  78. - if (fd == -1)
  79. - return UV__ERR(errno);
  80. -
  81. - handle->path = uv__strdup(path);
  82. - if (handle->path == NULL) {
  83. - uv__close_nocheckstdio(fd);
  84. - return UV_ENOMEM;
  85. - }
  86. -
  87. - handle->cb = cb;
  88. uv__handle_start(handle);
  89. uv__io_init(&handle->event_watcher, uv__fs_event, fd);
  90. uv__io_start(handle->loop, &handle->event_watcher, POLLIN);
  91. @@ -514,7 +522,7 @@ int uv_fs_event_stop(uv_fs_event_t* handle) {
  92. uv__handle_stop(handle);
  93. #if defined(__APPLE__) && MAC_OS_X_VERSION_MAX_ALLOWED >= 1070
  94. - if (!uv__has_forked_with_cfrunloop)
  95. + if (!uv__has_forked_with_cfrunloop && handle->cf_cb != NULL)
  96. r = uv__fsevents_close(handle);
  97. #endif
  98. diff --git a/deps/uv/test/test-fs-event.c b/deps/uv/test/test-fs-event.c
  99. index ea34bd63a70625c3e2c60d5a1bbb087c5f0bbb2e..4b8bb1ef03e54407cba8eef85179039632cc3f28 100644
  100. --- a/deps/uv/test/test-fs-event.c
  101. +++ b/deps/uv/test/test-fs-event.c
  102. @@ -656,6 +656,12 @@ TEST_IMPL(fs_event_watch_file_current_dir) {
  103. /* Setup */
  104. remove("watch_file");
  105. create_file("watch_file");
  106. +#if defined(__APPLE__) && !defined(MAC_OS_X_VERSION_10_12)
  107. + /* Empirically, kevent seems to (sometimes) report the preceeding
  108. + * create_file events prior to macOS 10.11.6 in the subsequent fs_event_start
  109. + * So let the system settle before running the test. */
  110. + uv_sleep(1100);
  111. +#endif
  112. r = uv_fs_event_init(loop, &fs_event);
  113. ASSERT(r == 0);