Make use of diskio

This commit is contained in:
Juan Cruz Viotti 2015-02-02 12:25:22 -04:00
parent 1a595412d5
commit 6ce0fdc4ab
8 changed files with 25 additions and 129 deletions

View File

@ -4,9 +4,11 @@ async = require('async')
resin = require('resin-sdk')
os = require('os')
visuals = require('resin-cli-visuals')
fs = require('fs')
progressStream = require('progress-stream')
drivelist = require('drivelist')
diskio = require('diskio')
commandOptions = require('./command-options')
drive = require('../drive/drive')
exports.list =
signature: 'devices'
@ -199,13 +201,25 @@ exports.init =
(confirmed, callback) ->
return done() if not confirmed
bar = new visuals.widgets.Progress('Writing Device OS')
imageFileSize = fs.statSync(params.image).size
drive.writeImage params.device, params.image,
progress: not options.quiet
onProgress: (status) ->
if imageFileSize is 0
error = new Error("Invalid OS image: #{params.image}. The image is 0 bytes.")
return callback(error)
progress = progressStream
length: imageFileSize
time: 500
if not options.quiet
bar = new visuals.widgets.Progress('Writing Device OS')
progress.on 'progress', (status) ->
console.log(bar.tick(status.percentage, status.eta))
, callback
imageFileStream = fs.createReadStream(params.image).pipe(progress)
diskio.writeStream(params.device, imageFileStream, callback)
], (error) ->
if os.platform() is 'win32' and error? and (error.code is 'EPERM' or error.code is 'EACCES')

View File

@ -1,29 +0,0 @@
progressStream = require('progress-stream')
fs = require('fs')
_ = require('lodash-contrib')
exports.writeImage = (imagePath, devicePath, onProgress, callback) ->
imageFileSize = fs.statSync(imagePath).size
if imageFileSize is 0
error = new Error("Invalid OS image: #{imagePath}. The image is 0 bytes.")
return callback(error)
progress = progressStream
length: imageFileSize
time: 500
progress.on('progress', onProgress or _.noop)
deviceFileStream = fs.createWriteStream devicePath,
flags: 'rs+'
deviceFileStream.on('error', callback)
imageFileStream = fs.createReadStream(imagePath)
imageFileStream
.pipe(progress)
.pipe(deviceFileStream)
.on('error', _.unary(callback))
.on('close', _.unary(callback))

View File

@ -1,57 +0,0 @@
os = require('os')
fs = require('fs')
_ = require('lodash-contrib')
async = require('async')
IS_WINDOWS = os.platform() is 'win32'
win32 = require('./win32')
agnostic = require('./agnostic')
exports.writeImage = (devicePath, imagePath, options = {}, callback = _.noop) ->
async.waterfall [
(callback) ->
fs.exists imagePath, (exists) ->
return callback() if exists
return callback(new Error("Invalid OS image: #{imagePath}"))
(callback) ->
return callback() if IS_WINDOWS
fs.exists devicePath, (exists) ->
return callback() if exists
return callback(new Error("Invalid device: #{devicePath}"))
(callback) ->
return callback() if not IS_WINDOWS
win32.eraseMBR(devicePath, callback)
(callback) ->
return callback() if not IS_WINDOWS
win32.rescanDrives(_.unary(callback))
(callback) ->
if not options.progress
options.onProgress = _.noop
agnostic.writeImage(imagePath, devicePath, options.onProgress, callback)
(callback) ->
return callback() if not IS_WINDOWS
win32.rescanDrives(_.unary(callback))
], (error) ->
return callback() if not error?
if error.code is 'EBUSY'
error.message = "Try umounting #{error.path} first."
if error.code is 'ENOENT'
error.message = "Invalid device #{error.path}"
# Prevents outer handler to take
# it as an usual ENOENT error
delete error.code
return callback(error)

View File

@ -1,33 +0,0 @@
childProcess = require('child_process')
path = require('path')
_ = require('lodash-contrib')
async = require('async')
fs = require('fs')
diskpart = require('diskpart')
exports.rescanDrives = (callback) ->
diskpart.evaluate([ 'rescan' ], callback)
exports.eraseMBR = (devicePath, callback) ->
bufferSize = 512
async.waterfall([
(callback) ->
fs.open(devicePath, 'rs+', null, callback)
(fd, callback) ->
buffer = new Buffer(bufferSize)
buffer.fill(0)
fs.write fd, buffer, 0, bufferSize, 0, (error, bytesWritten) ->
return callback(error) if error?
return callback(null, bytesWritten, fd)
(bytesWritten, fd, callback) ->
if bytesWritten isnt bufferSize
error = "Bytes written: #{bytesWritten}, expected #{bufferSize}"
return callback(error)
fs.close(fd, callback)
], callback)

View File

@ -1,4 +1,4 @@
.TH "RESIN" "1" "January 2015" "" ""
.TH "RESIN" "1" "February 2015" "" ""
.SH "NAME"
\fBresin\fR \- tab completion for resin
.SH DESCRIPTION

View File

@ -1,4 +1,4 @@
.TH "RESIN\-PLUGINS" "1" "January 2015" "" ""
.TH "RESIN\-PLUGINS" "1" "February 2015" "" ""
.SH "NAME"
\fBresin-plugins\fR \- Creating Resin CLI plugins
.SH DESCRIPTION

View File

@ -1,4 +1,4 @@
.TH "RESIN" "1" "January 2015" "" ""
.TH "RESIN" "1" "February 2015" "" ""
.SH "NAME"
\fBresin\fR \- command line tool to interact with resin\.io
.SH SYNOPSIS

View File

@ -28,7 +28,7 @@
"license": "MIT",
"optionalDependencies": {
"windosu": "^0.1.3",
"diskpart": "^1.0.0"
"diskpart": "^1.0.0"
},
"devDependencies": {
"chai": "~1.9.2",
@ -50,6 +50,7 @@
"capitano": "~1.3.0",
"coffee-script": "~1.8.0",
"conf.js": "^0.1.1",
"diskio": "git+https://git@github.com/resin-io/diskio.git",
"drivelist": "^1.1.0",
"git-cli": "~0.8.2",
"lodash": "~2.4.1",