From a7ac2c462a2ce78f86f991ca1482b61a37fd6b8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sa=C3=BAl=20Ibarra=20Corretg=C3=A9?= Date: Mon, 24 Feb 2014 00:51:13 +0100 Subject: [PATCH] unix, windows: add uv_fs_event_getpath --- include/uv.h | 9 +++++++++ src/uv-common.c | 20 ++++++++++++++++++++ test/test-fs-event.c | 32 ++++++++++++++++++++++++++++++++ test/test-list.h | 2 ++ 4 files changed, 63 insertions(+) diff --git a/include/uv.h b/include/uv.h index d8ee5f1c..2df6c89e 100644 --- a/include/uv.h +++ b/include/uv.h @@ -2035,6 +2035,15 @@ UV_EXTERN int uv_fs_event_start(uv_fs_event_t* handle, UV_EXTERN int uv_fs_event_stop(uv_fs_event_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_event_getpath(uv_fs_event_t* handle, + char* buf, + size_t* len); + /* Utility */ diff --git a/src/uv-common.c b/src/uv-common.c index e5fc5077..26a8df08 100644 --- a/src/uv-common.c +++ b/src/uv-common.c @@ -444,3 +444,23 @@ int uv__getaddrinfo_translate_error(int sys_err) { abort(); return 0; /* Pacify compiler. */ } + +int uv_fs_event_getpath(uv_fs_event_t* handle, char* buf, size_t* len) { + size_t required_len; + + if (!uv__is_active(handle)) { + *len = 0; + return UV_EINVAL; + } + + required_len = strlen(handle->path) + 1; + if (required_len > *len) { + *len = 0; + return UV_ENOBUFS; + } + + memcpy(buf, handle->path, required_len); + *len = required_len; + + return 0; +} diff --git a/test/test-fs-event.c b/test/test-fs-event.c index 9b14a5c3..fec5fd67 100644 --- a/test/test-fs-event.c +++ b/test/test-fs-event.c @@ -628,6 +628,38 @@ TEST_IMPL(fs_event_start_and_close) { return 0; } +TEST_IMPL(fs_event_getpath) { + uv_loop_t* loop = uv_default_loop(); + int r; + char buf[1024]; + size_t len; + + create_dir(loop, "watch_dir"); + + 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", 0); + ASSERT(r == 0); + len = sizeof buf; + r = uv_fs_event_getpath(&fs_event, buf, &len); + ASSERT(r == 0); + ASSERT(memcmp(buf, "watch_dir", len) == 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); + + remove("watch_dir/"); + MAKE_VALGRIND_HAPPY(); + return 0; +} + #if defined(__APPLE__) static int fs_event_error_reported; diff --git a/test/test-list.h b/test/test-list.h index 3c6509f5..29cef4f9 100644 --- a/test/test-list.h +++ b/test/test-list.h @@ -207,6 +207,7 @@ TEST_DECLARE (fs_event_close_with_pending_event) TEST_DECLARE (fs_event_close_in_callback) TEST_DECLARE (fs_event_start_and_close) TEST_DECLARE (fs_event_error_reporting) +TEST_DECLARE (fs_event_getpath) TEST_DECLARE (fs_readdir_empty_dir) TEST_DECLARE (fs_readdir_file) TEST_DECLARE (fs_open_dir) @@ -527,6 +528,7 @@ TASK_LIST_START TEST_ENTRY (fs_event_close_in_callback) TEST_ENTRY (fs_event_start_and_close) TEST_ENTRY (fs_event_error_reporting) + TEST_ENTRY (fs_event_getpath) TEST_ENTRY (fs_readdir_empty_dir) TEST_ENTRY (fs_readdir_file) TEST_ENTRY (fs_open_dir)