Skip to content

Commit

Permalink
fs: throw fs.stat{Sync} errors in JS
Browse files Browse the repository at this point in the history
PR-URL: nodejs#17914
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
joyeecheung committed Jan 16, 2018
1 parent 791975d commit 57d7638
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
18 changes: 15 additions & 3 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
};

Expand Down Expand Up @@ -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);
Expand Down
13 changes: 9 additions & 4 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,7 @@ static void InternalModuleStat(const FunctionCallbackInfo<Value>& args) {

static void Stat(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
Local<Context> context = env->context();

CHECK_GE(args.Length(), 1);

Expand All @@ -549,10 +550,14 @@ static void Stat(const FunctionCallbackInfo<Value>& 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<const uv_stat_t*>(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<const uv_stat_t*>(req_wrap.req.ptr));
}
}
}

Expand Down

0 comments on commit 57d7638

Please sign in to comment.