Skip to content

[WIP] fix(unlink): unlink file even the file descriptor has been closed already #47

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion lib/tmp.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,25 @@ function _createTmpFile(options, callback) {
fs.open(name, CREATE_FLAGS, opts.mode || FILE_MODE, function _fileCreated(err, fd) {
if (err) return cb(err);

cb(null, name, fd, _prepareTmpFileRemoveCallback(name, fd, opts));
var removeCallback = _prepareRemoveCallback(function(fdPath) {
try {
fs.closeSync(fdPath[0]);
} catch (e) {
// did user close the fd?
if (e.errno !== -1 * _c.EBADF) {
// no, rethrow
throw e;
}
} finally {
fs.unlinkSync(fdPath[1]);
}
}, [fd, name]);

if (!opts.keep) {
_removeObjects.unshift(removeCallback);
}

cb(null, name, fd, removeCallback);
});
});
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tmp",
"version": "0.0.24",
"version": "0.0.25",
"description": "Temporary file and directory creator",
"author": "KARASZI István <github@spam.raszi.hu> (http://raszi.hu/)",

Expand Down
8 changes: 7 additions & 1 deletion test/dir-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,13 @@ vows.describe('Directory creation').addBatch({

'should not return with an error': assert.isNull,
'should return with a name': Test.assertName,
'should be a directory': _testDir(040700)
'should be a directory': function (err, name) {
_testDir(040700)(err, name);
// make sure that everything gets cleaned up
fs.unlinkSync(path.join(name, 'should-be-removed.file'));
fs.unlinkSync(path.join(name, 'symlinkme-target'));
fs.rmdirSync(name);
}
},

'remove callback': {
Expand Down