Skip to content
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

Added removeCallback passing #25

Merged
merged 2 commits into from
Jul 9, 2014
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules/
.idea/
36 changes: 29 additions & 7 deletions lib/tmp.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,21 @@ function _createTmpFile(options, callback) {
fs.open(name, _c.O_CREAT | _c.O_EXCL | _c.O_RDWR, opts.mode || 0600, function _fileCreated(err, fd) {
if (err) return cb(err);

if (!opts.keep) _removeObjects.unshift([ fs.unlinkSync, name ]);
var removed = false;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the reason the keep the remove variable? Only the removeCallback function could set this var and therefore will be never true at the check.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or you're protecting the code from the double call?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a protection from double call.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But this is not enough protection, I think it would be better to do an fs.exists check before the removal.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It just protects from double call.
But not from double delete if user deletes file by themselves.
Also the last variant was not covered earlier. I think it should be a separate change.

var removeCallback = function() {
if (removed) {
return;
}

fs.unlinkSync(name);
removed = true;
};

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

cb(null, name, fd);
cb(null, name, fd, removeCallback);
});
});
}
Expand Down Expand Up @@ -218,15 +230,25 @@ function _createTmpDir(options, callback) {
fs.mkdir(name, opts.mode || 0700, function _dirCreated(err) {
if (err) return cb(err);

if (!opts.keep) {
var removed = false;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a duplicate code could you refactor that into a separate function which gets the remove function as the parameter (_rmdirRecursiveSync or fs.rmdirSync or fs.unlinkSync) and does the job?

var removeCallback = function() {
if (removed) {
return;
}

if (opts.unsafeCleanup) {
_removeObjects.unshift([ _rmdirRecursiveSync, name ]);
_rmdirRecursiveSync(name);
} else {
_removeObjects.unshift([ fs.rmdirSync, name ]);
fs.rmdirSync(name);
}
removed = true;
};

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

cb(null, name);
cb(null, name, removeCallback);
});
});
}
Expand All @@ -243,7 +265,7 @@ function _garbageCollector() {

for (var i = 0, length = _removeObjects.length; i < length; i++) {
try {
_removeObjects[i][0].call(null, _removeObjects[i][1]);
_removeObjects[i].call(null);
} catch (e) {
// already removed?
}
Expand Down