Skip to content

Commit

Permalink
Merge pull request #59 from kirilknysh/fix/open-directory
Browse files Browse the repository at this point in the history
fix: fs.open on directory as read-only
  • Loading branch information
streamich authored Jan 9, 2018
2 parents 53eb588 + 61ce424 commit b90c016
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
16 changes: 15 additions & 1 deletion src/__tests__/volume.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ describe('volume', () => {
});
describe('.open(path, flags[, mode], callback)', () => {
const vol = new Volume;
vol.mkdirSync('/test-dir');
it('Create new file at root (/test.txt)', done => {
vol.open('/test.txt', 'w', (err, fd) => {
expect(err).toBe(null);
Expand Down Expand Up @@ -338,7 +339,20 @@ describe('volume', () => {
expect(vol.root.getChild('b.txt').getNode().canWrite()).toBe(true);
done();
});
})
});
it('Error on incorrect flags for directory', done => {
vol.open('/test-dir', 'r+', (err, fd) => {
expect(err.code).toBe('EISDIR');
done();
});
});
it('Properly opens directory as read-only', done => {
vol.open('/test-dir', 'r', (err, fd) => {
expect(err).toBe(null);
expect(typeof fd).toBe('number');
done();
});
});
});
describe('.close(fd, callback)', () => {
const vol = new Volume;
Expand Down
2 changes: 1 addition & 1 deletion src/volume.ts
Original file line number Diff line number Diff line change
Expand Up @@ -844,7 +844,7 @@ export class Volume {
if(!realLink) throwError(ENOENT, 'open', link.getPath());

const node = realLink.getNode();
if(node.isDirectory())
if(node.isDirectory() && (flagsNum !== FLAGS.r))
throwError(EISDIR, 'open', link.getPath());

// Check node permissions
Expand Down

0 comments on commit b90c016

Please sign in to comment.