From 57d7638af3dab44129d6008f356b9b1598d72d51 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Thu, 28 Dec 2017 04:01:45 +0800 Subject: [PATCH] fs: throw fs.stat{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 | 18 +++++++++++++++--- src/node_file.cc | 13 +++++++++---- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/lib/fs.js b/lib/fs.js index 77c6998e531e1c..4cf7d51f09573b 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -441,7 +441,11 @@ fs.existsSync = function(path) { return false; } nullCheck(path); - binding.stat(pathModule.toNamespacedPath(path)); + const ctx = { path }; + binding.stat(pathModule.toNamespacedPath(path), undefined, ctx); + if (ctx.errno !== undefined) { + return false; + } return true; } catch (e) { return false; @@ -1127,7 +1131,11 @@ fs.statSync = function(path) { handleError((path = getPathFromURL(path))); nullCheck(path); validatePath(path); - binding.stat(pathModule.toNamespacedPath(path)); + const ctx = { path }; + binding.stat(pathModule.toNamespacedPath(path), undefined, ctx); + if (ctx.errno !== undefined) { + throw new errors.uvException(ctx); + } return statsFromValues(); }; @@ -1927,7 +1935,11 @@ fs.realpathSync = function realpathSync(p, options) { } } if (linkTarget === null) { - binding.stat(baseLong); + const ctx = { path: base }; + binding.stat(baseLong, undefined, ctx); + if (ctx.errno !== undefined) { + throw new errors.uvException(ctx); + } linkTarget = binding.readlink(baseLong); } resolvedLink = pathModule.resolve(previous, linkTarget); diff --git a/src/node_file.cc b/src/node_file.cc index 4566f08c1a6454..7054338c07f82d 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -539,6 +539,7 @@ static void InternalModuleStat(const FunctionCallbackInfo& args) { static void Stat(const FunctionCallbackInfo& args) { Environment* env = Environment::GetCurrent(args); + Local context = env->context(); CHECK_GE(args.Length(), 1); @@ -549,10 +550,14 @@ static void Stat(const FunctionCallbackInfo& args) { CHECK_EQ(args.Length(), 2); AsyncCall(env, args, "stat", UTF8, AfterStat, uv_fs_stat, *path); - } else { // stat(path) - SYNC_CALL(stat, *path, *path) - FillStatsArray(env->fs_stats_field_array(), - static_cast(SYNC_REQ.ptr)); + } else { // stat(path, undefined, ctx) + CHECK_EQ(args.Length(), 3); + fs_req_wrap req_wrap; + int err = SyncCall(env, args[2], &req_wrap, "stat", uv_fs_stat, *path); + if (err == 0) { + FillStatsArray(env->fs_stats_field_array(), + static_cast(req_wrap.req.ptr)); + } } }