• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright Joyent, Inc. and other Node contributors.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a
4// copy of this software and associated documentation files (the
5// "Software"), to deal in the Software without restriction, including
6// without limitation the rights to use, copy, modify, merge, publish,
7// distribute, sublicense, and/or sell copies of the Software, and to permit
8// persons to whom the Software is furnished to do so, subject to the
9// following conditions:
10//
11// The above copyright notice and this permission notice shall be included
12// in all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20// USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22// Flags: --expose-internals
23'use strict';
24const common = require('../common');
25
26const fixtures = require('../common/fixtures');
27
28const assert = require('assert');
29const fs = require('fs');
30const path = require('path');
31
32// 0 if not found in fs.constants
33const { O_APPEND = 0,
34        O_CREAT = 0,
35        O_EXCL = 0,
36        O_RDONLY = 0,
37        O_RDWR = 0,
38        O_SYNC = 0,
39        O_DSYNC = 0,
40        O_TRUNC = 0,
41        O_WRONLY = 0
42} = fs.constants;
43
44const { stringToFlags } = require('internal/fs/utils');
45
46assert.strictEqual(stringToFlags('r'), O_RDONLY);
47assert.strictEqual(stringToFlags('r+'), O_RDWR);
48assert.strictEqual(stringToFlags('rs+'), O_RDWR | O_SYNC);
49assert.strictEqual(stringToFlags('sr+'), O_RDWR | O_SYNC);
50assert.strictEqual(stringToFlags('w'), O_TRUNC | O_CREAT | O_WRONLY);
51assert.strictEqual(stringToFlags('w+'), O_TRUNC | O_CREAT | O_RDWR);
52assert.strictEqual(stringToFlags('a'), O_APPEND | O_CREAT | O_WRONLY);
53assert.strictEqual(stringToFlags('a+'), O_APPEND | O_CREAT | O_RDWR);
54
55assert.strictEqual(stringToFlags('wx'), O_TRUNC | O_CREAT | O_WRONLY | O_EXCL);
56assert.strictEqual(stringToFlags('xw'), O_TRUNC | O_CREAT | O_WRONLY | O_EXCL);
57assert.strictEqual(stringToFlags('wx+'), O_TRUNC | O_CREAT | O_RDWR | O_EXCL);
58assert.strictEqual(stringToFlags('xw+'), O_TRUNC | O_CREAT | O_RDWR | O_EXCL);
59assert.strictEqual(stringToFlags('ax'), O_APPEND | O_CREAT | O_WRONLY | O_EXCL);
60assert.strictEqual(stringToFlags('xa'), O_APPEND | O_CREAT | O_WRONLY | O_EXCL);
61assert.strictEqual(stringToFlags('as'), O_APPEND | O_CREAT | O_WRONLY | O_SYNC);
62assert.strictEqual(stringToFlags('sa'), O_APPEND | O_CREAT | O_WRONLY | O_SYNC);
63assert.strictEqual(stringToFlags('ax+'), O_APPEND | O_CREAT | O_RDWR | O_EXCL);
64assert.strictEqual(stringToFlags('xa+'), O_APPEND | O_CREAT | O_RDWR | O_EXCL);
65assert.strictEqual(stringToFlags('as+'), O_APPEND | O_CREAT | O_RDWR | O_SYNC);
66assert.strictEqual(stringToFlags('sa+'), O_APPEND | O_CREAT | O_RDWR | O_SYNC);
67
68('+ +a +r +w rw wa war raw r++ a++ w++ x +x x+ rx rx+ wxx wax xwx xxx')
69  .split(' ')
70  .forEach(function(flags) {
71    assert.throws(
72      () => stringToFlags(flags),
73      { code: 'ERR_INVALID_OPT_VALUE', name: 'TypeError' }
74    );
75  });
76
77assert.throws(
78  () => stringToFlags({}),
79  { code: 'ERR_INVALID_OPT_VALUE', name: 'TypeError' }
80);
81
82assert.throws(
83  () => stringToFlags(true),
84  { code: 'ERR_INVALID_OPT_VALUE', name: 'TypeError' }
85);
86
87assert.throws(
88  () => stringToFlags(null),
89  { code: 'ERR_INVALID_OPT_VALUE', name: 'TypeError' }
90);
91
92if (common.isLinux || common.isOSX) {
93  const tmpdir = require('../common/tmpdir');
94  tmpdir.refresh();
95  const file = path.join(tmpdir.path, 'a.js');
96  fs.copyFileSync(fixtures.path('a.js'), file);
97  fs.open(file, O_DSYNC, common.mustCall(assert.ifError));
98}
99