@@ -1667,6 +1667,27 @@ static void Unlink(const FunctionCallbackInfo<Value>& args) {
1667
1667
}
1668
1668
}
1669
1669
1670
+ static void UnlinkSync (const FunctionCallbackInfo<Value>& args) {
1671
+ Environment* env = Environment::GetCurrent (args);
1672
+
1673
+ const int argc = args.Length ();
1674
+ CHECK_GE (argc, 1 );
1675
+
1676
+ BufferValue path (env->isolate (), args[0 ]);
1677
+ CHECK_NOT_NULL (*path);
1678
+ THROW_IF_INSUFFICIENT_PERMISSIONS (
1679
+ env, permission::PermissionScope::kFileSystemWrite , path.ToStringView ());
1680
+
1681
+ uv_fs_t req;
1682
+ auto make = OnScopeLeave ([&req]() { uv_fs_req_cleanup (&req); });
1683
+ FS_SYNC_TRACE_BEGIN (unlink );
1684
+ int err = uv_fs_unlink (nullptr , &req, *path, nullptr );
1685
+ FS_SYNC_TRACE_END (unlink );
1686
+ if (err < 0 ) {
1687
+ return env->ThrowUVException (err, " unlink" , nullptr , *path);
1688
+ }
1689
+ }
1690
+
1670
1691
static void RMDir (const FunctionCallbackInfo<Value>& args) {
1671
1692
Environment* env = Environment::GetCurrent (args);
1672
1693
@@ -2155,7 +2176,7 @@ static void OpenSync(const FunctionCallbackInfo<Value>& args) {
2155
2176
uv_fs_t req;
2156
2177
auto make = OnScopeLeave ([&req]() { uv_fs_req_cleanup (&req); });
2157
2178
FS_SYNC_TRACE_BEGIN (open );
2158
- int err = uv_fs_open (nullptr , &req, *path, flags, mode, nullptr );
2179
+ auto err = uv_fs_open (nullptr , &req, *path, flags, mode, nullptr );
2159
2180
FS_SYNC_TRACE_END (open );
2160
2181
if (err < 0 ) {
2161
2182
return env->ThrowUVException (err, " open" , nullptr , path.out ());
@@ -2546,30 +2567,41 @@ static void ReadFileUtf8(const FunctionCallbackInfo<Value>& args) {
2546
2567
2547
2568
CHECK_GE (args.Length (), 2 );
2548
2569
2549
- BufferValue path (env->isolate (), args[0 ]);
2550
- CHECK_NOT_NULL (*path);
2551
-
2552
2570
CHECK (args[1 ]->IsInt32 ());
2553
2571
const int flags = args[1 ].As <Int32>()->Value ();
2554
2572
2555
- if (CheckOpenPermissions (env, path, flags).IsNothing ()) return ;
2556
-
2573
+ uv_file file;
2557
2574
uv_fs_t req;
2558
- auto defer_req_cleanup = OnScopeLeave ([&req]() { uv_fs_req_cleanup (&req); });
2559
2575
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 ());
2576
+ bool is_fd = args[0 ]->IsInt32 ();
2577
+
2578
+ // Check for file descriptor
2579
+ if (is_fd) {
2580
+ file = args[0 ].As <Int32>()->Value ();
2581
+ } else {
2582
+ BufferValue path (env->isolate (), args[0 ]);
2583
+ CHECK_NOT_NULL (*path);
2584
+ if (CheckOpenPermissions (env, path, flags).IsNothing ()) return ;
2585
+
2586
+ FS_SYNC_TRACE_BEGIN (open );
2587
+ file = uv_fs_open (nullptr , &req, *path, flags, O_RDONLY, nullptr );
2588
+ FS_SYNC_TRACE_END (open );
2589
+ if (req.result < 0 ) {
2590
+ uv_fs_req_cleanup (&req);
2591
+ // req will be cleaned up by scope leave.
2592
+ return env->ThrowUVException (req.result , " open" , nullptr , path.out ());
2593
+ }
2566
2594
}
2567
2595
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);
2596
+ auto defer_close = OnScopeLeave ([file, is_fd, &req]() {
2597
+ if (!is_fd) {
2598
+ FS_SYNC_TRACE_BEGIN (close );
2599
+ CHECK_EQ (0 , uv_fs_close (nullptr , &req, file, nullptr ));
2600
+ FS_SYNC_TRACE_END (close );
2601
+ }
2602
+ uv_fs_req_cleanup (&req);
2572
2603
});
2604
+
2573
2605
std::string result{};
2574
2606
char buffer[8192 ];
2575
2607
uv_buf_t buf = uv_buf_init (buffer, sizeof (buffer));
@@ -2580,7 +2612,7 @@ static void ReadFileUtf8(const FunctionCallbackInfo<Value>& args) {
2580
2612
if (req.result < 0 ) {
2581
2613
FS_SYNC_TRACE_END (read );
2582
2614
// req will be cleaned up by scope leave.
2583
- return env->ThrowUVException (req.result , " read" , nullptr , path. out () );
2615
+ return env->ThrowUVException (req.result , " read" , nullptr );
2584
2616
}
2585
2617
if (r <= 0 ) {
2586
2618
break ;
@@ -3429,15 +3461,15 @@ static void CreatePerIsolateProperties(IsolateData* isolate_data,
3429
3461
Isolate* isolate = isolate_data->isolate ();
3430
3462
3431
3463
SetMethod (isolate, target, " access" , Access);
3432
- SetMethodNoSideEffect (isolate, target, " accessSync" , AccessSync);
3464
+ SetMethod (isolate, target, " accessSync" , AccessSync);
3433
3465
SetMethod (isolate, target, " close" , Close);
3434
3466
SetMethod (isolate, target, " closeSync" , CloseSync);
3435
- SetMethodNoSideEffect (isolate, target, " existsSync" , ExistsSync);
3467
+ SetMethod (isolate, target, " existsSync" , ExistsSync);
3436
3468
SetMethod (isolate, target, " open" , Open);
3437
3469
SetMethod (isolate, target, " openSync" , OpenSync);
3438
3470
SetMethod (isolate, target, " openFileHandle" , OpenFileHandle);
3439
3471
SetMethod (isolate, target, " read" , Read);
3440
- SetMethodNoSideEffect (isolate, target, " readFileUtf8" , ReadFileUtf8);
3472
+ SetMethod (isolate, target, " readFileUtf8" , ReadFileUtf8);
3441
3473
SetMethod (isolate, target, " readBuffers" , ReadBuffers);
3442
3474
SetMethod (isolate, target, " fdatasync" , Fdatasync);
3443
3475
SetMethod (isolate, target, " fsync" , Fsync);
@@ -3458,12 +3490,13 @@ static void CreatePerIsolateProperties(IsolateData* isolate_data,
3458
3490
SetMethod (isolate, target, " symlink" , Symlink);
3459
3491
SetMethod (isolate, target, " readlink" , ReadLink);
3460
3492
SetMethod (isolate, target, " unlink" , Unlink);
3493
+ SetMethod (isolate, target, " unlinkSync" , UnlinkSync);
3461
3494
SetMethod (isolate, target, " writeBuffer" , WriteBuffer);
3462
3495
SetMethod (isolate, target, " writeBuffers" , WriteBuffers);
3463
3496
SetMethod (isolate, target, " writeString" , WriteString);
3464
3497
SetMethod (isolate, target, " realpath" , RealPath);
3465
3498
SetMethod (isolate, target, " copyFile" , CopyFile);
3466
- SetMethodNoSideEffect (isolate, target, " copyFileSync" , CopyFileSync);
3499
+ SetMethod (isolate, target, " copyFileSync" , CopyFileSync);
3467
3500
3468
3501
SetMethod (isolate, target, " chmod" , Chmod);
3469
3502
SetMethod (isolate, target, " fchmod" , FChmod);
@@ -3586,6 +3619,7 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
3586
3619
registry->Register (Symlink);
3587
3620
registry->Register (ReadLink);
3588
3621
registry->Register (Unlink);
3622
+ registry->Register (UnlinkSync);
3589
3623
registry->Register (WriteBuffer);
3590
3624
registry->Register (WriteBuffers);
3591
3625
registry->Register (WriteString);
0 commit comments