Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix: fix isRead check on file descriptor #289

Merged
merged 2 commits into from
Apr 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 | 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());
3cp marked this conversation as resolved.
Show resolved Hide resolved
});
});

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());
});
});
});