From 91f7bf031916610e3f629c99e8f612d6be56b49f Mon Sep 17 00:00:00 2001 From: Gideon Thomas Date: Sun, 16 Nov 2014 00:12:53 -0500 Subject: [PATCH] Fix #256 - fs.read() for dir paths should fail --- src/filesystem/implementation.js | 2 ++ tests/spec/fs.read.spec.js | 24 ++++++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/filesystem/implementation.js b/src/filesystem/implementation.js index 14c7d13..6d9c183 100644 --- a/src/filesystem/implementation.js +++ b/src/filesystem/implementation.js @@ -813,6 +813,8 @@ function read_data(context, ofd, buffer, offset, length, position, callback) { function read_file_data(error, result) { if(error) { callback(error); + } else if(result.mode === 'DIRECTORY') { + callback(new Errors.EISDIR('the named file is a directory', ofd.path)); } else { fileNode = result; context.getBuffer(fileNode.data, handle_file_data); diff --git a/tests/spec/fs.read.spec.js b/tests/spec/fs.read.spec.js index 7d2bf2b..6606da0 100644 --- a/tests/spec/fs.read.spec.js +++ b/tests/spec/fs.read.spec.js @@ -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(); + }); + }); + }); + }); });