[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

@ -30,12 +30,9 @@ 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,8 +52,10 @@ 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,9 +82,13 @@ 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:
@ -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 )
{ {
@ -175,7 +200,9 @@ 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 )
{ {
@ -196,7 +223,9 @@ 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 )
{ {
@ -217,7 +246,9 @@ 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 )
{ {
@ -234,5 +265,5 @@ PartitionSize::operator== ( const PartitionSize& other ) const
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>
@ -50,11 +50,18 @@ enum class SizeUnit
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 );
} }
@ -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()
@ -73,10 +69,12 @@ PartitionSizeTests::testUnitComparison_data()
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;
} }