Skip to content

Commit da7804f

Browse files
committedJan 16, 2018
fs: throw fs.lstat{Sync} errors in JS
PR-URL: nodejs#17914 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 57d7638 commit da7804f

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed
 

‎lib/fs.js

+20-4
Original file line numberDiff line numberDiff line change
@@ -1123,7 +1123,11 @@ fs.lstatSync = function(path) {
11231123
handleError((path = getPathFromURL(path)));
11241124
nullCheck(path);
11251125
validatePath(path);
1126-
binding.lstat(pathModule.toNamespacedPath(path));
1126+
const ctx = { path };
1127+
binding.lstat(pathModule.toNamespacedPath(path), undefined, ctx);
1128+
if (ctx.errno !== undefined) {
1129+
throw new errors.uvException(ctx);
1130+
}
11271131
return statsFromValues();
11281132
};
11291133

@@ -1874,7 +1878,11 @@ fs.realpathSync = function realpathSync(p, options) {
18741878

18751879
// On windows, check that the root exists. On unix there is no need.
18761880
if (isWindows && !knownHard[base]) {
1877-
binding.lstat(pathModule.toNamespacedPath(base));
1881+
const ctx = { path: base };
1882+
binding.lstat(pathModule.toNamespacedPath(base), undefined, ctx);
1883+
if (ctx.errno !== undefined) {
1884+
throw new errors.uvException(ctx);
1885+
}
18781886
knownHard[base] = true;
18791887
}
18801888

@@ -1914,7 +1922,11 @@ fs.realpathSync = function realpathSync(p, options) {
19141922
// for our internal use.
19151923

19161924
var baseLong = pathModule.toNamespacedPath(base);
1917-
binding.lstat(baseLong);
1925+
const ctx = { path: base };
1926+
binding.lstat(baseLong, undefined, ctx);
1927+
if (ctx.errno !== undefined) {
1928+
throw new errors.uvException(ctx);
1929+
}
19181930

19191931
if ((statValues[1/*mode*/] & S_IFMT) !== S_IFLNK) {
19201932
knownHard[base] = true;
@@ -1957,7 +1969,11 @@ fs.realpathSync = function realpathSync(p, options) {
19571969

19581970
// On windows, check that the root exists. On unix there is no need.
19591971
if (isWindows && !knownHard[base]) {
1960-
binding.lstat(pathModule.toNamespacedPath(base));
1972+
const ctx = { path: base };
1973+
binding.lstat(pathModule.toNamespacedPath(base), undefined, ctx);
1974+
if (ctx.errno !== undefined) {
1975+
throw new errors.uvException(ctx);
1976+
}
19611977
knownHard[base] = true;
19621978
}
19631979
}

‎src/node_file.cc

+9-4
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,7 @@ static void Stat(const FunctionCallbackInfo<Value>& args) {
563563

564564
static void LStat(const FunctionCallbackInfo<Value>& args) {
565565
Environment* env = Environment::GetCurrent(args);
566+
Local<Context> context = env->context();
566567

567568
CHECK_GE(args.Length(), 1);
568569

@@ -573,10 +574,14 @@ static void LStat(const FunctionCallbackInfo<Value>& args) {
573574
CHECK_EQ(args.Length(), 2);
574575
AsyncCall(env, args, "lstat", UTF8, AfterStat,
575576
uv_fs_lstat, *path);
576-
} else { // lstat(path)
577-
SYNC_CALL(lstat, *path, *path)
578-
FillStatsArray(env->fs_stats_field_array(),
579-
static_cast<const uv_stat_t*>(SYNC_REQ.ptr));
577+
} else { // lstat(path, undefined, ctx)
578+
CHECK_EQ(args.Length(), 3);
579+
fs_req_wrap req_wrap;
580+
int err = SyncCall(env, args[2], &req_wrap, "lstat", uv_fs_lstat, *path);
581+
if (err == 0) {
582+
FillStatsArray(env->fs_stats_field_array(),
583+
static_cast<const uv_stat_t*>(req_wrap.req.ptr));
584+
}
580585
}
581586
}
582587

0 commit comments

Comments
 (0)
Please sign in to comment.