From 925efacf8ae2b468082de109061bc54d375e8582 Mon Sep 17 00:00:00 2001 From: Sarat Addepalli Date: Thu, 15 Feb 2018 19:43:26 +0530 Subject: [PATCH] fs: support as and as+ flags in stringToFlags() PR-URL: https://github.com/nodejs/node/pull/18801 Reviewed-By: Joyee Cheung Reviewed-By: Matheus Marchini Reviewed-By: Anna Henningsen Reviewed-By: James M Snell Reviewed-By: Luigi Pinca Reviewed-By: Ruben Bridgewater --- doc/api/fs.md | 6 ++++++ lib/internal/fs.js | 4 ++++ test/parallel/test-fs-open-flags.js | 4 ++++ 3 files changed, 14 insertions(+) diff --git a/doc/api/fs.md b/doc/api/fs.md index 9f9db8c5e75cc7..751ba9e939dfc7 100644 --- a/doc/api/fs.md +++ b/doc/api/fs.md @@ -1794,11 +1794,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). diff --git a/lib/internal/fs.js b/lib/internal/fs.js index 05caf9c6496850..c6a31b4321809b 100644 --- a/lib/internal/fs.js +++ b/lib/internal/fs.js @@ -46,10 +46,14 @@ function stringToFlags(flag) { 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 Error('Unknown file open flag: ' + flag); diff --git a/test/parallel/test-fs-open-flags.js b/test/parallel/test-fs-open-flags.js index adc1f899bfb223..edb4d158b13b58 100644 --- a/test/parallel/test-fs-open-flags.js +++ b/test/parallel/test-fs-open-flags.js @@ -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(' ')