diff --git a/include/uv.h b/include/uv.h index 2df6c89ea..336ce27a7 100644 --- a/include/uv.h +++ b/include/uv.h @@ -1932,6 +1932,13 @@ UV_EXTERN int uv_fs_poll_start(uv_fs_poll_t* handle, UV_EXTERN int uv_fs_poll_stop(uv_fs_poll_t* handle); +/* + * Get the path befing monitored by the handle. The buffer must be preallocated + * by the user. Returns 0 on success or an error code < 0 in case of failure. + * On sucess, `buf` will contain the path and `len` its length. + */ +UV_EXTERN int uv_fs_poll_getpath(uv_fs_poll_t* handle, char* buf, size_t* len); + /* * UNIX signal handling on a per-event loop basis. The implementation is not diff --git a/src/fs-poll.c b/src/fs-poll.c index 7fdaaeb17..b3c7ac99d 100644 --- a/src/fs-poll.c +++ b/src/fs-poll.c @@ -118,6 +118,31 @@ int uv_fs_poll_stop(uv_fs_poll_t* handle) { } +int uv_fs_poll_getpath(uv_fs_poll_t* handle, char* buf, size_t* len) { + struct poll_ctx* ctx; + size_t required_len; + + if (!uv__is_active(handle)) { + *len = 0; + return UV_EINVAL; + } + + ctx = handle->poll_ctx; + assert(ctx != NULL); + + required_len = strlen(ctx->path) + 1; + if (required_len > *len) { + *len = 0; + return UV_ENOBUFS; + } + + memcpy(buf, ctx->path, required_len); + *len = required_len; + + return 0; +} + + void uv__fs_poll_close(uv_fs_poll_t* handle) { uv_fs_poll_stop(handle); } diff --git a/test/test-fs-poll.c b/test/test-fs-poll.c index 9213f04b3..27c6c3c3d 100644 --- a/test/test-fs-poll.c +++ b/test/test-fs-poll.c @@ -33,6 +33,11 @@ static void poll_cb(uv_fs_poll_t* handle, const uv_stat_t* prev, const uv_stat_t* curr); +static void poll_cb_fail(uv_fs_poll_t* handle, + int status, + const uv_stat_t* prev, + const uv_stat_t* curr); + static uv_fs_poll_t poll_handle; static uv_timer_t timer_handle; static uv_loop_t* loop; @@ -72,6 +77,14 @@ static void timer_cb(uv_timer_t* handle, int status) { } +static void poll_cb_fail(uv_fs_poll_t* handle, + int status, + const uv_stat_t* prev, + const uv_stat_t* curr) { + ASSERT(0 && "fail_cb called"); +} + + static void poll_cb(uv_fs_poll_t* handle, int status, const uv_stat_t* prev, @@ -144,3 +157,29 @@ TEST_IMPL(fs_poll) { MAKE_VALGRIND_HAPPY(); return 0; } + + +TEST_IMPL(fs_poll_getpath) { + char buf[1024]; + size_t len; + loop = uv_default_loop(); + + remove(FIXTURE); + + ASSERT(0 == uv_fs_poll_init(loop, &poll_handle)); + len = sizeof buf; + ASSERT(UV_EINVAL == uv_fs_poll_getpath(&poll_handle, buf, &len)); + ASSERT(0 == uv_fs_poll_start(&poll_handle, poll_cb_fail, FIXTURE, 100)); + len = sizeof buf; + ASSERT(0 == uv_fs_poll_getpath(&poll_handle, buf, &len)); + ASSERT(0 == memcmp(buf, FIXTURE, len)); + + uv_close((uv_handle_t*) &poll_handle, close_cb); + + ASSERT(0 == uv_run(loop, UV_RUN_DEFAULT)); + + ASSERT(close_cb_called == 1); + + MAKE_VALGRIND_HAPPY(); + return 0; +} diff --git a/test/test-list.h b/test/test-list.h index 29cef4f93..beefcb7b5 100644 --- a/test/test-list.h +++ b/test/test-list.h @@ -176,6 +176,7 @@ TEST_DECLARE (spawn_stdout_to_file) TEST_DECLARE (spawn_stdout_and_stderr_to_file) TEST_DECLARE (spawn_auto_unref) TEST_DECLARE (fs_poll) +TEST_DECLARE (fs_poll_getpath) TEST_DECLARE (kill) TEST_DECLARE (fs_file_noent) TEST_DECLARE (fs_file_nametoolong) @@ -475,6 +476,7 @@ TASK_LIST_START TEST_ENTRY (spawn_stdout_and_stderr_to_file) TEST_ENTRY (spawn_auto_unref) TEST_ENTRY (fs_poll) + TEST_ENTRY (fs_poll_getpath) TEST_ENTRY (kill) #ifdef _WIN32