Skip to content

Instantly share code, notes, and snippets.

@andrewrk
Created October 12, 2012 01:56
Show Gist options
  • Save andrewrk/3876926 to your computer and use it in GitHub Desktop.
Save andrewrk/3876926 to your computer and use it in GitHub Desktop.
function moveFile(source, dest, cb){
fs.rename(source, dest, function(err){
var ins, outs, had_error;
if (!err) {
return cb();
}
if (err.code !== 'EXDEV') {
return cb(err);
}
ins = fs.createReadStream(source);
outs = fs.createWriteStream(dest);
had_error = false;
ins.on('error', function(err){
had_error = true;
outs.destroy();
cb(err);
});
outs.on('error', function(err){
had_error = true;
ins.destroy();
cb(err);
});
outs.on('close', function(){
if (!had_error) {
cb();
}
});
ins.pipe(outs);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment