@@ -2190,7 +2190,7 @@ static void OpenSync(const FunctionCallbackInfo<Value>& args) {
2190
2190
uv_fs_t req;
2191
2191
auto make = OnScopeLeave ([&req]() { uv_fs_req_cleanup (&req); });
2192
2192
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 );
2194
2194
FS_SYNC_TRACE_END (open );
2195
2195
if (err < 0 ) {
2196
2196
return env->ThrowUVException (err, " open" , nullptr , path.out ());
@@ -2581,30 +2581,41 @@ static void ReadFileUtf8(const FunctionCallbackInfo<Value>& args) {
2581
2581
2582
2582
CHECK_GE (args.Length (), 2 );
2583
2583
2584
- BufferValue path (env->isolate (), args[0 ]);
2585
- CHECK_NOT_NULL (*path);
2586
-
2587
2584
CHECK (args[1 ]->IsInt32 ());
2588
2585
const int flags = args[1 ].As <Int32>()->Value ();
2589
2586
2590
- if (CheckOpenPermissions (env, path, flags).IsNothing ()) return ;
2591
-
2587
+ uv_file file;
2592
2588
uv_fs_t req;
2593
- auto defer_req_cleanup = OnScopeLeave ([&req]() { uv_fs_req_cleanup (&req); });
2594
2589
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
+ }
2601
2608
}
2602
2609
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);
2607
2617
});
2618
+
2608
2619
std::string result{};
2609
2620
char buffer[8192 ];
2610
2621
uv_buf_t buf = uv_buf_init (buffer, sizeof (buffer));
@@ -2615,7 +2626,7 @@ static void ReadFileUtf8(const FunctionCallbackInfo<Value>& args) {
2615
2626
if (req.result < 0 ) {
2616
2627
FS_SYNC_TRACE_END (read );
2617
2628
// 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 );
2619
2630
}
2620
2631
if (r <= 0 ) {
2621
2632
break ;
0 commit comments