Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fs: throw errors in JS land in *stat{Sync} APIs #17914

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 45 additions & 14 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -629,12 +633,11 @@ function readFileAfterClose(err) {
}

function tryStatSync(fd, isUserFd) {
var threw = true;
try {
binding.fstat(fd);
threw = false;
} finally {
if (threw && !isUserFd) fs.closeSync(fd);
const ctx = {};
binding.fstat(fd, undefined, ctx);
if (ctx.errno !== undefined && !isUserFd) {
fs.closeSync(fd);
throw new errors.uvException(ctx);
}
}

Expand Down Expand Up @@ -1111,23 +1114,35 @@ fs.stat = function(path, callback) {

fs.fstatSync = function(fd) {
validateUint32(fd, 'fd');
binding.fstat(fd);
const ctx = { fd };
binding.fstat(fd, undefined, ctx);
if (ctx.errno !== undefined) {
throw new errors.uvException(ctx);
}
return statsFromValues();
};

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

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

Expand Down Expand Up @@ -1866,7 +1881,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 @@ -1906,7 +1925,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 All @@ -1927,7 +1950,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);
Expand All @@ -1945,7 +1972,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
53 changes: 35 additions & 18 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -375,11 +375,10 @@ inline FSReqWrap* AsyncCall(Environment* env,
// the error number and the syscall in the context instead of
// creating an error in the C++ land.
template <typename Func, typename... Args>
inline void SyncCall(Environment* env, Local<Value> ctx,
inline int SyncCall(Environment* env, Local<Value> ctx, fs_req_wrap* req_wrap,
const char* syscall, Func fn, Args... args) {
fs_req_wrap req_wrap;
env->PrintSyncTrace();
int err = fn(env->event_loop(), &req_wrap.req, args..., nullptr);
int err = fn(env->event_loop(), &(req_wrap->req), args..., nullptr);
if (err < 0) {
Local<Context> context = env->context();
Local<Object> ctx_obj = ctx->ToObject(context).ToLocalChecked();
Expand All @@ -391,6 +390,7 @@ inline void SyncCall(Environment* env, Local<Value> ctx,
env->syscall_string(),
OneByteString(isolate, syscall)).FromJust();
}
return err;
}

#define SYNC_DEST_CALL(func, path, dest, ...) \
Expand Down Expand Up @@ -426,7 +426,8 @@ void Access(const FunctionCallbackInfo<Value>& args) {
AsyncCall(env, args, "access", UTF8, AfterNoArgs,
uv_fs_access, *path, mode);
} else { // access(path, mode, undefined, ctx)
SyncCall(env, args[3], "access", uv_fs_access, *path, mode);
fs_req_wrap req_wrap;
SyncCall(env, args[3], &req_wrap, "access", uv_fs_access, *path, mode);
}
}

Expand All @@ -446,7 +447,8 @@ void Close(const FunctionCallbackInfo<Value>& args) {
AsyncCall(env, args, "close", UTF8, AfterNoArgs,
uv_fs_close, fd);
} else { // close(fd, undefined, ctx)
SyncCall(env, args[2], "close", uv_fs_close, fd);
fs_req_wrap req_wrap;
SyncCall(env, args[2], &req_wrap, "close", uv_fs_close, fd);
}
}

Expand Down Expand Up @@ -537,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);

Expand All @@ -547,15 +550,20 @@ 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));
}
}
}

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

CHECK_GE(args.Length(), 1);

Expand All @@ -566,28 +574,37 @@ 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));
}
}
}

static void FStat(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
Local<Context> context = env->context();

CHECK(args[0]->IsInt32());

int fd = args[0]->Int32Value();
int fd = static_cast<int>(args[0]->Int32Value(context).FromJust());

if (args[1]->IsObject()) { // fstat(fd, req)
CHECK_EQ(args.Length(), 2);
AsyncCall(env, args, "fstat", UTF8, AfterStat,
uv_fs_fstat, fd);
} else { // fstat(fd)
SYNC_CALL(fstat, nullptr, fd)
FillStatsArray(env->fs_stats_field_array(),
static_cast<const uv_stat_t*>(SYNC_REQ.ptr));
} else { // fstat(fd, undefined, ctx)
CHECK_EQ(args.Length(), 3);
fs_req_wrap req_wrap;
int err = SyncCall(env, args[2], &req_wrap, "fstat", uv_fs_fstat, fd);
if (err == 0) {
FillStatsArray(env->fs_stats_field_array(),
static_cast<const uv_stat_t*>(req_wrap.req.ptr));
}
}
}

Expand Down
Loading