mirror of
https://github.com/balena-io/balena-cli.git
synced 2025-06-11 12:11:32 +00:00
Some fixes and refactoring
This commit is contained in:
parent
96d221fbcf
commit
9de4a3c556
@ -6,6 +6,7 @@ childProcess = require('child_process')
|
|||||||
progressStream = require('progress-stream')
|
progressStream = require('progress-stream')
|
||||||
|
|
||||||
IS_WINDOWS = os.platform() is 'win32'
|
IS_WINDOWS = os.platform() is 'win32'
|
||||||
|
DISK_IO_FLAGS = 'rs+'
|
||||||
|
|
||||||
exports.rescanDrives = (callback) ->
|
exports.rescanDrives = (callback) ->
|
||||||
return callback() if not IS_WINDOWS
|
return callback() if not IS_WINDOWS
|
||||||
@ -22,7 +23,7 @@ exports.eraseMBR = (devicePath, callback) ->
|
|||||||
async.waterfall([
|
async.waterfall([
|
||||||
|
|
||||||
(callback) ->
|
(callback) ->
|
||||||
fs.open(devicePath, 'rs+', null, callback)
|
fs.open(devicePath, DISK_IO_FLAGS, null, callback)
|
||||||
|
|
||||||
(fd, callback) ->
|
(fd, callback) ->
|
||||||
buffer = new Buffer(bufferSize)
|
buffer = new Buffer(bufferSize)
|
||||||
@ -48,13 +49,11 @@ exports.writeImage = (devicePath, imagePath, options = {}, callback = _.noop) ->
|
|||||||
if not IS_WINDOWS and not fs.existsSync(devicePath)
|
if not IS_WINDOWS and not fs.existsSync(devicePath)
|
||||||
return callback(new Error("Invalid device: #{devicePath}"))
|
return callback(new Error("Invalid device: #{devicePath}"))
|
||||||
|
|
||||||
imageFileStream = fs.createReadStream(imagePath)
|
|
||||||
|
|
||||||
deviceFileStream = fs.createWriteStream(devicePath, flags: 'rs+')
|
|
||||||
deviceFileStream.on('error', callback)
|
|
||||||
|
|
||||||
imageFileSize = fs.statSync(imagePath).size
|
imageFileSize = fs.statSync(imagePath).size
|
||||||
|
|
||||||
|
if imageFileSize is 0
|
||||||
|
return callback(new Error("Invalid OS image: #{imagePath}. The image is 0 bytes."))
|
||||||
|
|
||||||
progress = progressStream
|
progress = progressStream
|
||||||
length: imageFileSize
|
length: imageFileSize
|
||||||
time: 500
|
time: 500
|
||||||
@ -71,6 +70,10 @@ exports.writeImage = (devicePath, imagePath, options = {}, callback = _.noop) ->
|
|||||||
exports.rescanDrives(callback)
|
exports.rescanDrives(callback)
|
||||||
|
|
||||||
(callback) ->
|
(callback) ->
|
||||||
|
deviceFileStream = fs.createWriteStream(devicePath, flags: DISK_IO_FLAGS)
|
||||||
|
deviceFileStream.on('error', callback)
|
||||||
|
|
||||||
|
imageFileStream = fs.createReadStream(imagePath)
|
||||||
imageFileStream
|
imageFileStream
|
||||||
.pipe(progress)
|
.pipe(progress)
|
||||||
.pipe(deviceFileStream)
|
.pipe(deviceFileStream)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user