From 167e22937c612e18678e45e73a89dcc27e72fdc5 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Wed, 24 Jan 2018 07:03:11 +0800 Subject: [PATCH] fs: throw errors from fs.linkSync in JS MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/18348 Refs: https://github.com/nodejs/node/issues/18106 Reviewed-By: Michaƫl Zasso Reviewed-By: James M Snell --- lib/fs.js | 11 +++++++++-- src/node_file.cc | 12 ++++++++---- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/lib/fs.js b/lib/fs.js index c75a8501b67..4a69ef9bc46 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -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) { diff --git a/src/node_file.cc b/src/node_file.cc index d67d45d4a82..0129d2d3f99 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -624,7 +624,8 @@ static void Symlink(const FunctionCallbackInfo& args) { static void Link(const FunctionCallbackInfo& 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); @@ -633,11 +634,14 @@ static void Link(const FunctionCallbackInfo& 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); } }