Skip to content

Commit 57d7638

Browse files
committed
fs: throw fs.stat{Sync} errors in JS
PR-URL: nodejs#17914 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 791975d commit 57d7638

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

lib/fs.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,11 @@ fs.existsSync = function(path) {
441441
return false;
442442
}
443443
nullCheck(path);
444-
binding.stat(pathModule.toNamespacedPath(path));
444+
const ctx = { path };
445+
binding.stat(pathModule.toNamespacedPath(path), undefined, ctx);
446+
if (ctx.errno !== undefined) {
447+
return false;
448+
}
445449
return true;
446450
} catch (e) {
447451
return false;
@@ -1127,7 +1131,11 @@ fs.statSync = function(path) {
11271131
handleError((path = getPathFromURL(path)));
11281132
nullCheck(path);
11291133
validatePath(path);
1130-
binding.stat(pathModule.toNamespacedPath(path));
1134+
const ctx = { path };
1135+
binding.stat(pathModule.toNamespacedPath(path), undefined, ctx);
1136+
if (ctx.errno !== undefined) {
1137+
throw new errors.uvException(ctx);
1138+
}
11311139
return statsFromValues();
11321140
};
11331141

@@ -1927,7 +1935,11 @@ fs.realpathSync = function realpathSync(p, options) {
19271935
}
19281936
}
19291937
if (linkTarget === null) {
1930-
binding.stat(baseLong);
1938+
const ctx = { path: base };
1939+
binding.stat(baseLong, undefined, ctx);
1940+
if (ctx.errno !== undefined) {
1941+
throw new errors.uvException(ctx);
1942+
}
19311943
linkTarget = binding.readlink(baseLong);
19321944
}
19331945
resolvedLink = pathModule.resolve(previous, linkTarget);

src/node_file.cc

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,7 @@ static void InternalModuleStat(const FunctionCallbackInfo<Value>& args) {
539539

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

543544
CHECK_GE(args.Length(), 1);
544545

@@ -549,10 +550,14 @@ static void Stat(const FunctionCallbackInfo<Value>& args) {
549550
CHECK_EQ(args.Length(), 2);
550551
AsyncCall(env, args, "stat", UTF8, AfterStat,
551552
uv_fs_stat, *path);
552-
} else { // stat(path)
553-
SYNC_CALL(stat, *path, *path)
554-
FillStatsArray(env->fs_stats_field_array(),
555-
static_cast<const uv_stat_t*>(SYNC_REQ.ptr));
553+
} else { // stat(path, undefined, ctx)
554+
CHECK_EQ(args.Length(), 3);
555+
fs_req_wrap req_wrap;
556+
int err = SyncCall(env, args[2], &req_wrap, "stat", uv_fs_stat, *path);
557+
if (err == 0) {
558+
FillStatsArray(env->fs_stats_field_array(),
559+
static_cast<const uv_stat_t*>(req_wrap.req.ptr));
560+
}
556561
}
557562
}
558563

0 commit comments

Comments
 (0)