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: add accessibleSync() which returns a boolean #4217

Closed
wants to merge 1 commit 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
6 changes: 6 additions & 0 deletions doc/api/fs.md
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,12 @@ added: v0.11.15
Synchronous version of [`fs.access()`][]. This throws if any accessibility
checks fail, and does nothing otherwise.

## fs.accessibleSync(path[, mode])

Returns false if any accessibility checks fail, and returns true otherwise.
This version is faster and more convenient to use than [`fs.accessSync()`][]
if you don't need to know why the file is not accessible.

## fs.appendFile(file, data[, options], callback)
<!-- YAML
added: v0.6.7
Expand Down
11 changes: 11 additions & 0 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,17 @@ fs.accessSync = function(path, mode) {
binding.access(pathModule._makeLong(path), mode);
};

fs.accessibleSync = function(path, mode) {
nullCheck(path);

if (mode === undefined)
mode = fs.F_OK;
else
mode = mode | 0;

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

fs.exists = function(path, callback) {
if (!nullCheck(path, cb)) return;
var req = new FSReqWrap();
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 @@ -397,6 +398,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 @@ -1461,6 +1486,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
5 changes: 5 additions & 0 deletions test/parallel/test-fs-access.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ assert.doesNotThrow(function() {
fs.accessSync(__filename);
});

assert.equal(fs.accessibleSync(__filename), true, 'file should be accessible');

assert.doesNotThrow(function() {
var mode = fs.F_OK | fs.R_OK | fs.W_OK;

Expand All @@ -118,6 +120,9 @@ assert.throws(function() {
return err.code === 'ENOENT' && err.path === doesNotExist;
});

assert.equal(fs.accessibleSync(doesNotExist), false,
'file should not be accessible');

process.on('exit', function() {
removeFile(readOnlyFile);
removeFile(readWriteFile);
Expand Down