Skip to content

Commit

Permalink
fix #213 tmp.file must not unlink file when discarding the file descr…
Browse files Browse the repository at this point in the history
…iptor, similar to tmp.fileSync
  • Loading branch information
silkentrance committed Feb 3, 2020
1 parent 5195a26 commit b63cb58
Showing 1 changed file with 5 additions and 20 deletions.
25 changes: 5 additions & 20 deletions lib/tmp.js
Expand Up @@ -253,29 +253,14 @@ function file(options, callback) {
/* istanbul ignore else */
if (err) return cb(err);

// FIXME overall handling of opts.discardDescriptor is off
if (opts.discardDescriptor) {
// FIXME? must not unlink as the user expects the filename to be reserved
return fs.close(fd, function _discardCallback(err) {
/* istanbul ignore else */
if (err) {
// Low probability, and the file exists, so this could be
// ignored. If it isn't we certainly need to unlink the
// file, and if that fails too its error is more
// important.
try {
fs.unlinkSync(name);
} catch (e) {
if (!isENOENT(e)) {
err = e;
}
}
return cb(err);
}
cb(null, name, undefined, _prepareTmpFileRemoveCallback(name, -1, opts));
return fs.close(fd, function _discardCallback(possibleErr) {
// the chance of getting an error on close here is rather low and might occur in the most edgiest cases only
return cb(possibleErr, name, undefined, _prepareTmpFileRemoveCallback(name, -1, opts, false));
});
} else {
// FIXME detachDescriptor passes the descriptor whereas discardDescriptor closes it
// detachDescriptor passes the descriptor whereas discardDescriptor closes it, either way, we no longer care
// about the descriptor
const discardOrDetachDescriptor = opts.discardDescriptor || opts.detachDescriptor;
cb(null, name, fd, _prepareTmpFileRemoveCallback(name, discardOrDetachDescriptor ? -1 : fd, opts, false));
}
Expand Down

0 comments on commit b63cb58

Please sign in to comment.