From 15d7e3ee8866b2b518512c85c8087e11d07b432a Mon Sep 17 00:00:00 2001 From: Chunpeng Huo Date: Mon, 6 Jan 2020 00:50:01 +1100 Subject: [PATCH 1/2] fix: fix isRead check on file descriptor The argument flags must include one of the following access modes: O_RDONLY, O_WRONLY, or O_RDWR. These request opening the file read- only, write-only, or read/write, respectively. O_RDONLY is 0, O_WRONLY is 1, O_RDWR is 2, the flags use 2 bits for 3 states: 0 (0b00) / 1 (0b01) / 2 (0b10), the 2 bits will never be 3 (0b11). closes #288 --- lib/descriptor.js | 7 +------ test/lib/descriptor.spec.js | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 6 deletions(-) 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..9d64a8bc 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)', function() { + const fd = new FileDescriptor(constants.O_CREAT); + 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)', function() { + const fd = new FileDescriptor(constants.O_CREAT); + 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)', function() { + const fd = new FileDescriptor(constants.O_CREAT); + 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)', function() { + const fd = new FileDescriptor(constants.O_CREAT); + 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)', function() { + const fd = new FileDescriptor(constants.O_CREAT); + 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)', function() { + const fd = new FileDescriptor(constants.O_CREAT); + assert.isFalse(fd.isExclusive()); + }); }); }); From ede39f42b411f8acb218c68073b72c36aed33b09 Mon Sep 17 00:00:00 2001 From: Chunpeng Huo Date: Thu, 9 Jan 2020 15:38:47 +1100 Subject: [PATCH 2/2] test: clarify tests --- test/lib/descriptor.spec.js | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/test/lib/descriptor.spec.js b/test/lib/descriptor.spec.js index 9d64a8bc..ef5e8ed5 100644 --- a/test/lib/descriptor.spec.js +++ b/test/lib/descriptor.spec.js @@ -91,8 +91,8 @@ describe('FileDescriptor', function() { assert.isTrue(fd.isAppend()); }); - it('not opened for appending (O_CREAT)', function() { - const fd = new FileDescriptor(constants.O_CREAT); + it('not opened for appending (O_CREAT | O_RDONLY)', function() { + const fd = new FileDescriptor(constants.O_CREAT | constants.O_RDONLY); assert.isFalse(fd.isAppend()); }); }); @@ -158,8 +158,8 @@ describe('FileDescriptor', function() { assert.isFalse(fd.isTruncate()); }); - it('not opened for truncating (O_CREAT)', function() { - const fd = new FileDescriptor(constants.O_CREAT); + it('not opened for truncating (O_CREAT | O_RDONLY)', function() { + const fd = new FileDescriptor(constants.O_CREAT | constants.O_RDONLY); assert.isFalse(fd.isTruncate()); }); }); @@ -225,8 +225,8 @@ describe('FileDescriptor', function() { assert.isTrue(fd.isCreate()); }); - it('opened for creation (O_CREAT)', function() { - const fd = new FileDescriptor(constants.O_CREAT); + it('opened for creation (O_CREAT | O_RDONLY)', function() { + const fd = new FileDescriptor(constants.O_CREAT | constants.O_RDONLY); assert.isTrue(fd.isCreate()); }); }); @@ -292,8 +292,8 @@ describe('FileDescriptor', function() { assert.isTrue(fd.isRead()); }); - it('opened for reading (O_CREAT)', function() { - const fd = new FileDescriptor(constants.O_CREAT); + it('opened for reading (O_CREAT | O_RDONLY)', function() { + const fd = new FileDescriptor(constants.O_CREAT | constants.O_RDONLY); assert.isTrue(fd.isRead()); }); }); @@ -359,8 +359,8 @@ describe('FileDescriptor', function() { assert.isTrue(fd.isWrite()); }); - it('not opened for writing (O_CREAT)', function() { - const fd = new FileDescriptor(constants.O_CREAT); + it('not opened for writing (O_CREAT | O_RDONLY)', function() { + const fd = new FileDescriptor(constants.O_CREAT | constants.O_RDONLY); assert.isFalse(fd.isWrite()); }); }); @@ -426,8 +426,8 @@ describe('FileDescriptor', function() { assert.isTrue(fd.isExclusive()); }); - it('not opened for exclusive (O_CREAT)', function() { - const fd = new FileDescriptor(constants.O_CREAT); + it('not opened for exclusive (O_CREAT | O_RDONLY)', function() { + const fd = new FileDescriptor(constants.O_CREAT | constants.O_RDONLY); assert.isFalse(fd.isExclusive()); }); });