[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
PartitionIterator::operator++()

View file

@ -30,12 +30,9 @@ 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( "%" ), 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 }
};
@ -55,8 +52,10 @@ PartitionSize::PartitionSize( const QString& s )
{
m_value = s.toInt();
if ( m_value > 0 )
{
m_unit = SizeUnit::Byte;
}
}
if ( m_value <= 0 )
{
@ -69,9 +68,13 @@ qint64
PartitionSize::toSectors( qint64 totalSectors, qint64 sectorSize ) const
{
if ( !isValid() )
{
return -1;
}
if ( totalSectors < 1 || sectorSize < 1 )
{
return -1;
}
switch ( m_unit )
{
@ -79,9 +82,13 @@ PartitionSize::toSectors( qint64 totalSectors, qint64 sectorSize ) const
return -1;
case SizeUnit::Percent:
if ( value() == 100 )
{
return totalSectors; // Common-case, avoid futzing around
}
else
{
return totalSectors * value() / 100;
}
case SizeUnit::Byte:
case SizeUnit::KiB:
case SizeUnit::MiB:
@ -96,7 +103,9 @@ qint64
PartitionSize::toBytes( qint64 totalSectors, qint64 sectorSize ) const
{
if ( !isValid() )
{
return -1;
}
switch ( m_unit )
{
@ -104,11 +113,17 @@ PartitionSize::toBytes( qint64 totalSectors, qint64 sectorSize ) const
return -1;
case SizeUnit::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 SizeUnit::Byte:
case SizeUnit::KiB:
case SizeUnit::MiB:
@ -124,7 +139,9 @@ qint64
PartitionSize::toBytes( qint64 totalBytes ) const
{
if ( !isValid() )
{
return -1;
}
switch ( m_unit )
{
@ -132,11 +149,17 @@ PartitionSize::toBytes( qint64 totalBytes ) const
return -1;
case SizeUnit::Percent:
if ( totalBytes < 1 )
{
return -1;
}
if ( value() == 100 )
{
return totalBytes; // Common-case, avoid futzing around
}
else
{
return totalBytes * value() / 100;
}
case SizeUnit::Byte:
case SizeUnit::KiB:
case SizeUnit::MiB:
@ -152,7 +175,9 @@ qint64
PartitionSize::toBytes() const
{
if ( !isValid() )
{
return -1;
}
switch ( m_unit )
{
@ -175,7 +200,9 @@ bool
PartitionSize::operator<( const PartitionSize& other ) const
{
if ( !unitsComparable( m_unit, other.m_unit ) )
{
return false;
}
switch ( m_unit )
{
@ -196,7 +223,9 @@ bool
PartitionSize::operator>( const PartitionSize& other ) const
{
if ( !unitsComparable( m_unit, other.m_unit ) )
{
return false;
}
switch ( m_unit )
{
@ -217,7 +246,9 @@ bool
PartitionSize::operator==( const PartitionSize& other ) const
{
if ( !unitsComparable( m_unit, other.m_unit ) )
{
return false;
}
switch ( m_unit )
{
@ -234,5 +265,5 @@ PartitionSize::operator== ( const PartitionSize& other ) const
NOTREACHED return false;
}
}
} // namespace
} // namespace Partition
} // namespace CalamaresUtils

View file

@ -20,8 +20,8 @@
#ifndef PARTITION_PARTITIONSIZE_H
#define PARTITION_PARTITIONSIZE_H
#include "utils/Units.h"
#include "utils/NamedSuffix.h"
#include "utils/Units.h"
// Qt
#include <QString>
@ -50,11 +50,18 @@ enum class SizeUnit
class PartitionSize : public NamedSuffix< SizeUnit, SizeUnit::None >
{
public:
PartitionSize() : NamedSuffix() { }
PartitionSize( int v, SizeUnit u ) : NamedSuffix( v, u ) { }
PartitionSize()
: NamedSuffix()
{
}
PartitionSize( int v, SizeUnit u )
: NamedSuffix( v, u )
{
}
PartitionSize( const QString& );
bool isValid() const
bool
isValid() const
{
return ( unit() != SizeUnit::None ) && ( value() > 0 );
}
@ -107,16 +114,16 @@ public:
* be compared with each other, and all the explicit sizes (KiB, ...)
* 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 ) ||
( u1 == SizeUnit::Percent && u2 != SizeUnit::Percent ) ||
( u1 != SizeUnit::Percent && u2 == SizeUnit::Percent ) );
return !( ( u1 == SizeUnit::None || u2 == SizeUnit::None )
|| ( u1 == SizeUnit::Percent && u2 != SizeUnit::Percent )
|| ( u1 != SizeUnit::Percent && u2 == SizeUnit::Percent ) );
}
};
}
} // namespace
} // namespace Partition
} // namespace CalamaresUtils
#endif // PARTITION_PARTITIONSIZE_H

View file

@ -31,13 +31,9 @@ Q_DECLARE_METATYPE( SizeUnit )
QTEST_GUILESS_MAIN( PartitionSizeTests )
PartitionSizeTests::PartitionSizeTests()
{
}
PartitionSizeTests::PartitionSizeTests() {}
PartitionSizeTests::~PartitionSizeTests()
{
}
PartitionSizeTests::~PartitionSizeTests() {}
void
PartitionSizeTests::initTestCase()
@ -73,10 +69,12 @@ PartitionSizeTests::testUnitComparison_data()
static bool
original_compare( SizeUnit m_unit, SizeUnit other_m_unit )
{
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 ) )
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;
}
return true;
}