etcher/lib/shared/drive-constraints.js
Ștefan Daniel Mihăilă 050d187e6f refactor: extract SelectionStateModel stateless functions (#982)
`SelectionStateModel`'s methods `isSystemDrive` and `isDriveLocked`
don't depend on application state. They have been extracted in a different
AngularJS service: `DriveConstraintsModel`. The new service's actual
implementation is in `lib/src`, in order to be reused by the CLI.

Miscellaneous changes:

- Rename `lib/src` to `lib/shared`
- Refactor `drive-constraints` to throw when image is undefined

The default behaviour was to pretend that we're all good if the if
the image is not specified. We're not using this "feature", and
it can be dangerous if we forget to pass in the image.

- Make `isSystemDrive` return `false` if `system` property is undefined
- Use `drive-constraints` in `store.js`

Change-Type: patch
2017-01-06 12:42:11 -04:00

150 lines
3.5 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';
/**
* @summary Check if a drive is locked
* @function
* @public
*
* @description
* This usually points out a locked SD Card.
*
* @param {Object} drive - drive
* @returns {Boolean} whether the drive is locked
*
* @example
* if (constraints.isDriveLocked({
* device: '/dev/disk2',
* name: 'My Drive',
* size: 123456789,
* protected: true
* })) {
* console.log('This drive is locked (e.g: write-protected)');
* }
*/
exports.isDriveLocked = (drive) => {
return Boolean(drive.protected || false);
};
/**
* @summary Check if a drive is a system drive
* @function
* @public
* @param {Object} drive - drive
* @returns {Boolean} whether the drive is a system drive
*
* @example
* if (constraints.isSystemDrive({
* device: '/dev/disk2',
* name: 'My Drive',
* size: 123456789,
* protected: true,
* system: true
* })) {
* console.log('This drive is a system drive!');
* }
*/
exports.isSystemDrive = (drive) => {
return Boolean(drive.system || false);
};
/**
* @summary Check if a drive is large enough for an image
* @function
* @public
*
* @param {Object} drive - drive
* @param {Object} image - image
* @returns {Boolean} whether the drive is large enough
*
* @example
* if (constraints.isDriveLargeEnough({
* device: '/dev/disk2',
* name: 'My Drive',
* size: 1000000000
* }, {
* path: 'rpi.img',
* size: 1000000000
* })) {
* console.log('We can flash the image to this drive!');
* }
*/
exports.isDriveLargeEnough = (drive, image) => {
return drive.size >= (image.size || 0);
};
/**
* @summary Check if a drive is is valid, i.e. not locked and large enough for an image
* @function
* @public
*
* @param {Object} drive - drive
* @param {Object} image - image
* @returns {Boolean} whether the drive is valid
*
* @example
* if (constraints.isDriveValid({
* device: '/dev/disk2',
* name: 'My Drive',
* size: 1000000000,
* protected: false
* }, {
* path: 'rpi.img',
* size: 1000000000,
* recommendedDriveSize: 2000000000
* })) {
* console.log('This drive is valid!');
* }
*/
exports.isDriveValid = (drive, image) => {
return !this.isDriveLocked(drive) && this.isDriveLargeEnough(drive, image);
};
/**
* @summary Check if a drive meets the recommended drive size suggestion
* @function
* @public
*
* @description
* If the image doesn't have a recommended size, this function returns true.
*
* @param {Object} drive - drive
* @param {Object} image - image
* @returns {Boolean} whether the drive size is recommended
*
* @example
* const drive = {
* device: '/dev/disk2',
* name: 'My Drive',
* size: 4000000000
* };
*
* const image = {
* path: 'rpi.img',
* size: 2000000000
* recommendedDriveSize: 4000000000
* });
*
* if (constraints.isDriveSizeRecommended(drive, image)) {
* console.log('We meet the recommended drive size!');
* }
*/
exports.isDriveSizeRecommended = (drive, image) => {
return drive.size >= (image.recommendedDriveSize || 0);
};