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

Revert #17914 #18238

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: 14 additions & 45 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -441,11 +441,7 @@ fs.existsSync = function(path) {
return false;
}
nullCheck(path);
const ctx = { path };
binding.stat(pathModule.toNamespacedPath(path), undefined, ctx);
if (ctx.errno !== undefined) {
return false;
}
binding.stat(pathModule.toNamespacedPath(path));
return true;
} catch (e) {
return false;
Expand Down Expand Up @@ -633,11 +629,12 @@ function readFileAfterClose(err) {
}

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

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

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

fs.lstatSync = function(path) {
handleError((path = getPathFromURL(path)));
nullCheck(path);
validatePath(path);
const ctx = { path };
binding.lstat(pathModule.toNamespacedPath(path), undefined, ctx);
if (ctx.errno !== undefined) {
throw new errors.uvException(ctx);
}
binding.lstat(pathModule.toNamespacedPath(path));
return statsFromValues();
};

fs.statSync = function(path) {
handleError((path = getPathFromURL(path)));
nullCheck(path);
validatePath(path);
const ctx = { path };
binding.stat(pathModule.toNamespacedPath(path), undefined, ctx);
if (ctx.errno !== undefined) {
throw new errors.uvException(ctx);
}
binding.stat(pathModule.toNamespacedPath(path));
return statsFromValues();
};

Expand Down Expand Up @@ -1881,11 +1866,7 @@ fs.realpathSync = function realpathSync(p, options) {

// On windows, check that the root exists. On unix there is no need.
if (isWindows && !knownHard[base]) {
const ctx = { path: base };
binding.lstat(pathModule.toNamespacedPath(base), undefined, ctx);
if (ctx.errno !== undefined) {
throw new errors.uvException(ctx);
}
binding.lstat(pathModule.toNamespacedPath(base));
knownHard[base] = true;
}

Expand Down Expand Up @@ -1925,11 +1906,7 @@ fs.realpathSync = function realpathSync(p, options) {
// for our internal use.

var baseLong = pathModule.toNamespacedPath(base);
const ctx = { path: base };
binding.lstat(baseLong, undefined, ctx);
if (ctx.errno !== undefined) {
throw new errors.uvException(ctx);
}
binding.lstat(baseLong);

if ((statValues[1/*mode*/] & S_IFMT) !== S_IFLNK) {
knownHard[base] = true;
Expand All @@ -1950,11 +1927,7 @@ fs.realpathSync = function realpathSync(p, options) {
}
}
if (linkTarget === null) {
const ctx = { path: base };
binding.stat(baseLong, undefined, ctx);
if (ctx.errno !== undefined) {
throw new errors.uvException(ctx);
}
binding.stat(baseLong);
linkTarget = binding.readlink(baseLong);
}
resolvedLink = pathModule.resolve(previous, linkTarget);
Expand All @@ -1972,11 +1945,7 @@ fs.realpathSync = function realpathSync(p, options) {

// On windows, check that the root exists. On unix there is no need.
if (isWindows && !knownHard[base]) {
const ctx = { path: base };
binding.lstat(pathModule.toNamespacedPath(base), undefined, ctx);
if (ctx.errno !== undefined) {
throw new errors.uvException(ctx);
}
binding.lstat(pathModule.toNamespacedPath(base));
knownHard[base] = true;
}
}
Expand Down
53 changes: 18 additions & 35 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -375,10 +375,11 @@ 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 int SyncCall(Environment* env, Local<Value> ctx, fs_req_wrap* req_wrap,
inline void SyncCall(Environment* env, Local<Value> ctx,
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 @@ -390,7 +391,6 @@ inline int SyncCall(Environment* env, Local<Value> ctx, fs_req_wrap* req_wrap,
env->syscall_string(),
OneByteString(isolate, syscall)).FromJust();
}
return err;
}

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

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

Expand Down Expand Up @@ -539,7 +537,6 @@ 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 @@ -550,20 +547,15 @@ 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, 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));
}
} else { // stat(path)
SYNC_CALL(stat, *path, *path)
FillStatsArray(env->fs_stats_field_array(),
static_cast<const uv_stat_t*>(SYNC_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 @@ -574,37 +566,28 @@ 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, 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));
}
} else { // lstat(path)
SYNC_CALL(lstat, *path, *path)
FillStatsArray(env->fs_stats_field_array(),
static_cast<const uv_stat_t*>(SYNC_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 = static_cast<int>(args[0]->Int32Value(context).FromJust());
int fd = args[0]->Int32Value();

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, 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));
}
} else { // fstat(fd)
SYNC_CALL(fstat, nullptr, fd)
FillStatsArray(env->fs_stats_field_array(),
static_cast<const uv_stat_t*>(SYNC_REQ.ptr));
}
}

Expand Down
Loading