Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fs: add accessibleSync() which returns a boolean
Browse files Browse the repository at this point in the history
As opposed to accessSync() which does nothing or throws
dfabulich committed Jun 28, 2016

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent c0e48bf commit b0ae0f9
Showing 4 changed files with 48 additions and 0 deletions.
6 changes: 6 additions & 0 deletions doc/api/fs.md
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions lib/fs.js
Original file line number Diff line number Diff line change
@@ -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();
26 changes: 26 additions & 0 deletions src/node_file.cc
Original file line number Diff line number Diff line change
@@ -27,6 +27,7 @@
namespace node {

using v8::Array;
using v8::Boolean;
using v8::Context;
using v8::EscapableHandleScope;
using v8::Function;
@@ -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);
@@ -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);
5 changes: 5 additions & 0 deletions test/parallel/test-fs-access.js
Original file line number Diff line number Diff line change
@@ -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;

@@ -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);

0 comments on commit b0ae0f9

Please sign in to comment.