[partition] Switch to using the generic PartitionSize class
Instead of relying on a module-specific implementation, use the new PartitionSize class for storing partition sizes. Signed-off-by: Arnaud Ferraris <arnaud.ferraris@collabora.com>
This commit is contained in:
parent
8f9f8f1cc1
commit
80fd3d3353
@ -43,216 +43,6 @@
|
|||||||
namespace PartUtils
|
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 CalamaresUtils::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
|
QString
|
||||||
convenienceName( const Partition* const candidate )
|
convenienceName( const Partition* const candidate )
|
||||||
{
|
{
|
||||||
|
@ -36,78 +36,6 @@ class Partition;
|
|||||||
|
|
||||||
namespace PartUtils
|
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
|
* @brief Provides a nice human-readable name for @p candidate
|
||||||
|
@ -103,7 +103,8 @@ doAutopartition( PartitionCoreModule* core, Device* dev, Choices::AutoPartitionO
|
|||||||
{
|
{
|
||||||
if ( gs->contains( "efiSystemPartitionSize" ) )
|
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() );
|
uefisys_part_sizeB = part_size.toBytes( dev->capacity() );
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
@ -87,9 +87,9 @@ PartitionLayout::addEntry( PartitionLayout::PartitionEntry entry )
|
|||||||
|
|
||||||
PartitionLayout::PartitionEntry::PartitionEntry( const QString& size, const QString& min, const QString& max )
|
PartitionLayout::PartitionEntry::PartitionEntry( const QString& size, const QString& min, const QString& max )
|
||||||
{
|
{
|
||||||
partSize = PartUtils::PartSize( size );
|
partSize = Calamares::PartitionSize( size );
|
||||||
partMinSize = PartUtils::PartSize( min );
|
partMinSize = Calamares::PartitionSize( min );
|
||||||
partMaxSize = PartUtils::PartSize( max );
|
partMaxSize = Calamares::PartitionSize( max );
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
|
@ -20,6 +20,8 @@
|
|||||||
#ifndef PARTITIONLAYOUT_H
|
#ifndef PARTITIONLAYOUT_H
|
||||||
#define PARTITIONLAYOUT_H
|
#define PARTITIONLAYOUT_H
|
||||||
|
|
||||||
|
#include "partition/PartitionSize.h"
|
||||||
|
|
||||||
#include "core/PartUtils.h"
|
#include "core/PartUtils.h"
|
||||||
|
|
||||||
// KPMcore
|
// KPMcore
|
||||||
@ -41,9 +43,9 @@ public:
|
|||||||
QString partLabel;
|
QString partLabel;
|
||||||
QString partMountPoint;
|
QString partMountPoint;
|
||||||
FileSystem::Type partFileSystem = FileSystem::Unknown;
|
FileSystem::Type partFileSystem = FileSystem::Unknown;
|
||||||
PartUtils::PartSize partSize;
|
Calamares::PartitionSize partSize;
|
||||||
PartUtils::PartSize partMinSize;
|
Calamares::PartitionSize partMinSize;
|
||||||
PartUtils::PartSize partMaxSize;
|
Calamares::PartitionSize partMaxSize;
|
||||||
|
|
||||||
/// @brief All-zeroes PartitionEntry
|
/// @brief All-zeroes PartitionEntry
|
||||||
PartitionEntry() {}
|
PartitionEntry() {}
|
||||||
|
Loading…
Reference in New Issue
Block a user