Skip to content

Commit

Permalink
fs: throw errors from fs.linkSync in JS
Browse files Browse the repository at this point in the history
PR-URL: nodejs#18348
Refs: nodejs#18106
Reviewed-By: Michaël Zasso <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
joyeecheung authored and MayaLekova committed May 8, 2018
1 parent 8ab1549 commit 829f8b7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
11 changes: 9 additions & 2 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -1280,8 +1280,15 @@ fs.linkSync = function(existingPath, newPath) {
nullCheck(newPath);
validatePath(existingPath, 'existingPath');
validatePath(newPath, 'newPath');
return binding.link(pathModule.toNamespacedPath(existingPath),
pathModule.toNamespacedPath(newPath));

const ctx = { path: existingPath, dest: newPath };
const result = binding.link(pathModule.toNamespacedPath(existingPath),
pathModule.toNamespacedPath(newPath),
undefined, ctx);
if (ctx.errno !== undefined) {
throw new errors.uvException(ctx);
}
return result;
};

fs.unlink = function(path, callback) {
Expand Down
12 changes: 8 additions & 4 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,8 @@ static void Symlink(const FunctionCallbackInfo<Value>& args) {
static void Link(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);

CHECK_GE(args.Length(), 2);
int argc = args.Length();
CHECK_GE(argc, 3);

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

if (args[2]->IsObject()) { // link(src, dest, req)
CHECK_EQ(args.Length(), 3);
CHECK_EQ(argc, 3);
AsyncDestCall(env, args, "link", *dest, dest.length(), UTF8,
AfterNoArgs, uv_fs_link, *src, *dest);
} else { // link(src, dest)
SYNC_DEST_CALL(link, *src, *dest, *src, *dest)
} else { // link(src, dest, undefined, ctx)
CHECK_EQ(argc, 4);
fs_req_wrap req_wrap;
SyncCall(env, args[3], &req_wrap, "link",
uv_fs_link, *src, *dest);
}
}

Expand Down

0 comments on commit 829f8b7

Please sign in to comment.