diff --git a/lib/descriptor.js b/lib/descriptor.js index 85c07128..d8ec0efd 100644 --- a/lib/descriptor.js +++ b/lib/descriptor.js @@ -80,12 +80,7 @@ FileDescriptor.prototype.isCreate = function() { * @return {boolean} Opened for reading. */ FileDescriptor.prototype.isRead = function() { - // special treatment because O_RDONLY is 0 - return ( - this._flags === constants.O_RDONLY || - this._flags === (constants.O_RDONLY | constants.O_SYNC) || - (this._flags & constants.O_RDWR) === constants.O_RDWR - ); + return (this._flags & constants.O_WRONLY) !== constants.O_WRONLY; }; /** diff --git a/test/lib/descriptor.spec.js b/test/lib/descriptor.spec.js index 2d40ec80..ef5e8ed5 100644 --- a/test/lib/descriptor.spec.js +++ b/test/lib/descriptor.spec.js @@ -1,5 +1,6 @@ 'use strict'; +const constants = require('constants'); const FileDescriptor = require('../../lib/descriptor'); const helper = require('../helper'); @@ -89,6 +90,11 @@ describe('FileDescriptor', function() { const fd = new FileDescriptor(flags('ax+')); assert.isTrue(fd.isAppend()); }); + + it('not opened for appending (O_CREAT | O_RDONLY)', function() { + const fd = new FileDescriptor(constants.O_CREAT | constants.O_RDONLY); + assert.isFalse(fd.isAppend()); + }); }); describe('#isTruncate()', function() { @@ -151,6 +157,11 @@ describe('FileDescriptor', function() { const fd = new FileDescriptor(flags('ax+')); assert.isFalse(fd.isTruncate()); }); + + it('not opened for truncating (O_CREAT | O_RDONLY)', function() { + const fd = new FileDescriptor(constants.O_CREAT | constants.O_RDONLY); + assert.isFalse(fd.isTruncate()); + }); }); describe('#isCreate()', function() { @@ -213,6 +224,11 @@ describe('FileDescriptor', function() { const fd = new FileDescriptor(flags('ax+')); assert.isTrue(fd.isCreate()); }); + + it('opened for creation (O_CREAT | O_RDONLY)', function() { + const fd = new FileDescriptor(constants.O_CREAT | constants.O_RDONLY); + assert.isTrue(fd.isCreate()); + }); }); describe('#isRead()', function() { @@ -275,6 +291,11 @@ describe('FileDescriptor', function() { const fd = new FileDescriptor(flags('ax+')); assert.isTrue(fd.isRead()); }); + + it('opened for reading (O_CREAT | O_RDONLY)', function() { + const fd = new FileDescriptor(constants.O_CREAT | constants.O_RDONLY); + assert.isTrue(fd.isRead()); + }); }); describe('#isWrite()', function() { @@ -337,6 +358,11 @@ describe('FileDescriptor', function() { const fd = new FileDescriptor(flags('ax+')); assert.isTrue(fd.isWrite()); }); + + it('not opened for writing (O_CREAT | O_RDONLY)', function() { + const fd = new FileDescriptor(constants.O_CREAT | constants.O_RDONLY); + assert.isFalse(fd.isWrite()); + }); }); describe('#isExclusive()', function() { @@ -399,5 +425,10 @@ describe('FileDescriptor', function() { const fd = new FileDescriptor(flags('ax+')); assert.isTrue(fd.isExclusive()); }); + + it('not opened for exclusive (O_CREAT | O_RDONLY)', function() { + const fd = new FileDescriptor(constants.O_CREAT | constants.O_RDONLY); + assert.isFalse(fd.isExclusive()); + }); }); });