@@ -2155,7 +2155,7 @@ static void OpenSync(const FunctionCallbackInfo<Value>& args) {
2155
2155
uv_fs_t req;
2156
2156
auto make = OnScopeLeave ([&req]() { uv_fs_req_cleanup (&req); });
2157
2157
FS_SYNC_TRACE_BEGIN (open );
2158
- int err = uv_fs_open (nullptr , &req, *path, flags, mode, nullptr );
2158
+ auto err = uv_fs_open (nullptr , &req, *path, flags, mode, nullptr );
2159
2159
FS_SYNC_TRACE_END (open );
2160
2160
if (err < 0 ) {
2161
2161
return env->ThrowUVException (err, " open" , nullptr , path.out ());
@@ -2546,30 +2546,41 @@ static void ReadFileUtf8(const FunctionCallbackInfo<Value>& args) {
2546
2546
2547
2547
CHECK_GE (args.Length (), 2 );
2548
2548
2549
- BufferValue path (env->isolate (), args[0 ]);
2550
- CHECK_NOT_NULL (*path);
2551
-
2552
2549
CHECK (args[1 ]->IsInt32 ());
2553
2550
const int flags = args[1 ].As <Int32>()->Value ();
2554
2551
2555
- if (CheckOpenPermissions (env, path, flags).IsNothing ()) return ;
2556
-
2552
+ uv_file file;
2557
2553
uv_fs_t req;
2558
- auto defer_req_cleanup = OnScopeLeave ([&req]() { uv_fs_req_cleanup (&req); });
2559
2554
2560
- FS_SYNC_TRACE_BEGIN (open );
2561
- uv_file file = uv_fs_open (nullptr , &req, *path, flags, 438 , nullptr );
2562
- FS_SYNC_TRACE_END (open );
2563
- if (req.result < 0 ) {
2564
- // req will be cleaned up by scope leave.
2565
- return env->ThrowUVException (req.result , " open" , nullptr , path.out ());
2555
+ bool is_fd = args[0 ]->IsInt32 ();
2556
+
2557
+ // Check for file descriptor
2558
+ if (is_fd) {
2559
+ file = args[0 ].As <Int32>()->Value ();
2560
+ } else {
2561
+ BufferValue path (env->isolate (), args[0 ]);
2562
+ CHECK_NOT_NULL (*path);
2563
+ if (CheckOpenPermissions (env, path, flags).IsNothing ()) return ;
2564
+
2565
+ FS_SYNC_TRACE_BEGIN (open );
2566
+ file = uv_fs_open (nullptr , &req, *path, flags, O_RDONLY, nullptr );
2567
+ FS_SYNC_TRACE_END (open );
2568
+ if (req.result < 0 ) {
2569
+ uv_fs_req_cleanup (&req);
2570
+ // req will be cleaned up by scope leave.
2571
+ return env->ThrowUVException (req.result , " open" , nullptr , path.out ());
2572
+ }
2566
2573
}
2567
2574
2568
- auto defer_close = OnScopeLeave ([file]() {
2569
- uv_fs_t close_req;
2570
- CHECK_EQ (0 , uv_fs_close (nullptr , &close_req, file, nullptr ));
2571
- uv_fs_req_cleanup (&close_req);
2575
+ auto defer_close = OnScopeLeave ([file, is_fd, &req]() {
2576
+ if (!is_fd) {
2577
+ FS_SYNC_TRACE_BEGIN (close );
2578
+ CHECK_EQ (0 , uv_fs_close (nullptr , &req, file, nullptr ));
2579
+ FS_SYNC_TRACE_END (close );
2580
+ }
2581
+ uv_fs_req_cleanup (&req);
2572
2582
});
2583
+
2573
2584
std::string result{};
2574
2585
char buffer[8192 ];
2575
2586
uv_buf_t buf = uv_buf_init (buffer, sizeof (buffer));
@@ -2580,7 +2591,7 @@ static void ReadFileUtf8(const FunctionCallbackInfo<Value>& args) {
2580
2591
if (req.result < 0 ) {
2581
2592
FS_SYNC_TRACE_END (read );
2582
2593
// req will be cleaned up by scope leave.
2583
- return env->ThrowUVException (req.result , " read" , nullptr , path. out () );
2594
+ return env->ThrowUVException (req.result , " read" , nullptr );
2584
2595
}
2585
2596
if (r <= 0 ) {
2586
2597
break ;
0 commit comments