From 4aefb15c701f969eb1c464b37c4e4cb289fb2aa4 Mon Sep 17 00:00:00 2001 From: Juan Cruz Viotti Date: Tue, 3 Mar 2015 12:38:45 -0400 Subject: [PATCH] Prevent resin-write infinite loop --- bin/resin-write | 10 ++++------ build/actions/os.js | 5 ++++- lib/actions/os.coffee | 13 +++++++++++-- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/bin/resin-write b/bin/resin-write index f3d07202..e0fa8d30 100644 --- a/bin/resin-write +++ b/bin/resin-write @@ -1,10 +1,7 @@ #!/usr/bin/env node -// http://coffeescript.org/documentation/docs/register.html -require('coffee-script/register'); - -var os = require('../lib/actions/os.coffee'); -var errors = require('../lib/errors'); +var os = require('../build/actions/os'); +var errors = require('../build/errors'); // TODO: Do some error handling when image or device are incorrect @@ -12,7 +9,8 @@ os.install.action({ image: process.argv[2], device: process.argv[3] }, { - yes: true + yes: true, + fromScript: true }, function(error) { return errors.handle(error); }); diff --git a/build/actions/os.js b/build/actions/os.js index b5cbc76b..28edee4a 100644 --- a/build/actions/os.js +++ b/build/actions/os.js @@ -120,7 +120,10 @@ } ], function(error) { var resinWritePath, windosu; - if (os.platform() === 'win32' && (error != null) && (error.code === 'EPERM' || error.code === 'EACCES')) { + if (error == null) { + return done(); + } + if (_.all([os.platform() === 'win32', error.code === 'EPERM' || error.code === 'EACCES', !options.fromScript])) { windosu = require('windosu'); resinWritePath = "\"" + (path.join(__dirname, '..', '..', 'bin', 'resin-write')) + "\""; return windosu.exec("\"" + process.argv[0] + "\" " + resinWritePath + " \"" + params.image + "\" \"" + params.device + "\""); diff --git a/lib/actions/os.coffee b/lib/actions/os.coffee index 6d4aafc4..ec2cae45 100644 --- a/lib/actions/os.coffee +++ b/lib/actions/os.coffee @@ -152,9 +152,18 @@ exports.install = diskio.writeStream(params.device, imageFileStream, callback) ], (error) -> - if os.platform() is 'win32' and error? and (error.code is 'EPERM' or error.code is 'EACCES') - windosu = require('windosu') + return done() if not error? + if _.all [ + os.platform() is 'win32' + error.code is 'EPERM' or error.code is 'EACCES' + + # Prevent re-running resin-write infinitely + # If we have an EPERM or EACCES even after running + # windosu, we throw it as there is not much we can do + not options.fromScript + ] + windosu = require('windosu') # Need to escape every path to avoid errors resinWritePath = "\"#{path.join(__dirname, '..', '..', 'bin', 'resin-write')}\"" windosu.exec("\"#{process.argv[0]}\" #{resinWritePath} \"#{params.image}\" \"#{params.device}\"")