Skip to content

Commit

Permalink
fs: add uv_open_osfhandle
Browse files Browse the repository at this point in the history
Adds uv_open_osfhandle to complete uv_get_osfhandle

Ref: nodejs/node#15433
Ref: nodejs/node-addon-api#304
  • Loading branch information
bzoz committed Jul 19, 2018
1 parent 31a06f2 commit d2a00b5
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 0 deletions.
9 changes: 9 additions & 0 deletions docs/src/fs.rst
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,15 @@ Helper functions
.. versionadded:: 1.12.0
.. c:function:: int uv_open_osfhandle(int os_fd)
For a OS-dependent handle, get the file descriptor in the C runtime.
On UNIX, returns the ``os_fd`` intact. On Windows, this calls `_open_osfhandle <https://msdn.microsoft.com/en-us/library/bdts1c9x.aspx>`_.
Note that the return value is still owned by the CRT,
any attempts to close it or to use it after closing the handle may lead to malfunction.
.. versionadded:: 1.22.1
File open constants
-------------------
Expand Down
1 change: 1 addition & 0 deletions include/uv.h
Original file line number Diff line number Diff line change
Expand Up @@ -1065,6 +1065,7 @@ UV_EXTERN int uv_set_process_title(const char* title);
UV_EXTERN int uv_resident_set_memory(size_t* rss);
UV_EXTERN int uv_uptime(double* uptime);
UV_EXTERN uv_os_fd_t uv_get_osfhandle(int fd);
UV_EXTERN int uv_open_osfhandle(uv_os_fd_t os_fd);

typedef struct {
long tv_sec;
Expand Down
3 changes: 3 additions & 0 deletions src/unix/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -1338,6 +1338,9 @@ uv_os_fd_t uv_get_osfhandle(int fd) {
return fd;
}

int uv_open_osfhandle(uv_os_fd_t os_fd) {
return os_fd;
}

uv_pid_t uv_os_getpid(void) {
return getpid();
Expand Down
4 changes: 4 additions & 0 deletions src/win/handle.c
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,7 @@ int uv_is_closing(const uv_handle_t* handle) {
uv_os_fd_t uv_get_osfhandle(int fd) {
return uv__get_osfhandle(fd);
}

int uv_open_osfhandle(uv_os_fd_t os_fd) {
return _open_osfhandle((intptr_t) os_fd, 0);
}
46 changes: 46 additions & 0 deletions test/test-fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -3069,6 +3069,52 @@ TEST_IMPL(get_osfhandle_valid_handle) {
return 0;
}

TEST_IMPL(open_osfhandle_valid_handle) {
int r;
uv_os_fd_t handle;
int fd;

/* Setup. */
unlink("test_file");

loop = uv_default_loop();

r = uv_fs_open(NULL,
&open_req1,
"test_file",
O_RDWR | O_CREAT,
S_IWUSR | S_IRUSR,
NULL);
ASSERT(r >= 0);
ASSERT(open_req1.result >= 0);
uv_fs_req_cleanup(&open_req1);

handle = uv_get_osfhandle(open_req1.result);
#ifdef _WIN32
ASSERT(handle != INVALID_HANDLE_VALUE);
#else
ASSERT(handle >= 0);
#endif

fd = uv_open_osfhandle(handle);
#ifdef _WIN32
ASSERT(fd > 0);
#else
ASSERT(fd == open_req1.result);
#endif

r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
ASSERT(r == 0);
ASSERT(close_req.result == 0);
uv_fs_req_cleanup(&close_req);

/* Cleanup. */
unlink("test_file");

MAKE_VALGRIND_HAPPY();
return 0;
}

TEST_IMPL(fs_file_pos_after_op_with_offset) {
int r;

Expand Down
2 changes: 2 additions & 0 deletions test/test-list.h
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ TEST_DECLARE (fs_rename_to_existing_file)
TEST_DECLARE (fs_write_multiple_bufs)
TEST_DECLARE (fs_read_write_null_arguments)
TEST_DECLARE (get_osfhandle_valid_handle)
TEST_DECLARE (open_osfhandle_valid_handle)
TEST_DECLARE (fs_write_alotof_bufs)
TEST_DECLARE (fs_write_alotof_bufs_with_offset)
TEST_DECLARE (fs_file_pos_after_op_with_offset)
Expand Down Expand Up @@ -887,6 +888,7 @@ TASK_LIST_START
TEST_ENTRY (fs_fchmod_archive_readonly)
#endif
TEST_ENTRY (get_osfhandle_valid_handle)
TEST_ENTRY (open_osfhandle_valid_handle)
TEST_ENTRY (threadpool_queue_work_simple)
TEST_ENTRY (threadpool_queue_work_einval)
TEST_ENTRY (threadpool_multiple_event_loops)
Expand Down

0 comments on commit d2a00b5

Please sign in to comment.