summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdriaan de Groot <groot@kde.org>2019-05-09 13:32:15 +0200
committerGitHub <noreply@github.com>2019-05-09 13:32:15 +0200
commitf25b1528a501d212964cba289b2ec23305ea2b18 (patch)
treeecd36c66e533d4ade44ef3df6fb8a72105eaedf7
parent0864153eb66497340fd17ef3ff4db98ccdf7976a (diff)
parenteb57be1be805d59015f6b28f5837b94442cc5d0f (diff)
Merge pull request #1144 from a-wai/factor-partsize-class
Move partition size classes into libcalamares
-rw-r--r--src/libcalamares/CMakeLists.txt5
-rw-r--r--src/libcalamares/partition/PartitionSize.cpp238
-rw-r--r--src/libcalamares/partition/PartitionSize.h103
-rw-r--r--src/libcalamares/utils/Units.h14
-rw-r--r--src/modules/fsresizer/ResizeFSJob.cpp66
-rw-r--r--src/modules/fsresizer/ResizeFSJob.h48
-rw-r--r--src/modules/fsresizer/Tests.cpp20
-rw-r--r--src/modules/partition/core/PartUtils.cpp210
-rw-r--r--src/modules/partition/core/PartUtils.h96
-rw-r--r--src/modules/partition/core/PartitionActions.cpp7
-rw-r--r--src/modules/partition/core/PartitionLayout.cpp6
-rw-r--r--src/modules/partition/core/PartitionLayout.h8
12 files changed, 388 insertions, 433 deletions
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 - <https://github.com/calamares> ===
+ *
+ * Copyright 2019, Collabora Ltd <arnaud.ferraris@collabora.com>
+ *
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include "partition/PartitionSize.h"
+#include "utils/Logger.h"
+#include "utils/Units.h"
+
+namespace Calamares
+{
+
+static const NamedEnumTable<SizeUnit>&
+unitSuffixes()
+{
+ static const NamedEnumTable<SizeUnit> 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<unsigned long long>( value() ) );
+ case unit_t::MiB:
+ return CalamaresUtils::MiBtoBytes( static_cast<unsigned long long>( value() ) );
+ case unit_t::GiB:
+ return CalamaresUtils::GiBtoBytes( static_cast<unsigned long long>( 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 - <https://github.com/calamares> ===
+ *
+ * Copyright 2019, Collabora Ltd <arnaud.ferraris@collabora.com>
+ *
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef PARTITION_PARTITIONSIZE_H
+#define PARTITION_PARTITIONSIZE_H
+
+#include "utils/Units.h"
+#include "utils/NamedSuffix.h"
+
+// Qt
+#include <QString>
+
+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<SizeUnit, SizeUnit::None>
+{
+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
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/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<ResizeFSJob::RelativeUnit>&
-unitSuffixes()
-{
- using Unit = ResizeFSJob::RelativeUnit;
-
- static const NamedEnumTable<Unit> 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<unsigned long long>( 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 <CppJob.h>
-#include "utils/NamedSuffix.h"
+#include "partition/PartitionSize.h"
#include "utils/PluginFactory.h"
#include <PluginDllMacro.h>
@@ -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<RelativeUnit, RelativeUnit::None>
- {
- 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 );
}
diff --git a/src/modules/partition/core/PartUtils.cpp b/src/modules/partition/core/PartUtils.cpp
index 251fc62db..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<SizeUnit>&
-unitSuffixes()
-{
- static const NamedEnumTable<SizeUnit> 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 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<unsigned long long>( value() ) );
- case unit_t::MiB:
- return CalamaresUtils::MiBtoBytes( static_cast<unsigned long long>( value() ) );
- case unit_t::GiB:
- return CalamaresUtils::GiBtoBytes( static_cast<unsigned long long>( 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 4653468c3..1b4ee0b71 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<SizeUnit, SizeUnit::None>
-{
-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
@@ -171,30 +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 );
-
-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..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
@@ -115,11 +116,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
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() {}