Skip to content

Commit 7f7d1d3

Browse files
bnoordhuisjasnell
authored andcommitted
fs: move stringToFlags() to lib/internal
PR-URL: #7162 Refs: #6413 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Сковорода Никита Андреевич <[email protected]>
1 parent 2f05af4 commit 7f7d1d3

File tree

3 files changed

+67
-70
lines changed

3 files changed

+67
-70
lines changed

lib/fs.js

+1-51
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const FSReqWrap = binding.FSReqWrap;
1616
const FSEvent = process.binding('fs_event_wrap').FSEvent;
1717
const internalFS = require('internal/fs');
1818
const assertEncoding = internalFS.assertEncoding;
19+
const stringToFlags = internalFS.stringToFlags;
1920
const SyncWriteStream = internalFS.SyncWriteStream;
2021

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

33-
const O_APPEND = constants.O_APPEND || 0;
34-
const O_CREAT = constants.O_CREAT || 0;
35-
const O_EXCL = constants.O_EXCL || 0;
36-
const O_RDONLY = constants.O_RDONLY || 0;
37-
const O_RDWR = constants.O_RDWR || 0;
38-
const O_SYNC = constants.O_SYNC || 0;
39-
const O_TRUNC = constants.O_TRUNC || 0;
40-
const O_WRONLY = constants.O_WRONLY || 0;
41-
4234
const isWindows = process.platform === 'win32';
4335

4436
const DEBUG = process.env.NODE_DEBUG && /fs/.test(process.env.NODE_DEBUG);
@@ -521,48 +513,6 @@ fs.readFileSync = function(path, options) {
521513
};
522514

523515

524-
// Used by binding.open and friends
525-
function stringToFlags(flag) {
526-
// Return early if it's a number
527-
if (typeof flag === 'number') {
528-
return flag;
529-
}
530-
531-
switch (flag) {
532-
case 'r' : return O_RDONLY;
533-
case 'rs' : // fall through
534-
case 'sr' : return O_RDONLY | O_SYNC;
535-
case 'r+' : return O_RDWR;
536-
case 'rs+' : // fall through
537-
case 'sr+' : return O_RDWR | O_SYNC;
538-
539-
case 'w' : return O_TRUNC | O_CREAT | O_WRONLY;
540-
case 'wx' : // fall through
541-
case 'xw' : return O_TRUNC | O_CREAT | O_WRONLY | O_EXCL;
542-
543-
case 'w+' : return O_TRUNC | O_CREAT | O_RDWR;
544-
case 'wx+': // fall through
545-
case 'xw+': return O_TRUNC | O_CREAT | O_RDWR | O_EXCL;
546-
547-
case 'a' : return O_APPEND | O_CREAT | O_WRONLY;
548-
case 'ax' : // fall through
549-
case 'xa' : return O_APPEND | O_CREAT | O_WRONLY | O_EXCL;
550-
551-
case 'a+' : return O_APPEND | O_CREAT | O_RDWR;
552-
case 'ax+': // fall through
553-
case 'xa+': return O_APPEND | O_CREAT | O_RDWR | O_EXCL;
554-
}
555-
556-
throw new Error('Unknown file open flag: ' + flag);
557-
}
558-
559-
// exported but hidden, only used by test/simple/test-fs-open-flags.js
560-
Object.defineProperty(exports, '_stringToFlags', {
561-
enumerable: false,
562-
value: stringToFlags
563-
});
564-
565-
566516
// Yes, the follow could be easily DRYed up but I provide the explicit
567517
// list to make the arguments clear.
568518

lib/internal/fs.js

+44
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@ const Buffer = require('buffer').Buffer;
44
const Writable = require('stream').Writable;
55
const fs = require('fs');
66
const util = require('util');
7+
const constants = process.binding('constants').fs;
8+
9+
const O_APPEND = constants.O_APPEND | 0;
10+
const O_CREAT = constants.O_CREAT | 0;
11+
const O_EXCL = constants.O_EXCL | 0;
12+
const O_RDONLY = constants.O_RDONLY | 0;
13+
const O_RDWR = constants.O_RDWR | 0;
14+
const O_SYNC = constants.O_SYNC | 0;
15+
const O_TRUNC = constants.O_TRUNC | 0;
16+
const O_WRONLY = constants.O_WRONLY | 0;
717

