unix, windows: add uv_fs_event_getpath

This commit is contained in:
Saúl Ibarra Corretgé 2014-02-24 00:51:13 +01:00
parent e5bdef9b59
commit a7ac2c462a
4 changed files with 63 additions and 0 deletions

View File

@ -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 */

View File

@ -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;
}

View File

@ -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;

View File

@ -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)