Skip to content

Commit

Permalink
fs: move stringToFlags() to lib/internal
Browse files Browse the repository at this point in the history
PR-URL: #7162
Refs: #6413
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Сковорода Никита Андреевич <chalkerx@gmail.com>
  • Loading branch information
bnoordhuis committed Sep 23, 2016
1 parent a3f861d commit a8d2c9d
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 70 deletions.
52 changes: 1 addition & 51 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const FSReqWrap = binding.FSReqWrap;
const FSEvent = process.binding('fs_event_wrap').FSEvent;
const internalFS = require('internal/fs');
const assertEncoding = internalFS.assertEncoding;
const stringToFlags = internalFS.stringToFlags;
const SyncWriteStream = internalFS.SyncWriteStream;

Object.defineProperty(exports, 'constants', {
Expand All @@ -30,15 +31,6 @@ const Writable = Stream.Writable;
const kMinPoolSpace = 128;
const kMaxLength = require('buffer').kMaxLength;

const O_APPEND = constants.O_APPEND || 0;
const O_CREAT = constants.O_CREAT || 0;
const O_EXCL = constants.O_EXCL || 0;
const O_RDONLY = constants.O_RDONLY || 0;
const O_RDWR = constants.O_RDWR || 0;
const O_SYNC = constants.O_SYNC || 0;
const O_TRUNC = constants.O_TRUNC || 0;
const O_WRONLY = constants.O_WRONLY || 0;

const isWindows = process.platform === 'win32';

const DEBUG = process.env.NODE_DEBUG && /fs/.test(process.env.NODE_DEBUG);
Expand Down Expand Up @@ -522,48 +514,6 @@ fs.readFileSync = function(path, options) {
};


// Used by binding.open and friends
function stringToFlags(flag) {
// Return early if it's a number
if (typeof flag === 'number') {
return flag;
}

switch (flag) {
case 'r' : return O_RDONLY;
case 'rs' : // fall through
case 'sr' : return O_RDONLY | O_SYNC;
case 'r+' : return O_RDWR;
case 'rs+' : // fall through
case 'sr+' : return O_RDWR | O_SYNC;

case 'w' : return O_TRUNC | O_CREAT | O_WRONLY;
case 'wx' : // fall through
case 'xw' : return O_TRUNC | O_CREAT | O_WRONLY | O_EXCL;

case 'w+' : return O_TRUNC | O_CREAT | O_RDWR;
case 'wx+': // fall through
case 'xw+': return O_TRUNC | O_CREAT | O_RDWR | O_EXCL;

case 'a' : return O_APPEND | O_CREAT | O_WRONLY;
case 'ax' : // fall through
case 'xa' : return O_APPEND | O_CREAT | O_WRONLY | O_EXCL;

case 'a+' : return O_APPEND | O_CREAT | O_RDWR;
case 'ax+': // fall through
case 'xa+': return O_APPEND | O_CREAT | O_RDWR | O_EXCL;
}

throw new Error('Unknown file open flag: ' + flag);
}

// exported but hidden, only used by test/simple/test-fs-open-flags.js
Object.defineProperty(exports, '_stringToFlags', {
enumerable: false,
value: stringToFlags
});


// Yes, the follow could be easily DRYed up but I provide the explicit
// list to make the arguments clear.

Expand Down
44 changes: 44 additions & 0 deletions lib/internal/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ const Buffer = require('buffer').Buffer;
const Stream = require('stream').Stream;
const fs = require('fs');
const util = require('util');
const constants = process.binding('constants').fs;

const O_APPEND = constants.O_APPEND | 0;
const O_CREAT = constants.O_CREAT | 0;
const O_EXCL = constants.O_EXCL | 0;
const O_RDONLY = constants.O_RDONLY | 0;
const O_RDWR = constants.O_RDWR | 0;
const O_SYNC = constants.O_SYNC | 0;
const O_TRUNC = constants.O_TRUNC | 0;
const O_WRONLY = constants.O_WRONLY | 0;

function assertEncoding(encoding) {
if (encoding && !Buffer.isEncoding(encoding)) {
Expand All @@ -12,6 +22,40 @@ function assertEncoding(encoding) {
}
exports.assertEncoding = assertEncoding;

function stringToFlags(flag) {
if (typeof flag === 'number') {
return flag;
}

switch (flag) {
case 'r' : return O_RDONLY;
case 'rs' : // Fall through.
case 'sr' : return O_RDONLY | O_SYNC;
case 'r+' : return O_RDWR;
case 'rs+' : // Fall through.
case 'sr+' : return O_RDWR | O_SYNC;

case 'w' : return O_TRUNC | O_CREAT | O_WRONLY;
case 'wx' : // Fall through.
case 'xw' : return O_TRUNC | O_CREAT | O_WRONLY | O_EXCL;

case 'w+' : return O_TRUNC | O_CREAT | O_RDWR;
case 'wx+': // Fall through.
case 'xw+': return O_TRUNC | O_CREAT | O_RDWR | O_EXCL;

case 'a' : return O_APPEND | O_CREAT | O_WRONLY;
case 'ax' : // Fall through.
case 'xa' : return O_APPEND | O_CREAT | O_WRONLY | O_EXCL;

case 'a+' : return O_APPEND | O_CREAT | O_RDWR;
case 'ax+': // Fall through.
case 'xa+': return O_APPEND | O_CREAT | O_RDWR | O_EXCL;
}

throw new Error('Unknown file open flag: ' + flag);
}
exports.stringToFlags = stringToFlags;

// Temporary hack for process.stdout and process.stderr when piped to files.
function SyncWriteStream(fd, options) {
Stream.call(this);
Expand Down
41 changes: 22 additions & 19 deletions test/parallel/test-fs-open-flags.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Flags: --expose_internals
'use strict';
require('../common');
var assert = require('assert');
Expand All @@ -12,39 +13,41 @@ var O_RDWR = fs.constants.O_RDWR || 0;
var O_TRUNC = fs.constants.O_TRUNC || 0;
var O_WRONLY = fs.constants.O_WRONLY || 0;

assert.equal(fs._stringToFlags('r'), O_RDONLY);
assert.equal(fs._stringToFlags('r+'), O_RDWR);
assert.equal(fs._stringToFlags('w'), O_TRUNC | O_CREAT | O_WRONLY);
assert.equal(fs._stringToFlags('w+'), O_TRUNC | O_CREAT | O_RDWR);
assert.equal(fs._stringToFlags('a'), O_APPEND | O_CREAT | O_WRONLY);
assert.equal(fs._stringToFlags('a+'), O_APPEND | O_CREAT | O_RDWR);

assert.equal(fs._stringToFlags('wx'), O_TRUNC | O_CREAT | O_WRONLY | O_EXCL);
assert.equal(fs._stringToFlags('xw'), O_TRUNC | O_CREAT | O_WRONLY | O_EXCL);
assert.equal(fs._stringToFlags('wx+'), O_TRUNC | O_CREAT | O_RDWR | O_EXCL);
assert.equal(fs._stringToFlags('xw+'), O_TRUNC | O_CREAT | O_RDWR | O_EXCL);
assert.equal(fs._stringToFlags('ax'), O_APPEND | O_CREAT | O_WRONLY | O_EXCL);
assert.equal(fs._stringToFlags('xa'), O_APPEND | O_CREAT | O_WRONLY | O_EXCL);
assert.equal(fs._stringToFlags('ax+'), O_APPEND | O_CREAT | O_RDWR | O_EXCL);
assert.equal(fs._stringToFlags('xa+'), O_APPEND | O_CREAT | O_RDWR | O_EXCL);
const { stringToFlags } = require('internal/fs');

assert.equal(stringToFlags('r'), O_RDONLY);
assert.equal(stringToFlags('r+'), O_RDWR);
assert.equal(stringToFlags('w'), O_TRUNC | O_CREAT | O_WRONLY);
assert.equal(stringToFlags('w+'), O_TRUNC | O_CREAT | O_RDWR);
assert.equal(stringToFlags('a'), O_APPEND | O_CREAT | O_WRONLY);
assert.equal(stringToFlags('a+'), O_APPEND | O_CREAT | O_RDWR);

assert.equal(stringToFlags('wx'), O_TRUNC | O_CREAT | O_WRONLY | O_EXCL);
assert.equal(stringToFlags('xw'), O_TRUNC | O_CREAT | O_WRONLY | O_EXCL);
assert.equal(stringToFlags('wx+'), O_TRUNC | O_CREAT | O_RDWR | O_EXCL);
assert.equal(stringToFlags('xw+'), O_TRUNC | O_CREAT | O_RDWR | O_EXCL);
assert.equal(stringToFlags('ax'), O_APPEND | O_CREAT | O_WRONLY | O_EXCL);
assert.equal(stringToFlags('xa'), O_APPEND | O_CREAT | O_WRONLY | O_EXCL);
assert.equal(stringToFlags('ax+'), O_APPEND | O_CREAT | O_RDWR | O_EXCL);
assert.equal(stringToFlags('xa+'), O_APPEND | O_CREAT | O_RDWR | O_EXCL);

('+ +a +r +w rw wa war raw r++ a++ w++ x +x x+ rx rx+ wxx wax xwx xxx')
.split(' ')
.forEach(function(flags) {
assert.throws(function() { fs._stringToFlags(flags); });
assert.throws(function() { stringToFlags(flags); });
});

assert.throws(
() => fs._stringToFlags({}),
() => stringToFlags({}),
/Unknown file open flag: \[object Object\]/
);

assert.throws(
() => fs._stringToFlags(true),
() => stringToFlags(true),
/Unknown file open flag: true/
);

assert.throws(
() => fs._stringToFlags(null),
() => stringToFlags(null),
/Unknown file open flag: null/
);

0 comments on commit a8d2c9d

Please sign in to comment.