Skip to content

Commit 167e229

Browse files
committed
fs: throw errors from fs.linkSync in JS
PR-URL: #18348 Refs: #18106 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 32bf0f6 commit 167e229

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

lib/fs.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1280,8 +1280,15 @@ fs.linkSync = function(existingPath, newPath) {
12801280
nullCheck(newPath);
12811281
validatePath(existingPath, 'existingPath');
12821282
validatePath(newPath, 'newPath');
1283-
return binding.link(pathModule.toNamespacedPath(existingPath),
1284-
pathModule.toNamespacedPath(newPath));
1283+
1284+
const ctx = { path: existingPath, dest: newPath };
1285+
const result = binding.link(pathModule.toNamespacedPath(existingPath),
1286+
pathModule.toNamespacedPath(newPath),
1287+
undefined, ctx);
1288+
if (ctx.errno !== undefined) {
1289+
throw new errors.uvException(ctx);
1290+
}
1291+
return result;
12851292
};
12861293

12871294
fs.unlink = function(path, callback) {

src/node_file.cc

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,8 @@ static void Symlink(const FunctionCallbackInfo<Value>& args) {
624624
static void Link(const FunctionCallbackInfo<Value>& args) {
625625
Environment* env = Environment::GetCurrent(args);
626626

627-
CHECK_GE(args.Length(), 2);
627+
int argc = args.Length();
628+
CHECK_GE(argc, 3);
628629

629630
BufferValue src(env->isolate(), args[0]);
630631
CHECK_NE(*src, nullptr);
@@ -633,11 +634,14 @@ static void Link(const FunctionCallbackInfo<Value>& args) {
633634
CHECK_NE(*dest, nullptr);
634635

635636
if (args[2]->IsObject()) { // link(src, dest, req)
636-
CHECK_EQ(args.Length(), 3);
637+
CHECK_EQ(argc, 3);
637638
AsyncDestCall(env, args, "link", *dest, dest.length(), UTF8,
638639
AfterNoArgs, uv_fs_link, *src, *dest);
639-
} else { // link(src, dest)
640-
SYNC_DEST_CALL(link, *src, *dest, *src, *dest)
640+
} else { // link(src, dest, undefined, ctx)
641+
CHECK_EQ(argc, 4);
642+
fs_req_wrap req_wrap;
643+
SyncCall(env, args[3], &req_wrap, "link",
644+
uv_fs_link, *src, *dest);
641645
}
642646
}
643647

0 commit comments

Comments
 (0)