Merge pull request #325 from gideonthomas/issue256

Fix #256 - fs.read() for dir paths should fail
This commit is contained in:
Alan K 2014-11-20 12:32:33 -05:00
commit b1cf2d7dfb
2 changed files with 26 additions and 0 deletions

View File

@ -813,6 +813,8 @@ function read_data(context, ofd, buffer, offset, length, position, callback) {
function read_file_data(error, result) { function read_file_data(error, result) {
if(error) { if(error) {
callback(error); callback(error);
} else if(result.mode === 'DIRECTORY') {
callback(new Errors.EISDIR('the named file is a directory', ofd.path));
} else { } else {
fileNode = result; fileNode = result;
context.getBuffer(fileNode.data, handle_file_data); context.getBuffer(fileNode.data, handle_file_data);

View File

@ -61,4 +61,28 @@ describe('fs.read', function() {
}); });
}); });
}); });
it('should fail to read a directory', function(done) {
var fs = util.fs();
var buf = new Filer.Buffer(20);
var buf2 = new Filer.Buffer(20);
buf.fill(0);
buf2.fill(0);
fs.mkdir('/mydir', function(error) {
if(error) throw err;
fs.open('/mydir', 'r', function(error, fd) {
if(error) throw error;
fs.read(fd, buf, 0, buf.length, 0, function(error, result) {
expect(error).to.exist;
expect(error.code).to.equal('EISDIR');
expect(result).to.equal(0);
expect(buf).to.deep.equal(buf2);
done();
});
});
});
});
}); });