Skip to content

Commit

Permalink
lib/package: Handle EPERM/EACCES errors on fs.rename caused by antivi…
Browse files Browse the repository at this point in the history
…rus programs in Windows locking newly created dirs.
  • Loading branch information
tomas committed Dec 29, 2014
1 parent 354db3c commit 40baf40
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion lib/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,29 @@ var unpack = function(zip, dest, cb) {
}

var package = module.exports;
var move = function(from, to, cb) {
if (process.platform != 'win32')
return fs.rename(from, to, cb);

// on windows, antivirus softwares lock new folders until all files are scanned
// which causes a EPERM error when doing a fs.rename. to prevent this from ruining
// the process, we'll retry the fs.rename 10 times every one second if we do get a EPERM error.
function like_a_boss(attempt) {
fs.rename(from, to, function(err) {

// if no error, or err is not EPERM/EACCES, we're done
if (!err || (err.code != 'EPERM' && err.code != 'EACCES'))
cb();
else if (attempt >= 10) // max attempts reached, so give up.
cb(err);
else
setTimeout(function() { like_a_boss(attempt + 1) }, 1000);

})
}

like_a_boss(1);
}

package.verify_checksum = function(version, filename, file, cb) {

Expand Down Expand Up @@ -230,7 +253,7 @@ package.install = function(zip, dest, cb) {
if (err) return done(err);

log('Renaming to ' + final_path);
fs.rename(new_path, final_path, function(err) {
move(new_path, final_path, function(err) {
if (err) return done(err);

// make absolutely sure that the bins are executable!
Expand Down

0 comments on commit 40baf40

Please sign in to comment.