From d32733bf5920cf9e3bcf81a765ff4076337c54aa Mon Sep 17 00:00:00 2001 From: Arnaud Ferraris Date: Wed, 17 Apr 2019 18:40:30 +0200 Subject: [PATCH] Switch to using PartSize class for partition sizes Every call of `ParseStringSize` is replaced by using an instance of the `PartUtils::PartSize` class. This commit also removes the now-unused previous size parsing functions. Signed-off-by: Arnaud Ferraris --- src/modules/partition/core/PartUtils.cpp | 93 ------------------- src/modules/partition/core/PartUtils.h | 16 ---- .../partition/core/PartitionActions.cpp | 7 +- .../partition/core/PartitionLayout.cpp | 14 +-- src/modules/partition/core/PartitionLayout.h | 11 +-- 5 files changed, 17 insertions(+), 124 deletions(-) diff --git a/src/modules/partition/core/PartUtils.cpp b/src/modules/partition/core/PartUtils.cpp index b923ddbbb..75b49701d 100644 --- a/src/modules/partition/core/PartUtils.cpp +++ b/src/modules/partition/core/PartUtils.cpp @@ -694,99 +694,6 @@ findFS( QString fsName, FileSystem::Type* fsType ) return fsName; } -static qint64 -sizeToBytes( double size, SizeUnit unit, qint64 totalSize ) -{ - qint64 bytes; - - switch ( unit ) - { - case SizeUnit::Percent: - bytes = qint64( static_cast( totalSize ) * size / 100.0L ); - break; - case SizeUnit::KiB: - bytes = CalamaresUtils::KiBtoBytes(size); - break; - case SizeUnit::MiB: - bytes = CalamaresUtils::MiBtoBytes(size); - break; - case SizeUnit::GiB: - bytes = CalamaresUtils::GiBtoBytes(size); - break; - default: - bytes = size; - break; - } - - return bytes; -} - -double -parseSizeString( const QString& sizeString, SizeUnit* unit ) -{ - double value; - bool ok; - QString valueString; - QString unitString; - - QRegExp rx( "[KkMmGg%]" ); - int pos = rx.indexIn( sizeString ); - if (pos > 0) - { - valueString = sizeString.mid( 0, pos ); - unitString = sizeString.mid( pos ); - } - else - valueString = sizeString; - - value = valueString.toDouble( &ok ); - if ( !ok ) - { - /* - * In case the conversion fails, a size of 100% allows a few cases to pass - * anyway (e.g. when it is the last partition of the layout) - */ - *unit = SizeUnit::Percent; - return 100.0L; - } - - if ( unitString.length() > 0 ) - { - if ( unitString.at(0) == '%' ) - *unit = SizeUnit::Percent; - else if ( unitString.at(0).toUpper() == 'K' ) - *unit = SizeUnit::KiB; - else if ( unitString.at(0).toUpper() == 'M' ) - *unit = SizeUnit::MiB; - else if ( unitString.at(0).toUpper() == 'G' ) - *unit = SizeUnit::GiB; - else - *unit = SizeUnit::Byte; - } - else - { - *unit = SizeUnit::Byte; - } - - return value; -} - -qint64 -parseSizeString( const QString& sizeString, qint64 totalSize ) -{ - SizeUnit unit; - double value = parseSizeString( sizeString, &unit ); - - return sizeToBytes( value, unit, totalSize ); -} - -qint64 -sizeToSectors( double size, SizeUnit unit, qint64 totalSectors, qint64 logicalSize ) -{ - qint64 bytes = sizeToBytes( size, unit, totalSectors * logicalSize ); - return bytesToSectors( static_cast( bytes ), logicalSize ); -} - } // nmamespace PartUtils /* Implementation of methods for FstabEntry, from OsproberEntry.h */ diff --git a/src/modules/partition/core/PartUtils.h b/src/modules/partition/core/PartUtils.h index 2e1420006..e6a5209ed 100644 --- a/src/modules/partition/core/PartUtils.h +++ b/src/modules/partition/core/PartUtils.h @@ -171,22 +171,6 @@ bool isEfiBootable( const Partition* candidate ); */ QString findFS( QString fsName, FileSystem::Type* fsType ); -/** - * @brief Parse a partition size string and return its value and unit used. - * @param sizeString the string to parse. - * @param unit pointer to a SizeUnit variable for storing the parsed unit. - * @return the size value, as parsed from the input string. - */ -double parseSizeString( const QString& sizeString, SizeUnit* unit ); - -/** - * @brief Parse a partition size string and return its value in bytes. - * @param sizeString the string to parse. - * @param totalSize the size of the selected drive (used when the size is expressed in %) - * @return the size value in bytes. - */ -qint64 parseSizeString( const QString& sizeString, qint64 totalSize ); - /** * @brief Convert a partition size to a sectors count. * @param size the partition size. diff --git a/src/modules/partition/core/PartitionActions.cpp b/src/modules/partition/core/PartitionActions.cpp index 074783186..0bc0d1860 100644 --- a/src/modules/partition/core/PartitionActions.cpp +++ b/src/modules/partition/core/PartitionActions.cpp @@ -102,9 +102,14 @@ doAutopartition( PartitionCoreModule* core, Device* dev, Choices::AutoPartitionO if ( isEfi ) { if ( gs->contains( "efiSystemPartitionSize" ) ) - uefisys_part_sizeB = PartUtils::parseSizeString( gs->value( "efiSystemPartitionSize" ).toString(), dev->capacity() ); + { + PartUtils::PartSize part_size = PartUtils::PartSize( gs->value( "efiSystemPartitionSize" ).toString() ); + uefisys_part_sizeB = part_size.toBytes( dev->capacity() ); + } else + { uefisys_part_sizeB = 300_MiB; + } } // Since sectors count from 0, if the space is 2048 sectors in size, diff --git a/src/modules/partition/core/PartitionLayout.cpp b/src/modules/partition/core/PartitionLayout.cpp index 963a6ceb1..0f2724868 100644 --- a/src/modules/partition/core/PartitionLayout.cpp +++ b/src/modules/partition/core/PartitionLayout.cpp @@ -2,7 +2,7 @@ * * Copyright 2014-2017, Teo Mrnjavac * Copyright 2017-2018, Adriaan de Groot - * Copyright 2018, Collabora Ltd + * Copyright 2018-2019, Collabora Ltd * * Calamares is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -77,11 +77,11 @@ PartitionLayout::addEntry( PartitionLayout::PartitionEntry entry ) PartitionLayout::PartitionEntry::PartitionEntry( const QString& size, const QString& min, const QString& max ) { - partSize = PartUtils::parseSizeString( size , &partSizeUnit ); + partSize = PartUtils::PartSize( size ); if ( !min.isEmpty() ) - partMinSize = PartUtils::parseSizeString( min , &partMinSizeUnit ); + partMinSize = PartUtils::PartSize( min ); if ( !max.isEmpty() ) - partMaxSize = PartUtils::parseSizeString( max , &partMaxSizeUnit ); + partMaxSize = PartUtils::PartSize( max ); } void @@ -128,9 +128,9 @@ PartitionLayout::execute( Device *dev, qint64 firstSector, Partition *currentPartition = nullptr; // Calculate partition size - size = PartUtils::sizeToSectors( part.partSize, part.partSizeUnit, totalSize, dev->logicalSize() ); - minSize = PartUtils::sizeToSectors( part.partMinSize, part.partMinSizeUnit, totalSize, dev->logicalSize() ); - maxSize = PartUtils::sizeToSectors( part.partMaxSize, part.partMaxSizeUnit, totalSize, dev->logicalSize() ); + size = part.partSize.toSectors( totalSize, dev->logicalSize() ); + minSize = part.partMinSize.toSectors( totalSize, dev->logicalSize() ); + maxSize = part.partMaxSize.toSectors( totalSize, dev->logicalSize() ); if ( size < minSize ) size = minSize; if ( size > maxSize ) diff --git a/src/modules/partition/core/PartitionLayout.h b/src/modules/partition/core/PartitionLayout.h index cc7478226..034182669 100644 --- a/src/modules/partition/core/PartitionLayout.h +++ b/src/modules/partition/core/PartitionLayout.h @@ -1,6 +1,6 @@ /* === This file is part of Calamares - === * - * Copyright 2018, Collabora Ltd + * Copyright 2018-2019, Collabora Ltd * Copyright 2019, Adriaan de Groot * * Calamares is free software: you can redistribute it and/or modify @@ -43,12 +43,9 @@ public: QString partLabel; QString partMountPoint; FileSystem::Type partFileSystem = FileSystem::Unknown; - double partSize = 0.0L; - PartUtils::SizeUnit partSizeUnit = PartUtils::SizeUnit::Percent; - double partMinSize = 0.0L; - PartUtils::SizeUnit partMinSizeUnit = PartUtils::SizeUnit::Percent; - double partMaxSize = 100.0L; - PartUtils::SizeUnit partMaxSizeUnit = PartUtils::SizeUnit::Percent; + PartUtils::PartSize partSize = PartUtils::PartSize(0, PartUtils::SizeUnit::Percent); + PartUtils::PartSize partMinSize = PartUtils::PartSize(0, PartUtils::SizeUnit::Percent); + PartUtils::PartSize partMaxSize = PartUtils::PartSize(100, PartUtils::SizeUnit::Percent); /// @brief All-zeroes PartitionEntry PartitionEntry() {}