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: undeprecate existsSync; use access instead of stat for performance #7455

Closed
wants to merge 3 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
11 changes: 4 additions & 7 deletions doc/api/fs.md
Original file line number Diff line number Diff line change
Expand Up @@ -597,8 +597,9 @@ deprecated: v1.0.0
* `path` {String | Buffer}
* `callback` {Function}

Test whether or not the given path exists by checking with the file system.
Then call the `callback` argument with either true or false. Example:
Test whether or not the given path exists and is accessible by checking
with the file system. Then call the `callback` argument with either
true or false. Example:

```js
fs.exists('/etc/passwd', (exists) => {
Expand All @@ -615,16 +616,12 @@ non-existent.
## fs.existsSync(path)
<!-- YAML
added: v0.1.21
deprecated: v1.0.0
-->

> Stability: 0 - Deprecated: Use [`fs.statSync()`][] or [`fs.accessSync()`][]
> instead.

* `path` {String | Buffer}

Synchronous version of [`fs.exists()`][].
Returns `true` if the file exists, `false` otherwise.
Returns `true` if the file exists and is accessible, `false` otherwise.

## fs.fchmod(fd, mode, callback)
<!-- YAML
Expand Down
14 changes: 5 additions & 9 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,22 +250,18 @@ fs.accessSync = function(path, mode) {

fs.exists = function(path, callback) {
if (!nullCheck(path, cb)) return;
var req = new FSReqWrap();
req.oncomplete = cb;
binding.stat(pathModule._makeLong(path), req);
function cb(err, stats) {
fs.access(path, fs.F_OK, cb);
function cb(err) {
if (callback) callback(err ? false : true);
}
};

fs.existsSync = function(path) {
try {
nullCheck(path);
binding.stat(pathModule._makeLong(path));
return true;
} catch (e) {
if (String(path).indexOf('\u0000') !== -1) {
return false;
}

return binding.accessibleSync(pathModule._makeLong(path), fs.F_OK);
};

fs.readFile = function(path, options, callback_) {
Expand Down
26 changes: 26 additions & 0 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
namespace node {

using v8::Array;
using v8::Boolean;
using v8::Context;
using v8::EscapableHandleScope;
using v8::Function;
Expand Down Expand Up @@ -399,6 +400,30 @@ static void Access(const FunctionCallbackInfo<Value>& args) {
}
}

static void AccessibleSync(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args.GetIsolate());
HandleScope scope(env->isolate());

if (args.Length() < 2)
return TYPE_ERROR("path and mode are required");
if (!args[1]->IsInt32())
return TYPE_ERROR("mode must be an integer");

BufferValue path(env->isolate(), args[0]);
ASSERT_PATH(path)

int mode = static_cast<int>(args[1]->Int32Value());

fs_req_wrap req_wrap;
env->PrintSyncTrace();
int err = uv_fs_access(
env->event_loop(), &req_wrap.req, *path, mode, nullptr);

bool failed = err < 0;

args.GetReturnValue().Set(Boolean::New(env->isolate(), !failed));
}


static void Close(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
Expand Down Expand Up @@ -1448,6 +1473,7 @@ void InitFs(Local<Object> target,
env->NewFunctionTemplate(FSInitialize)->GetFunction());

env->SetMethod(target, "access", Access);
env->SetMethod(target, "accessibleSync", AccessibleSync);
env->SetMethod(target, "close", Close);
env->SetMethod(target, "open", Open);
env->SetMethod(target, "read", Read);
Expand Down