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

* added extended test for fs.access

* making requested changes
This commit is contained in:
Deepanjali Gerangal 2018-12-03 23:08:40 -05:00 committed by David Humphrey
parent 0f94c476e7
commit 0f93a04e40
3 changed files with 104 additions and 9 deletions

View File

@ -7,7 +7,7 @@ var errors = {};
//'0:OK:success', //'0:OK:success',
//'1:EOF:end of file', //'1:EOF:end of file',
//'2:EADDRINFO:getaddrinfo error', //'2:EADDRINFO:getaddrinfo error',
//'3:EACCES:permission denied', '3:EACCES:permission denied',
//'4:EAGAIN:resource temporarily unavailable', //'4:EAGAIN:resource temporarily unavailable',
//'5:EADDRINUSE:address already in use', //'5:EADDRINUSE:address already in use',
//'6:EADDRNOTAVAIL:address not available', //'6:EADDRNOTAVAIL:address not available',

View File

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

View File

@ -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 fs = util.fs();
var contents = 'This is a file.'; var contents = 'This is a file.';
@ -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();
});
});
});
}); });