test-fs-event.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173
  1. /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to
  5. * deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  7. * sell copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  18. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  19. * IN THE SOFTWARE.
  20. */
  21. #include "uv.h"
  22. #include "task.h"
  23. #include <string.h>
  24. #include <fcntl.h>
  25. #if defined(__APPLE__) && !TARGET_OS_IPHONE
  26. # include <AvailabilityMacros.h>
  27. #endif
  28. #ifndef HAVE_KQUEUE
  29. # if defined(__APPLE__) || \
  30. defined(__DragonFly__) || \
  31. defined(__FreeBSD__) || \
  32. defined(__FreeBSD_kernel__) || \
  33. defined(__OpenBSD__) || \
  34. defined(__NetBSD__)
  35. # define HAVE_KQUEUE 1
  36. # endif
  37. #endif
  38. #if defined(__arm__)/* Increase the timeout so the test passes on arm CI bots */
  39. # define CREATE_TIMEOUT 100
  40. #else
  41. # define CREATE_TIMEOUT 1
  42. #endif
  43. static uv_fs_event_t fs_event;
  44. static const char file_prefix[] = "fsevent-";
  45. static const int fs_event_file_count = 16;
  46. #if defined(__APPLE__) || defined(_WIN32)
  47. static const char file_prefix_in_subdir[] = "subdir";
  48. static int fs_multievent_cb_called;
  49. #endif
  50. static uv_timer_t timer;
  51. static int timer_cb_called;
  52. static int close_cb_called;
  53. static int fs_event_created;
  54. static int fs_event_removed;
  55. static int fs_event_cb_called;
  56. #if defined(PATH_MAX)
  57. static char fs_event_filename[PATH_MAX];
  58. #else
  59. static char fs_event_filename[1024];
  60. #endif /* defined(PATH_MAX) */
  61. static int timer_cb_touch_called;
  62. static int timer_cb_exact_called;
  63. static void fs_event_fail(uv_fs_event_t* handle,
  64. const char* filename,
  65. int events,
  66. int status) {
  67. ASSERT(0 && "should never be called");
  68. }
  69. static void create_dir(const char* name) {
  70. int r;
  71. uv_fs_t req;
  72. r = uv_fs_mkdir(NULL, &req, name, 0755, NULL);
  73. ASSERT(r == 0 || r == UV_EEXIST);
  74. uv_fs_req_cleanup(&req);
  75. }
  76. static void create_file(const char* name) {
  77. int r;
  78. uv_file file;
  79. uv_fs_t req;
  80. r = uv_fs_open(NULL, &req, name, O_WRONLY | O_CREAT, S_IWUSR | S_IRUSR, NULL);
  81. ASSERT(r >= 0);
  82. file = r;
  83. uv_fs_req_cleanup(&req);
  84. r = uv_fs_close(NULL, &req, file, NULL);
  85. ASSERT(r == 0);
  86. uv_fs_req_cleanup(&req);
  87. }
  88. static void touch_file(const char* name) {
  89. int r;
  90. uv_file file;
  91. uv_fs_t req;
  92. uv_buf_t buf;
  93. r = uv_fs_open(NULL, &req, name, O_RDWR, 0, NULL);
  94. ASSERT(r >= 0);
  95. file = r;
  96. uv_fs_req_cleanup(&req);
  97. buf = uv_buf_init("foo", 4);
  98. r = uv_fs_write(NULL, &req, file, &buf, 1, -1, NULL);
  99. ASSERT(r >= 0);
  100. uv_fs_req_cleanup(&req);
  101. r = uv_fs_close(NULL, &req, file, NULL);
  102. ASSERT(r == 0);
  103. uv_fs_req_cleanup(&req);
  104. }
  105. static void close_cb(uv_handle_t* handle) {
  106. ASSERT(handle != NULL);
  107. close_cb_called++;
  108. }
  109. static void fail_cb(uv_fs_event_t* handle,
  110. const char* path,
  111. int events,
  112. int status) {
  113. ASSERT(0 && "fail_cb called");
  114. }
  115. static void fs_event_cb_dir(uv_fs_event_t* handle, const char* filename,
  116. int events, int status) {
  117. ++fs_event_cb_called;
  118. ASSERT(handle == &fs_event);
  119. ASSERT(status == 0);
  120. ASSERT(events == UV_CHANGE);
  121. #if defined(__APPLE__) || defined(_WIN32) || defined(__linux__)
  122. ASSERT(strcmp(filename, "file1") == 0);
  123. #else
  124. ASSERT(filename == NULL || strcmp(filename, "file1") == 0);
  125. #endif
  126. ASSERT(0 == uv_fs_event_stop(handle));
  127. uv_close((uv_handle_t*)handle, close_cb);
  128. }
  129. static const char* fs_event_get_filename(int i) {
  130. snprintf(fs_event_filename,
  131. sizeof(fs_event_filename),
  132. "watch_dir/%s%d",
  133. file_prefix,
  134. i);
  135. return fs_event_filename;
  136. }
  137. static void fs_event_create_files(uv_timer_t* handle) {
  138. /* Make sure we're not attempting to create files we do not intend */
  139. ASSERT(fs_event_created < fs_event_file_count);
  140. /* Create the file */
  141. create_file(fs_event_get_filename(fs_event_created));
  142. if (++fs_event_created < fs_event_file_count) {
  143. /* Create another file on a different event loop tick. We do it this way
  144. * to avoid fs events coalescing into one fs event. */
  145. ASSERT(0 == uv_timer_start(&timer,
  146. fs_event_create_files,
  147. CREATE_TIMEOUT,
  148. 0));
  149. }
  150. }
  151. static void fs_event_unlink_files(uv_timer_t* handle) {
  152. int r;
  153. int i;
  154. /* NOTE: handle might be NULL if invoked not as timer callback */
  155. if (handle == NULL) {
  156. /* Unlink all files */
  157. for (i = 0; i < 16; i++) {
  158. r = remove(fs_event_get_filename(i));
  159. if (handle != NULL)
  160. ASSERT(r == 0);
  161. }
  162. } else {
  163. /* Make sure we're not attempting to remove files we do not intend */
  164. ASSERT(fs_event_removed < fs_event_file_count);
  165. /* Remove the file */
  166. ASSERT(0 == remove(fs_event_get_filename(fs_event_removed)));
  167. if (++fs_event_removed < fs_event_file_count) {
  168. /* Remove another file on a different event loop tick. We do it this way
  169. * to avoid fs events coalescing into one fs event. */
  170. ASSERT(0 == uv_timer_start(&timer, fs_event_unlink_files, 1, 0));
  171. }
  172. }
  173. }
  174. static void fs_event_cb_dir_multi_file(uv_fs_event_t* handle,
  175. const char* filename,
  176. int events,
  177. int status) {
  178. fs_event_cb_called++;
  179. ASSERT(handle == &fs_event);
  180. ASSERT(status == 0);
  181. ASSERT(events == UV_CHANGE || events == UV_RENAME);
  182. #if defined(__APPLE__) || defined(_WIN32) || defined(__linux__)
  183. ASSERT(strncmp(filename, file_prefix, sizeof(file_prefix) - 1) == 0);
  184. #else
  185. ASSERT(filename == NULL ||
  186. strncmp(filename, file_prefix, sizeof(file_prefix) - 1) == 0);
  187. #endif
  188. if (fs_event_created + fs_event_removed == fs_event_file_count) {
  189. /* Once we've processed all create events, delete all files */
  190. ASSERT(0 == uv_timer_start(&timer, fs_event_unlink_files, 1, 0));
  191. } else if (fs_event_cb_called == 2 * fs_event_file_count) {
  192. /* Once we've processed all create and delete events, stop watching */
  193. uv_close((uv_handle_t*) &timer, close_cb);
  194. uv_close((uv_handle_t*) handle, close_cb);
  195. }
  196. }
  197. #if defined(__APPLE__) || defined(_WIN32)
  198. static const char* fs_event_get_filename_in_subdir(int i) {
  199. snprintf(fs_event_filename,
  200. sizeof(fs_event_filename),
  201. "watch_dir/subdir/%s%d",
  202. file_prefix,
  203. i);
  204. return fs_event_filename;
  205. }
  206. static void fs_event_create_files_in_subdir(uv_timer_t* handle) {
  207. /* Make sure we're not attempting to create files we do not intend */
  208. ASSERT(fs_event_created < fs_event_file_count);
  209. /* Create the file */
  210. create_file(fs_event_get_filename_in_subdir(fs_event_created));
  211. if (++fs_event_created < fs_event_file_count) {
  212. /* Create another file on a different event loop tick. We do it this way
  213. * to avoid fs events coalescing into one fs event. */
  214. ASSERT(0 == uv_timer_start(&timer, fs_event_create_files_in_subdir, 1, 0));
  215. }
  216. }
  217. static void fs_event_unlink_files_in_subdir(uv_timer_t* handle) {
  218. int r;
  219. int i;
  220. /* NOTE: handle might be NULL if invoked not as timer callback */
  221. if (handle == NULL) {
  222. /* Unlink all files */
  223. for (i = 0; i < 16; i++) {
  224. r = remove(fs_event_get_filename_in_subdir(i));
  225. if (handle != NULL)
  226. ASSERT(r == 0);
  227. }
  228. } else {
  229. /* Make sure we're not attempting to remove files we do not intend */
  230. ASSERT(fs_event_removed < fs_event_file_count);
  231. /* Remove the file */
  232. ASSERT(0 == remove(fs_event_get_filename_in_subdir(fs_event_removed)));
  233. if (++fs_event_removed < fs_event_file_count) {
  234. /* Remove another file on a different event loop tick. We do it this way
  235. * to avoid fs events coalescing into one fs event. */
  236. ASSERT(0 == uv_timer_start(&timer, fs_event_unlink_files_in_subdir, 1, 0));
  237. }
  238. }
  239. }
  240. static void fs_event_cb_dir_multi_file_in_subdir(uv_fs_event_t* handle,
  241. const char* filename,
  242. int events,
  243. int status) {
  244. #ifdef _WIN32
  245. /* Each file created (or deleted) will cause this callback to be called twice
  246. * under Windows: once with the name of the file, and second time with the
  247. * name of the directory. We will ignore the callback for the directory
  248. * itself. */
  249. if (filename && strcmp(filename, file_prefix_in_subdir) == 0)
  250. return;
  251. #endif
  252. /* It may happen that the "subdir" creation event is captured even though
  253. * we started watching after its actual creation.
  254. */
  255. if (strcmp(filename, "subdir") == 0)
  256. return;
  257. fs_multievent_cb_called++;
  258. ASSERT(handle == &fs_event);
  259. ASSERT(status == 0);
  260. ASSERT(events == UV_CHANGE || events == UV_RENAME);
  261. #if defined(__APPLE__) || defined(_WIN32) || defined(__linux__)
  262. ASSERT(strncmp(filename,
  263. file_prefix_in_subdir,
  264. sizeof(file_prefix_in_subdir) - 1) == 0);
  265. #else
  266. ASSERT(filename == NULL ||
  267. strncmp(filename,
  268. file_prefix_in_subdir,
  269. sizeof(file_prefix_in_subdir) - 1) == 0);
  270. #endif
  271. if (fs_event_created == fs_event_file_count &&
  272. fs_multievent_cb_called == fs_event_created) {
  273. /* Once we've processed all create events, delete all files */
  274. ASSERT(0 == uv_timer_start(&timer, fs_event_unlink_files_in_subdir, 1, 0));
  275. } else if (fs_multievent_cb_called == 2 * fs_event_file_count) {
  276. /* Once we've processed all create and delete events, stop watching */
  277. ASSERT(fs_event_removed == fs_event_file_count);
  278. uv_close((uv_handle_t*) &timer, close_cb);
  279. uv_close((uv_handle_t*) handle, close_cb);
  280. }
  281. }
  282. #endif
  283. static void fs_event_cb_file(uv_fs_event_t* handle, const char* filename,
  284. int events, int status) {
  285. ++fs_event_cb_called;
  286. ASSERT(handle == &fs_event);
  287. ASSERT(status == 0);
  288. ASSERT(events == UV_CHANGE);
  289. #if defined(__APPLE__) || defined(_WIN32) || defined(__linux__)
  290. ASSERT(strcmp(filename, "file2") == 0);
  291. #else
  292. ASSERT(filename == NULL || strcmp(filename, "file2") == 0);
  293. #endif
  294. ASSERT(0 == uv_fs_event_stop(handle));
  295. uv_close((uv_handle_t*)handle, close_cb);
  296. }
  297. static void timer_cb_close_handle(uv_timer_t* timer) {
  298. uv_handle_t* handle;
  299. ASSERT(timer != NULL);
  300. handle = timer->data;
  301. uv_close((uv_handle_t*)timer, NULL);
  302. uv_close((uv_handle_t*)handle, close_cb);
  303. }
  304. static void fs_event_cb_file_current_dir(uv_fs_event_t* handle,
  305. const char* filename, int events, int status) {
  306. ASSERT(fs_event_cb_called == 0);
  307. ++fs_event_cb_called;
  308. ASSERT(handle == &fs_event);
  309. ASSERT(status == 0);
  310. ASSERT(events == UV_CHANGE);
  311. #if defined(__APPLE__) || defined(_WIN32) || defined(__linux__)
  312. ASSERT(strcmp(filename, "watch_file") == 0);
  313. #else
  314. ASSERT(filename == NULL || strcmp(filename, "watch_file") == 0);
  315. #endif
  316. /* Regression test for SunOS: touch should generate just one event. */
  317. {
  318. static uv_timer_t timer;
  319. uv_timer_init(handle->loop, &timer);
  320. timer.data = handle;
  321. uv_timer_start(&timer, timer_cb_close_handle, 250, 0);
  322. }
  323. }
  324. static void timer_cb_file(uv_timer_t* handle) {
  325. ++timer_cb_called;
  326. if (timer_cb_called == 1) {
  327. touch_file("watch_dir/file1");
  328. } else {
  329. touch_file("watch_dir/file2");
  330. uv_close((uv_handle_t*)handle, close_cb);
  331. }
  332. }
  333. static void timer_cb_touch(uv_timer_t* timer) {
  334. uv_close((uv_handle_t*)timer, NULL);
  335. touch_file("watch_file");
  336. timer_cb_touch_called++;
  337. }
  338. static void timer_cb_exact(uv_timer_t* handle) {
  339. int r;
  340. if (timer_cb_exact_called == 0) {
  341. touch_file("watch_dir/file.js");
  342. } else {
  343. uv_close((uv_handle_t*)handle, NULL);
  344. r = uv_fs_event_stop(&fs_event);
  345. ASSERT(r == 0);
  346. uv_close((uv_handle_t*) &fs_event, NULL);
  347. }
  348. ++timer_cb_exact_called;
  349. }
  350. static void timer_cb_watch_twice(uv_timer_t* handle) {
  351. uv_fs_event_t* handles = handle->data;
  352. uv_close((uv_handle_t*) (handles + 0), NULL);
  353. uv_close((uv_handle_t*) (handles + 1), NULL);
  354. uv_close((uv_handle_t*) handle, NULL);
  355. }
  356. static void fs_event_cb_close(uv_fs_event_t* handle,
  357. const char* filename,
  358. int events,
  359. int status) {
  360. ASSERT(status == 0);
  361. ASSERT(fs_event_cb_called < 3);
  362. ++fs_event_cb_called;
  363. if (fs_event_cb_called == 3) {
  364. uv_close((uv_handle_t*) handle, close_cb);
  365. }
  366. }
  367. TEST_IMPL(fs_event_watch_dir) {
  368. #if defined(NO_FS_EVENTS)
  369. RETURN_SKIP(NO_FS_EVENTS);
  370. #elif defined(__MVS__)
  371. RETURN_SKIP("Directory watching not supported on this platform.");
  372. #endif
  373. uv_loop_t* loop = uv_default_loop();
  374. int r;
  375. /* Setup */
  376. fs_event_unlink_files(NULL);
  377. remove("watch_dir/file2");
  378. remove("watch_dir/file1");
  379. remove("watch_dir/");
  380. create_dir("watch_dir");
  381. r = uv_fs_event_init(loop, &fs_event);
  382. ASSERT(r == 0);
  383. r = uv_fs_event_start(&fs_event, fs_event_cb_dir_multi_file, "watch_dir", 0);
  384. ASSERT(r == 0);
  385. r = uv_timer_init(loop, &timer);
  386. ASSERT(r == 0);
  387. r = uv_timer_start(&timer, fs_event_create_files, 100, 0);
  388. ASSERT(r == 0);
  389. uv_run(loop, UV_RUN_DEFAULT);
  390. ASSERT(fs_event_cb_called == fs_event_created + fs_event_removed);
  391. ASSERT(close_cb_called == 2);
  392. /* Cleanup */
  393. fs_event_unlink_files(NULL);
  394. remove("watch_dir/file2");
  395. remove("watch_dir/file1");
  396. remove("watch_dir/");
  397. MAKE_VALGRIND_HAPPY();
  398. return 0;
  399. }
  400. TEST_IMPL(fs_event_watch_dir_recursive) {
  401. #if defined(__APPLE__) || defined(_WIN32)
  402. uv_loop_t* loop;
  403. int r;
  404. uv_fs_event_t fs_event_root;
  405. /* Setup */
  406. loop = uv_default_loop();
  407. fs_event_unlink_files(NULL);
  408. remove("watch_dir/file2");
  409. remove("watch_dir/file1");
  410. remove("watch_dir/subdir");
  411. remove("watch_dir/");
  412. create_dir("watch_dir");
  413. create_dir("watch_dir/subdir");
  414. r = uv_fs_event_init(loop, &fs_event);
  415. ASSERT(r == 0);
  416. r = uv_fs_event_start(&fs_event,
  417. fs_event_cb_dir_multi_file_in_subdir,
  418. "watch_dir",
  419. UV_FS_EVENT_RECURSIVE);
  420. ASSERT(r == 0);
  421. r = uv_timer_init(loop, &timer);
  422. ASSERT(r == 0);
  423. r = uv_timer_start(&timer, fs_event_create_files_in_subdir, 100, 0);
  424. ASSERT(r == 0);
  425. #ifndef _WIN32
  426. /* Also try to watch the root directory.
  427. * This will be noisier, so we're just checking for any couple events to happen. */
  428. r = uv_fs_event_init(loop, &fs_event_root);
  429. ASSERT(r == 0);
  430. r = uv_fs_event_start(&fs_event_root,
  431. fs_event_cb_close,
  432. "/",
  433. UV_FS_EVENT_RECURSIVE);
  434. ASSERT(r == 0);
  435. #else
  436. fs_event_cb_called += 3;
  437. close_cb_called += 1;
  438. (void)fs_event_root;
  439. #endif
  440. uv_run(loop, UV_RUN_DEFAULT);
  441. ASSERT(fs_multievent_cb_called == fs_event_created + fs_event_removed);
  442. ASSERT(fs_event_cb_called == 3);
  443. ASSERT(close_cb_called == 3);
  444. /* Cleanup */
  445. fs_event_unlink_files_in_subdir(NULL);
  446. remove("watch_dir/file2");
  447. remove("watch_dir/file1");
  448. remove("watch_dir/subdir");
  449. remove("watch_dir/");
  450. MAKE_VALGRIND_HAPPY();
  451. return 0;
  452. #else
  453. RETURN_SKIP("Recursive directory watching not supported on this platform.");
  454. #endif
  455. }
  456. #ifdef _WIN32
  457. TEST_IMPL(fs_event_watch_dir_short_path) {
  458. uv_loop_t* loop;
  459. uv_fs_t req;
  460. int has_shortnames;
  461. int r;
  462. /* Setup */
  463. loop = uv_default_loop();
  464. remove("watch_dir/file1");
  465. remove("watch_dir/");
  466. create_dir("watch_dir");
  467. create_file("watch_dir/file1");
  468. /* Newer version of Windows ship with
  469. HKLM\SYSTEM\CurrentControlSet\Control\FileSystem\NtfsDisable8dot3NameCreation
  470. not equal to 0. So we verify the files we created are addressable by a 8.3
  471. short name */
  472. has_shortnames = uv_fs_stat(NULL, &req, "watch_~1", NULL) != UV_ENOENT;
  473. if (has_shortnames) {
  474. r = uv_fs_event_init(loop, &fs_event);
  475. ASSERT(r == 0);
  476. r = uv_fs_event_start(&fs_event, fs_event_cb_dir, "watch_~1", 0);
  477. ASSERT(r == 0);
  478. r = uv_timer_init(loop, &timer);
  479. ASSERT(r == 0);
  480. r = uv_timer_start(&timer, timer_cb_file, 100, 0);
  481. ASSERT(r == 0);
  482. uv_run(loop, UV_RUN_DEFAULT);
  483. ASSERT(fs_event_cb_called == 1);
  484. ASSERT(timer_cb_called == 1);
  485. ASSERT(close_cb_called == 1);
  486. }
  487. /* Cleanup */
  488. remove("watch_dir/file1");
  489. remove("watch_dir/");
  490. MAKE_VALGRIND_HAPPY();
  491. if (!has_shortnames)
  492. RETURN_SKIP("Was not able to address files with 8.3 short name.");
  493. return 0;
  494. }
  495. #endif
  496. TEST_IMPL(fs_event_watch_file) {
  497. #if defined(NO_FS_EVENTS)
  498. RETURN_SKIP(NO_FS_EVENTS);
  499. #endif
  500. uv_loop_t* loop = uv_default_loop();
  501. int r;
  502. /* Setup */
  503. remove("watch_dir/file2");
  504. remove("watch_dir/file1");
  505. remove("watch_dir/");
  506. create_dir("watch_dir");
  507. create_file("watch_dir/file1");
  508. create_file("watch_dir/file2");
  509. r = uv_fs_event_init(loop, &fs_event);
  510. ASSERT(r == 0);
  511. r = uv_fs_event_start(&fs_event, fs_event_cb_file, "watch_dir/file2", 0);
  512. ASSERT(r == 0);
  513. r = uv_timer_init(loop, &timer);
  514. ASSERT(r == 0);
  515. r = uv_timer_start(&timer, timer_cb_file, 100, 100);
  516. ASSERT(r == 0);
  517. uv_run(loop, UV_RUN_DEFAULT);
  518. ASSERT(fs_event_cb_called == 1);
  519. ASSERT(timer_cb_called == 2);
  520. ASSERT(close_cb_called == 2);
  521. /* Cleanup */
  522. remove("watch_dir/file2");
  523. remove("watch_dir/file1");
  524. remove("watch_dir/");
  525. MAKE_VALGRIND_HAPPY();
  526. return 0;
  527. }
  528. TEST_IMPL(fs_event_watch_file_exact_path) {
  529. /*
  530. This test watches a file named "file.jsx" and modifies a file named
  531. "file.js". The test verifies that no events occur for file.jsx.
  532. */
  533. #if defined(NO_FS_EVENTS)
  534. RETURN_SKIP(NO_FS_EVENTS);
  535. #endif
  536. uv_loop_t* loop;
  537. int r;
  538. loop = uv_default_loop();
  539. /* Setup */
  540. remove("watch_dir/file.js");
  541. remove("watch_dir/file.jsx");
  542. remove("watch_dir/");
  543. create_dir("watch_dir");
  544. create_file("watch_dir/file.js");
  545. create_file("watch_dir/file.jsx");
  546. #if defined(__APPLE__) && !defined(MAC_OS_X_VERSION_10_12)
  547. /* Empirically, FSEvents seems to (reliably) report the preceeding
  548. * create_file events prior to macOS 10.11.6 in the subsequent fs_watch
  549. * creation, but that behavior hasn't been observed to occur on newer
  550. * versions. Give a long delay here to let the system settle before running
  551. * the test. */
  552. uv_sleep(1100);
  553. uv_update_time(loop);
  554. #endif
  555. r = uv_fs_event_init(loop, &fs_event);
  556. ASSERT(r == 0);
  557. r = uv_fs_event_start(&fs_event, fs_event_fail, "watch_dir/file.jsx", 0);
  558. ASSERT(r == 0);
  559. r = uv_timer_init(loop, &timer);
  560. ASSERT(r == 0);
  561. r = uv_timer_start(&timer, timer_cb_exact, 100, 100);
  562. ASSERT(r == 0);
  563. r = uv_run(loop, UV_RUN_DEFAULT);
  564. ASSERT(r == 0);
  565. ASSERT(timer_cb_exact_called == 2);
  566. /* Cleanup */
  567. remove("watch_dir/file.js");
  568. remove("watch_dir/file.jsx");
  569. remove("watch_dir/");
  570. MAKE_VALGRIND_HAPPY();
  571. return 0;
  572. }
  573. TEST_IMPL(fs_event_watch_file_twice) {
  574. #if defined(NO_FS_EVENTS)
  575. RETURN_SKIP(NO_FS_EVENTS);
  576. #endif
  577. const char path[] = "test/fixtures/empty_file";
  578. uv_fs_event_t watchers[2];
  579. uv_timer_t timer;
  580. uv_loop_t* loop;
  581. loop = uv_default_loop();
  582. timer.data = watchers;
  583. ASSERT(0 == uv_fs_event_init(loop, watchers + 0));
  584. ASSERT(0 == uv_fs_event_start(watchers + 0, fail_cb, path, 0));
  585. ASSERT(0 == uv_fs_event_init(loop, watchers + 1));
  586. ASSERT(0 == uv_fs_event_start(watchers + 1, fail_cb, path, 0));
  587. ASSERT(0 == uv_timer_init(loop, &timer));
  588. ASSERT(0 == uv_timer_start(&timer, timer_cb_watch_twice, 10, 0));
  589. ASSERT(0 == uv_run(loop, UV_RUN_DEFAULT));
  590. MAKE_VALGRIND_HAPPY();
  591. return 0;
  592. }
  593. TEST_IMPL(fs_event_watch_file_current_dir) {
  594. #if defined(NO_FS_EVENTS)
  595. RETURN_SKIP(NO_FS_EVENTS);
  596. #endif
  597. uv_timer_t timer;
  598. uv_loop_t* loop;
  599. int r;
  600. loop = uv_default_loop();
  601. /* Setup */
  602. remove("watch_file");
  603. create_file("watch_file");
  604. #if defined(__APPLE__) && !defined(MAC_OS_X_VERSION_10_12)
  605. /* Empirically, kevent seems to (sometimes) report the preceeding
  606. * create_file events prior to macOS 10.11.6 in the subsequent fs_event_start
  607. * So let the system settle before running the test. */
  608. uv_sleep(1100);
  609. uv_update_time(loop);
  610. #endif
  611. r = uv_fs_event_init(loop, &fs_event);
  612. ASSERT(r == 0);
  613. r = uv_fs_event_start(&fs_event,
  614. fs_event_cb_file_current_dir,
  615. "watch_file",
  616. 0);
  617. ASSERT(r == 0);
  618. r = uv_timer_init(loop, &timer);
  619. ASSERT(r == 0);
  620. r = uv_timer_start(&timer, timer_cb_touch, 1100, 0);
  621. ASSERT(r == 0);
  622. ASSERT(timer_cb_touch_called == 0);
  623. ASSERT(fs_event_cb_called == 0);
  624. ASSERT(close_cb_called == 0);
  625. uv_run(loop, UV_RUN_DEFAULT);
  626. ASSERT(timer_cb_touch_called == 1);
  627. ASSERT(fs_event_cb_called == 1);
  628. ASSERT(close_cb_called == 1);
  629. /* Cleanup */
  630. remove("watch_file");
  631. MAKE_VALGRIND_HAPPY();
  632. return 0;
  633. }
  634. #ifdef _WIN32
  635. TEST_IMPL(fs_event_watch_file_root_dir) {
  636. uv_loop_t* loop;
  637. int r;
  638. const char* sys_drive = getenv("SystemDrive");
  639. char path[] = "\\\\?\\X:\\bootsect.bak";
  640. ASSERT(sys_drive != NULL);
  641. strncpy(path + sizeof("\\\\?\\") - 1, sys_drive, 1);
  642. loop = uv_default_loop();
  643. r = uv_fs_event_init(loop, &fs_event);
  644. ASSERT(r == 0);
  645. r = uv_fs_event_start(&fs_event, fail_cb, path, 0);
  646. if (r == UV_ENOENT)
  647. RETURN_SKIP("bootsect.bak doesn't exist in system root.\n");
  648. ASSERT(r == 0);
  649. uv_close((uv_handle_t*) &fs_event, NULL);
  650. MAKE_VALGRIND_HAPPY();
  651. return 0;
  652. }
  653. #endif
  654. TEST_IMPL(fs_event_no_callback_after_close) {
  655. #if defined(NO_FS_EVENTS)
  656. RETURN_SKIP(NO_FS_EVENTS);
  657. #endif
  658. uv_loop_t* loop = uv_default_loop();
  659. int r;
  660. /* Setup */
  661. remove("watch_dir/file1");
  662. remove("watch_dir/");
  663. create_dir("watch_dir");
  664. create_file("watch_dir/file1");
  665. r = uv_fs_event_init(loop, &fs_event);
  666. ASSERT(r == 0);
  667. r = uv_fs_event_start(&fs_event,
  668. fs_event_cb_file,
  669. "watch_dir/file1",
  670. 0);
  671. ASSERT(r == 0);
  672. uv_close((uv_handle_t*)&fs_event, close_cb);
  673. touch_file("watch_dir/file1");
  674. uv_run(loop, UV_RUN_DEFAULT);
  675. ASSERT(fs_event_cb_called == 0);
  676. ASSERT(close_cb_called == 1);
  677. /* Cleanup */
  678. remove("watch_dir/file1");
  679. remove("watch_dir/");
  680. MAKE_VALGRIND_HAPPY();
  681. return 0;
  682. }
  683. TEST_IMPL(fs_event_no_callback_on_close) {
  684. #if defined(NO_FS_EVENTS)
  685. RETURN_SKIP(NO_FS_EVENTS);
  686. #endif
  687. uv_loop_t* loop = uv_default_loop();
  688. int r;
  689. /* Setup */
  690. remove("watch_dir/file1");
  691. remove("watch_dir/");
  692. create_dir("watch_dir");
  693. create_file("watch_dir/file1");
  694. r = uv_fs_event_init(loop, &fs_event);
  695. ASSERT(r == 0);
  696. r = uv_fs_event_start(&fs_event,
  697. fs_event_cb_file,
  698. "watch_dir/file1",
  699. 0);
  700. ASSERT(r == 0);
  701. uv_close((uv_handle_t*)&fs_event, close_cb);
  702. uv_run(loop, UV_RUN_DEFAULT);
  703. ASSERT(fs_event_cb_called == 0);
  704. ASSERT(close_cb_called == 1);
  705. /* Cleanup */
  706. remove("watch_dir/file1");
  707. remove("watch_dir/");
  708. MAKE_VALGRIND_HAPPY();
  709. return 0;
  710. }
  711. static void timer_cb(uv_timer_t* handle) {
  712. int r;
  713. r = uv_fs_event_init(handle->loop, &fs_event);
  714. ASSERT(r == 0);
  715. r = uv_fs_event_start(&fs_event, fs_event_fail, ".", 0);
  716. ASSERT(r == 0);
  717. uv_close((uv_handle_t*)&fs_event, close_cb);
  718. uv_close((uv_handle_t*)handle, close_cb);
  719. }
  720. TEST_IMPL(fs_event_immediate_close) {
  721. #if defined(NO_FS_EVENTS)
  722. RETURN_SKIP(NO_FS_EVENTS);
  723. #endif
  724. uv_timer_t timer;
  725. uv_loop_t* loop;
  726. int r;
  727. loop = uv_default_loop();
  728. r = uv_timer_init(loop, &timer);
  729. ASSERT(r == 0);
  730. r = uv_timer_start(&timer, timer_cb, 1, 0);
  731. ASSERT(r == 0);
  732. uv_run(loop, UV_RUN_DEFAULT);
  733. ASSERT(close_cb_called == 2);
  734. MAKE_VALGRIND_HAPPY();
  735. return 0;
  736. }
  737. TEST_IMPL(fs_event_close_with_pending_event) {
  738. #if defined(NO_FS_EVENTS)
  739. RETURN_SKIP(NO_FS_EVENTS);
  740. #endif
  741. uv_loop_t* loop;
  742. int r;
  743. loop = uv_default_loop();
  744. create_dir("watch_dir");
  745. create_file("watch_dir/file");
  746. r = uv_fs_event_init(loop, &fs_event);
  747. ASSERT(r == 0);
  748. r = uv_fs_event_start(&fs_event, fs_event_fail, "watch_dir", 0);
  749. ASSERT(r == 0);
  750. /* Generate an fs event. */
  751. touch_file("watch_dir/file");
  752. uv_close((uv_handle_t*)&fs_event, close_cb);
  753. uv_run(loop, UV_RUN_DEFAULT);
  754. ASSERT(close_cb_called == 1);
  755. /* Clean up */
  756. remove("watch_dir/file");
  757. remove("watch_dir/");
  758. MAKE_VALGRIND_HAPPY();
  759. return 0;
  760. }
  761. TEST_IMPL(fs_event_close_in_callback) {
  762. #if defined(NO_FS_EVENTS)
  763. RETURN_SKIP(NO_FS_EVENTS);
  764. #elif defined(__MVS__)
  765. RETURN_SKIP("Directory watching not supported on this platform.");
  766. #endif
  767. uv_loop_t* loop;
  768. int r;
  769. loop = uv_default_loop();
  770. fs_event_unlink_files(NULL);
  771. create_dir("watch_dir");
  772. r = uv_fs_event_init(loop, &fs_event);
  773. ASSERT(r == 0);
  774. r = uv_fs_event_start(&fs_event, fs_event_cb_close, "watch_dir", 0);
  775. ASSERT(r == 0);
  776. r = uv_timer_init(loop, &timer);
  777. ASSERT(r == 0);
  778. r = uv_timer_start(&timer, fs_event_create_files, 100, 0);
  779. ASSERT(r == 0);
  780. uv_run(loop, UV_RUN_DEFAULT);
  781. uv_close((uv_handle_t*)&timer, close_cb);
  782. uv_run(loop, UV_RUN_ONCE);
  783. ASSERT(close_cb_called == 2);
  784. ASSERT(fs_event_cb_called == 3);
  785. /* Clean up */
  786. fs_event_unlink_files(NULL);
  787. remove("watch_dir/");
  788. MAKE_VALGRIND_HAPPY();
  789. return 0;
  790. }
  791. TEST_IMPL(fs_event_start_and_close) {
  792. #if defined(NO_FS_EVENTS)
  793. RETURN_SKIP(NO_FS_EVENTS);
  794. #endif
  795. uv_loop_t* loop;
  796. uv_fs_event_t fs_event1;
  797. uv_fs_event_t fs_event2;
  798. int r;
  799. loop = uv_default_loop();
  800. create_dir("watch_dir");
  801. r = uv_fs_event_init(loop, &fs_event1);
  802. ASSERT(r == 0);
  803. r = uv_fs_event_start(&fs_event1, fs_event_cb_dir, "watch_dir", 0);
  804. ASSERT(r == 0);
  805. r = uv_fs_event_init(loop, &fs_event2);
  806. ASSERT(r == 0);
  807. r = uv_fs_event_start(&fs_event2, fs_event_cb_dir, "watch_dir", 0);
  808. ASSERT(r == 0);
  809. uv_close((uv_handle_t*) &fs_event2, close_cb);
  810. uv_close((uv_handle_t*) &fs_event1, close_cb);
  811. uv_run(loop, UV_RUN_DEFAULT);
  812. ASSERT(close_cb_called == 2);
  813. remove("watch_dir/");
  814. MAKE_VALGRIND_HAPPY();
  815. return 0;
  816. }
  817. TEST_IMPL(fs_event_getpath) {
  818. #if defined(NO_FS_EVENTS)
  819. RETURN_SKIP(NO_FS_EVENTS);
  820. #endif
  821. uv_loop_t* loop = uv_default_loop();
  822. unsigned i;
  823. int r;
  824. char buf[1024];
  825. size_t len;
  826. const char* const watch_dir[] = {
  827. "watch_dir",
  828. "watch_dir/",
  829. "watch_dir///",
  830. "watch_dir/subfolder/..",
  831. "watch_dir//subfolder//..//",
  832. };
  833. create_dir("watch_dir");
  834. create_dir("watch_dir/subfolder");
  835. for (i = 0; i < ARRAY_SIZE(watch_dir); i++) {
  836. r = uv_fs_event_init(loop, &fs_event);
  837. ASSERT(r == 0);
  838. len = sizeof buf;
  839. r = uv_fs_event_getpath(&fs_event, buf, &len);
  840. ASSERT(r == UV_EINVAL);
  841. r = uv_fs_event_start(&fs_event, fail_cb, watch_dir[i], 0);
  842. ASSERT(r == 0);
  843. len = 0;
  844. r = uv_fs_event_getpath(&fs_event, buf, &len);
  845. ASSERT(r == UV_ENOBUFS);
  846. ASSERT(len < sizeof buf); /* sanity check */
  847. ASSERT(len == strlen(watch_dir[i]) + 1);
  848. r = uv_fs_event_getpath(&fs_event, buf, &len);
  849. ASSERT(r == 0);
  850. ASSERT(len == strlen(watch_dir[i]));
  851. ASSERT(strcmp(buf, watch_dir[i]) == 0);
  852. r = uv_fs_event_stop(&fs_event);
  853. ASSERT(r == 0);
  854. uv_close((uv_handle_t*) &fs_event, close_cb);
  855. uv_run(loop, UV_RUN_DEFAULT);
  856. ASSERT(close_cb_called == 1);
  857. close_cb_called = 0;
  858. }
  859. remove("watch_dir/");
  860. MAKE_VALGRIND_HAPPY();
  861. return 0;
  862. }
  863. #if defined(__APPLE__)
  864. static int fs_event_error_reported;
  865. static void fs_event_error_report_cb(uv_fs_event_t* handle,
  866. const char* filename,
  867. int events,
  868. int status) {
  869. if (status != 0)
  870. fs_event_error_reported = status;
  871. }
  872. static void timer_cb_nop(uv_timer_t* handle) {
  873. ++timer_cb_called;
  874. uv_close((uv_handle_t*) handle, close_cb);
  875. }
  876. static void fs_event_error_report_close_cb(uv_handle_t* handle) {
  877. ASSERT(handle != NULL);
  878. close_cb_called++;
  879. /* handle is allocated on-stack, no need to free it */
  880. }
  881. TEST_IMPL(fs_event_error_reporting) {
  882. unsigned int i;
  883. uv_loop_t loops[1024];
  884. uv_fs_event_t events[ARRAY_SIZE(loops)];
  885. uv_loop_t* loop;
  886. uv_fs_event_t* event;
  887. TEST_FILE_LIMIT(ARRAY_SIZE(loops) * 3);
  888. remove("watch_dir/");
  889. create_dir("watch_dir");
  890. /* Create a lot of loops, and start FSEventStream in each of them.
  891. * Eventually, this should create enough streams to make FSEventStreamStart()
  892. * fail.
  893. */
  894. for (i = 0; i < ARRAY_SIZE(loops); i++) {
  895. loop = &loops[i];
  896. ASSERT(0 == uv_loop_init(loop));
  897. event = &events[i];
  898. timer_cb_called = 0;
  899. close_cb_called = 0;
  900. ASSERT(0 == uv_fs_event_init(loop, event));
  901. ASSERT(0 == uv_fs_event_start(event,
  902. fs_event_error_report_cb,
  903. "watch_dir",
  904. 0));
  905. uv_unref((uv_handle_t*) event);
  906. /* Let loop run for some time */
  907. ASSERT(0 == uv_timer_init(loop, &timer));
  908. ASSERT(0 == uv_timer_start(&timer, timer_cb_nop, 2, 0));
  909. uv_run(loop, UV_RUN_DEFAULT);
  910. ASSERT(1 == timer_cb_called);
  911. ASSERT(1 == close_cb_called);
  912. if (fs_event_error_reported != 0)
  913. break;
  914. }
  915. /* At least one loop should fail */
  916. ASSERT(fs_event_error_reported == UV_EMFILE);
  917. /* Stop and close all events, and destroy loops */
  918. do {
  919. loop = &loops[i];
  920. event = &events[i];
  921. ASSERT(0 == uv_fs_event_stop(event));
  922. uv_ref((uv_handle_t*) event);
  923. uv_close((uv_handle_t*) event, fs_event_error_report_close_cb);
  924. close_cb_called = 0;
  925. uv_run(loop, UV_RUN_DEFAULT);
  926. ASSERT(close_cb_called == 1);
  927. uv_loop_close(loop);
  928. } while (i-- != 0);
  929. remove("watch_dir/");
  930. MAKE_VALGRIND_HAPPY();
  931. return 0;
  932. }
  933. #else /* !defined(__APPLE__) */
  934. TEST_IMPL(fs_event_error_reporting) {
  935. /* No-op, needed only for FSEvents backend */
  936. MAKE_VALGRIND_HAPPY();
  937. return 0;
  938. }
  939. #endif /* defined(__APPLE__) */
  940. TEST_IMPL(fs_event_watch_invalid_path) {
  941. #if defined(NO_FS_EVENTS)
  942. RETURN_SKIP(NO_FS_EVENTS);
  943. #endif
  944. uv_loop_t* loop;
  945. int r;
  946. loop = uv_default_loop();
  947. r = uv_fs_event_init(loop, &fs_event);
  948. ASSERT(r == 0);
  949. r = uv_fs_event_start(&fs_event, fs_event_cb_file, "<:;", 0);
  950. ASSERT(r != 0);
  951. ASSERT(uv_is_active((uv_handle_t*) &fs_event) == 0);
  952. r = uv_fs_event_start(&fs_event, fs_event_cb_file, "", 0);
  953. ASSERT(r != 0);
  954. ASSERT(uv_is_active((uv_handle_t*) &fs_event) == 0);
  955. MAKE_VALGRIND_HAPPY();
  956. return 0;
  957. }