Fixes bug introduced in github issue #132 where renaming a file to itself can cause an unhandled error

This commit is contained in:
Issac Goldstand 2013-05-09 13:09:59 +03:00
parent ad8229145e
commit 2da01cc611

View File

@ -23,10 +23,12 @@ function DateRollingFileStream(filename, pattern, options, now) {
this.pattern = pattern || '.yyyy-MM-dd'; this.pattern = pattern || '.yyyy-MM-dd';
this.now = now || Date.now; this.now = now || Date.now;
this.lastTimeWeWroteSomething = format.asString(this.pattern, new Date(this.now())); this.lastTimeWeWroteSomething = format.asString(this.pattern, new Date(this.now()));
this.alwaysIncludePattern = false;
this.baseFilename = filename; this.baseFilename = filename;
if (options) { if (options) {
if (options.alwaysIncludePattern) { if (options.alwaysIncludePattern) {
this.alwaysIncludePattern = true;
filename = filename + this.lastTimeWeWroteSomething; filename = filename + this.lastTimeWeWroteSomething;
} }
delete options.alwaysIncludePattern; delete options.alwaysIncludePattern;
@ -75,8 +77,13 @@ DateRollingFileStream.prototype.roll = function(filename, callback) {
} }
function renameTheCurrentFile(cb) { function renameTheCurrentFile(cb) {
if (this.alwaysIncludePattern) {
// no-op
cb();
} else {
debug("Renaming the " + filename + " -> " + newFilename); debug("Renaming the " + filename + " -> " + newFilename);
fs.rename(filename, newFilename, cb); fs.rename(filename, newFilename, cb);
} }
}
}; };