2019-05-08 19:23:07 +02:00
|
|
|
/* === This file is part of Calamares - <https://github.com/calamares> ===
|
|
|
|
*
|
|
|
|
* Copyright 2019, Collabora Ltd <arnaud.ferraris@collabora.com>
|
2019-05-13 13:44:30 +02:00
|
|
|
* Copyright 2019, Adriaan de Groot <groot@kde.org>
|
2019-05-08 19:23:07 +02:00
|
|
|
*
|
|
|
|
* 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"
|
|
|
|
|
2019-05-14 11:52:58 +02:00
|
|
|
namespace CalamaresUtils
|
|
|
|
{
|
|
|
|
namespace Partition
|
2019-05-08 19:23:07 +02:00
|
|
|
{
|
|
|
|
|
2019-08-04 22:13:58 +02:00
|
|
|
static const NamedEnumTable< SizeUnit >&
|
2019-05-08 19:23:07 +02:00
|
|
|
unitSuffixes()
|
|
|
|
{
|
2019-08-04 22:13:58 +02:00
|
|
|
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 },
|
2019-05-08 19:23:07 +02:00
|
|
|
{ QStringLiteral( "GiB" ), SizeUnit::GiB }
|
|
|
|
};
|
|
|
|
|
|
|
|
return names;
|
|
|
|
}
|
|
|
|
|
|
|
|
PartitionSize::PartitionSize( const QString& s )
|
|
|
|
: NamedSuffix( unitSuffixes(), s )
|
|
|
|
{
|
2019-05-13 13:44:30 +02:00
|
|
|
if ( ( unit() == SizeUnit::Percent ) && ( value() > 100 || value() < 0 ) )
|
2019-05-08 19:23:07 +02:00
|
|
|
{
|
|
|
|
cDebug() << "Percent value" << value() << "is not valid.";
|
|
|
|
m_value = 0;
|
|
|
|
}
|
|
|
|
|
2019-05-13 13:44:30 +02:00
|
|
|
if ( m_unit == SizeUnit::None )
|
2019-05-08 19:23:07 +02:00
|
|
|
{
|
|
|
|
m_value = s.toInt();
|
|
|
|
if ( m_value > 0 )
|
2019-08-04 22:13:58 +02:00
|
|
|
{
|
2019-05-13 13:44:30 +02:00
|
|
|
m_unit = SizeUnit::Byte;
|
2019-08-04 22:13:58 +02:00
|
|
|
}
|
2019-05-08 19:23:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( m_value <= 0 )
|
|
|
|
{
|
|
|
|
m_value = 0;
|
2019-05-13 13:44:30 +02:00
|
|
|
m_unit = SizeUnit::None;
|
2019-05-08 19:23:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
qint64
|
|
|
|
PartitionSize::toSectors( qint64 totalSectors, qint64 sectorSize ) const
|
|
|
|
{
|
|
|
|
if ( !isValid() )
|
2019-08-04 22:13:58 +02:00
|
|
|
{
|
2019-05-08 19:23:07 +02:00
|
|
|
return -1;
|
2019-08-04 22:13:58 +02:00
|
|
|
}
|
2019-05-08 19:23:07 +02:00
|
|
|
if ( totalSectors < 1 || sectorSize < 1 )
|
2019-08-04 22:13:58 +02:00
|
|
|
{
|
2019-05-08 19:23:07 +02:00
|
|
|
return -1;
|
2019-08-04 22:13:58 +02:00
|
|
|
}
|
2019-05-08 19:23:07 +02:00
|
|
|
|
|
|
|
switch ( m_unit )
|
|
|
|
{
|
2019-05-13 13:44:30 +02:00
|
|
|
case SizeUnit::None:
|
2019-05-08 19:23:07 +02:00
|
|
|
return -1;
|
2019-05-13 13:44:30 +02:00
|
|
|
case SizeUnit::Percent:
|
2019-05-08 19:23:07 +02:00
|
|
|
if ( value() == 100 )
|
2019-08-04 22:13:58 +02:00
|
|
|
{
|
2019-05-08 19:23:07 +02:00
|
|
|
return totalSectors; // Common-case, avoid futzing around
|
2019-08-04 22:13:58 +02:00
|
|
|
}
|
2019-05-08 19:23:07 +02:00
|
|
|
else
|
2019-08-04 22:13:58 +02:00
|
|
|
{
|
2019-05-08 19:23:07 +02:00
|
|
|
return totalSectors * value() / 100;
|
2019-08-04 22:13:58 +02:00
|
|
|
}
|
2019-05-13 13:44:30 +02:00
|
|
|
case SizeUnit::Byte:
|
|
|
|
case SizeUnit::KiB:
|
|
|
|
case SizeUnit::MiB:
|
|
|
|
case SizeUnit::GiB:
|
2019-08-04 22:13:58 +02:00
|
|
|
return CalamaresUtils::bytesToSectors( toBytes(), sectorSize );
|
2019-05-08 19:23:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
qint64
|
|
|
|
PartitionSize::toBytes( qint64 totalSectors, qint64 sectorSize ) const
|
|
|
|
{
|
|
|
|
if ( !isValid() )
|
2019-08-04 22:13:58 +02:00
|
|
|
{
|
2019-05-08 19:23:07 +02:00
|
|
|
return -1;
|
2019-08-04 22:13:58 +02:00
|
|
|
}
|
2019-05-08 19:23:07 +02:00
|
|
|
|
|
|
|
switch ( m_unit )
|
|
|
|
{
|
2019-05-13 13:44:30 +02:00
|
|
|
case SizeUnit::None:
|
2019-05-08 19:23:07 +02:00
|
|
|
return -1;
|
2019-05-13 13:44:30 +02:00
|
|
|
case SizeUnit::Percent:
|
2019-05-08 19:23:07 +02:00
|
|
|
if ( totalSectors < 1 || sectorSize < 1 )
|
2019-08-04 22:13:58 +02:00
|
|
|
{
|
2019-05-08 19:23:07 +02:00
|
|
|
return -1;
|
2019-08-04 22:13:58 +02:00
|
|
|
}
|
2019-05-08 19:23:07 +02:00
|
|
|
if ( value() == 100 )
|
2019-08-04 22:13:58 +02:00
|
|
|
{
|
2019-05-08 19:23:07 +02:00
|
|
|
return totalSectors * sectorSize; // Common-case, avoid futzing around
|
2019-08-04 22:13:58 +02:00
|
|
|
}
|
2019-05-08 19:23:07 +02:00
|
|
|
else
|
2019-08-04 22:13:58 +02:00
|
|
|
{
|
2019-05-08 19:23:07 +02:00
|
|
|
return totalSectors * value() / 100;
|
2019-08-04 22:13:58 +02:00
|
|
|
}
|
2019-05-13 13:44:30 +02:00
|
|
|
case SizeUnit::Byte:
|
|
|
|
case SizeUnit::KiB:
|
|
|
|
case SizeUnit::MiB:
|
|
|
|
case SizeUnit::GiB:
|
2019-05-08 19:23:07 +02:00
|
|
|
return toBytes();
|
|
|
|
}
|
|
|
|
|
|
|
|
// notreached
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
qint64
|
|
|
|
PartitionSize::toBytes( qint64 totalBytes ) const
|
|
|
|
{
|
|
|
|
if ( !isValid() )
|
2019-08-04 22:13:58 +02:00
|
|
|
{
|
2019-05-08 19:23:07 +02:00
|
|
|
return -1;
|
2019-08-04 22:13:58 +02:00
|
|
|
}
|
2019-05-08 19:23:07 +02:00
|
|
|
|
|
|
|
switch ( m_unit )
|
|
|
|
{
|
2019-05-13 13:44:30 +02:00
|
|
|
case SizeUnit::None:
|
2019-05-08 19:23:07 +02:00
|
|
|
return -1;
|
2019-05-13 13:44:30 +02:00
|
|
|
case SizeUnit::Percent:
|
2019-05-08 19:23:07 +02:00
|
|
|
if ( totalBytes < 1 )
|
2019-08-04 22:13:58 +02:00
|
|
|
{
|
2019-05-08 19:23:07 +02:00
|
|
|
return -1;
|
2019-08-04 22:13:58 +02:00
|
|
|
}
|
2019-05-08 19:23:07 +02:00
|
|
|
if ( value() == 100 )
|
2019-08-04 22:13:58 +02:00
|
|
|
{
|
2019-05-08 19:23:07 +02:00
|
|
|
return totalBytes; // Common-case, avoid futzing around
|
2019-08-04 22:13:58 +02:00
|
|
|
}
|
2019-05-08 19:23:07 +02:00
|
|
|
else
|
2019-08-04 22:13:58 +02:00
|
|
|
{
|
2019-05-08 19:23:07 +02:00
|
|
|
return totalBytes * value() / 100;
|
2019-08-04 22:13:58 +02:00
|
|
|
}
|
2019-05-13 13:44:30 +02:00
|
|
|
case SizeUnit::Byte:
|
|
|
|
case SizeUnit::KiB:
|
|
|
|
case SizeUnit::MiB:
|
|
|
|
case SizeUnit::GiB:
|
2019-05-08 19:23:07 +02:00
|
|
|
return toBytes();
|
|
|
|
}
|
|
|
|
|
|
|
|
// notreached
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
qint64
|
|
|
|
PartitionSize::toBytes() const
|
|
|
|
{
|
|
|
|
if ( !isValid() )
|
2019-08-04 22:13:58 +02:00
|
|
|
{
|
2019-05-08 19:23:07 +02:00
|
|
|
return -1;
|
2019-08-04 22:13:58 +02:00
|
|
|
}
|
2019-05-08 19:23:07 +02:00
|
|
|
|
|
|
|
switch ( m_unit )
|
|
|
|
{
|
2019-05-13 13:44:30 +02:00
|
|
|
case SizeUnit::None:
|
|
|
|
case SizeUnit::Percent:
|
2019-05-13 12:23:19 +02:00
|
|
|
return -1;
|
2019-05-13 13:44:30 +02:00
|
|
|
case SizeUnit::Byte:
|
2019-05-08 19:23:07 +02:00
|
|
|
return value();
|
2019-05-13 13:44:30 +02:00
|
|
|
case SizeUnit::KiB:
|
2019-08-04 22:13:58 +02:00
|
|
|
return CalamaresUtils::KiBtoBytes( static_cast< unsigned long long >( value() ) );
|
2019-05-13 13:44:30 +02:00
|
|
|
case SizeUnit::MiB:
|
2019-08-04 22:13:58 +02:00
|
|
|
return CalamaresUtils::MiBtoBytes( static_cast< unsigned long long >( value() ) );
|
2019-05-13 13:44:30 +02:00
|
|
|
case SizeUnit::GiB:
|
2019-08-04 22:13:58 +02:00
|
|
|
return CalamaresUtils::GiBtoBytes( static_cast< unsigned long long >( value() ) );
|
2019-05-08 19:23:07 +02:00
|
|
|
}
|
2019-05-13 18:04:31 +02:00
|
|
|
NOTREACHED return -1;
|
2019-05-08 19:23:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2019-08-04 22:13:58 +02:00
|
|
|
PartitionSize::operator<( const PartitionSize& other ) const
|
2019-05-08 19:23:07 +02:00
|
|
|
{
|
2019-05-13 13:44:30 +02:00
|
|
|
if ( !unitsComparable( m_unit, other.m_unit ) )
|
2019-08-04 22:13:58 +02:00
|
|
|
{
|
2019-05-08 19:23:07 +02:00
|
|
|
return false;
|
2019-08-04 22:13:58 +02:00
|
|
|
}
|
2019-05-08 19:23:07 +02:00
|
|
|
|
|
|
|
switch ( m_unit )
|
|
|
|
{
|
2019-05-13 13:54:09 +02:00
|
|
|
case SizeUnit::None:
|
|
|
|
return false;
|
2019-05-13 13:44:30 +02:00
|
|
|
case SizeUnit::Percent:
|
2019-05-08 19:23:07 +02:00
|
|
|
return ( m_value < other.m_value );
|
2019-05-13 13:44:30 +02:00
|
|
|
case SizeUnit::Byte:
|
|
|
|
case SizeUnit::KiB:
|
|
|
|
case SizeUnit::MiB:
|
|
|
|
case SizeUnit::GiB:
|
2019-08-04 22:13:58 +02:00
|
|
|
return ( toBytes() < other.toBytes() );
|
2019-05-08 19:23:07 +02:00
|
|
|
}
|
2019-05-13 18:04:31 +02:00
|
|
|
NOTREACHED return false;
|
2019-05-08 19:23:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2019-08-04 22:13:58 +02:00
|
|
|
PartitionSize::operator>( const PartitionSize& other ) const
|
2019-05-08 19:23:07 +02:00
|
|
|
{
|
2019-05-13 13:44:30 +02:00
|
|
|
if ( !unitsComparable( m_unit, other.m_unit ) )
|
2019-08-04 22:13:58 +02:00
|
|
|
{
|
2019-05-08 19:23:07 +02:00
|
|
|
return false;
|
2019-08-04 22:13:58 +02:00
|
|
|
}
|
2019-05-08 19:23:07 +02:00
|
|
|
|
|
|
|
switch ( m_unit )
|
|
|
|
{
|
2019-05-13 13:54:09 +02:00
|
|
|
case SizeUnit::None:
|
|
|
|
return false;
|
2019-05-13 13:44:30 +02:00
|
|
|
case SizeUnit::Percent:
|
2019-05-08 19:23:07 +02:00
|
|
|
return ( m_value > other.m_value );
|
2019-05-13 13:44:30 +02:00
|
|
|
case SizeUnit::Byte:
|
|
|
|
case SizeUnit::KiB:
|
|
|
|
case SizeUnit::MiB:
|
|
|
|
case SizeUnit::GiB:
|
2019-08-04 22:13:58 +02:00
|
|
|
return ( toBytes() > other.toBytes() );
|
2019-05-08 19:23:07 +02:00
|
|
|
}
|
2019-05-13 18:04:31 +02:00
|
|
|
NOTREACHED return false;
|
2019-05-08 19:23:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2019-08-04 22:13:58 +02:00
|
|
|
PartitionSize::operator==( const PartitionSize& other ) const
|
2019-05-08 19:23:07 +02:00
|
|
|
{
|
2019-05-13 13:44:30 +02:00
|
|
|
if ( !unitsComparable( m_unit, other.m_unit ) )
|
2019-08-04 22:13:58 +02:00
|
|
|
{
|
2019-05-08 19:23:07 +02:00
|
|
|
return false;
|
2019-08-04 22:13:58 +02:00
|
|
|
}
|
2019-05-08 19:23:07 +02:00
|
|
|
|
|
|
|
switch ( m_unit )
|
|
|
|
{
|
2019-05-13 13:54:09 +02:00
|
|
|
case SizeUnit::None:
|
|
|
|
return false;
|
2019-05-13 13:44:30 +02:00
|
|
|
case SizeUnit::Percent:
|
2019-05-08 19:23:07 +02:00
|
|
|
return ( m_value == other.m_value );
|
2019-05-13 13:44:30 +02:00
|
|
|
case SizeUnit::Byte:
|
|
|
|
case SizeUnit::KiB:
|
|
|
|
case SizeUnit::MiB:
|
|
|
|
case SizeUnit::GiB:
|
2019-08-04 22:13:58 +02:00
|
|
|
return ( toBytes() == other.toBytes() );
|
2019-05-08 19:23:07 +02:00
|
|
|
}
|
2019-05-13 18:04:31 +02:00
|
|
|
NOTREACHED return false;
|
2019-05-08 19:23:07 +02:00
|
|
|
}
|
|
|
|
|
2019-08-04 22:13:58 +02:00
|
|
|
} // namespace Partition
|
|
|
|
} // namespace CalamaresUtils
|