fs: throw fs.lstat{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 14:51:05 +08:00
parent 57d7638af3
commit da7804f259
No known key found for this signature in database
GPG Key ID: F586868AAD831D0C
2 changed files with 29 additions and 8 deletions

View File

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

View File

@ -563,6 +563,7 @@ static void Stat(const FunctionCallbackInfo<Value>& args) {
static void LStat(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
Local<Context> context = env->context();
CHECK_GE(args.Length(), 1);
@ -573,10 +574,14 @@ static void LStat(const FunctionCallbackInfo<Value>& 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<const uv_stat_t*>(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<const uv_stat_t*>(req_wrap.req.ptr));
}
}
}