Skip to content

Commit

Permalink
Trap EMFILE on both read and write streams
Browse files Browse the repository at this point in the history
  • Loading branch information
davglass committed Oct 5, 2012
1 parent 0d2bc35 commit b960127
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions lib/index.js
Expand Up @@ -118,18 +118,20 @@ var createFiles = function(files, to, options, callback) {
copy = function(from, to, callback) {
var fromFile = fs.createReadStream(from),
toFile = fs.createWriteStream(to),
bail;
bail,
onError = function (err) {
if (/EMFILE/.test(err)) {
bail = true;
setTimeout(function() {
copy(from, to, callback);
}, 50);
} else if (err) {
options.errors.push(err);
}
};

fromFile.on('error', function (err) {
if (/EMFILE/.test(err)) {
bail = true;
setTimeout(function() {
copy(from, to, callback);
}, 50);
} else if (err) {
options.errors.push(err);
}
});
fromFile.on('error', onError);
toFile.on('error', onError);
fromFile.pipe(toFile);
fromFile.once('end', function() {
if (!bail) {
Expand Down

0 comments on commit b960127

Please sign in to comment.