Skip to content

Commit

Permalink
fs: support as and as+ flags in stringToFlags()
Browse files Browse the repository at this point in the history
PR-URL: #18801
Reviewed-By: Joyee Cheung <[email protected]>
Reviewed-By: Matheus Marchini <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
  • Loading branch information
SirR4T authored and MylesBorins committed Mar 20, 2018
1 parent 49391a7 commit 607b33c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
6 changes: 6 additions & 0 deletions doc/api/fs.md
Original file line number Diff line number Diff line change
Expand Up @@ -2008,11 +2008,17 @@ The file is created if it does not exist.

* `'ax'` - Like `'a'` but fails if `path` exists.

* `'as'` - Open file for appending in synchronous mode.
The file is created if it does not exist.

* `'a+'` - Open file for reading and appending.
The file is created if it does not exist.

* `'ax+'` - Like `'a+'` but fails if `path` exists.

* `'as+'` - Open file for reading and appending in synchronous mode.
The file is created if it does not exist.

`mode` sets the file mode (permission and sticky bits), but only if the file was
created. It defaults to `0o666` (readable and writable).

Expand Down
4 changes: 4 additions & 0 deletions lib/internal/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,14 @@ function stringToFlags(flags) {
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 'as' : // Fall through.
case 'sa' : return O_APPEND | O_CREAT | O_WRONLY | O_SYNC;

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

throw new errors.TypeError('ERR_INVALID_OPT_VALUE', 'flags', flags);
Expand Down
4 changes: 4 additions & 0 deletions test/parallel/test-fs-open-flags.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,12 @@ assert.strictEqual(stringToFlags('wx+'), O_TRUNC | O_CREAT | O_RDWR | O_EXCL);
assert.strictEqual(stringToFlags('xw+'), O_TRUNC | O_CREAT | O_RDWR | O_EXCL);
assert.strictEqual(stringToFlags('ax'), O_APPEND | O_CREAT | O_WRONLY | O_EXCL);
assert.strictEqual(stringToFlags('xa'), O_APPEND | O_CREAT | O_WRONLY | O_EXCL);
assert.strictEqual(stringToFlags('as'), O_APPEND | O_CREAT | O_WRONLY | O_SYNC);
assert.strictEqual(stringToFlags('sa'), O_APPEND | O_CREAT | O_WRONLY | O_SYNC);
assert.strictEqual(stringToFlags('ax+'), O_APPEND | O_CREAT | O_RDWR | O_EXCL);
assert.strictEqual(stringToFlags('xa+'), O_APPEND | O_CREAT | O_RDWR | O_EXCL);
assert.strictEqual(stringToFlags('as+'), O_APPEND | O_CREAT | O_RDWR | O_SYNC);
assert.strictEqual(stringToFlags('sa+'), O_APPEND | O_CREAT | O_RDWR | O_SYNC);

('+ +a +r +w rw wa war raw r++ a++ w++ x +x x+ rx rx+ wxx wax xwx xxx')
.split(' ')
Expand Down

0 comments on commit 607b33c

Please sign in to comment.