write-file-atomic
Advanced tools
Comparing version 3.0.2 to 3.0.3
52
index.js
@@ -58,2 +58,18 @@ 'use strict' | ||
// https://github.com/isaacs/node-graceful-fs/blob/master/polyfills.js#L315-L342 | ||
function isChownErrOk (err) { | ||
if (err.code === 'ENOSYS') { | ||
return true | ||
} | ||
const nonroot = !process.getuid || process.getuid() !== 0 | ||
if (nonroot) { | ||
if (err.code === 'EINVAL' || err.code === 'EPERM') { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
async function writeFileAsync (filename, data, options = {}) { | ||
@@ -111,7 +127,15 @@ if (typeof options === 'string') { | ||
if (options.chown) { | ||
await promisify(fs.chown)(tmpfile, options.chown.uid, options.chown.gid) | ||
await promisify(fs.chown)(tmpfile, options.chown.uid, options.chown.gid).catch(err => { | ||
if (!isChownErrOk(err)) { | ||
throw err | ||
} | ||
}) | ||
} | ||
if (options.mode) { | ||
await promisify(fs.chmod)(tmpfile, options.mode) | ||
await promisify(fs.chmod)(tmpfile, options.mode).catch(err => { | ||
if (!isChownErrOk(err)) { | ||
throw err | ||
} | ||
}) | ||
} | ||
@@ -198,6 +222,26 @@ | ||
} | ||
fs.closeSync(fd) | ||
fd = null | ||
if (options.chown) fs.chownSync(tmpfile, options.chown.uid, options.chown.gid) | ||
if (options.mode) fs.chmodSync(tmpfile, options.mode) | ||
if (options.chown) { | ||
try { | ||
fs.chownSync(tmpfile, options.chown.uid, options.chown.gid) | ||
} catch (err) { | ||
if (!isChownErrOk(err)) { | ||
throw err | ||
} | ||
} | ||
} | ||
if (options.mode) { | ||
try { | ||
fs.chmodSync(tmpfile, options.mode) | ||
} catch (err) { | ||
if (!isChownErrOk(err)) { | ||
throw err | ||
} | ||
} | ||
} | ||
fs.renameSync(tmpfile, filename) | ||
@@ -204,0 +248,0 @@ threw = false |
{ | ||
"name": "write-file-atomic", | ||
"version": "3.0.2", | ||
"version": "3.0.3", | ||
"description": "Write files in an atomic fashion w/configurable ownership", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
12787
227