From 19eea4f5028e4eba1be8fe6602e7ead5b47e5676 Mon Sep 17 00:00:00 2001 From: gideonthomas Date: Mon, 22 Sep 2014 11:16:02 -0400 Subject: [PATCH] Fix #288 - fs.unlink should fail on directory nodes --- src/filesystem/implementation.js | 12 +++++++++++- tests/spec/fs.unlink.spec.js | 20 ++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/filesystem/implementation.js b/src/filesystem/implementation.js index d3e6e9a..51adb17 100644 --- a/src/filesystem/implementation.js +++ b/src/filesystem/implementation.js @@ -1040,6 +1040,16 @@ function unlink_node(context, path, callback) { } } + function check_if_node_is_directory(error, result) { + if(error) { + callback(error); + } else if(result.mode === 'DIRECTORY') { + callback(new Errors.EISDIR('unlink not permitted on directories', name)); + } else { + update_file_node(null, result); + } + } + function check_if_file_exists(error, result) { if(error) { callback(error); @@ -1048,7 +1058,7 @@ function unlink_node(context, path, callback) { if(!_(directoryData).has(name)) { callback(new Errors.ENOENT('a component of the path does not name an existing file', name)); } else { - context.getObject(directoryData[name].id, update_file_node); + context.getObject(directoryData[name].id, check_if_node_is_directory); } } } diff --git a/tests/spec/fs.unlink.spec.js b/tests/spec/fs.unlink.spec.js index 0b1f0ac..4492822 100644 --- a/tests/spec/fs.unlink.spec.js +++ b/tests/spec/fs.unlink.spec.js @@ -82,4 +82,24 @@ describe('fs.unlink', function() { }); }); }); + + it('should not unlink directories', function (done) { + var fs = util.fs(); + + fs.mkdir('/mydir', function (error) { + if(error) throw error; + + fs.unlink('/mydir', function (error) { + expect(error).to.exist; + expect(error.code).to.equal('EISDIR'); + + fs.stat('/mydir', function (error, stats) { + expect(error).not.to.exist; + expect(stats).to.exist; + expect(stats.type).to.equal('DIRECTORY'); + done(); + }); + }); + }); + }); });