From 36bc0e6308e080564d9188d350cace291433f479 Mon Sep 17 00:00:00 2001 From: Arnaud Ferraris Date: Wed, 8 May 2019 19:20:38 +0200 Subject: [PATCH 1/5] [partition] Move bytesToSectors() to CalamaresUtils namespace In order to prepare for future refactoring of the PartSize class, move the bytesToSectors() function to libcalamares in the CalamaresUtils namespace. Signed-off-by: Arnaud Ferraris --- src/libcalamares/utils/Units.h | 14 ++++++++++++++ src/modules/partition/core/PartUtils.cpp | 2 +- src/modules/partition/core/PartUtils.h | 14 -------------- src/modules/partition/core/PartitionActions.cpp | 4 ++-- 4 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/libcalamares/utils/Units.h b/src/libcalamares/utils/Units.h index 74c809362..e6d46aaed 100644 --- a/src/libcalamares/utils/Units.h +++ b/src/libcalamares/utils/Units.h @@ -78,5 +78,19 @@ constexpr int BytesToMiB( qint64 b ) return int( b / 1024 / 1024 ); } +constexpr qint64 alignBytesToBlockSize( qint64 bytes, qint64 blocksize ) +{ + qint64 blocks = bytes / blocksize; + + if ( blocks * blocksize != bytes ) + ++blocks; + return blocks * blocksize; +} + +constexpr qint64 bytesToSectors( qint64 bytes, qint64 blocksize ) +{ + return alignBytesToBlockSize( alignBytesToBlockSize( bytes, blocksize), MiBtoBytes(1ULL) ) / blocksize; +} + } // namespace #endif diff --git a/src/modules/partition/core/PartUtils.cpp b/src/modules/partition/core/PartUtils.cpp index 251fc62db..4dfd6d81f 100644 --- a/src/modules/partition/core/PartUtils.cpp +++ b/src/modules/partition/core/PartUtils.cpp @@ -101,7 +101,7 @@ PartSize::toSectors( qint64 totalSectors, qint64 sectorSize ) const case unit_t::KiB: case unit_t::MiB: case unit_t::GiB: - return bytesToSectors ( toBytes(), sectorSize ); + return CalamaresUtils::bytesToSectors ( toBytes(), sectorSize ); } return -1; diff --git a/src/modules/partition/core/PartUtils.h b/src/modules/partition/core/PartUtils.h index 4653468c3..8f531b97e 100644 --- a/src/modules/partition/core/PartUtils.h +++ b/src/modules/partition/core/PartUtils.h @@ -181,20 +181,6 @@ QString findFS( QString fsName, FileSystem::Type* fsType ); */ qint64 sizeToSectors( double size, SizeUnit unit, qint64 totalSectors, qint64 logicalSize ); -constexpr qint64 alignBytesToBlockSize( qint64 bytes, qint64 blocksize ) -{ - qint64 blocks = bytes / blocksize; - - if ( blocks * blocksize != bytes ) - ++blocks; - return blocks * blocksize; -} - -constexpr qint64 bytesToSectors( qint64 bytes, qint64 blocksize ) -{ - return alignBytesToBlockSize( alignBytesToBlockSize( bytes, blocksize), MiBtoBytes(1ULL) ) / blocksize; -} - } #endif // PARTUTILS_H diff --git a/src/modules/partition/core/PartitionActions.cpp b/src/modules/partition/core/PartitionActions.cpp index 761b8e4e3..5a9c0b4c7 100644 --- a/src/modules/partition/core/PartitionActions.cpp +++ b/src/modules/partition/core/PartitionActions.cpp @@ -115,11 +115,11 @@ doAutopartition( PartitionCoreModule* core, Device* dev, Choices::AutoPartitionO // Since sectors count from 0, if the space is 2048 sectors in size, // the first free sector has number 2048 (and there are 2048 sectors // before that one, numbered 0..2047). - qint64 firstFreeSector = PartUtils::bytesToSectors( empty_space_sizeB, dev->logicalSize() ); + qint64 firstFreeSector = CalamaresUtils::bytesToSectors( empty_space_sizeB, dev->logicalSize() ); if ( isEfi ) { - qint64 efiSectorCount = PartUtils::bytesToSectors( uefisys_part_sizeB, dev->logicalSize() ); + qint64 efiSectorCount = CalamaresUtils::bytesToSectors( uefisys_part_sizeB, dev->logicalSize() ); Q_ASSERT( efiSectorCount > 0 ); // Since sectors count from 0, and this partition is created starting From 4937668b5b33dbf686da4b629d776acec8028b25 Mon Sep 17 00:00:00 2001 From: Arnaud Ferraris Date: Wed, 8 May 2019 19:23:07 +0200 Subject: [PATCH 2/5] [libcalamares] Add generic PartitionSize class Using PartUtils::PartSize as reference, this commit creates a new PartitionSize class in libcalamares, which will then be used in every module needing such a class. Signed-off-by: Arnaud Ferraris --- src/libcalamares/CMakeLists.txt | 5 +- src/libcalamares/partition/PartitionSize.cpp | 238 +++++++++++++++++++ src/libcalamares/partition/PartitionSize.h | 103 ++++++++ 3 files changed, 345 insertions(+), 1 deletion(-) create mode 100644 src/libcalamares/partition/PartitionSize.cpp create mode 100644 src/libcalamares/partition/PartitionSize.h diff --git a/src/libcalamares/CMakeLists.txt b/src/libcalamares/CMakeLists.txt index c4b9fa40c..bc3ce0511 100644 --- a/src/libcalamares/CMakeLists.txt +++ b/src/libcalamares/CMakeLists.txt @@ -22,6 +22,9 @@ set( libSources ProcessJob.cpp Settings.cpp ) +set( partSources + partition/PartitionSize.cpp +) set( utilsSources utils/CalamaresUtilsSystem.cpp utils/CommandList.cpp @@ -72,7 +75,7 @@ if( WITH_PYTHON ) ) endif() -add_library( calamares SHARED ${libSources} ${kdsagSources} ${utilsSources} ) +add_library( calamares SHARED ${libSources} ${kdsagSources} ${partSources} ${utilsSources} ) set_target_properties( calamares PROPERTIES VERSION ${CALAMARES_VERSION_SHORT} diff --git a/src/libcalamares/partition/PartitionSize.cpp b/src/libcalamares/partition/PartitionSize.cpp new file mode 100644 index 000000000..edff0fe1e --- /dev/null +++ b/src/libcalamares/partition/PartitionSize.cpp @@ -0,0 +1,238 @@ +/* === This file is part of Calamares - === + * + * Copyright 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Calamares is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Calamares. If not, see . + */ + +#include "partition/PartitionSize.h" +#include "utils/Logger.h" +#include "utils/Units.h" + +namespace Calamares +{ + +static const NamedEnumTable& +unitSuffixes() +{ + static const NamedEnumTable names{ + { QStringLiteral( "%" ), SizeUnit::Percent }, + { QStringLiteral( "K" ), SizeUnit::KiB }, + { QStringLiteral( "KiB" ), SizeUnit::KiB }, + { QStringLiteral( "M" ), SizeUnit::MiB }, + { QStringLiteral( "MiB" ), SizeUnit::MiB }, + { QStringLiteral( "G" ), SizeUnit::GiB }, + { QStringLiteral( "GiB" ), SizeUnit::GiB } + }; + + return names; +} + +PartitionSize::PartitionSize( const QString& s ) + : NamedSuffix( unitSuffixes(), s ) +{ + if ( ( unit() == unit_t::Percent ) && ( value() > 100 || value() < 0 ) ) + { + cDebug() << "Percent value" << value() << "is not valid."; + m_value = 0; + } + + if ( m_unit == unit_t::None ) + { + m_value = s.toInt(); + if ( m_value > 0 ) + m_unit = unit_t::Byte; + } + + if ( m_value <= 0 ) + { + m_value = 0; + m_unit = unit_t::None; + } +} + +qint64 +PartitionSize::toSectors( qint64 totalSectors, qint64 sectorSize ) const +{ + if ( !isValid() ) + return -1; + if ( totalSectors < 1 || sectorSize < 1 ) + return -1; + + switch ( m_unit ) + { + case unit_t::None: + return -1; + case unit_t::Percent: + if ( value() == 100 ) + return totalSectors; // Common-case, avoid futzing around + else + return totalSectors * value() / 100; + case unit_t::Byte: + case unit_t::KiB: + case unit_t::MiB: + case unit_t::GiB: + return CalamaresUtils::bytesToSectors ( toBytes(), sectorSize ); + } + + return -1; +} + +qint64 +PartitionSize::toBytes( qint64 totalSectors, qint64 sectorSize ) const +{ + if ( !isValid() ) + return -1; + + switch ( m_unit ) + { + case unit_t::None: + return -1; + case unit_t::Percent: + if ( totalSectors < 1 || sectorSize < 1 ) + return -1; + if ( value() == 100 ) + return totalSectors * sectorSize; // Common-case, avoid futzing around + else + return totalSectors * value() / 100; + case unit_t::Byte: + case unit_t::KiB: + case unit_t::MiB: + case unit_t::GiB: + return toBytes(); + } + + // notreached + return -1; +} + +qint64 +PartitionSize::toBytes( qint64 totalBytes ) const +{ + if ( !isValid() ) + return -1; + + switch ( m_unit ) + { + case unit_t::None: + return -1; + case unit_t::Percent: + if ( totalBytes < 1 ) + return -1; + if ( value() == 100 ) + return totalBytes; // Common-case, avoid futzing around + else + return totalBytes * value() / 100; + case unit_t::Byte: + case unit_t::KiB: + case unit_t::MiB: + case unit_t::GiB: + return toBytes(); + } + + // notreached + return -1; +} + +qint64 +PartitionSize::toBytes() const +{ + if ( !isValid() ) + return -1; + + switch ( m_unit ) + { + case unit_t::Byte: + return value(); + case unit_t::KiB: + return CalamaresUtils::KiBtoBytes( static_cast( value() ) ); + case unit_t::MiB: + return CalamaresUtils::MiBtoBytes( static_cast( value() ) ); + case unit_t::GiB: + return CalamaresUtils::GiBtoBytes( static_cast( value() ) ); + default: + break; + } + + // Reached only when unit is Percent or None + return -1; +} + +bool +PartitionSize::operator< ( const PartitionSize& other ) const +{ + if ( ( m_unit == unit_t::None || other.m_unit == unit_t::None ) || + ( m_unit == unit_t::Percent && other.m_unit != unit_t::Percent ) || + ( m_unit != unit_t::Percent && other.m_unit == unit_t::Percent ) ) + return false; + + switch ( m_unit ) + { + case unit_t::Percent: + return ( m_value < other.m_value ); + case unit_t::Byte: + case unit_t::KiB: + case unit_t::MiB: + case unit_t::GiB: + return ( toBytes() < other.toBytes () ); + } + + return false; +} + +bool +PartitionSize::operator> ( const PartitionSize& other ) const +{ + if ( ( m_unit == unit_t::None || other.m_unit == unit_t::None ) || + ( m_unit == unit_t::Percent && other.m_unit != unit_t::Percent ) || + ( m_unit != unit_t::Percent && other.m_unit == unit_t::Percent ) ) + return false; + + switch ( m_unit ) + { + case unit_t::Percent: + return ( m_value > other.m_value ); + case unit_t::Byte: + case unit_t::KiB: + case unit_t::MiB: + case unit_t::GiB: + return ( toBytes() > other.toBytes () ); + } + + return false; +} + +bool +PartitionSize::operator== ( const PartitionSize& other ) const +{ + if ( ( m_unit == unit_t::None || other.m_unit == unit_t::None ) || + ( m_unit == unit_t::Percent && other.m_unit != unit_t::Percent ) || + ( m_unit != unit_t::Percent && other.m_unit == unit_t::Percent ) ) + return false; + + switch ( m_unit ) + { + case unit_t::Percent: + return ( m_value == other.m_value ); + case unit_t::Byte: + case unit_t::KiB: + case unit_t::MiB: + case unit_t::GiB: + return ( toBytes() == other.toBytes () ); + } + + return false; +} + +} // namespace Calamares diff --git a/src/libcalamares/partition/PartitionSize.h b/src/libcalamares/partition/PartitionSize.h new file mode 100644 index 000000000..13ffa5c70 --- /dev/null +++ b/src/libcalamares/partition/PartitionSize.h @@ -0,0 +1,103 @@ +/* === This file is part of Calamares - === + * + * Copyright 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Calamares is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Calamares. If not, see . + */ + +#ifndef PARTITION_PARTITIONSIZE_H +#define PARTITION_PARTITIONSIZE_H + +#include "utils/Units.h" +#include "utils/NamedSuffix.h" + +// Qt +#include + +namespace Calamares +{ + +enum class SizeUnit +{ + None, + Percent, + Byte, + KiB, + MiB, + GiB +}; + +/** @brief Partition size expressions + * + * Sizes can be specified in bytes, KiB, MiB, GiB or percent (of + * the available drive space are on). This class handles parsing + * of such strings from the config file. + */ +class PartitionSize : public NamedSuffix +{ +public: + PartitionSize() : NamedSuffix() { } + PartitionSize( int v, unit_t u ) : NamedSuffix( v, u ) { } + PartitionSize( const QString& ); + + bool isValid() const + { + return ( unit() != SizeUnit::None ) && ( value() > 0 ); + } + + bool operator< ( const PartitionSize& other ) const; + bool operator> ( const PartitionSize& other ) const; + bool operator== ( const PartitionSize& other ) const; + + /** @brief Convert the size to the number of sectors @p totalSectors . + * + * Each sector has size @p sectorSize, for converting sizes in Bytes, + * KiB, MiB or GiB to sector counts. + * + * @return the number of sectors needed, or -1 for invalid sizes. + */ + qint64 toSectors( qint64 totalSectors, qint64 sectorSize ) const; + + /** @brief Convert the size to bytes. + * + * The device's sectors count @p totalSectors and sector size + * @p sectoreSize are used to calculated the total size, which + * is then used to calculate the size when using Percent. + * + * @return the size in bytes, or -1 for invalid sizes. + */ + qint64 toBytes( qint64 totalSectors, qint64 sectorSize ) const; + + /** @brief Convert the size to bytes. + * + * Total size @p totalBytes is needed for sizes in Percent. This + * parameter is unused in any other case. + * + * @return the size in bytes, or -1 for invalid sizes. + */ + qint64 toBytes( qint64 totalBytes ) const; + + /** @brief Convert the size to bytes. + * + * This method is only valid for sizes in Bytes, KiB, MiB or GiB. + * It will return -1 in any other case. + * + * @return the size in bytes, or -1 if it cannot be calculated. + */ + qint64 toBytes() const; +}; + +} // namespace Calamares + +#endif // PARTITION_PARTITIONSIZE_H From 8f9f8f1cc1586a82f2cef66885b78a1cb5690a58 Mon Sep 17 00:00:00 2001 From: Arnaud Ferraris Date: Wed, 8 May 2019 19:24:38 +0200 Subject: [PATCH 3/5] [fsresizer] Switch to using the generic PartitionSize class Instead of relying on a module-specific implementation, use the new PartitionSize class for storing partition sizes. Signed-off-by: Arnaud Ferraris --- src/modules/fsresizer/ResizeFSJob.cpp | 66 ++------------------------- src/modules/fsresizer/ResizeFSJob.h | 48 ++----------------- src/modules/fsresizer/Tests.cpp | 20 ++++---- 3 files changed, 17 insertions(+), 117 deletions(-) diff --git a/src/modules/fsresizer/ResizeFSJob.cpp b/src/modules/fsresizer/ResizeFSJob.cpp index a925037d1..0ab583367 100644 --- a/src/modules/fsresizer/ResizeFSJob.cpp +++ b/src/modules/fsresizer/ResizeFSJob.cpp @@ -41,64 +41,6 @@ #include "core/PartitionIterator.h" -static const NamedEnumTable& -unitSuffixes() -{ - using Unit = ResizeFSJob::RelativeUnit; - - static const NamedEnumTable names{ - { QStringLiteral( "%" ), Unit::Percent }, - { QStringLiteral( "MiB" ), Unit::Absolute } - }; - - return names; -} - -ResizeFSJob::RelativeSize::RelativeSize( const QString& s ) - : NamedSuffix( unitSuffixes(), s ) -{ - if ( ( unit() == RelativeUnit::Percent ) && ( value() > 100 ) ) - { - cDebug() << "Percent value" << value() << "is not valid."; - m_value = 0; - m_unit = RelativeUnit::None; - } - - if ( !m_value ) - m_unit = RelativeUnit::None; -} - -qint64 -ResizeFSJob::RelativeSize::apply( qint64 totalSectors, qint64 sectorSize ) -{ - if ( !isValid() ) - return -1; - if ( sectorSize < 1 ) - return -1; - - switch ( m_unit ) - { - case unit_t::None: - return -1; - case unit_t::Absolute: - return CalamaresUtils::MiBtoBytes( static_cast( value() ) ) / sectorSize; - case unit_t::Percent: - if ( value() == 100 ) - return totalSectors; // Common-case, avoid futzing around - else - return totalSectors * value() / 100; - } - - // notreached - return -1; -} - -qint64 -ResizeFSJob::RelativeSize::apply( Device* d ) -{ - return apply( d->totalLogical(), d->logicalSize() ); -} - ResizeFSJob::ResizeFSJob( QObject* parent ) : Calamares::CppJob( parent ) , m_required( false ) @@ -203,7 +145,7 @@ ResizeFSJob::findGrownEnd( ResizeFSJob::PartitionMatch m ) qint64 expand = last_available - last_currently; // number of sectors if ( m_atleast.isValid() ) { - qint64 required = m_atleast.apply( m.first ); + qint64 required = m_atleast.toSectors( m.first->totalLogical(), m.first->logicalSize() ); if ( expand < required ) { cDebug() << Logger::SubEntry << "need to expand by" << required << "but only" << expand << "is available."; @@ -211,7 +153,7 @@ ResizeFSJob::findGrownEnd( ResizeFSJob::PartitionMatch m ) } } - qint64 wanted = m_size.apply( expand, m.first->logicalSize() ); + qint64 wanted = m_size.toSectors( expand, m.first->logicalSize() ); if ( wanted < expand ) { cDebug() << Logger::SubEntry << "only growing by" << wanted << "instead of full" << expand; @@ -330,8 +272,8 @@ ResizeFSJob::setConfigurationMap( const QVariantMap& configurationMap ) return; } - m_size = RelativeSize( configurationMap["size"].toString() ); - m_atleast = RelativeSize( configurationMap["atleast"].toString() ); + m_size = Calamares::PartitionSize( configurationMap["size"].toString() ); + m_atleast = Calamares::PartitionSize( configurationMap["atleast"].toString() ); m_required = CalamaresUtils::getBool( configurationMap, "required", false ); } diff --git a/src/modules/fsresizer/ResizeFSJob.h b/src/modules/fsresizer/ResizeFSJob.h index d575f18a9..97696e40b 100644 --- a/src/modules/fsresizer/ResizeFSJob.h +++ b/src/modules/fsresizer/ResizeFSJob.h @@ -24,7 +24,7 @@ #include -#include "utils/NamedSuffix.h" +#include "partition/PartitionSize.h" #include "utils/PluginFactory.h" #include @@ -38,48 +38,6 @@ class PLUGINDLLEXPORT ResizeFSJob : public Calamares::CppJob Q_OBJECT public: - enum class RelativeUnit - { - None, - Percent, - Absolute - }; - - /** @brief Size expressions - * - * Sizes can be specified in MiB or percent (of the device they - * are on). This class handles parsing of such strings from the - * config file. - */ - class RelativeSize : public NamedSuffix - { - public: - RelativeSize() : NamedSuffix() { }; - RelativeSize( const QString& ); - - bool isValid() const - { - return ( unit() != RelativeUnit::None ) && ( value() > 0 ); - } - - /** @brief Apply this size to the number of sectors @p totalSectors . - * - * Each sector has size @p sectorSize , for converting absolute - * sizes in MiB to sector counts. - * - * For invalid sizes, returns -1. - * For absolute sizes, returns the number of sectors needed. - * For percent sizes, returns that percent of the number of sectors. - */ - qint64 apply( qint64 totalSectors, qint64 sectorSize ); - - /** @brief Apply this size to the given device. - * - * Equivalent to apply( d->totalLogical(), d->logicalSize() ) - */ - qint64 apply( Device* d ); - } ; - explicit ResizeFSJob( QObject* parent = nullptr ); virtual ~ResizeFSJob() override; @@ -97,8 +55,8 @@ public: } private: - RelativeSize m_size; - RelativeSize m_atleast; + Calamares::PartitionSize m_size; + Calamares::PartitionSize m_atleast; QString m_fsname; // Either this, or devicename, is set, not both QString m_devicename; bool m_required; diff --git a/src/modules/fsresizer/Tests.cpp b/src/modules/fsresizer/Tests.cpp index 190a1d279..417d30c33 100644 --- a/src/modules/fsresizer/Tests.cpp +++ b/src/modules/fsresizer/Tests.cpp @@ -57,8 +57,8 @@ void FSResizerTests::testConfigurationRobust() j.setConfigurationMap( QVariantMap() ); QVERIFY( j.m_fsname.isEmpty() ); QVERIFY( j.m_devicename.isEmpty() ); - QCOMPARE( j.m_size.unit(), ResizeFSJob::RelativeUnit::None ); - QCOMPARE( j.m_atleast.unit(), ResizeFSJob::RelativeUnit::None ); + QCOMPARE( j.m_size.unit(), Calamares::SizeUnit::None ); + QCOMPARE( j.m_atleast.unit(), Calamares::SizeUnit::None ); // Config is missing fs and dev, so it isn't valid YAML::Node doc0 = YAML::Load( R"(--- @@ -68,8 +68,8 @@ atleast: 600MiB j.setConfigurationMap( CalamaresUtils::yamlMapToVariant( doc0 ).toMap() ); QVERIFY( j.m_fsname.isEmpty() ); QVERIFY( j.m_devicename.isEmpty() ); - QCOMPARE( j.m_size.unit(), ResizeFSJob::RelativeUnit::None ); - QCOMPARE( j.m_atleast.unit(), ResizeFSJob::RelativeUnit::None ); + QCOMPARE( j.m_size.unit(), Calamares::SizeUnit::None ); + QCOMPARE( j.m_atleast.unit(), Calamares::SizeUnit::None ); QCOMPARE( j.m_size.value(), 0 ); QCOMPARE( j.m_atleast.value(), 0 ); } @@ -87,8 +87,8 @@ atleast: 600MiB j.setConfigurationMap( CalamaresUtils::yamlMapToVariant( doc0 ).toMap() ); QVERIFY( !j.m_fsname.isEmpty() ); QVERIFY( j.m_devicename.isEmpty() ); - QCOMPARE( j.m_size.unit(), ResizeFSJob::RelativeUnit::Percent ); - QCOMPARE( j.m_atleast.unit(), ResizeFSJob::RelativeUnit::Absolute ); + QCOMPARE( j.m_size.unit(), Calamares::SizeUnit::Percent ); + QCOMPARE( j.m_atleast.unit(), Calamares::SizeUnit::Percent ); QCOMPARE( j.m_size.value(), 100 ); QCOMPARE( j.m_atleast.value(), 600 ); @@ -102,8 +102,8 @@ atleast: 127 % j.setConfigurationMap( CalamaresUtils::yamlMapToVariant( doc0 ).toMap() ); QVERIFY( !j.m_fsname.isEmpty() ); QVERIFY( !j.m_devicename.isEmpty() ); - QCOMPARE( j.m_size.unit(), ResizeFSJob::RelativeUnit::Absolute ); - QCOMPARE( j.m_atleast.unit(), ResizeFSJob::RelativeUnit::None ); + QCOMPARE( j.m_size.unit(), Calamares::SizeUnit::Percent ); + QCOMPARE( j.m_atleast.unit(), Calamares::SizeUnit::None ); QCOMPARE( j.m_size.value(), 72 ); QCOMPARE( j.m_atleast.value(), 0 ); @@ -117,8 +117,8 @@ size: 71MiB j.setConfigurationMap( CalamaresUtils::yamlMapToVariant( doc0 ).toMap() ); QVERIFY( !j.m_fsname.isEmpty() ); QVERIFY( j.m_devicename.isEmpty() ); - QCOMPARE( j.m_size.unit(), ResizeFSJob::RelativeUnit::Absolute ); - QCOMPARE( j.m_atleast.unit(), ResizeFSJob::RelativeUnit::None ); + QCOMPARE( j.m_size.unit(), Calamares::SizeUnit::Percent ); + QCOMPARE( j.m_atleast.unit(), Calamares::SizeUnit::None ); QCOMPARE( j.m_size.value(), 71 ); QCOMPARE( j.m_atleast.value(), 0 ); } From 80fd3d33537579718ff0bd0b4a7583826c7db06b Mon Sep 17 00:00:00 2001 From: Arnaud Ferraris Date: Wed, 8 May 2019 19:26:31 +0200 Subject: [PATCH 4/5] [partition] Switch to using the generic PartitionSize class Instead of relying on a module-specific implementation, use the new PartitionSize class for storing partition sizes. Signed-off-by: Arnaud Ferraris --- src/modules/partition/core/PartUtils.cpp | 210 ------------------ src/modules/partition/core/PartUtils.h | 72 ------ .../partition/core/PartitionActions.cpp | 3 +- .../partition/core/PartitionLayout.cpp | 6 +- src/modules/partition/core/PartitionLayout.h | 8 +- 5 files changed, 10 insertions(+), 289 deletions(-) diff --git a/src/modules/partition/core/PartUtils.cpp b/src/modules/partition/core/PartUtils.cpp index 4dfd6d81f..9bcd2bb8e 100644 --- a/src/modules/partition/core/PartUtils.cpp +++ b/src/modules/partition/core/PartUtils.cpp @@ -43,216 +43,6 @@ namespace PartUtils { -static const NamedEnumTable& -unitSuffixes() -{ - static const NamedEnumTable names{ - { QStringLiteral( "%" ), SizeUnit::Percent }, - { QStringLiteral( "B" ), SizeUnit::Byte }, - { QStringLiteral( "K" ), SizeUnit::KiB }, - { QStringLiteral( "M" ), SizeUnit::MiB }, - { QStringLiteral( "G" ), SizeUnit::GiB } - }; - - return names; -} - -PartSize::PartSize( const QString& s ) - : NamedSuffix( unitSuffixes(), s ) -{ - if ( ( unit() == SizeUnit::Percent ) && ( value() > 100 || value() < 0 ) ) - { - cDebug() << "Percent value" << value() << "is not valid."; - m_value = 0; - } - - if ( m_unit == SizeUnit::None ) - { - m_value = s.toInt(); - if ( m_value > 0 ) - m_unit = SizeUnit::Byte; - } - - if ( m_value <= 0 ) - { - m_value = 0; - m_unit = SizeUnit::None; - } -} - -qint64 -PartSize::toSectors( qint64 totalSectors, qint64 sectorSize ) const -{ - if ( !isValid() ) - return -1; - if ( totalSectors < 1 || sectorSize < 1 ) - return -1; - - switch ( m_unit ) - { - case unit_t::None: - return -1; - case unit_t::Percent: - if ( value() == 100 ) - return totalSectors; // Common-case, avoid futzing around - else - return totalSectors * value() / 100; - case unit_t::Byte: - case unit_t::KiB: - case unit_t::MiB: - case unit_t::GiB: - return CalamaresUtils::bytesToSectors ( toBytes(), sectorSize ); - } - - return -1; -} - -qint64 -PartSize::toBytes( qint64 totalSectors, qint64 sectorSize ) const -{ - if ( !isValid() ) - return -1; - - switch ( m_unit ) - { - case unit_t::None: - return -1; - case unit_t::Percent: - if ( totalSectors < 1 || sectorSize < 1 ) - return -1; - if ( value() == 100 ) - return totalSectors * sectorSize; // Common-case, avoid futzing around - else - return totalSectors * value() / 100; - case unit_t::Byte: - case unit_t::KiB: - case unit_t::MiB: - case unit_t::GiB: - return toBytes(); - } - - // notreached - return -1; -} - -qint64 -PartSize::toBytes( qint64 totalBytes ) const -{ - if ( !isValid() ) - return -1; - - switch ( m_unit ) - { - case unit_t::None: - return -1; - case unit_t::Percent: - if ( totalBytes < 1 ) - return -1; - if ( value() == 100 ) - return totalBytes; // Common-case, avoid futzing around - else - return totalBytes * value() / 100; - case unit_t::Byte: - case unit_t::KiB: - case unit_t::MiB: - case unit_t::GiB: - return toBytes(); - } - - // notreached - return -1; -} - -qint64 -PartSize::toBytes() const -{ - if ( !isValid() ) - return -1; - - switch ( m_unit ) - { - case unit_t::Byte: - return value(); - case unit_t::KiB: - return CalamaresUtils::KiBtoBytes( static_cast( value() ) ); - case unit_t::MiB: - return CalamaresUtils::MiBtoBytes( static_cast( value() ) ); - case unit_t::GiB: - return CalamaresUtils::GiBtoBytes( static_cast( value() ) ); - default: - break; - } - - // Reached only when unit is Percent or None - return -1; -} - -bool -PartSize::operator< ( const PartSize& other ) const -{ - if ( ( m_unit == SizeUnit::None || other.m_unit == SizeUnit::None ) || - ( m_unit == SizeUnit::Percent && other.m_unit != SizeUnit::Percent ) || - ( m_unit != SizeUnit::Percent && other.m_unit == SizeUnit::Percent ) ) - return false; - - switch ( m_unit ) - { - case SizeUnit::Percent: - return ( m_value < other.m_value ); - case SizeUnit::Byte: - case SizeUnit::KiB: - case SizeUnit::MiB: - case SizeUnit::GiB: - return ( toBytes() < other.toBytes () ); - } - - return false; -} - -bool -PartSize::operator> ( const PartSize& other ) const -{ - if ( ( m_unit == SizeUnit::None || other.m_unit == SizeUnit::None ) || - ( m_unit == SizeUnit::Percent && other.m_unit != SizeUnit::Percent ) || - ( m_unit != SizeUnit::Percent && other.m_unit == SizeUnit::Percent ) ) - return false; - - switch ( m_unit ) - { - case SizeUnit::Percent: - return ( m_value > other.m_value ); - case SizeUnit::Byte: - case SizeUnit::KiB: - case SizeUnit::MiB: - case SizeUnit::GiB: - return ( toBytes() > other.toBytes () ); - } - - return false; -} - -bool -PartSize::operator== ( const PartSize& other ) const -{ - if ( ( m_unit == SizeUnit::None || other.m_unit == SizeUnit::None ) || - ( m_unit == SizeUnit::Percent && other.m_unit != SizeUnit::Percent ) || - ( m_unit != SizeUnit::Percent && other.m_unit == SizeUnit::Percent ) ) - return false; - - switch ( m_unit ) - { - case SizeUnit::Percent: - return ( m_value == other.m_value ); - case SizeUnit::Byte: - case SizeUnit::KiB: - case SizeUnit::MiB: - case SizeUnit::GiB: - return ( toBytes() == other.toBytes () ); - } - - return false; -} - QString convenienceName( const Partition* const candidate ) { diff --git a/src/modules/partition/core/PartUtils.h b/src/modules/partition/core/PartUtils.h index 8f531b97e..71d786963 100644 --- a/src/modules/partition/core/PartUtils.h +++ b/src/modules/partition/core/PartUtils.h @@ -36,78 +36,6 @@ class Partition; namespace PartUtils { -using CalamaresUtils::MiBtoBytes; - -enum class SizeUnit -{ - None, - Percent, - Byte, - KiB, - MiB, - GiB -}; - -/** @brief Partition size expressions - * - * Sizes can be specified in bytes, KiB, MiB, GiB or percent (of - * the available drive space are on). This class handles parsing - * of such strings from the config file. - */ -class PartSize : public NamedSuffix -{ -public: - PartSize() : NamedSuffix() { } - PartSize( int v, unit_t u ) : NamedSuffix( v, u ) { } - PartSize( const QString& ); - - bool isValid() const - { - return ( unit() != SizeUnit::None ) && ( value() > 0 ); - } - - bool operator< ( const PartSize& other ) const; - bool operator> ( const PartSize& other ) const; - bool operator== ( const PartSize& other ) const; - - /** @brief Convert the size to the number of sectors @p totalSectors . - * - * Each sector has size @p sectorSize, for converting sizes in Bytes, - * KiB, MiB or GiB to sector counts. - * - * @return the number of sectors needed, or -1 for invalid sizes. - */ - qint64 toSectors( qint64 totalSectors, qint64 sectorSize ) const; - - /** @brief Convert the size to bytes. - * - * The device's sectors count @p totalSectors and sector size - * @p sectoreSize are used to calculated the total size, which - * is then used to calculate the size when using Percent. - * - * @return the size in bytes, or -1 for invalid sizes. - */ - qint64 toBytes( qint64 totalSectors, qint64 sectorSize ) const; - - /** @brief Convert the size to bytes. - * - * Total size @p totalBytes is needed for sizes in Percent. This - * parameter is unused in any other case. - * - * @return the size in bytes, or -1 for invalid sizes. - */ - qint64 toBytes( qint64 totalBytes ) const; - - /** @brief Convert the size to bytes. - * - * This method is only valid for sizes in Bytes, KiB, MiB or GiB. - * It will return -1 in any other case. - * - * @return the size in bytes, or -1 if it cannot be calculated. - */ - qint64 toBytes() const; -}; - /** * @brief Provides a nice human-readable name for @p candidate diff --git a/src/modules/partition/core/PartitionActions.cpp b/src/modules/partition/core/PartitionActions.cpp index 5a9c0b4c7..1c89a5b7f 100644 --- a/src/modules/partition/core/PartitionActions.cpp +++ b/src/modules/partition/core/PartitionActions.cpp @@ -103,7 +103,8 @@ doAutopartition( PartitionCoreModule* core, Device* dev, Choices::AutoPartitionO { if ( gs->contains( "efiSystemPartitionSize" ) ) { - PartUtils::PartSize part_size = PartUtils::PartSize( gs->value( "efiSystemPartitionSize" ).toString() ); + Calamares::PartitionSize part_size = Calamares::PartitionSize( + gs->value( "efiSystemPartitionSize" ).toString() ); uefisys_part_sizeB = part_size.toBytes( dev->capacity() ); } else diff --git a/src/modules/partition/core/PartitionLayout.cpp b/src/modules/partition/core/PartitionLayout.cpp index 35a540a96..a988da3f7 100644 --- a/src/modules/partition/core/PartitionLayout.cpp +++ b/src/modules/partition/core/PartitionLayout.cpp @@ -87,9 +87,9 @@ PartitionLayout::addEntry( PartitionLayout::PartitionEntry entry ) PartitionLayout::PartitionEntry::PartitionEntry( const QString& size, const QString& min, const QString& max ) { - partSize = PartUtils::PartSize( size ); - partMinSize = PartUtils::PartSize( min ); - partMaxSize = PartUtils::PartSize( max ); + partSize = Calamares::PartitionSize( size ); + partMinSize = Calamares::PartitionSize( min ); + partMaxSize = Calamares::PartitionSize( max ); } bool diff --git a/src/modules/partition/core/PartitionLayout.h b/src/modules/partition/core/PartitionLayout.h index 1d2fee410..74bc09873 100644 --- a/src/modules/partition/core/PartitionLayout.h +++ b/src/modules/partition/core/PartitionLayout.h @@ -20,6 +20,8 @@ #ifndef PARTITIONLAYOUT_H #define PARTITIONLAYOUT_H +#include "partition/PartitionSize.h" + #include "core/PartUtils.h" // KPMcore @@ -41,9 +43,9 @@ public: QString partLabel; QString partMountPoint; FileSystem::Type partFileSystem = FileSystem::Unknown; - PartUtils::PartSize partSize; - PartUtils::PartSize partMinSize; - PartUtils::PartSize partMaxSize; + Calamares::PartitionSize partSize; + Calamares::PartitionSize partMinSize; + Calamares::PartitionSize partMaxSize; /// @brief All-zeroes PartitionEntry PartitionEntry() {} From eb57be1be805d59015f6b28f5837b94442cc5d0f Mon Sep 17 00:00:00 2001 From: Arnaud Ferraris Date: Wed, 8 May 2019 19:26:57 +0200 Subject: [PATCH 5/5] [partition] Remove deprecated sizeToSectors() function Signed-off-by: Arnaud Ferraris --- src/modules/partition/core/PartUtils.h | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/src/modules/partition/core/PartUtils.h b/src/modules/partition/core/PartUtils.h index 71d786963..1b4ee0b71 100644 --- a/src/modules/partition/core/PartUtils.h +++ b/src/modules/partition/core/PartUtils.h @@ -99,16 +99,6 @@ bool isEfiBootable( const Partition* candidate ); */ QString findFS( QString fsName, FileSystem::Type* fsType ); -/** - * @brief Convert a partition size to a sectors count. - * @param size the partition size. - * @param unit the partition size unit. - * @param totalSectors the total number of sectors of the selected drive. - * @param logicalSize the sector size, in bytes. - * @return the number of sectors to be used for the given partition size. - */ -qint64 sizeToSectors( double size, SizeUnit unit, qint64 totalSectors, qint64 logicalSize ); - } #endif // PARTUTILS_H