Skip to content

Commit

Permalink
Merge pull request #289 from tschaub/fix-file-isRead
Browse files Browse the repository at this point in the history
fix: fix isRead check on file descriptor
  • Loading branch information
tschaub committed Apr 21, 2020
2 parents cd83f45 + ede39f4 commit c40c2e1
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
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
@@ -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 | O_RDONLY)', function() {
const fd = new FileDescriptor(constants.O_CREAT | constants.O_RDONLY);
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 | O_RDONLY)', function() {
const fd = new FileDescriptor(constants.O_CREAT | constants.O_RDONLY);
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 | O_RDONLY)', function() {
const fd = new FileDescriptor(constants.O_CREAT | constants.O_RDONLY);
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 | O_RDONLY)', function() {
const fd = new FileDescriptor(constants.O_CREAT | constants.O_RDONLY);
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 | O_RDONLY)', function() {
const fd = new FileDescriptor(constants.O_CREAT | constants.O_RDONLY);
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 | O_RDONLY)', function() {
const fd = new FileDescriptor(constants.O_CREAT | constants.O_RDONLY);
assert.isFalse(fd.isExclusive());
});
});
});

0 comments on commit c40c2e1

Please sign in to comment.