Merge pull request #293 from gideonthomas/issue288

Fix #288 - fs.unlink should fail on directory nodes
This commit is contained in:
Alan K 2014-09-23 12:17:21 -04:00
commit af5eea103a
2 changed files with 31 additions and 1 deletions

View File

@ -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);
}
}
}

View File

@ -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();
});
});
});
});
});