From 96d221fbcf6420407a7df0d4c3fd648b483cebf7 Mon Sep 17 00:00:00 2001 From: Juan Cruz Viotti Date: Wed, 28 Jan 2015 15:13:43 -0400 Subject: [PATCH] Catch createWriteStream errors --- lib/drive/drive.coffee | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/lib/drive/drive.coffee b/lib/drive/drive.coffee index e705782f..85b0c043 100644 --- a/lib/drive/drive.coffee +++ b/lib/drive/drive.coffee @@ -1,5 +1,6 @@ os = require('os') fs = require('fs') +_ = require('lodash-contrib') async = require('async') childProcess = require('child_process') progressStream = require('progress-stream') @@ -47,12 +48,10 @@ exports.writeImage = (devicePath, imagePath, options = {}, callback = _.noop) -> if not IS_WINDOWS and not fs.existsSync(devicePath) return callback(new Error("Invalid device: #{devicePath}")) - imageFile = fs.createReadStream(imagePath) + imageFileStream = fs.createReadStream(imagePath) - deviceFile = fs.createWriteStream devicePath, - - # Required by Windows to work correctly - flags: 'rs+' + deviceFileStream = fs.createWriteStream(devicePath, flags: 'rs+') + deviceFileStream.on('error', callback) imageFileSize = fs.statSync(imagePath).size @@ -63,7 +62,7 @@ exports.writeImage = (devicePath, imagePath, options = {}, callback = _.noop) -> if options.progress progress.on('progress', options.onProgress) - async.waterfall([ + async.waterfall [ (callback) -> exports.eraseMBR(devicePath, callback) @@ -72,22 +71,23 @@ exports.writeImage = (devicePath, imagePath, options = {}, callback = _.noop) -> exports.rescanDrives(callback) (callback) -> - imageFile + imageFileStream .pipe(progress) - .pipe(deviceFile) + .pipe(deviceFileStream) # TODO: We should make use of nodewindows.elevate() # if we get an EPERM error. - .on 'error', (error) -> - if error.code is 'EBUSY' - error.message = "Try umounting #{error.path} first." - return callback(error) + .on('error', _.unary(callback)) - .on('close', callback) + .on('close', _.unary(callback)) (callback) -> exports.rescanDrives(callback) - ], callback) + ], (error) -> + return callback() if not error? + if error.code is 'EBUSY' + error.message = "Try umounting #{error.path} first." + return callback(error)