diff --git a/src/errors.js b/src/errors.js index b30b64b0..58c17f96 100644 --- a/src/errors.js +++ b/src/errors.js @@ -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', diff --git a/src/filesystem/implementation.js b/src/filesystem/implementation.js index 7d532051..c9edd946 100644 --- a/src/filesystem/implementation.js +++ b/src/filesystem/implementation.js @@ -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)) ; }); } @@ -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); } diff --git a/tests/spec/fs.access.spec.js b/tests/spec/fs.access.spec.js index da8ae736..bd99c008 100644 --- a/tests/spec/fs.access.spec.js +++ b/tests/spec/fs.access.spec.js @@ -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.'; @@ -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(); + }); + }); + }); });