@@ -2190,7 +2190,7 @@ static void OpenSync(const FunctionCallbackInfo<Value>& args) {
21902190 uv_fs_t req;
21912191 auto make = OnScopeLeave ([&req]() { uv_fs_req_cleanup (&req); });
21922192 FS_SYNC_TRACE_BEGIN (open);
2193- int err = uv_fs_open (nullptr , &req, *path, flags, mode, nullptr );
2193+ auto err = uv_fs_open (nullptr , &req, *path, flags, mode, nullptr );
21942194 FS_SYNC_TRACE_END (open);
21952195 if (err < 0 ) {
21962196 return env->ThrowUVException (err, " open" , nullptr , path.out ());
@@ -2581,30 +2581,41 @@ static void ReadFileUtf8(const FunctionCallbackInfo<Value>& args) {
25812581
25822582 CHECK_GE (args.Length (), 2 );
25832583
2584- BufferValue path (env->isolate (), args[0 ]);
2585- CHECK_NOT_NULL (*path);
2586-
25872584 CHECK (args[1 ]->IsInt32 ());
25882585 const int flags = args[1 ].As <Int32>()->Value ();
25892586
2590- if (CheckOpenPermissions (env, path, flags).IsNothing ()) return ;
2591-
2587+ uv_file file;
25922588 uv_fs_t req;
2593- auto defer_req_cleanup = OnScopeLeave ([&req]() { uv_fs_req_cleanup (&req); });
25942589
2595- FS_SYNC_TRACE_BEGIN (open);
2596- uv_file file = uv_fs_open (nullptr , &req, *path, flags, 438 , nullptr );
2597- FS_SYNC_TRACE_END (open);
2598- if (req.result < 0 ) {
2599- // req will be cleaned up by scope leave.
2600- return env->ThrowUVException (req.result , " open" , nullptr , path.out ());
2590+ bool is_fd = args[0 ]->IsInt32 ();
2591+
2592+ // Check for file descriptor
2593+ if (is_fd) {
2594+ file = args[0 ].As <Int32>()->Value ();
2595+ } else {
2596+ BufferValue path (env->isolate (), args[0 ]);
2597+ CHECK_NOT_NULL (*path);
2598+ if (CheckOpenPermissions (env, path, flags).IsNothing ()) return ;
2599+
2600+ FS_SYNC_TRACE_BEGIN (open);
2601+ file = uv_fs_open (nullptr , &req, *path, flags, O_RDONLY, nullptr );
2602+ FS_SYNC_TRACE_END (open);
2603+ if (req.result < 0 ) {
2604+ uv_fs_req_cleanup (&req);
2605+ // req will be cleaned up by scope leave.
2606+ return env->ThrowUVException (req.result , " open" , nullptr , path.out ());
2607+ }
26012608 }
26022609
2603- auto defer_close = OnScopeLeave ([file]() {
2604- uv_fs_t close_req;
2605- CHECK_EQ (0 , uv_fs_close (nullptr , &close_req, file, nullptr ));
2606- uv_fs_req_cleanup (&close_req);
2610+ auto defer_close = OnScopeLeave ([file, is_fd, &req]() {
2611+ if (!is_fd) {
2612+ FS_SYNC_TRACE_BEGIN (close);
2613+ CHECK_EQ (0 , uv_fs_close (nullptr , &req, file, nullptr ));
2614+ FS_SYNC_TRACE_END (close);
2615+ }
2616+ uv_fs_req_cleanup (&req);
26072617 });
2618+
26082619 std::string result{};
26092620 char buffer[8192 ];
26102621 uv_buf_t buf = uv_buf_init (buffer, sizeof (buffer));
@@ -2615,7 +2626,7 @@ static void ReadFileUtf8(const FunctionCallbackInfo<Value>& args) {
26152626 if (req.result < 0 ) {
26162627 FS_SYNC_TRACE_END (read);
26172628 // req will be cleaned up by scope leave.
2618- return env->ThrowUVException (req.result , " read" , nullptr , path. out () );
2629+ return env->ThrowUVException (req.result , " read" , nullptr );
26192630 }
26202631 if (r <= 0 ) {
26212632 break ;
0 commit comments