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 } = fs.constants; 42 43const { stringToFlags } = require('internal/fs/utils'); 44 45assert.strictEqual(stringToFlags('r'), O_RDONLY); 46assert.strictEqual(stringToFlags('r+'), O_RDWR); 47assert.strictEqual(stringToFlags('rs+'), O_RDWR | O_SYNC); 48assert.strictEqual(stringToFlags('sr+'), O_RDWR | O_SYNC); 49assert.strictEqual(stringToFlags('w'), O_TRUNC | O_CREAT | O_WRONLY); 50assert.strictEqual(stringToFlags('w+'), O_TRUNC | O_CREAT | O_RDWR); 51assert.strictEqual(stringToFlags('a'), O_APPEND | O_CREAT | O_WRONLY); 52assert.strictEqual(stringToFlags('a+'), O_APPEND | O_CREAT | O_RDWR); 53 54assert.strictEqual(stringToFlags('wx'), O_TRUNC | O_CREAT | O_WRONLY | O_EXCL); 55assert.strictEqual(stringToFlags('xw'), O_TRUNC | O_CREAT | O_WRONLY | O_EXCL); 56assert.strictEqual(stringToFlags('wx+'), O_TRUNC | O_CREAT | O_RDWR | O_EXCL); 57assert.strictEqual(stringToFlags('xw+'), O_TRUNC | O_CREAT | O_RDWR | O_EXCL); 58assert.strictEqual(stringToFlags('ax'), O_APPEND | O_CREAT | O_WRONLY | O_EXCL); 59assert.strictEqual(stringToFlags('xa'), O_APPEND | O_CREAT | O_WRONLY | O_EXCL); 60assert.strictEqual(stringToFlags('as'), O_APPEND | O_CREAT | O_WRONLY | O_SYNC); 61assert.strictEqual(stringToFlags('sa'), O_APPEND | O_CREAT | O_WRONLY | O_SYNC); 62assert.strictEqual(stringToFlags('ax+'), O_APPEND | O_CREAT | O_RDWR | O_EXCL); 63assert.strictEqual(stringToFlags('xa+'), O_APPEND | O_CREAT | O_RDWR | O_EXCL); 64assert.strictEqual(stringToFlags('as+'), O_APPEND | O_CREAT | O_RDWR | O_SYNC); 65assert.strictEqual(stringToFlags('sa+'), O_APPEND | O_CREAT | O_RDWR | O_SYNC); 66 67('+ +a +r +w rw wa war raw r++ a++ w++ x +x x+ rx rx+ wxx wax xwx xxx') 68 .split(' ') 69 .forEach(function(flags) { 70 assert.throws( 71 () => stringToFlags(flags), 72 { code: 'ERR_INVALID_ARG_VALUE', name: 'TypeError' } 73 ); 74 }); 75 76assert.throws( 77 () => stringToFlags({}), 78 { code: 'ERR_INVALID_ARG_VALUE', name: 'TypeError' } 79); 80 81assert.throws( 82 () => stringToFlags(true), 83 { code: 'ERR_INVALID_ARG_VALUE', name: 'TypeError' } 84); 85 86if (common.isLinux || common.isOSX) { 87 const tmpdir = require('../common/tmpdir'); 88 tmpdir.refresh(); 89 const file = path.join(tmpdir.path, 'a.js'); 90 fs.copyFileSync(fixtures.path('a.js'), file); 91 fs.open(file, O_DSYNC, common.mustSucceed((fd) => { 92 fs.closeSync(fd); 93 })); 94} 95