Skip to content

Commit

Permalink
fs: throw fs.lstat{Sync} errors in JS
Browse files Browse the repository at this point in the history
PR-URL: #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 57d7638 commit da7804f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
24 changes: 20 additions & 4 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
};

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}
Expand Down
13 changes: 9 additions & 4 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);

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

Expand Down

0 comments on commit da7804f

Please sign in to comment.