From da548f59d18c081279eb5009bc8c979172c35045 Mon Sep 17 00:00:00 2001 From: Alexis Svinartchouk Date: Thu, 21 Mar 2019 14:47:47 +0100 Subject: [PATCH 1/2] Replace promise chains with async/await in child-writer Change-type: patch --- lib/gui/modules/child-writer.js | 139 +++++++++++++------------------- 1 file changed, 54 insertions(+), 85 deletions(-) diff --git a/lib/gui/modules/child-writer.js b/lib/gui/modules/child-writer.js index 84fcbdc0..f30dd577 100644 --- a/lib/gui/modules/child-writer.js +++ b/lib/gui/modules/child-writer.js @@ -82,12 +82,10 @@ const terminate = (code) => { * @example * handleError(new Error('Something bad happened!')) */ -const handleError = (error) => { +const handleError = async (error) => { ipc.of[IPC_SERVER_ID].emit('error', errors.toJSON(error)) - Bluebird.delay(DISCONNECT_DELAY) - .then(() => { - terminate(EXIT_CODES.GENERAL_ERROR) - }) + await Bluebird.delay(DISCONNECT_DELAY) + terminate(EXIT_CODES.GENERAL_ERROR) } /** @@ -97,40 +95,33 @@ const handleError = (error) => { * @param {Boolean} verify - whether to validate the writes or not * @param {Function} onProgress - function to call on progress * @param {Function} onFail - function to call on fail - * @param {Function} onFinish - function to call on finish - * @param {Function} onError - function to call on error - * @returns {Promise} + * @returns {Promise<{ bytesWritten, devices, errors} >} * * @example * writeAndValidate(source, destinations, verify, onProgress, onFail, onFinish, onError) */ -const writeAndValidate = (source, destinations, verify, onProgress, onFail, onFinish, onError) => { - return source.getInnerSource() - .then((innerSource) => { - return sdk.multiWrite.pipeSourceToDestinations( - innerSource, - destinations, - onFail, - onProgress, - verify - ) - }) - .then(({ failures, bytesWritten }) => { - const result = { - bytesWritten, - devices: { - failed: failures.size, - successful: destinations.length - failures.size - }, - errors: [] - } - for (const [ destination, error ] of failures) { - error.device = destination.drive.device - result.errors.push(error) - } - onFinish(result) - }) - .catch(onError) +const writeAndValidate = async (source, destinations, verify, onProgress, onFail) => { + const innerSource = await source.getInnerSource() + const { failures, bytesWritten } = await sdk.multiWrite.pipeSourceToDestinations( + innerSource, + destinations, + onFail, + onProgress, + verify + ) + const result = { + bytesWritten, + devices: { + failed: failures.size, + successful: destinations.length - failures.size + }, + errors: [] + } + for (const [ destination, error ] of failures) { + error.device = destination.drive.device + result.errors.push(error) + } + return result } ipc.connectTo(IPC_SERVER_ID, () => { @@ -159,7 +150,7 @@ ipc.connectTo(IPC_SERVER_ID, () => { terminate(EXIT_CODES.SUCCESS) }) - ipc.of[IPC_SERVER_ID].on('write', (options) => { + ipc.of[IPC_SERVER_ID].on('write', async (options) => { /** * @summary Progress handler * @param {Object} state - progress state @@ -172,52 +163,20 @@ ipc.connectTo(IPC_SERVER_ID, () => { let exitCode = EXIT_CODES.SUCCESS - /** - * @summary Finish handler - * @param {Object} results - Flash results - * @example - * writer.on('finish', onFinish) - */ - const onFinish = (results) => { - log(`Finish: ${results.bytesWritten}`) - results.errors = _.map(results.errors, (error) => { - return errors.toJSON(error) - }) - ipc.of[IPC_SERVER_ID].emit('done', { results }) - Bluebird.delay(DISCONNECT_DELAY) - .then(() => { - terminate(exitCode) - }) - } - /** * @summary Abort handler * @example * writer.on('abort', onAbort) */ - const onAbort = () => { + const onAbort = async () => { log('Abort') ipc.of[IPC_SERVER_ID].emit('abort') - Bluebird.delay(DISCONNECT_DELAY) - .then(() => { - terminate(exitCode) - }) + await Bluebird.delay(DISCONNECT_DELAY) + terminate(exitCode) } ipc.of[IPC_SERVER_ID].on('cancel', onAbort) - /** - * @summary Error handler - * @param {Error} error - error - * @example - * writer.on('error', onError) - */ - const onError = (error) => { - log(`Error: ${error.message}`) - exitCode = EXIT_CODES.GENERAL_ERROR - ipc.of[IPC_SERVER_ID].emit('error', errors.toJSON(error)) - } - /** * @summary Failure handler (non-fatal errors) * @param {SourceDestination} destination - destination @@ -234,24 +193,34 @@ ipc.connectTo(IPC_SERVER_ID, () => { } const destinations = _.map(options.destinations, 'device') - const dests = _.map(options.destinations, (destination) => { - return new sdk.sourceDestination.BlockDevice(destination, options.unmountOnSuccess) - }) - const source = new sdk.sourceDestination.File(options.imagePath, sdk.sourceDestination.File.OpenFlags.Read) - writeAndValidate( - source, - dests, - options.validateWriteOnSuccess, - onProgress, - onFail, - onFinish, - onError - ) - log(`Image: ${options.imagePath}`) log(`Devices: ${destinations.join(', ')}`) log(`Umount on success: ${options.unmountOnSuccess}`) log(`Validate on success: ${options.validateWriteOnSuccess}`) + const dests = _.map(options.destinations, (destination) => { + return new sdk.sourceDestination.BlockDevice(destination, options.unmountOnSuccess) + }) + const source = new sdk.sourceDestination.File(options.imagePath, sdk.sourceDestination.File.OpenFlags.Read) + try { + const results = await writeAndValidate( + source, + dests, + options.validateWriteOnSuccess, + onProgress, + onFail + ) + log(`Finish: ${results.bytesWritten}`) + results.errors = _.map(results.errors, (error) => { + return errors.toJSON(error) + }) + ipc.of[IPC_SERVER_ID].emit('done', { results }) + await Bluebird.delay(DISCONNECT_DELAY) + terminate(exitCode) + } catch (error) { + log(`Error: ${error.message}`) + exitCode = EXIT_CODES.GENERAL_ERROR + ipc.of[IPC_SERVER_ID].emit('error', errors.toJSON(error)) + } }) ipc.of[IPC_SERVER_ID].on('connect', () => { From 52a325881402001f148902db0c36075cb74aae5c Mon Sep 17 00:00:00 2001 From: Alexis Svinartchouk Date: Thu, 21 Mar 2019 16:46:50 +0100 Subject: [PATCH 2/2] Option for trimming ext partitions on raw images Changelog-entry: Option for trimming ext partitions on raw images Change-type: patch --- lib/gui/app/models/settings.js | 1 + lib/gui/app/modules/image-writer.js | 6 +++++- .../pages/settings/templates/settings.tpl.html | 11 +++++++++++ lib/gui/modules/child-writer.js | 16 ++++++++++++++-- 4 files changed, 31 insertions(+), 3 deletions(-) diff --git a/lib/gui/app/models/settings.js b/lib/gui/app/models/settings.js index 5f3dcd89..1002a407 100644 --- a/lib/gui/app/models/settings.js +++ b/lib/gui/app/models/settings.js @@ -37,6 +37,7 @@ const DEFAULT_SETTINGS = { errorReporting: true, unmountOnSuccess: true, validateWriteOnSuccess: true, + trim: false, updatesEnabled: packageJSON.updates.enabled && !_.includes([ 'rpm', 'deb' ], packageJSON.packageType), lastSleptUpdateNotifier: null, lastSleptUpdateNotifierVersion: null, diff --git a/lib/gui/app/modules/image-writer.js b/lib/gui/app/modules/image-writer.js index 6ea21e4a..e9ab6719 100644 --- a/lib/gui/app/modules/image-writer.js +++ b/lib/gui/app/modules/image-writer.js @@ -172,7 +172,8 @@ exports.performWrite = (image, drives, onProgress) => { uuid: flashState.getFlashUuid(), flashInstanceUuid: flashState.getFlashUuid(), unmountOnSuccess: settings.get('unmountOnSuccess'), - validateWriteOnSuccess: settings.get('validateWriteOnSuccess') + validateWriteOnSuccess: settings.get('validateWriteOnSuccess'), + trim: settings.get('trim') } ipc.server.on('fail', ({ device, error }) => { @@ -200,6 +201,7 @@ exports.performWrite = (image, drives, onProgress) => { imagePath: image, destinations: drives, validateWriteOnSuccess: settings.get('validateWriteOnSuccess'), + trim: settings.get('trim'), unmountOnSuccess: settings.get('unmountOnSuccess') }) }) @@ -312,6 +314,7 @@ exports.flash = (image, drives) => { flashInstanceUuid: flashState.getFlashUuid(), unmountOnSuccess: settings.get('unmountOnSuccess'), validateWriteOnSuccess: settings.get('validateWriteOnSuccess'), + trim: settings.get('trim'), applicationSessionUuid: store.getState().toJS().applicationSessionUuid, flashingWorkflowUuid: store.getState().toJS().flashingWorkflowUuid } @@ -375,6 +378,7 @@ exports.cancel = () => { flashInstanceUuid: flashState.getFlashUuid(), unmountOnSuccess: settings.get('unmountOnSuccess'), validateWriteOnSuccess: settings.get('validateWriteOnSuccess'), + trim: settings.get('trim'), applicationSessionUuid: store.getState().toJS().applicationSessionUuid, flashingWorkflowUuid: store.getState().toJS().flashingWorkflowUuid, status: 'cancel' diff --git a/lib/gui/app/pages/settings/templates/settings.tpl.html b/lib/gui/app/pages/settings/templates/settings.tpl.html index 313c2cc2..5a0a5d64 100644 --- a/lib/gui/app/pages/settings/templates/settings.tpl.html +++ b/lib/gui/app/pages/settings/templates/settings.tpl.html @@ -43,6 +43,17 @@ +
+ +
+