Skip to content

Commit

Permalink
fix: fix isRead check on file descriptor
Browse files Browse the repository at this point in the history
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
  • Loading branch information
3cp committed Jan 5, 2020
1 parent ad93ac5 commit 579425e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
7 changes: 1 addition & 6 deletions lib/descriptor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

/**
Expand Down
31 changes: 31 additions & 0 deletions test/lib/descriptor.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

const constants = require('constants');
const FileDescriptor = require('../../lib/descriptor');
const helper = require('../helper');

Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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());
});
});
});

0 comments on commit 579425e

Please # to comment.