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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for issue #561 - Extend fs.access to support R_OK, W_OK, and X_OK #601

Merged
merged 2 commits into from Dec 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/errors.js
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
Expand Up @@ -441,15 +441,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 @@ -1699,7 +1701,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
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) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you add a comment above this that says:

// See bug https://github.com/filerjs/filer/issues/602

Copy link
Contributor

Choose a reason for hiding this comment

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

I filed #602 to deal with this later.

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();
});
});
});
});