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 ); }