818
function assertEncoding(encoding) {
919
if (encoding && !Buffer.isEncoding(encoding)) {
@@ -12,6 +22,40 @@ function assertEncoding(encoding) {
1222
}
1323
exports.assertEncoding = assertEncoding;
1424

25+
function stringToFlags(flag) {
26+
if (typeof flag === 'number') {
27+
return flag;
28+
}
29+
30+
switch (flag) {
31+
case 'r' : return O_RDONLY;
32+
case 'rs' : // Fall through.
33+
case 'sr' : return O_RDONLY | O_SYNC;
34+
case 'r+' : return O_RDWR;
35+
case 'rs+' : // Fall through.
36+
case 'sr+' : return O_RDWR | O_SYNC;
37+
38+
case 'w' : return O_TRUNC | O_CREAT | O_WRONLY;
39+
case 'wx' : // Fall through.
40+
case 'xw' : return O_TRUNC | O_CREAT | O_WRONLY | O_EXCL;
41+
42+
case 'w+' : return O_TRUNC | O_CREAT | O_RDWR;
43+
case 'wx+': // Fall through.
44+
case 'xw+': return O_TRUNC | O_CREAT | O_RDWR | O_EXCL;
45+
46+
case 'a' : return O_APPEND | O_CREAT | O_WRONLY;
47+
case 'ax' : // Fall through.
48+
case 'xa' : return O_APPEND | O_CREAT | O_WRONLY | O_EXCL;
49+
50+
case 'a+' : return O_APPEND | O_CREAT | O_RDWR;
51+
case 'ax+': // Fall through.
52+
case 'xa+': return O_APPEND | O_CREAT | O_RDWR | O_EXCL;
53+
}
54+
55+
throw new Error('Unknown file open flag: ' + flag);
56+
}
57+
exports.stringToFlags = stringToFlags;
58+
1559
// Temporary hack for process.stdout and process.stderr when piped to files.
1660
function SyncWriteStream(fd, options) {
1761
Writable.call(this);

test/parallel/test-fs-open-flags.js

+22-19
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Flags: --expose_internals
12
'use strict';
23
require('../common');
34
var assert = require('assert');
@@ -12,39 +13,41 @@ var O_RDWR = fs.constants.O_RDWR || 0;
1213
var O_TRUNC = fs.constants.O_TRUNC || 0;
1314
var O_WRONLY = fs.constants.O_WRONLY || 0;
1415

15-
assert.equal(fs._stringToFlags('r'), O_RDONLY);
16-
assert.equal(fs._stringToFlags('r+'), O_RDWR);
17-
assert.equal(fs._stringToFlags('w'), O_TRUNC | O_CREAT | O_WRONLY);
18-
assert.equal(fs._stringToFlags('w+'), O_TRUNC | O_CREAT | O_RDWR);
19-
assert.equal(fs._stringToFlags('a'), O_APPEND | O_CREAT | O_WRONLY);
20-
assert.equal(fs._stringToFlags('a+'), O_APPEND | O_CREAT | O_RDWR);
21-
22-
assert.equal(fs._stringToFlags('wx'), O_TRUNC | O_CREAT | O_WRONLY | O_EXCL);
23-
assert.equal(fs._stringToFlags('xw'), O_TRUNC | O_CREAT | O_WRONLY | O_EXCL);
24-
assert.equal(fs._stringToFlags('wx+'), O_TRUNC | O_CREAT | O_RDWR | O_EXCL);
25-
assert.equal(fs._stringToFlags('xw+'), O_TRUNC | O_CREAT | O_RDWR | O_EXCL);
26-
assert.equal(fs._stringToFlags('ax'), O_APPEND | O_CREAT | O_WRONLY | O_EXCL);
27-
assert.equal(fs._stringToFlags('xa'), O_APPEND | O_CREAT | O_WRONLY | O_EXCL);
28-
assert.equal(fs._stringToFlags('ax+'), O_APPEND | O_CREAT | O_RDWR | O_EXCL);
29-
assert.equal(fs._stringToFlags('xa+'), O_APPEND | O_CREAT | O_RDWR | O_EXCL);
16+
const { stringToFlags } = require('internal/fs');
17+
18+
assert.equal(stringToFlags('r'), O_RDONLY);
19+
assert.equal(stringToFlags('r+'), O_RDWR);
20+
assert.equal(stringToFlags('w'), O_TRUNC | O_CREAT | O_WRONLY);
21+
assert.equal(stringToFlags('w+'), O_TRUNC | O_CREAT | O_RDWR);
22+
assert.equal(stringToFlags('a'), O_APPEND | O_CREAT | O_WRONLY);
23+
assert.equal(stringToFlags('a+'), O_APPEND | O_CREAT | O_RDWR);
24+
25+
assert.equal(stringToFlags('wx'), O_TRUNC | O_CREAT | O_WRONLY | O_EXCL);
26+
assert.equal(stringToFlags('xw'), O_TRUNC | O_CREAT | O_WRONLY | O_EXCL);
27+
assert.equal(stringToFlags('wx+'), O_TRUNC | O_CREAT | O_RDWR | O_EXCL);
28+
assert.equal(stringToFlags('xw+'), O_TRUNC | O_CREAT | O_RDWR | O_EXCL);
29+
assert.equal(stringToFlags('ax'), O_APPEND | O_CREAT | O_WRONLY | O_EXCL);
30+
assert.equal(stringToFlags('xa'), O_APPEND | O_CREAT | O_WRONLY | O_EXCL);
31+
assert.equal(stringToFlags('ax+'), O_APPEND | O_CREAT | O_RDWR | O_EXCL);
32+
assert.equal(stringToFlags('xa+'), O_APPEND | O_CREAT | O_RDWR | O_EXCL);
3033

3134
('+ +a +r +w rw wa war raw r++ a++ w++ x +x x+ rx rx+ wxx wax xwx xxx')
3235
.split(' ')
3336
.forEach(function(flags) {
34-
assert.throws(function() { fs._stringToFlags(flags); });
37+
assert.throws(function() { stringToFlags(flags); });
3538
});
3639

3740
assert.throws(
38-
() => fs._stringToFlags({}),
41+
() => stringToFlags({}),
3942
/Unknown file open flag: \[object Object\]/
4043
);
4144

4245
assert.throws(
43-
() => fs._stringToFlags(true),
46+
() => stringToFlags(true),
4447
/Unknown file open flag: true/
4548
);
4649

4750
assert.throws(
48-
() => fs._stringToFlags(null),
51+
() => stringToFlags(null),
4952
/Unknown file open flag: null/
5053
);

0 commit comments

Comments
 (0)