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