Fix race where pipe finishes before file is created

When there are 0 length files, this is easily reproducible.
This commit is contained in:
Girish Ramakrishnan
2017-09-27 19:31:07 -07:00
parent 4674653982
commit eeef221b4e

View File

@@ -39,6 +39,11 @@ function upload(apiConfig, backupFilePath, sourceStream, callback) {
var fileStream = fs.createWriteStream(backupFilePath);
// this pattern is required to ensure that the file got created before 'finish'
fileStream.on('open', function () {
sourceStream.pipe(fileStream);
});
fileStream.on('error', function (error) {
debug('[%s] upload: out stream error.', backupFilePath, error);
callback(new BackupsError(BackupsError.EXTERNAL_ERROR, error.message));
@@ -55,8 +60,6 @@ function upload(apiConfig, backupFilePath, sourceStream, callback) {
callback(null);
});
sourceStream.pipe(fileStream);
});
}