From da7804f25914fe5a460db01c2e234dafc3649dfc Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Thu, 28 Dec 2017 14:51:05 +0800 Subject: [PATCH] fs: throw fs.lstat{Sync} errors in JS PR-URL: https://github.com/nodejs/node/pull/17914 Reviewed-By: Matteo Collina Reviewed-By: James M Snell --- lib/fs.js | 24 ++++++++++++++++++++---- src/node_file.cc | 13 +++++++++---- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/lib/fs.js b/lib/fs.js index 4cf7d51f09573b..6d389c8c53bb8c 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -1123,7 +1123,11 @@ fs.lstatSync = function(path) { handleError((path = getPathFromURL(path))); nullCheck(path); validatePath(path); - binding.lstat(pathModule.toNamespacedPath(path)); + const ctx = { path }; + binding.lstat(pathModule.toNamespacedPath(path), undefined, ctx); + if (ctx.errno !== undefined) { + throw new errors.uvException(ctx); + } return statsFromValues(); }; @@ -1874,7 +1878,11 @@ fs.realpathSync = function realpathSync(p, options) { // On windows, check that the root exists. On unix there is no need. if (isWindows && !knownHard[base]) { - binding.lstat(pathModule.toNamespacedPath(base)); + const ctx = { path: base }; + binding.lstat(pathModule.toNamespacedPath(base), undefined, ctx); + if (ctx.errno !== undefined) { + throw new errors.uvException(ctx); + } knownHard[base] = true; } @@ -1914,7 +1922,11 @@ fs.realpathSync = function realpathSync(p, options) { // for our internal use. var baseLong = pathModule.toNamespacedPath(base); - binding.lstat(baseLong); + const ctx = { path: base }; + binding.lstat(baseLong, undefined, ctx); + if (ctx.errno !== undefined) { + throw new errors.uvException(ctx); + } if ((statValues[1/*mode*/] & S_IFMT) !== S_IFLNK) { knownHard[base] = true; @@ -1957,7 +1969,11 @@ fs.realpathSync = function realpathSync(p, options) { // On windows, check that the root exists. On unix there is no need. if (isWindows && !knownHard[base]) { - binding.lstat(pathModule.toNamespacedPath(base)); + const ctx = { path: base }; + binding.lstat(pathModule.toNamespacedPath(base), undefined, ctx); + if (ctx.errno !== undefined) { + throw new errors.uvException(ctx); + } knownHard[base] = true; } } diff --git a/src/node_file.cc b/src/node_file.cc index 7054338c07f82d..9ca94550d2cf15 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -563,6 +563,7 @@ static void Stat(const FunctionCallbackInfo& args) { static void LStat(const FunctionCallbackInfo& args) { Environment* env = Environment::GetCurrent(args); + Local context = env->context(); CHECK_GE(args.Length(), 1); @@ -573,10 +574,14 @@ static void LStat(const FunctionCallbackInfo& args) { CHECK_EQ(args.Length(), 2); AsyncCall(env, args, "lstat", UTF8, AfterStat, uv_fs_lstat, *path); - } else { // lstat(path) - SYNC_CALL(lstat, *path, *path) - FillStatsArray(env->fs_stats_field_array(), - static_cast(SYNC_REQ.ptr)); + } else { // lstat(path, undefined, ctx) + CHECK_EQ(args.Length(), 3); + fs_req_wrap req_wrap; + int err = SyncCall(env, args[2], &req_wrap, "lstat", uv_fs_lstat, *path); + if (err == 0) { + FillStatsArray(env->fs_stats_field_array(), + static_cast(req_wrap.req.ptr)); + } } }