unix, windows: add uv_fs_poll_getpath

This commit is contained in:
Saúl Ibarra Corretgé 2014-02-24 22:51:59 +01:00
parent a7ac2c462a
commit 8c9d5dce57
4 changed files with 73 additions and 0 deletions

View File

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

View File

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

View File

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

View File

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