-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix file descriptor leak on sync errors
- Loading branch information
Showing
1 changed file
with
22 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -180,27 +180,30 @@ function writeFileSync (filename, data, options) { | |
} | ||
var tmpfile = getTmpname(filename) | ||
|
||
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 fd | ||
var cleanup = cleanupOnExit(tmpfile) | ||
var removeOnExitHandler = onExit(cleanup) | ||
|
||
try { | ||
|
||
var cleanup = cleanupOnExit(tmpfile) | ||
var removeOnExitHandler = onExit(cleanup) | ||
var fd = fs.openSync(tmpfile, 'w', options.mode) | ||
fd = fs.openSync(tmpfile, 'w', options.mode) | ||
if (Buffer.isBuffer(data)) { | ||
fs.writeSync(fd, data, 0, data.length, 0) | ||
} else if (data != null) { | ||
|
@@ -215,6 +218,7 @@ function writeFileSync (filename, data, options) { | |
fs.renameSync(tmpfile, filename) | ||
removeOnExitHandler() | ||
} catch (err) { | ||
if (fd) fs.closeSync(fd) | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
iarna
Author
Contributor
|
||
removeOnExitHandler() | ||
cleanup() | ||
throw err | ||
|
@iarna I wonder if this is wrong? Getting lots of
which I think is probably from this?