fs: throw fs.stat{Sync} errors in JS

PR-URL: https://github.com/nodejs/node/pull/17914
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Joyee Cheung 2017-12-28 04:01:45 +08:00
parent 791975d189
commit 57d7638af3
No known key found for this signature in database
GPG Key ID: F586868AAD831D0C
2 changed files with 24 additions and 7 deletions

View File

@ -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);

View File

@ -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);
@ -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));
}
}
}