[libcalamares] Refortmat the partition service

This commit is contained in:
Adriaan de Groot 2019-06-13 12:35:32 +02:00
parent ebc4ebbbcc
commit ca9f21d805
5 changed files with 127 additions and 88 deletions

View file

@ -37,7 +37,10 @@ PartitionIterator::PartitionIterator( PartitionTable* table )
{ {
} }
Partition* PartitionIterator::operator*() const { return m_current; } Partition* PartitionIterator::operator*() const
{
return m_current;
}
void void
PartitionIterator::operator++() PartitionIterator::operator++()

View file

@ -26,16 +26,13 @@ namespace CalamaresUtils
namespace Partition namespace Partition
{ {
static const NamedEnumTable<SizeUnit>& static const NamedEnumTable< SizeUnit >&
unitSuffixes() unitSuffixes()
{ {
static const NamedEnumTable<SizeUnit> names{ static const NamedEnumTable< SizeUnit > names {
{ QStringLiteral( "%" ), SizeUnit::Percent }, { QStringLiteral( "%" ), SizeUnit::Percent }, { QStringLiteral( "K" ), SizeUnit::KiB },
{ QStringLiteral( "K" ), SizeUnit::KiB }, { QStringLiteral( "KiB" ), SizeUnit::KiB }, { QStringLiteral( "M" ), SizeUnit::MiB },
{ QStringLiteral( "KiB" ), SizeUnit::KiB }, { QStringLiteral( "MiB" ), SizeUnit::MiB }, { QStringLiteral( "G" ), SizeUnit::GiB },
{ QStringLiteral( "M" ), SizeUnit::MiB },
{ QStringLiteral( "MiB" ), SizeUnit::MiB },
{ QStringLiteral( "G" ), SizeUnit::GiB },
{ QStringLiteral( "GiB" ), SizeUnit::GiB } { QStringLiteral( "GiB" ), SizeUnit::GiB }
}; };
@ -55,7 +52,9 @@ PartitionSize::PartitionSize( const QString& s )
{ {
m_value = s.toInt(); m_value = s.toInt();
if ( m_value > 0 ) if ( m_value > 0 )
{
m_unit = SizeUnit::Byte; m_unit = SizeUnit::Byte;
}
} }
if ( m_value <= 0 ) if ( m_value <= 0 )
@ -69,9 +68,13 @@ qint64
PartitionSize::toSectors( qint64 totalSectors, qint64 sectorSize ) const PartitionSize::toSectors( qint64 totalSectors, qint64 sectorSize ) const
{ {
if ( !isValid() ) if ( !isValid() )
{
return -1; return -1;
}
if ( totalSectors < 1 || sectorSize < 1 ) if ( totalSectors < 1 || sectorSize < 1 )
{
return -1; return -1;
}
switch ( m_unit ) switch ( m_unit )
{ {
@ -79,14 +82,18 @@ PartitionSize::toSectors( qint64 totalSectors, qint64 sectorSize ) const
return -1; return -1;
case SizeUnit::Percent: case SizeUnit::Percent:
if ( value() == 100 ) if ( value() == 100 )
{
return totalSectors; // Common-case, avoid futzing around return totalSectors; // Common-case, avoid futzing around
}
else else
{
return totalSectors * value() / 100; return totalSectors * value() / 100;
}
case SizeUnit::Byte: case SizeUnit::Byte:
case SizeUnit::KiB: case SizeUnit::KiB:
case SizeUnit::MiB: case SizeUnit::MiB:
case SizeUnit::GiB: case SizeUnit::GiB:
return CalamaresUtils::bytesToSectors ( toBytes(), sectorSize ); return CalamaresUtils::bytesToSectors( toBytes(), sectorSize );
} }
return -1; return -1;
@ -96,7 +103,9 @@ qint64
PartitionSize::toBytes( qint64 totalSectors, qint64 sectorSize ) const PartitionSize::toBytes( qint64 totalSectors, qint64 sectorSize ) const
{ {
if ( !isValid() ) if ( !isValid() )
{
return -1; return -1;
}
switch ( m_unit ) switch ( m_unit )
{ {
@ -104,11 +113,17 @@ PartitionSize::toBytes( qint64 totalSectors, qint64 sectorSize ) const
return -1; return -1;
case SizeUnit::Percent: case SizeUnit::Percent:
if ( totalSectors < 1 || sectorSize < 1 ) if ( totalSectors < 1 || sectorSize < 1 )
{
return -1; return -1;
}
if ( value() == 100 ) if ( value() == 100 )
{
return totalSectors * sectorSize; // Common-case, avoid futzing around return totalSectors * sectorSize; // Common-case, avoid futzing around
}
else else
{
return totalSectors * value() / 100; return totalSectors * value() / 100;
}
case SizeUnit::Byte: case SizeUnit::Byte:
case SizeUnit::KiB: case SizeUnit::KiB:
case SizeUnit::MiB: case SizeUnit::MiB:
@ -124,7 +139,9 @@ qint64
PartitionSize::toBytes( qint64 totalBytes ) const PartitionSize::toBytes( qint64 totalBytes ) const
{ {
if ( !isValid() ) if ( !isValid() )
{
return -1; return -1;
}
switch ( m_unit ) switch ( m_unit )
{ {
@ -132,11 +149,17 @@ PartitionSize::toBytes( qint64 totalBytes ) const
return -1; return -1;
case SizeUnit::Percent: case SizeUnit::Percent:
if ( totalBytes < 1 ) if ( totalBytes < 1 )
{
return -1; return -1;
}
if ( value() == 100 ) if ( value() == 100 )
{
return totalBytes; // Common-case, avoid futzing around return totalBytes; // Common-case, avoid futzing around
}
else else
{
return totalBytes * value() / 100; return totalBytes * value() / 100;
}
case SizeUnit::Byte: case SizeUnit::Byte:
case SizeUnit::KiB: case SizeUnit::KiB:
case SizeUnit::MiB: case SizeUnit::MiB:
@ -152,7 +175,9 @@ qint64
PartitionSize::toBytes() const PartitionSize::toBytes() const
{ {
if ( !isValid() ) if ( !isValid() )
{
return -1; return -1;
}
switch ( m_unit ) switch ( m_unit )
{ {
@ -162,20 +187,22 @@ PartitionSize::toBytes() const
case SizeUnit::Byte: case SizeUnit::Byte:
return value(); return value();
case SizeUnit::KiB: case SizeUnit::KiB:
return CalamaresUtils::KiBtoBytes( static_cast<unsigned long long>( value() ) ); return CalamaresUtils::KiBtoBytes( static_cast< unsigned long long >( value() ) );
case SizeUnit::MiB: case SizeUnit::MiB:
return CalamaresUtils::MiBtoBytes( static_cast<unsigned long long>( value() ) ); return CalamaresUtils::MiBtoBytes( static_cast< unsigned long long >( value() ) );
case SizeUnit::GiB: case SizeUnit::GiB:
return CalamaresUtils::GiBtoBytes( static_cast<unsigned long long>( value() ) ); return CalamaresUtils::GiBtoBytes( static_cast< unsigned long long >( value() ) );
} }
NOTREACHED return -1; NOTREACHED return -1;
} }
bool bool
PartitionSize::operator< ( const PartitionSize& other ) const PartitionSize::operator<( const PartitionSize& other ) const
{ {
if ( !unitsComparable( m_unit, other.m_unit ) ) if ( !unitsComparable( m_unit, other.m_unit ) )
{
return false; return false;
}
switch ( m_unit ) switch ( m_unit )
{ {
@ -187,16 +214,18 @@ PartitionSize::operator< ( const PartitionSize& other ) const
case SizeUnit::KiB: case SizeUnit::KiB:
case SizeUnit::MiB: case SizeUnit::MiB:
case SizeUnit::GiB: case SizeUnit::GiB:
return ( toBytes() < other.toBytes () ); return ( toBytes() < other.toBytes() );
} }
NOTREACHED return false; NOTREACHED return false;
} }
bool bool
PartitionSize::operator> ( const PartitionSize& other ) const PartitionSize::operator>( const PartitionSize& other ) const
{ {
if ( !unitsComparable( m_unit, other.m_unit ) ) if ( !unitsComparable( m_unit, other.m_unit ) )
{
return false; return false;
}
switch ( m_unit ) switch ( m_unit )
{ {
@ -208,16 +237,18 @@ PartitionSize::operator> ( const PartitionSize& other ) const
case SizeUnit::KiB: case SizeUnit::KiB:
case SizeUnit::MiB: case SizeUnit::MiB:
case SizeUnit::GiB: case SizeUnit::GiB:
return ( toBytes() > other.toBytes () ); return ( toBytes() > other.toBytes() );
} }
NOTREACHED return false; NOTREACHED return false;
} }
bool bool
PartitionSize::operator== ( const PartitionSize& other ) const PartitionSize::operator==( const PartitionSize& other ) const
{ {
if ( !unitsComparable( m_unit, other.m_unit ) ) if ( !unitsComparable( m_unit, other.m_unit ) )
{
return false; return false;
}
switch ( m_unit ) switch ( m_unit )
{ {
@ -229,10 +260,10 @@ PartitionSize::operator== ( const PartitionSize& other ) const
case SizeUnit::KiB: case SizeUnit::KiB:
case SizeUnit::MiB: case SizeUnit::MiB:
case SizeUnit::GiB: case SizeUnit::GiB:
return ( toBytes() == other.toBytes () ); return ( toBytes() == other.toBytes() );
} }
NOTREACHED return false; NOTREACHED return false;
} }
} } // namespace Partition
} // namespace } // namespace CalamaresUtils

View file

@ -20,8 +20,8 @@
#ifndef PARTITION_PARTITIONSIZE_H #ifndef PARTITION_PARTITIONSIZE_H
#define PARTITION_PARTITIONSIZE_H #define PARTITION_PARTITIONSIZE_H
#include "utils/Units.h"
#include "utils/NamedSuffix.h" #include "utils/NamedSuffix.h"
#include "utils/Units.h"
// Qt // Qt
#include <QString> #include <QString>
@ -47,21 +47,28 @@ enum class SizeUnit
* the available drive space are on). This class handles parsing * the available drive space are on). This class handles parsing
* of such strings from the config file. * of such strings from the config file.
*/ */
class PartitionSize : public NamedSuffix<SizeUnit, SizeUnit::None> class PartitionSize : public NamedSuffix< SizeUnit, SizeUnit::None >
{ {
public: public:
PartitionSize() : NamedSuffix() { } PartitionSize()
PartitionSize( int v, SizeUnit u ) : NamedSuffix( v, u ) { } : NamedSuffix()
{
}
PartitionSize( int v, SizeUnit u )
: NamedSuffix( v, u )
{
}
PartitionSize( const QString& ); PartitionSize( const QString& );
bool isValid() const bool
isValid() const
{ {
return ( unit() != SizeUnit::None ) && ( value() > 0 ); return ( unit() != SizeUnit::None ) && ( value() > 0 );
} }
bool operator< ( const PartitionSize& other ) const; bool operator<( const PartitionSize& other ) const;
bool operator> ( const PartitionSize& other ) const; 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 . /** @brief Convert the size to the number of sectors @p totalSectors .
* *
@ -107,16 +114,16 @@ public:
* be compared with each other, and all the explicit sizes (KiB, ...) * be compared with each other, and all the explicit sizes (KiB, ...)
* can be compared with each other. * can be compared with each other.
*/ */
static constexpr bool unitsComparable( const SizeUnit u1, const SizeUnit u2 ) static constexpr bool
unitsComparable( const SizeUnit u1, const SizeUnit u2 )
{ {
return !( ( u1 == SizeUnit::None || u2 == SizeUnit::None ) || return !( ( u1 == SizeUnit::None || u2 == SizeUnit::None )
( u1 == SizeUnit::Percent && u2 != SizeUnit::Percent ) || || ( u1 == SizeUnit::Percent && u2 != SizeUnit::Percent )
( u1 != SizeUnit::Percent && u2 == SizeUnit::Percent ) ); || ( u1 != SizeUnit::Percent && u2 == SizeUnit::Percent ) );
} }
}; };
} } // namespace Partition
} // namespace } // namespace CalamaresUtils
#endif // PARTITION_PARTITIONSIZE_H #endif // PARTITION_PARTITIONSIZE_H

View file

@ -31,13 +31,9 @@ Q_DECLARE_METATYPE( SizeUnit )
QTEST_GUILESS_MAIN( PartitionSizeTests ) QTEST_GUILESS_MAIN( PartitionSizeTests )
PartitionSizeTests::PartitionSizeTests() PartitionSizeTests::PartitionSizeTests() {}
{
}
PartitionSizeTests::~PartitionSizeTests() PartitionSizeTests::~PartitionSizeTests() {}
{
}
void void
PartitionSizeTests::initTestCase() PartitionSizeTests::initTestCase()
@ -47,36 +43,38 @@ PartitionSizeTests::initTestCase()
void void
PartitionSizeTests::testUnitComparison_data() PartitionSizeTests::testUnitComparison_data()
{ {
QTest::addColumn<SizeUnit>("u1"); QTest::addColumn< SizeUnit >( "u1" );
QTest::addColumn<SizeUnit>("u2"); QTest::addColumn< SizeUnit >( "u2" );
QTest::addColumn<bool>("comparable"); QTest::addColumn< bool >( "comparable" );
QTest::newRow("nones") << SizeUnit::None << SizeUnit::None << false; QTest::newRow( "nones" ) << SizeUnit::None << SizeUnit::None << false;
QTest::newRow("none+%") << SizeUnit::None << SizeUnit::Percent<< false; QTest::newRow( "none+%" ) << SizeUnit::None << SizeUnit::Percent << false;
QTest::newRow("%+none") << SizeUnit::Percent << SizeUnit::None << false; QTest::newRow( "%+none" ) << SizeUnit::Percent << SizeUnit::None << false;
QTest::newRow("KiB+none") << SizeUnit::KiB << SizeUnit::None << false; QTest::newRow( "KiB+none" ) << SizeUnit::KiB << SizeUnit::None << false;
QTest::newRow("none+MiB") << SizeUnit::None << SizeUnit::MiB << false; QTest::newRow( "none+MiB" ) << SizeUnit::None << SizeUnit::MiB << false;
QTest::newRow("KiB+KiB") << SizeUnit::KiB << SizeUnit::KiB << true; QTest::newRow( "KiB+KiB" ) << SizeUnit::KiB << SizeUnit::KiB << true;
QTest::newRow("KiB+MiB") << SizeUnit::KiB << SizeUnit::MiB << true; QTest::newRow( "KiB+MiB" ) << SizeUnit::KiB << SizeUnit::MiB << true;
QTest::newRow("KiB+GiB") << SizeUnit::KiB << SizeUnit::GiB << true; QTest::newRow( "KiB+GiB" ) << SizeUnit::KiB << SizeUnit::GiB << true;
QTest::newRow("MiB+MiB") << SizeUnit::MiB << SizeUnit::MiB << true; QTest::newRow( "MiB+MiB" ) << SizeUnit::MiB << SizeUnit::MiB << true;
QTest::newRow("MiB+GiB") << SizeUnit::MiB << SizeUnit::GiB << true; QTest::newRow( "MiB+GiB" ) << SizeUnit::MiB << SizeUnit::GiB << true;
QTest::newRow("GiB+GiB") << SizeUnit::GiB << SizeUnit::GiB << true; QTest::newRow( "GiB+GiB" ) << SizeUnit::GiB << SizeUnit::GiB << true;
QTest::newRow("%+None") << SizeUnit::Percent << SizeUnit::None << false; QTest::newRow( "%+None" ) << SizeUnit::Percent << SizeUnit::None << false;
QTest::newRow("%+%") << SizeUnit::Percent << SizeUnit::Percent << true; QTest::newRow( "%+%" ) << SizeUnit::Percent << SizeUnit::Percent << true;
QTest::newRow("%+KiB") << SizeUnit::Percent << SizeUnit::KiB << false; QTest::newRow( "%+KiB" ) << SizeUnit::Percent << SizeUnit::KiB << false;
} }
static bool static bool
original_compare( SizeUnit m_unit, SizeUnit other_m_unit ) original_compare( SizeUnit m_unit, SizeUnit other_m_unit )
{ {
if ( ( m_unit == SizeUnit::None || other_m_unit == SizeUnit::None ) || 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 )
( m_unit != SizeUnit::Percent && other_m_unit == SizeUnit::Percent ) ) || ( m_unit != SizeUnit::Percent && other_m_unit == SizeUnit::Percent ) )
{
return false; return false;
}
return true; return true;
} }
@ -104,33 +102,33 @@ PartitionSizeTests::testUnitComparison()
void void
PartitionSizeTests::testUnitNormalisation_data() PartitionSizeTests::testUnitNormalisation_data()
{ {
QTest::addColumn<SizeUnit>("u1"); QTest::addColumn< SizeUnit >( "u1" );
QTest::addColumn<int>("v"); QTest::addColumn< int >( "v" );
QTest::addColumn<long>("bytes"); QTest::addColumn< long >( "bytes" );
QTest::newRow("none") << SizeUnit::None << 16 << -1L; QTest::newRow( "none" ) << SizeUnit::None << 16 << -1L;
QTest::newRow("none") << SizeUnit::None << 0 << -1L; QTest::newRow( "none" ) << SizeUnit::None << 0 << -1L;
QTest::newRow("none") << SizeUnit::None << -2 << -1L; QTest::newRow( "none" ) << SizeUnit::None << -2 << -1L;
QTest::newRow("percent") << SizeUnit::Percent << 0 << -1L; QTest::newRow( "percent" ) << SizeUnit::Percent << 0 << -1L;
QTest::newRow("percent") << SizeUnit::Percent << 16 << -1L; QTest::newRow( "percent" ) << SizeUnit::Percent << 16 << -1L;
QTest::newRow("percent") << SizeUnit::Percent << -2 << -1L; QTest::newRow( "percent" ) << SizeUnit::Percent << -2 << -1L;
QTest::newRow("KiB") << SizeUnit::KiB << 0 << -1L; QTest::newRow( "KiB" ) << SizeUnit::KiB << 0 << -1L;
QTest::newRow("KiB") << SizeUnit::KiB << 1 << 1024L; QTest::newRow( "KiB" ) << SizeUnit::KiB << 1 << 1024L;
QTest::newRow("KiB") << SizeUnit::KiB << 1000 << 1024000L; QTest::newRow( "KiB" ) << SizeUnit::KiB << 1000 << 1024000L;
QTest::newRow("KiB") << SizeUnit::KiB << 1024 << 1024 * 1024L; QTest::newRow( "KiB" ) << SizeUnit::KiB << 1024 << 1024 * 1024L;
QTest::newRow("KiB") << SizeUnit::KiB << -2 << -1L; QTest::newRow( "KiB" ) << SizeUnit::KiB << -2 << -1L;
QTest::newRow("MiB") << SizeUnit::MiB << 0 << -1L; QTest::newRow( "MiB" ) << SizeUnit::MiB << 0 << -1L;
QTest::newRow("MiB") << SizeUnit::MiB << 1 << 1024 * 1024L; QTest::newRow( "MiB" ) << SizeUnit::MiB << 1 << 1024 * 1024L;
QTest::newRow("MiB") << SizeUnit::MiB << 1000 << 1024 * 1024000L; QTest::newRow( "MiB" ) << SizeUnit::MiB << 1000 << 1024 * 1024000L;
QTest::newRow("MiB") << SizeUnit::MiB << 1024 << 1024 * 1024 * 1024L; QTest::newRow( "MiB" ) << SizeUnit::MiB << 1024 << 1024 * 1024 * 1024L;
QTest::newRow("MiB") << SizeUnit::MiB << -2 << -1L; QTest::newRow( "MiB" ) << SizeUnit::MiB << -2 << -1L;
QTest::newRow("GiB") << SizeUnit::GiB << 0 << -1L; QTest::newRow( "GiB" ) << SizeUnit::GiB << 0 << -1L;
QTest::newRow("GiB") << SizeUnit::GiB << 1 << 1024 * 1024 * 1024L; QTest::newRow( "GiB" ) << SizeUnit::GiB << 1 << 1024 * 1024 * 1024L;
QTest::newRow("GiB") << SizeUnit::GiB << 2 << 2048 * 1024 * 1024L; QTest::newRow( "GiB" ) << SizeUnit::GiB << 2 << 2048 * 1024 * 1024L;
} }
void void
@ -140,5 +138,5 @@ PartitionSizeTests::testUnitNormalisation()
QFETCH( int, v ); QFETCH( int, v );
QFETCH( long, bytes ); QFETCH( long, bytes );
QCOMPARE( PartitionSize( v, u1 ).toBytes(), static_cast<qint64>( bytes ) ); QCOMPARE( PartitionSize( v, u1 ).toBytes(), static_cast< qint64 >( bytes ) );
} }