Skip to content

Commit

Permalink
Fix for issue #561 - Extend fs.access to support R_OK, W_OK, and X_OK (
Browse files Browse the repository at this point in the history
…#601)

* added extended test for fs.access

* making requested changes
  • Loading branch information
deepanjali19 authored and humphd committed Dec 4, 2018
1 parent 0f94c47 commit 0f93a04
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ var errors = {};
//'0:OK:success',
//'1:EOF:end of file',
//'2:EADDRINFO:getaddrinfo error',
//'3:EACCES:permission denied',
'3:EACCES:permission denied',
//'4:EAGAIN:resource temporarily unavailable',
//'5:EADDRINUSE:address already in use',
//'6:EADDRNOTAVAIL:address not available',
Expand Down
16 changes: 9 additions & 7 deletions src/filesystem/implementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -439,15 +439,17 @@ function make_directory(context, path, callback) {

function access_file(context, path, mode, callback) {
path = normalize(path);
find_node(context, path, function (err) {
find_node(context, path, function (err, node) {
if (err) {
return callback(err);
}
/*
TODO: we currently only support Constants.fsConstants.F_OK
Working to fix the issue: https://github.com/filerjs/filer/issues/561
*/
callback(null);
if(mode === Constants.F_OK){
return callback(null);
}
if (!(mode & Constants.X_OK) || (node.mode & (Constants.fsConstants.S_IXUSR | Constants.fsConstants.S_IXGRP | Constants.fsConstants.S_IXOTH))){
return callback(null);
}
callback(new Errors.EACCES('permission denied',path)) ;
});
}

Expand Down Expand Up @@ -1687,7 +1689,7 @@ function access(fs, context, path, mode, callback) {
}

if (!pathCheck(path, callback)) return;
mode = mode | 0;
mode = mode | Constants.fsConstants.F_OK;
access_file(context, path, mode, callback);
}

Expand Down
95 changes: 94 additions & 1 deletion tests/spec/fs.access.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('fs.access', function () {
});
});

it('should return no error if file does exist', function (done) {
it('should return no error if file does exist and mode = F_OK', function (done) {
var fs = util.fs();
var contents = 'This is a file.';

Expand All @@ -34,4 +34,97 @@ describe('fs.access', function () {
});
});

it('should return no error if file does exist and mode = R_OK', function (done) {
var fs = util.fs();
var contents = 'This is a file.';

fs.writeFile('/myfile', contents, function (error) {
if (error) throw error;

fs.access('/myfile', fs.constants.R_OK, function (error) {
expect(error).not.to.exist;
done();
});
});
});

it('should return no error if file does exist and mode = W_OK', function (done) {
var fs = util.fs();
var contents = 'This is a file.';

fs.writeFile('/myfile', contents, function (error) {
if (error) throw error;

fs.access('/myfile', fs.constants.W_OK, function (error) {
expect(error).not.to.exist;
done();
});
});
});

// See bug https://github.com/filerjs/filer/issues/602
it.skip('should return an error if file is not executable and mode = X_OK', function (done) {
var fs = util.fs();
var contents = 'This is a file.';

fs.writeFile('/myfile', contents, function (error) {
if (error) throw error;

fs.chmod('/myfile', 0o644, function(error){
if (error) throw error;

fs.access('/myfile', fs.constants.X_OK, function (error) {
expect(error).to.exist;
expect(error.code).to.equal('EACCES');
done();
});
});
});
});

it('should return no error if file does exist and mode = X_OK', function (done) {
var fs = util.fs();
var contents = 'This is a file.';

fs.writeFile('/myfile', contents, function (error) {
if (error) throw error;

fs.chmod('/myfile', 0o777, function(error){
if (error) throw error;

fs.access('/myfile', fs.constants.X_OK, function (error) {
expect(error).not.to.exist;
done();
});
});
});
});

it('should return no error if file does exist and no mode is passed', function (done) {
var fs = util.fs();
var contents = 'This is a file.';

fs.writeFile('/myfile', contents, function (error) {
if (error) throw error;

fs.access('/myfile', function (error) {
expect(error).not.to.exist;
done();
});
});
});

it('should return no error if file does exist and mode = R_OK | W_OK', function (done) {
var fs = util.fs();
var contents = 'This is a file.';

fs.writeFile('/myfile', contents, function (error) {
if (error) throw error;

fs.access('/myfile', fs.constants.R_OK | fs.constants.W_OK, function (error) {
expect(error).not.to.exist;
done();
});
});
});
});

0 comments on commit 0f93a04

Please sign in to comment.