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 access() and accessSync() #114

Closed
wants to merge 2 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
33 changes: 33 additions & 0 deletions doc/api/fs.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -656,10 +656,43 @@ that leaves you vulnerable to race conditions: another process may remove the
file between the calls to `fs.exists()` and `fs.open()`. Just open the file
and handle the error when it's not there.

`fs.exists()` will be deprecated.

## fs.existsSync(path)

Synchronous version of `fs.exists`.

`fs.existsSync()` will be deprecated.

## fs.access(path[, mode], callback)

Tests a user's permissions for the file specified by `path`. `mode` is an
optional integer that specifies the accessibility checks to be performed. The
following constants define the possible values of `mode`. It is possible to
create a mask consisting of the bitwise OR of two or more values.

- `fs.F_OK` - File is visible to the calling process. This is useful for
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So cryptic. I know I'm coming out of nowhere but what about e.g. fs.access.EXISTS, fs.access.READABLE, fs.access.WRITABLE, fs.access.EXECUTABLE.

determining if a file exists, but says nothing about `rwx` permissions.
Default if no `mode` is specified.
- `fs.R_OK` - File can be read by the calling process.
- `fs.W_OK` - File can be written by the calling process.
- `fs.X_OK` - File can be executed by the calling process. This has no effect
on Windows (will behave like `fs.F_OK`).

The final argument, `callback`, is a callback function that is invoked with
a possible error argument. If any of the accessibility checks fail, the error
argument will be populated. The following example checks if the file
`/etc/passwd` can be read and written by the current process.

fs.access('/etc/passwd', fs.R_OK | fs.W_OK, function(err) {
util.debug(err ? 'no access!' : 'can read/write');
});

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

Synchronous version of `fs.access`. This throws if any accessibility checks
fail, and does nothing otherwise.

## Class: fs.Stats

Objects returned from `fs.stat()`, `fs.lstat()` and `fs.fstat()` and their
Expand Down
35 changes: 35 additions & 0 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ var O_RDWR = constants.O_RDWR || 0;
var O_SYNC = constants.O_SYNC || 0;
var O_TRUNC = constants.O_TRUNC || 0;
var O_WRONLY = constants.O_WRONLY || 0;
var F_OK = constants.F_OK || 0;
var R_OK = constants.R_OK || 0;
var W_OK = constants.W_OK || 0;
var X_OK = constants.X_OK || 0;

var isWindows = process.platform === 'win32';

Expand Down Expand Up @@ -182,6 +186,37 @@ fs.Stats.prototype.isSocket = function() {
return this._checkModeProperty(constants.S_IFSOCK);
};

fs.F_OK = F_OK;
fs.R_OK = R_OK;
fs.W_OK = W_OK;
fs.X_OK = X_OK;

fs.access = function(path, mode, callback) {
if (!nullCheck(path, callback))
return;

if (typeof mode === 'function') {
callback = mode;
mode = F_OK;
} else if (typeof callback !== 'function') {
throw new TypeError('callback must be a function');
}

mode = mode | 0;
binding.access(pathModule._makeLong(path), mode, callback);
};

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

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

binding.access(pathModule._makeLong(path), mode);
};

fs.exists = function(path, callback) {
if (!nullCheck(path, cb)) return;
binding.stat(pathModule._makeLong(path), cb);
Expand Down
16 changes: 16 additions & 0 deletions src/node_constants.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1107,6 +1107,22 @@ void DefineSystemConstants(Handle<Object> target) {
#ifdef S_IXOTH
NODE_DEFINE_CONSTANT(target, S_IXOTH);
#endif

#ifdef F_OK
NODE_DEFINE_CONSTANT(target, F_OK);
#endif

#ifdef R_OK
NODE_DEFINE_CONSTANT(target, R_OK);
#endif

#ifdef W_OK
NODE_DEFINE_CONSTANT(target, W_OK);
#endif

#ifdef X_OK
NODE_DEFINE_CONSTANT(target, X_OK);
#endif
}

void DefineUVConstants(Handle<Object> target) {
Expand Down
24 changes: 24 additions & 0 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ static void After(uv_fs_t *req) {

switch (req->fs_type) {
// These all have no data to pass.
case UV_FS_ACCESS:
case UV_FS_CLOSE:
case UV_FS_RENAME:
case UV_FS_UNLINK:
Expand Down Expand Up @@ -308,6 +309,28 @@ struct fs_req_wrap {
#define SYNC_RESULT err


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

if (args.Length() < 2)
return THROW_BAD_ARGS;
if (!args[0]->IsString())
return TYPE_ERROR("path must be a string");
if (!args[1]->IsInt32())
return TYPE_ERROR("mode must be an integer");

node::Utf8Value path(args[0]);
int mode = static_cast<int>(args[1]->Int32Value());

if (args[2]->IsFunction()) {
ASYNC_CALL(access, args[2], *path, mode);
} else {
SYNC_CALL(access, *path, *path, mode);
}
}


static void Close(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);

Expand Down Expand Up @@ -1103,6 +1126,7 @@ void InitFs(Handle<Object> target,
target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "FSInitialize"),
env->NewFunctionTemplate(FSInitialize)->GetFunction());

env->SetMethod(target, "access", Access);
env->SetMethod(target, "close", Close);
env->SetMethod(target, "open", Open);
env->SetMethod(target, "read", Read);
Expand Down
99 changes: 99 additions & 0 deletions test/simple/test-fs-access.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// Copyright io.js contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.

var common = require('../common');
var assert = require('assert');
var fs = require('fs');
var path = require('path');
var doesNotExist = __filename + '__this_should_not_exist';
var readOnlyFile = path.join(common.tmpDir, 'read_only_file');

var removeFile = function(file) {
try {
fs.unlinkSync(file);
} catch (err) {
// Ignore error
}
};

var createReadOnlyFile = function(file) {
removeFile(file);
fs.writeFileSync(file, '');
fs.chmodSync(file, 0444);
};

createReadOnlyFile(readOnlyFile);

assert(typeof fs.F_OK === 'number');
assert(typeof fs.R_OK === 'number');
assert(typeof fs.W_OK === 'number');
assert(typeof fs.X_OK === 'number');

fs.access(__filename, function(err) {
assert.strictEqual(err, null, 'error should not exist');
});

fs.access(__filename, fs.R_OK, function(err) {
assert.strictEqual(err, null, 'error should not exist');
});

fs.access(doesNotExist, function(err) {
assert.notEqual(err, null, 'error should exist');
assert.strictEqual(err.code, 'ENOENT');
assert.strictEqual(err.path, doesNotExist);
});

fs.access(readOnlyFile, fs.F_OK | fs.R_OK, function(err) {
assert.strictEqual(err, null, 'error should not exist');
});

fs.access(readOnlyFile, fs.W_OK, function(err) {
assert.notEqual(err, null, 'error should exist');
assert.strictEqual(err.path, readOnlyFile);
});

assert.throws(function() {
fs.access(100, fs.F_OK, function(err) {});
}, /path must be a string/);

assert.throws(function() {
fs.access(__filename, fs.F_OK);
}, /callback must be a function/);

assert.doesNotThrow(function() {
fs.accessSync(__filename);
});

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

fs.accessSync(__filename, mode);
});

assert.throws(function() {
fs.accessSync(doesNotExist);
}, function (err) {
return err.code === 'ENOENT' && err.path === doesNotExist;
});

process.on('exit', function() {
removeFile(readOnlyFile);
});