Skip to content

Commit b0ae0f9

Browse files
committed
fs: add accessibleSync() which returns a boolean
As opposed to accessSync() which does nothing or throws
1 parent c0e48bf commit b0ae0f9

File tree

4 files changed

+48
-0
lines changed

4 files changed

+48
-0
lines changed

doc/api/fs.md

+6
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,12 @@ added: v0.11.15
345345
Synchronous version of [`fs.access()`][]. This throws if any accessibility
346346
checks fail, and does nothing otherwise.
347347

348+
## fs.accessibleSync(path[, mode])
349+
350+
Returns false if any accessibility checks fail, and returns true otherwise.
351+
This version is faster and more convenient to use than [`fs.accessSync()`][]
352+
if you don't need to know why the file is not accessible.
353+
348354
## fs.appendFile(file, data[, options], callback)
349355
<!-- YAML
350356
added: v0.6.7

lib/fs.js

+11
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,17 @@ fs.accessSync = function(path, mode) {
248248
binding.access(pathModule._makeLong(path), mode);
249249
};
250250

251+
fs.accessibleSync = function(path, mode) {
252+
nullCheck(path);
253+
254+
if (mode === undefined)
255+
mode = fs.F_OK;
256+
else
257+
mode = mode | 0;
258+
259+
return binding.accessibleSync(pathModule._makeLong(path), mode);
260+
};
261+
251262
fs.exists = function(path, callback) {
252263
if (!nullCheck(path, cb)) return;
253264
var req = new FSReqWrap();

src/node_file.cc

+26
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
namespace node {
2828

2929
using v8::Array;
30+
using v8::Boolean;
3031
using v8::Context;
3132
using v8::EscapableHandleScope;
3233
using v8::Function;
@@ -397,6 +398,30 @@ static void Access(const FunctionCallbackInfo<Value>& args) {
397398
}
398399
}
399400

401+
static void AccessibleSync(const FunctionCallbackInfo<Value>& args) {
402+
Environment* env = Environment::GetCurrent(args.GetIsolate());
403+
HandleScope scope(env->isolate());
404+
405+
if (args.Length() < 2)
406+
return TYPE_ERROR("path and mode are required");
407+
if (!args[1]->IsInt32())
408+
return TYPE_ERROR("mode must be an integer");
409+
410+
BufferValue path(env->isolate(), args[0]);
411+
ASSERT_PATH(path)
412+
413+
int mode = static_cast<int>(args[1]->Int32Value());
414+
415+
fs_req_wrap req_wrap;
416+
env->PrintSyncTrace();
417+
int err = uv_fs_access(
418+
env->event_loop(), &req_wrap.req, *path, mode, nullptr);
419+
420+
bool failed = err < 0;
421+
422+
args.GetReturnValue().Set(Boolean::New(env->isolate(), !failed));
423+
}
424+
400425

401426
static void Close(const FunctionCallbackInfo<Value>& args) {
402427
Environment* env = Environment::GetCurrent(args);
@@ -1461,6 +1486,7 @@ void InitFs(Local<Object> target,
14611486
env->NewFunctionTemplate(FSInitialize)->GetFunction());
14621487

14631488
env->SetMethod(target, "access", Access);
1489+
env->SetMethod(target, "accessibleSync", AccessibleSync);
14641490
env->SetMethod(target, "close", Close);
14651491
env->SetMethod(target, "open", Open);
14661492
env->SetMethod(target, "read", Read);

test/parallel/test-fs-access.js

+5
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ assert.doesNotThrow(function() {
106106
fs.accessSync(__filename);
107107
});
108108

109+
assert.equal(fs.accessibleSync(__filename), true, 'file should be accessible');
110+
109111
assert.doesNotThrow(function() {
110112
var mode = fs.F_OK | fs.R_OK | fs.W_OK;
111113

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

123+
assert.equal(fs.accessibleSync(doesNotExist), false,
124+
'file should not be accessible');
125+
121126
process.on('exit', function() {
122127
removeFile(readOnlyFile);
123128
removeFile(readWriteFile);

0 commit comments

Comments
 (0)