write-file-atomic
Advanced tools
Comparing version 2.4.1 to 2.4.2
@@ -0,1 +1,6 @@ | ||
# 2.4.2 | ||
* A pair of patches to fix some fd leaks. We would leak fds with sync use | ||
when errors occured and with async use any time fsync was not in use. (#34) | ||
# 2.4.1 | ||
@@ -2,0 +7,0 @@ |
63
index.js
@@ -124,11 +124,14 @@ 'use strict' | ||
}).then(function syncAndClose () { | ||
if (options.fsync !== false) { | ||
return new Promise(function (resolve, reject) { | ||
return new Promise(function (resolve, reject) { | ||
if (options.fsync !== false) { | ||
fs.fsync(fd, function (err) { | ||
if (err) reject(err) | ||
if (err) fs.close(fd, () => reject(err)) | ||
else fs.close(fd, resolve) | ||
}) | ||
}) | ||
} | ||
} else { | ||
fs.close(fd, resolve) | ||
} | ||
}) | ||
}).then(function chown () { | ||
fd = null | ||
if (options.chown) { | ||
@@ -162,5 +165,9 @@ return new Promise(function (resolve, reject) { | ||
}, function fail (err) { | ||
removeOnExitHandler() | ||
fs.unlink(tmpfile, function () { | ||
callback(err) | ||
return new Promise(resolve => { | ||
return fd ? fs.close(fd, resolve) : resolve() | ||
}).then(() => { | ||
removeOnExitHandler() | ||
fs.unlink(tmpfile, function () { | ||
callback(err) | ||
}) | ||
}) | ||
@@ -185,23 +192,26 @@ }).then(function checkQueue () { | ||
try { | ||
if (!options.mode || !options.chown) { | ||
// Either mode or chown is not explicitly set | ||
// Default behavior is to copy it from original file | ||
try { | ||
var stats = fs.statSync(filename) | ||
options = Object.assign({}, options) | ||
if (!options.mode) { | ||
options.mode = stats.mode | ||
} | ||
if (!options.chown && process.getuid) { | ||
options.chown = { uid: stats.uid, gid: stats.gid } | ||
} | ||
} catch (ex) { | ||
// ignore stat errors | ||
if (!options.mode || !options.chown) { | ||
// Either mode or chown is not explicitly set | ||
// Default behavior is to copy it from original file | ||
try { | ||
var stats = fs.statSync(filename) | ||
options = Object.assign({}, options) | ||
if (!options.mode) { | ||
options.mode = stats.mode | ||
} | ||
if (!options.chown && process.getuid) { | ||
options.chown = { uid: stats.uid, gid: stats.gid } | ||
} | ||
} catch (ex) { | ||
// ignore stat errors | ||
} | ||
} | ||
var cleanup = cleanupOnExit(tmpfile) | ||
var removeOnExitHandler = onExit(cleanup) | ||
var fd = fs.openSync(tmpfile, 'w', options.mode) | ||
var fd | ||
var cleanup = cleanupOnExit(tmpfile) | ||
var removeOnExitHandler = onExit(cleanup) | ||
try { | ||
fd = fs.openSync(tmpfile, 'w', options.mode) | ||
if (Buffer.isBuffer(data)) { | ||
@@ -221,2 +231,3 @@ fs.writeSync(fd, data, 0, data.length, 0) | ||
} catch (err) { | ||
if (fd) fs.closeSync(fd) | ||
removeOnExitHandler() | ||
@@ -223,0 +234,0 @@ cleanup() |
{ | ||
"name": "write-file-atomic", | ||
"version": "2.4.1", | ||
"version": "2.4.2", | ||
"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
11900
218