123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173 |
- /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to
- * deal in the Software without restriction, including without limitation the
- * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
- * sell copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
- * IN THE SOFTWARE.
- */
- #include "uv.h"
- #include "task.h"
- #include <string.h>
- #include <fcntl.h>
- #if defined(__APPLE__) && !TARGET_OS_IPHONE
- # include <AvailabilityMacros.h>
- #endif
- #ifndef HAVE_KQUEUE
- # if defined(__APPLE__) || \
- defined(__DragonFly__) || \
- defined(__FreeBSD__) || \
- defined(__FreeBSD_kernel__) || \
- defined(__OpenBSD__) || \
- defined(__NetBSD__)
- # define HAVE_KQUEUE 1
- # endif
- #endif
- #if defined(__arm__)/* Increase the timeout so the test passes on arm CI bots */
- # define CREATE_TIMEOUT 100
- #else
- # define CREATE_TIMEOUT 1
- #endif
- static uv_fs_event_t fs_event;
- 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;
- static int close_cb_called;
- static int fs_event_created;
- static int fs_event_removed;
- static int fs_event_cb_called;
- #if defined(PATH_MAX)
- static char fs_event_filename[PATH_MAX];
- #else
- static char fs_event_filename[1024];
- #endif /* defined(PATH_MAX) */
- static int timer_cb_touch_called;
- static int timer_cb_exact_called;
- static void fs_event_fail(uv_fs_event_t* handle,
- const char* filename,
- int events,
- int status) {
- ASSERT(0 && "should never be called");
- }
- static void create_dir(const char* name) {
- int r;
- uv_fs_t req;
- r = uv_fs_mkdir(NULL, &req, name, 0755, NULL);
- ASSERT(r == 0 || r == UV_EEXIST);
- uv_fs_req_cleanup(&req);
- }
- static void create_file(const char* name) {
- int r;
- uv_file file;
- uv_fs_t req;
- r = uv_fs_open(NULL, &req, name, O_WRONLY | O_CREAT, S_IWUSR | S_IRUSR, NULL);
- ASSERT(r >= 0);
- file = r;
- uv_fs_req_cleanup(&req);
- r = uv_fs_close(NULL, &req, file, NULL);
- ASSERT(r == 0);
- uv_fs_req_cleanup(&req);
- }
- static void touch_file(const char* name) {
- int r;
- uv_file file;
- uv_fs_t req;
- uv_buf_t buf;
- r = uv_fs_open(NULL, &req, name, O_RDWR, 0, NULL);
- ASSERT(r >= 0);
- file = r;
- uv_fs_req_cleanup(&req);
- buf = uv_buf_init("foo", 4);
- r = uv_fs_write(NULL, &req, file, &buf, 1, -1, NULL);
- ASSERT(r >= 0);
- uv_fs_req_cleanup(&req);
- r = uv_fs_close(NULL, &req, file, NULL);
- ASSERT(r == 0);
- uv_fs_req_cleanup(&req);
- }
- static void close_cb(uv_handle_t* handle) {
- ASSERT(handle != NULL);
- close_cb_called++;
- }
- static void fail_cb(uv_fs_event_t* handle,
- const char* path,
- int events,
- int status) {
- ASSERT(0 && "fail_cb called");
- }
- static void fs_event_cb_dir(uv_fs_event_t* handle, const char* filename,
- int events, int status) {
- ++fs_event_cb_called;
- ASSERT(handle == &fs_event);
- ASSERT(status == 0);
- ASSERT(events == UV_CHANGE);
- #if defined(__APPLE__) || defined(_WIN32) || defined(__linux__)
- ASSERT(strcmp(filename, "file1") == 0);
- #else
- ASSERT(filename == NULL || strcmp(filename, "file1") == 0);
- #endif
- ASSERT(0 == uv_fs_event_stop(handle));
- uv_close((uv_handle_t*)handle, close_cb);
- }
- static const char* fs_event_get_filename(int i) {
- snprintf(fs_event_filename,
- sizeof(fs_event_filename),
- "watch_dir/%s%d",
- file_prefix,
- i);
- return fs_event_filename;
- }
- static void fs_event_create_files(uv_timer_t* handle) {
- /* Make sure we're not attempting to create files we do not intend */
- ASSERT(fs_event_created < fs_event_file_count);
- /* Create the file */
- create_file(fs_event_get_filename(fs_event_created));
- if (++fs_event_created < fs_event_file_count) {
- /* Create another file on a different event loop tick. We do it this way
- * to avoid fs events coalescing into one fs event. */
- ASSERT(0 == uv_timer_start(&timer,
- fs_event_create_files,
- CREATE_TIMEOUT,
- 0));
- }
- }
- static void fs_event_unlink_files(uv_timer_t* handle) {
- int r;
- int i;
- /* NOTE: handle might be NULL if invoked not as timer callback */
- if (handle == NULL) {
- /* Unlink all files */
- for (i = 0; i < 16; i++) {
- r = remove(fs_event_get_filename(i));
- if (handle != NULL)
- ASSERT(r == 0);
- }
- } else {
- /* Make sure we're not attempting to remove files we do not intend */
- ASSERT(fs_event_removed < fs_event_file_count);
- /* Remove the file */
- ASSERT(0 == remove(fs_event_get_filename(fs_event_removed)));
- if (++fs_event_removed < fs_event_file_count) {
- /* Remove another file on a different event loop tick. We do it this way
- * to avoid fs events coalescing into one fs event. */
- ASSERT(0 == uv_timer_start(&timer, fs_event_unlink_files, 1, 0));
- }
- }
- }
- static void fs_event_cb_dir_multi_file(uv_fs_event_t* handle,
- const char* filename,
- int events,
- int status) {
- fs_event_cb_called++;
- ASSERT(handle == &fs_event);
- ASSERT(status == 0);
- ASSERT(events == UV_CHANGE || events == UV_RENAME);
- #if defined(__APPLE__) || defined(_WIN32) || defined(__linux__)
- ASSERT(strncmp(filename, file_prefix, sizeof(file_prefix) - 1) == 0);
- #else
- ASSERT(filename == NULL ||
- strncmp(filename, file_prefix, sizeof(file_prefix) - 1) == 0);
- #endif
- 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, 1, 0));
- } else if (fs_event_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);
- }
- }
- #if defined(__APPLE__) || defined(_WIN32)
- static const char* fs_event_get_filename_in_subdir(int i) {
- snprintf(fs_event_filename,
- sizeof(fs_event_filename),
- "watch_dir/subdir/%s%d",
- file_prefix,
- i);
- return fs_event_filename;
- }
- static void fs_event_create_files_in_subdir(uv_timer_t* handle) {
- /* Make sure we're not attempting to create files we do not intend */
- ASSERT(fs_event_created < fs_event_file_count);
- /* Create the file */
- create_file(fs_event_get_filename_in_subdir(fs_event_created));
- if (++fs_event_created < fs_event_file_count) {
- /* Create another file on a different event loop tick. We do it this way
- * to avoid fs events coalescing into one fs event. */
- ASSERT(0 == uv_timer_start(&timer, fs_event_create_files_in_subdir, 1, 0));
- }
- }
- static void fs_event_unlink_files_in_subdir(uv_timer_t* handle) {
- int r;
- int i;
- /* NOTE: handle might be NULL if invoked not as timer callback */
- if (handle == NULL) {
- /* Unlink all files */
- for (i = 0; i < 16; i++) {
- r = remove(fs_event_get_filename_in_subdir(i));
- if (handle != NULL)
- ASSERT(r == 0);
- }
- } else {
- /* Make sure we're not attempting to remove files we do not intend */
- ASSERT(fs_event_removed < fs_event_file_count);
- /* Remove the file */
- ASSERT(0 == remove(fs_event_get_filename_in_subdir(fs_event_removed)));
- if (++fs_event_removed < fs_event_file_count) {
- /* Remove another file on a different event loop tick. We do it this way
- * to avoid fs events coalescing into one fs event. */
- ASSERT(0 == uv_timer_start(&timer, fs_event_unlink_files_in_subdir, 1, 0));
- }
- }
- }
- static void fs_event_cb_dir_multi_file_in_subdir(uv_fs_event_t* handle,
- const char* filename,
- int events,
- int status) {
- #ifdef _WIN32
- /* Each file created (or deleted) will cause this callback to be called twice
- * under Windows: once with the name of the file, and second time with the
- * name of the directory. We will ignore the callback for the directory
- * itself. */
- if (filename && strcmp(filename, file_prefix_in_subdir) == 0)
- return;
- #endif
- /* It may happen that the "subdir" creation event is captured even though
- * we started watching after its actual creation.
- */
- if (strcmp(filename, "subdir") == 0)
- return;
- fs_multievent_cb_called++;
- ASSERT(handle == &fs_event);
- ASSERT(status == 0);
- ASSERT(events == UV_CHANGE || events == UV_RENAME);
- #if defined(__APPLE__) || defined(_WIN32) || defined(__linux__)
- ASSERT(strncmp(filename,
- file_prefix_in_subdir,
- sizeof(file_prefix_in_subdir) - 1) == 0);
- #else
- ASSERT(filename == NULL ||
- strncmp(filename,
- file_prefix_in_subdir,
- sizeof(file_prefix_in_subdir) - 1) == 0);
- #endif
- if (fs_event_created == fs_event_file_count &&
- fs_multievent_cb_called == fs_event_created) {
- /* 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_multievent_cb_called == 2 * fs_event_file_count) {
- /* Once we've processed all create and delete events, stop watching */
- ASSERT(fs_event_removed == fs_event_file_count);
- uv_close((uv_handle_t*) &timer, close_cb);
- uv_close((uv_handle_t*) handle, close_cb);
- }
- }
- #endif
- static void fs_event_cb_file(uv_fs_event_t* handle, const char* filename,
- int events, int status) {
- ++fs_event_cb_called;
- ASSERT(handle == &fs_event);
- ASSERT(status == 0);
- ASSERT(events == UV_CHANGE);
- #if defined(__APPLE__) || defined(_WIN32) || defined(__linux__)
- ASSERT(strcmp(filename, "file2") == 0);
- #else
- ASSERT(filename == NULL || strcmp(filename, "file2") == 0);
- #endif
- ASSERT(0 == uv_fs_event_stop(handle));
- uv_close((uv_handle_t*)handle, close_cb);
- }
- static void timer_cb_close_handle(uv_timer_t* timer) {
- uv_handle_t* handle;
- ASSERT(timer != NULL);
- handle = timer->data;
- uv_close((uv_handle_t*)timer, NULL);
- uv_close((uv_handle_t*)handle, close_cb);
- }
- static void fs_event_cb_file_current_dir(uv_fs_event_t* handle,
- const char* filename, int events, int status) {
- ASSERT(fs_event_cb_called == 0);
- ++fs_event_cb_called;
- ASSERT(handle == &fs_event);
- ASSERT(status == 0);
- ASSERT(events == UV_CHANGE);
- #if defined(__APPLE__) || defined(_WIN32) || defined(__linux__)
- ASSERT(strcmp(filename, "watch_file") == 0);
- #else
- ASSERT(filename == NULL || strcmp(filename, "watch_file") == 0);
- #endif
- /* Regression test for SunOS: touch should generate just one event. */
- {
- static uv_timer_t timer;
- uv_timer_init(handle->loop, &timer);
- timer.data = handle;
- uv_timer_start(&timer, timer_cb_close_handle, 250, 0);
- }
- }
- static void timer_cb_file(uv_timer_t* handle) {
- ++timer_cb_called;
- if (timer_cb_called == 1) {
- touch_file("watch_dir/file1");
- } else {
- touch_file("watch_dir/file2");
- uv_close((uv_handle_t*)handle, close_cb);
- }
- }
- static void timer_cb_touch(uv_timer_t* timer) {
- uv_close((uv_handle_t*)timer, NULL);
- touch_file("watch_file");
- timer_cb_touch_called++;
- }
- static void timer_cb_exact(uv_timer_t* handle) {
- int r;
- if (timer_cb_exact_called == 0) {
- touch_file("watch_dir/file.js");
- } else {
- uv_close((uv_handle_t*)handle, NULL);
- r = uv_fs_event_stop(&fs_event);
- ASSERT(r == 0);
- uv_close((uv_handle_t*) &fs_event, NULL);
- }
- ++timer_cb_exact_called;
- }
- static void timer_cb_watch_twice(uv_timer_t* handle) {
- uv_fs_event_t* handles = handle->data;
- uv_close((uv_handle_t*) (handles + 0), NULL);
- uv_close((uv_handle_t*) (handles + 1), NULL);
- 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);
- #elif defined(__MVS__)
- RETURN_SKIP("Directory watching not supported on this platform.");
- #endif
- uv_loop_t* loop = uv_default_loop();
- int r;
- /* Setup */
- fs_event_unlink_files(NULL);
- remove("watch_dir/file2");
- remove("watch_dir/file1");
- remove("watch_dir/");
- create_dir("watch_dir");
- r = uv_fs_event_init(loop, &fs_event);
- ASSERT(r == 0);
- r = uv_fs_event_start(&fs_event, fs_event_cb_dir_multi_file, "watch_dir", 0);
- ASSERT(r == 0);
- r = uv_timer_init(loop, &timer);
- ASSERT(r == 0);
- r = uv_timer_start(&timer, fs_event_create_files, 100, 0);
- ASSERT(r == 0);
- uv_run(loop, UV_RUN_DEFAULT);
- ASSERT(fs_event_cb_called == fs_event_created + fs_event_removed);
- ASSERT(close_cb_called == 2);
- /* Cleanup */
- fs_event_unlink_files(NULL);
- remove("watch_dir/file2");
- remove("watch_dir/file1");
- remove("watch_dir/");
- MAKE_VALGRIND_HAPPY();
- 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();
- fs_event_unlink_files(NULL);
- remove("watch_dir/file2");
- remove("watch_dir/file1");
- remove("watch_dir/subdir");
- remove("watch_dir/");
- create_dir("watch_dir");
- create_dir("watch_dir/subdir");
- 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);
- 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_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);
- remove("watch_dir/file2");
- remove("watch_dir/file1");
- remove("watch_dir/subdir");
- remove("watch_dir/");
- MAKE_VALGRIND_HAPPY();
- return 0;
- #else
- RETURN_SKIP("Recursive directory watching not supported on this platform.");
- #endif
- }
- #ifdef _WIN32
- TEST_IMPL(fs_event_watch_dir_short_path) {
- uv_loop_t* loop;
- uv_fs_t req;
- int has_shortnames;
- int r;
- /* Setup */
- loop = uv_default_loop();
- remove("watch_dir/file1");
- remove("watch_dir/");
- create_dir("watch_dir");
- create_file("watch_dir/file1");
- /* Newer version of Windows ship with
- HKLM\SYSTEM\CurrentControlSet\Control\FileSystem\NtfsDisable8dot3NameCreation
- not equal to 0. So we verify the files we created are addressable by a 8.3
- short name */
- has_shortnames = uv_fs_stat(NULL, &req, "watch_~1", NULL) != UV_ENOENT;
- if (has_shortnames) {
- r = uv_fs_event_init(loop, &fs_event);
- ASSERT(r == 0);
- r = uv_fs_event_start(&fs_event, fs_event_cb_dir, "watch_~1", 0);
- ASSERT(r == 0);
- r = uv_timer_init(loop, &timer);
- ASSERT(r == 0);
- r = uv_timer_start(&timer, timer_cb_file, 100, 0);
- ASSERT(r == 0);
- uv_run(loop, UV_RUN_DEFAULT);
- ASSERT(fs_event_cb_called == 1);
- ASSERT(timer_cb_called == 1);
- ASSERT(close_cb_called == 1);
- }
- /* Cleanup */
- remove("watch_dir/file1");
- remove("watch_dir/");
- MAKE_VALGRIND_HAPPY();
- if (!has_shortnames)
- RETURN_SKIP("Was not able to address files with 8.3 short name.");
- return 0;
- }
- #endif
- TEST_IMPL(fs_event_watch_file) {
- #if defined(NO_FS_EVENTS)
- RETURN_SKIP(NO_FS_EVENTS);
- #endif
- uv_loop_t* loop = uv_default_loop();
- int r;
- /* Setup */
- remove("watch_dir/file2");
- remove("watch_dir/file1");
- remove("watch_dir/");
- create_dir("watch_dir");
- create_file("watch_dir/file1");
- create_file("watch_dir/file2");
- r = uv_fs_event_init(loop, &fs_event);
- ASSERT(r == 0);
- r = uv_fs_event_start(&fs_event, fs_event_cb_file, "watch_dir/file2", 0);
- ASSERT(r == 0);
- r = uv_timer_init(loop, &timer);
- ASSERT(r == 0);
- r = uv_timer_start(&timer, timer_cb_file, 100, 100);
- ASSERT(r == 0);
- uv_run(loop, UV_RUN_DEFAULT);
- ASSERT(fs_event_cb_called == 1);
- ASSERT(timer_cb_called == 2);
- ASSERT(close_cb_called == 2);
- /* Cleanup */
- remove("watch_dir/file2");
- remove("watch_dir/file1");
- remove("watch_dir/");
- MAKE_VALGRIND_HAPPY();
- return 0;
- }
- TEST_IMPL(fs_event_watch_file_exact_path) {
- /*
- This test watches a file named "file.jsx" and modifies a file named
- "file.js". The test verifies that no events occur for file.jsx.
- */
- #if defined(NO_FS_EVENTS)
- RETURN_SKIP(NO_FS_EVENTS);
- #endif
- uv_loop_t* loop;
- int r;
- loop = uv_default_loop();
- /* Setup */
- remove("watch_dir/file.js");
- remove("watch_dir/file.jsx");
- remove("watch_dir/");
- create_dir("watch_dir");
- create_file("watch_dir/file.js");
- create_file("watch_dir/file.jsx");
- #if defined(__APPLE__) && !defined(MAC_OS_X_VERSION_10_12)
- /* Empirically, FSEvents seems to (reliably) report the preceeding
- * create_file events prior to macOS 10.11.6 in the subsequent fs_watch
- * creation, but that behavior hasn't been observed to occur on newer
- * versions. Give a long delay here to let the system settle before running
- * the test. */
- uv_sleep(1100);
- uv_update_time(loop);
- #endif
- r = uv_fs_event_init(loop, &fs_event);
- ASSERT(r == 0);
- r = uv_fs_event_start(&fs_event, fs_event_fail, "watch_dir/file.jsx", 0);
- ASSERT(r == 0);
- r = uv_timer_init(loop, &timer);
- ASSERT(r == 0);
- r = uv_timer_start(&timer, timer_cb_exact, 100, 100);
- ASSERT(r == 0);
- r = uv_run(loop, UV_RUN_DEFAULT);
- ASSERT(r == 0);
- ASSERT(timer_cb_exact_called == 2);
- /* Cleanup */
- remove("watch_dir/file.js");
- remove("watch_dir/file.jsx");
- remove("watch_dir/");
- MAKE_VALGRIND_HAPPY();
- return 0;
- }
- TEST_IMPL(fs_event_watch_file_twice) {
- #if defined(NO_FS_EVENTS)
- RETURN_SKIP(NO_FS_EVENTS);
- #endif
- const char path[] = "test/fixtures/empty_file";
- uv_fs_event_t watchers[2];
- uv_timer_t timer;
- uv_loop_t* loop;
- loop = uv_default_loop();
- timer.data = watchers;
- ASSERT(0 == uv_fs_event_init(loop, watchers + 0));
- ASSERT(0 == uv_fs_event_start(watchers + 0, fail_cb, path, 0));
- ASSERT(0 == uv_fs_event_init(loop, watchers + 1));
- ASSERT(0 == uv_fs_event_start(watchers + 1, fail_cb, path, 0));
- ASSERT(0 == uv_timer_init(loop, &timer));
- ASSERT(0 == uv_timer_start(&timer, timer_cb_watch_twice, 10, 0));
- ASSERT(0 == uv_run(loop, UV_RUN_DEFAULT));
- MAKE_VALGRIND_HAPPY();
- return 0;
- }
- TEST_IMPL(fs_event_watch_file_current_dir) {
- #if defined(NO_FS_EVENTS)
- RETURN_SKIP(NO_FS_EVENTS);
- #endif
- uv_timer_t timer;
- uv_loop_t* loop;
- int r;
- loop = uv_default_loop();
- /* Setup */
- remove("watch_file");
- create_file("watch_file");
- #if defined(__APPLE__) && !defined(MAC_OS_X_VERSION_10_12)
- /* Empirically, kevent seems to (sometimes) report the preceeding
- * create_file events prior to macOS 10.11.6 in the subsequent fs_event_start
- * So let the system settle before running the test. */
- uv_sleep(1100);
- uv_update_time(loop);
- #endif
- r = uv_fs_event_init(loop, &fs_event);
- ASSERT(r == 0);
- r = uv_fs_event_start(&fs_event,
- fs_event_cb_file_current_dir,
- "watch_file",
- 0);
- ASSERT(r == 0);
- r = uv_timer_init(loop, &timer);
- ASSERT(r == 0);
- r = uv_timer_start(&timer, timer_cb_touch, 1100, 0);
- ASSERT(r == 0);
- ASSERT(timer_cb_touch_called == 0);
- ASSERT(fs_event_cb_called == 0);
- ASSERT(close_cb_called == 0);
- uv_run(loop, UV_RUN_DEFAULT);
- ASSERT(timer_cb_touch_called == 1);
- ASSERT(fs_event_cb_called == 1);
- ASSERT(close_cb_called == 1);
- /* Cleanup */
- remove("watch_file");
- MAKE_VALGRIND_HAPPY();
- return 0;
- }
- #ifdef _WIN32
- TEST_IMPL(fs_event_watch_file_root_dir) {
- uv_loop_t* loop;
- int r;
- const char* sys_drive = getenv("SystemDrive");
- char path[] = "\\\\?\\X:\\bootsect.bak";
- ASSERT(sys_drive != NULL);
- strncpy(path + sizeof("\\\\?\\") - 1, sys_drive, 1);
- loop = uv_default_loop();
- r = uv_fs_event_init(loop, &fs_event);
- ASSERT(r == 0);
- r = uv_fs_event_start(&fs_event, fail_cb, path, 0);
- if (r == UV_ENOENT)
- RETURN_SKIP("bootsect.bak doesn't exist in system root.\n");
- ASSERT(r == 0);
- uv_close((uv_handle_t*) &fs_event, NULL);
- MAKE_VALGRIND_HAPPY();
- return 0;
- }
- #endif
- TEST_IMPL(fs_event_no_callback_after_close) {
- #if defined(NO_FS_EVENTS)
- RETURN_SKIP(NO_FS_EVENTS);
- #endif
- uv_loop_t* loop = uv_default_loop();
- int r;
- /* Setup */
- remove("watch_dir/file1");
- remove("watch_dir/");
- create_dir("watch_dir");
- create_file("watch_dir/file1");
- r = uv_fs_event_init(loop, &fs_event);
- ASSERT(r == 0);
- r = uv_fs_event_start(&fs_event,
- fs_event_cb_file,
- "watch_dir/file1",
- 0);
- ASSERT(r == 0);
- uv_close((uv_handle_t*)&fs_event, close_cb);
- touch_file("watch_dir/file1");
- uv_run(loop, UV_RUN_DEFAULT);
- ASSERT(fs_event_cb_called == 0);
- ASSERT(close_cb_called == 1);
- /* Cleanup */
- remove("watch_dir/file1");
- remove("watch_dir/");
- MAKE_VALGRIND_HAPPY();
- return 0;
- }
- TEST_IMPL(fs_event_no_callback_on_close) {
- #if defined(NO_FS_EVENTS)
- RETURN_SKIP(NO_FS_EVENTS);
- #endif
- uv_loop_t* loop = uv_default_loop();
- int r;
- /* Setup */
- remove("watch_dir/file1");
- remove("watch_dir/");
- create_dir("watch_dir");
- create_file("watch_dir/file1");
- r = uv_fs_event_init(loop, &fs_event);
- ASSERT(r == 0);
- r = uv_fs_event_start(&fs_event,
- fs_event_cb_file,
- "watch_dir/file1",
- 0);
- ASSERT(r == 0);
- uv_close((uv_handle_t*)&fs_event, close_cb);
- uv_run(loop, UV_RUN_DEFAULT);
- ASSERT(fs_event_cb_called == 0);
- ASSERT(close_cb_called == 1);
- /* Cleanup */
- remove("watch_dir/file1");
- remove("watch_dir/");
- MAKE_VALGRIND_HAPPY();
- return 0;
- }
- static void timer_cb(uv_timer_t* handle) {
- int r;
- r = uv_fs_event_init(handle->loop, &fs_event);
- ASSERT(r == 0);
- r = uv_fs_event_start(&fs_event, fs_event_fail, ".", 0);
- ASSERT(r == 0);
- uv_close((uv_handle_t*)&fs_event, close_cb);
- uv_close((uv_handle_t*)handle, close_cb);
- }
- TEST_IMPL(fs_event_immediate_close) {
- #if defined(NO_FS_EVENTS)
- RETURN_SKIP(NO_FS_EVENTS);
- #endif
- uv_timer_t timer;
- uv_loop_t* loop;
- int r;
- loop = uv_default_loop();
- r = uv_timer_init(loop, &timer);
- ASSERT(r == 0);
- r = uv_timer_start(&timer, timer_cb, 1, 0);
- ASSERT(r == 0);
- uv_run(loop, UV_RUN_DEFAULT);
- ASSERT(close_cb_called == 2);
- MAKE_VALGRIND_HAPPY();
- return 0;
- }
- TEST_IMPL(fs_event_close_with_pending_event) {
- #if defined(NO_FS_EVENTS)
- RETURN_SKIP(NO_FS_EVENTS);
- #endif
- uv_loop_t* loop;
- int r;
- loop = uv_default_loop();
- create_dir("watch_dir");
- create_file("watch_dir/file");
- r = uv_fs_event_init(loop, &fs_event);
- ASSERT(r == 0);
- r = uv_fs_event_start(&fs_event, fs_event_fail, "watch_dir", 0);
- ASSERT(r == 0);
- /* Generate an fs event. */
- touch_file("watch_dir/file");
- uv_close((uv_handle_t*)&fs_event, close_cb);
- uv_run(loop, UV_RUN_DEFAULT);
- ASSERT(close_cb_called == 1);
- /* Clean up */
- remove("watch_dir/file");
- remove("watch_dir/");
- MAKE_VALGRIND_HAPPY();
- return 0;
- }
- TEST_IMPL(fs_event_close_in_callback) {
- #if defined(NO_FS_EVENTS)
- RETURN_SKIP(NO_FS_EVENTS);
- #elif defined(__MVS__)
- RETURN_SKIP("Directory watching not supported on this platform.");
- #endif
- uv_loop_t* loop;
- int r;
- loop = uv_default_loop();
- fs_event_unlink_files(NULL);
- create_dir("watch_dir");
- r = uv_fs_event_init(loop, &fs_event);
- ASSERT(r == 0);
- r = uv_fs_event_start(&fs_event, fs_event_cb_close, "watch_dir", 0);
- ASSERT(r == 0);
- r = uv_timer_init(loop, &timer);
- ASSERT(r == 0);
- r = uv_timer_start(&timer, fs_event_create_files, 100, 0);
- ASSERT(r == 0);
- uv_run(loop, UV_RUN_DEFAULT);
- uv_close((uv_handle_t*)&timer, close_cb);
- uv_run(loop, UV_RUN_ONCE);
- ASSERT(close_cb_called == 2);
- ASSERT(fs_event_cb_called == 3);
- /* Clean up */
- fs_event_unlink_files(NULL);
- remove("watch_dir/");
- MAKE_VALGRIND_HAPPY();
- return 0;
- }
- TEST_IMPL(fs_event_start_and_close) {
- #if defined(NO_FS_EVENTS)
- RETURN_SKIP(NO_FS_EVENTS);
- #endif
- uv_loop_t* loop;
- uv_fs_event_t fs_event1;
- uv_fs_event_t fs_event2;
- int r;
- loop = uv_default_loop();
- create_dir("watch_dir");
- r = uv_fs_event_init(loop, &fs_event1);
- ASSERT(r == 0);
- r = uv_fs_event_start(&fs_event1, fs_event_cb_dir, "watch_dir", 0);
- ASSERT(r == 0);
- r = uv_fs_event_init(loop, &fs_event2);
- ASSERT(r == 0);
- r = uv_fs_event_start(&fs_event2, fs_event_cb_dir, "watch_dir", 0);
- ASSERT(r == 0);
- uv_close((uv_handle_t*) &fs_event2, close_cb);
- uv_close((uv_handle_t*) &fs_event1, close_cb);
- uv_run(loop, UV_RUN_DEFAULT);
- ASSERT(close_cb_called == 2);
- remove("watch_dir/");
- MAKE_VALGRIND_HAPPY();
- return 0;
- }
- TEST_IMPL(fs_event_getpath) {
- #if defined(NO_FS_EVENTS)
- RETURN_SKIP(NO_FS_EVENTS);
- #endif
- uv_loop_t* loop = uv_default_loop();
- unsigned i;
- int r;
- char buf[1024];
- size_t len;
- const char* const watch_dir[] = {
- "watch_dir",
- "watch_dir/",
- "watch_dir///",
- "watch_dir/subfolder/..",
- "watch_dir//subfolder//..//",
- };
- create_dir("watch_dir");
- create_dir("watch_dir/subfolder");
- for (i = 0; i < ARRAY_SIZE(watch_dir); i++) {
- r = uv_fs_event_init(loop, &fs_event);
- ASSERT(r == 0);
- len = sizeof buf;
- r = uv_fs_event_getpath(&fs_event, buf, &len);
- ASSERT(r == UV_EINVAL);
- r = uv_fs_event_start(&fs_event, fail_cb, watch_dir[i], 0);
- ASSERT(r == 0);
- len = 0;
- r = uv_fs_event_getpath(&fs_event, buf, &len);
- ASSERT(r == UV_ENOBUFS);
- ASSERT(len < sizeof buf); /* sanity check */
- ASSERT(len == strlen(watch_dir[i]) + 1);
- r = uv_fs_event_getpath(&fs_event, buf, &len);
- ASSERT(r == 0);
- ASSERT(len == strlen(watch_dir[i]));
- ASSERT(strcmp(buf, watch_dir[i]) == 0);
- r = uv_fs_event_stop(&fs_event);
- ASSERT(r == 0);
- uv_close((uv_handle_t*) &fs_event, close_cb);
- uv_run(loop, UV_RUN_DEFAULT);
- ASSERT(close_cb_called == 1);
- close_cb_called = 0;
- }
- remove("watch_dir/");
- MAKE_VALGRIND_HAPPY();
- return 0;
- }
- #if defined(__APPLE__)
- static int fs_event_error_reported;
- static void fs_event_error_report_cb(uv_fs_event_t* handle,
- const char* filename,
- int events,
- int status) {
- if (status != 0)
- fs_event_error_reported = status;
- }
- static void timer_cb_nop(uv_timer_t* handle) {
- ++timer_cb_called;
- uv_close((uv_handle_t*) handle, close_cb);
- }
- static void fs_event_error_report_close_cb(uv_handle_t* handle) {
- ASSERT(handle != NULL);
- close_cb_called++;
- /* handle is allocated on-stack, no need to free it */
- }
- TEST_IMPL(fs_event_error_reporting) {
- unsigned int i;
- uv_loop_t loops[1024];
- uv_fs_event_t events[ARRAY_SIZE(loops)];
- uv_loop_t* loop;
- uv_fs_event_t* event;
- TEST_FILE_LIMIT(ARRAY_SIZE(loops) * 3);
- remove("watch_dir/");
- create_dir("watch_dir");
- /* Create a lot of loops, and start FSEventStream in each of them.
- * Eventually, this should create enough streams to make FSEventStreamStart()
- * fail.
- */
- for (i = 0; i < ARRAY_SIZE(loops); i++) {
- loop = &loops[i];
- ASSERT(0 == uv_loop_init(loop));
- event = &events[i];
- timer_cb_called = 0;
- close_cb_called = 0;
- ASSERT(0 == uv_fs_event_init(loop, event));
- ASSERT(0 == uv_fs_event_start(event,
- fs_event_error_report_cb,
- "watch_dir",
- 0));
- uv_unref((uv_handle_t*) event);
- /* Let loop run for some time */
- ASSERT(0 == uv_timer_init(loop, &timer));
- ASSERT(0 == uv_timer_start(&timer, timer_cb_nop, 2, 0));
- uv_run(loop, UV_RUN_DEFAULT);
- ASSERT(1 == timer_cb_called);
- ASSERT(1 == close_cb_called);
- if (fs_event_error_reported != 0)
- break;
- }
- /* At least one loop should fail */
- ASSERT(fs_event_error_reported == UV_EMFILE);
- /* Stop and close all events, and destroy loops */
- do {
- loop = &loops[i];
- event = &events[i];
- ASSERT(0 == uv_fs_event_stop(event));
- uv_ref((uv_handle_t*) event);
- uv_close((uv_handle_t*) event, fs_event_error_report_close_cb);
- close_cb_called = 0;
- uv_run(loop, UV_RUN_DEFAULT);
- ASSERT(close_cb_called == 1);
- uv_loop_close(loop);
- } while (i-- != 0);
- remove("watch_dir/");
- MAKE_VALGRIND_HAPPY();
- return 0;
- }
- #else /* !defined(__APPLE__) */
- TEST_IMPL(fs_event_error_reporting) {
- /* No-op, needed only for FSEvents backend */
- MAKE_VALGRIND_HAPPY();
- return 0;
- }
- #endif /* defined(__APPLE__) */
- TEST_IMPL(fs_event_watch_invalid_path) {
- #if defined(NO_FS_EVENTS)
- RETURN_SKIP(NO_FS_EVENTS);
- #endif
- uv_loop_t* loop;
- int r;
- loop = uv_default_loop();
- r = uv_fs_event_init(loop, &fs_event);
- ASSERT(r == 0);
- r = uv_fs_event_start(&fs_event, fs_event_cb_file, "<:;", 0);
- ASSERT(r != 0);
- ASSERT(uv_is_active((uv_handle_t*) &fs_event) == 0);
- r = uv_fs_event_start(&fs_event, fs_event_cb_file, "", 0);
- ASSERT(r != 0);
- ASSERT(uv_is_active((uv_handle_t*) &fs_event) == 0);
- MAKE_VALGRIND_HAPPY();
- return 0;
- }
|