Skip to content

Commit

Permalink
Merge pull request #242 from maxwellgerber/patch/open-directory-error
Browse files Browse the repository at this point in the history
Throw error when a directory is opened
  • Loading branch information
tschaub committed Aug 9, 2018
2 parents e363112 + f8e4eb6 commit 076fca8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/binding.js
Expand Up @@ -457,6 +457,12 @@ Binding.prototype.open = function(pathname, flags, mode, callback) {
if (descriptor.isWrite() && !item.canWrite()) {
throw new FSError('EACCES', pathname);
}
if (
item instanceof Directory &&
(descriptor.isTruncate() || descriptor.isAppend())
) {
throw new FSError('EISDIR', pathname);
}
if (descriptor.isTruncate()) {
if (item instanceof Directory) {
throw new FSError('EISDIR');
Expand Down
14 changes: 14 additions & 0 deletions test/lib/binding.spec.js
Expand Up @@ -695,6 +695,13 @@ describe('Binding', function() {
assert.equal(String(file.getContent()), '');
});

it('generates error if file is directory (w)', function() {
var binding = new Binding(system);
assert.throws(function() {
binding.open('mock-dir', flags('w'));
});
});

it('generates error if file exists (wx)', function() {
var binding = new Binding(system);
assert.throws(function() {
Expand Down Expand Up @@ -764,6 +771,13 @@ describe('Binding', function() {
assert.equal(String(file.getContent()), 'one content');
});

it('generates error if file is directory (a)', function() {
var binding = new Binding(system);
assert.throws(function() {
binding.open('mock-dir', flags('a'));
});
});

it('opens a new file for appending (ax)', function() {
var binding = new Binding(system);
binding.open('new.txt', flags('ax'), parseInt('0664', 8));
Expand Down

0 comments on commit 076fca8

Please sign in to comment.