etcher/lib/cli/writer.js
Juan Cruz Viotti b7d6d3d9a1 feat(GUI): display a nice alert ribbon if drive runs out of space (#588)
We try our best to check that the images the user select are too big for
the selected drive as early as possible, but this probes to be
problematic with certain compressed formats, like bzip2, which doesn't
store any information about the uncompressed size, requiring a ~50s
intensive computation as a minimum to find it out.

For these kinds of formats, we don't perform an early check, but instead
gracefully handle the case where the drive doesn't have any more space.

This PR handles an `ENOSPC` error by displaying the alert orange ribbon,
and prompting the user to retry with a larger drive. This is a huge
improvement over the cryptic `EIO` error what was thrown before, and
over having Etcher freeze at a certain percentage point.

Change-Type: minor
Changelog-Entry: Display a nice alert ribbon if drive runs out of space.
See: https://github.com/resin-io/etcher/issues/571
Signed-off-by: Juan Cruz Viotti <jviottidc@gmail.com>
2016-07-24 15:32:00 -04:00

87 lines
2.7 KiB
JavaScript

/*
* Copyright 2016 Resin.io
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
const imageWrite = require('etcher-image-write');
const imageStream = require('etcher-image-stream');
const Bluebird = require('bluebird');
const umount = Bluebird.promisifyAll(require('umount'));
const os = require('os');
const isWindows = os.platform() === 'win32';
/**
* @summary Write an image to a disk drive
* @function
* @public
*
* @description
* See https://github.com/resin-io-modules/etcher-image-write for information
* about the `state` object passed to `onProgress` callback.
*
* @param {String} imagePath - path to image
* @param {Object} drive - drive
* @param {Object} options - options
* @param {Boolean} [options.unmountOnSuccess=false] - unmount on success
* @param {Boolean} [options.validateWriteOnSuccess=false] - validate write on success
* @param {Function} onProgress - on progress callback (state)
*
* @fulfil {Boolean} - whether the operation was successful
* @returns {Promise}
*
* @example
* writer.writeImage('path/to/image.img', {
* device: '/dev/disk2'
* }, {
* unmountOnSuccess: true,
* validateWriteOnSuccess: true
* }, (state) => {
* console.log(state.percentage);
* }).then(() => {
* console.log('Done!');
* });
*/
exports.writeImage = (imagePath, drive, options, onProgress) => {
return umount.umountAsync(drive.device).then(() => {
return imageStream.getFromFilePath(imagePath);
}).then((image) => {
return imageWrite.write(drive, image, {
check: options.validateWriteOnSuccess,
transform: image.transform
});
}).then((writer) => {
return new Bluebird((resolve, reject) => {
writer.on('progress', onProgress);
writer.on('error', reject);
writer.on('done', resolve);
});
}).tap(() => {
if (!options.unmountOnSuccess) {
return;
}
if (isWindows && drive.mountpoint) {
// The `can-ignore` annotation is EncloseJS (http://enclosejs.com) specific.
const removedrive = Bluebird.promisifyAll(require('removedrive', 'can-ignore'));
return removedrive.ejectAsync(drive.mountpoint);
}
return umount.umountAsync(drive.device);
});
};