Switch to using PartSize class for partition sizes

Every call of `ParseStringSize` is replaced by using an instance of the
`PartUtils::PartSize` class.

This commit also removes the now-unused previous size parsing functions.

Signed-off-by: Arnaud Ferraris <arnaud.ferraris@collabora.com>
This commit is contained in:
Arnaud Ferraris 2019-04-17 18:40:30 +02:00
parent 3a58ae5e8b
commit d32733bf59
5 changed files with 17 additions and 124 deletions

View File

@ -694,99 +694,6 @@ findFS( QString fsName, FileSystem::Type* fsType )
return fsName;
}
static qint64
sizeToBytes( double size, SizeUnit unit, qint64 totalSize )
{
qint64 bytes;
switch ( unit )
{
case SizeUnit::Percent:
bytes = qint64( static_cast<double>( totalSize ) * size / 100.0L );
break;
case SizeUnit::KiB:
bytes = CalamaresUtils::KiBtoBytes(size);
break;
case SizeUnit::MiB:
bytes = CalamaresUtils::MiBtoBytes(size);
break;
case SizeUnit::GiB:
bytes = CalamaresUtils::GiBtoBytes(size);
break;
default:
bytes = size;
break;
}
return bytes;
}
double
parseSizeString( const QString& sizeString, SizeUnit* unit )
{
double value;
bool ok;
QString valueString;
QString unitString;
QRegExp rx( "[KkMmGg%]" );
int pos = rx.indexIn( sizeString );
if (pos > 0)
{
valueString = sizeString.mid( 0, pos );
unitString = sizeString.mid( pos );
}
else
valueString = sizeString;
value = valueString.toDouble( &ok );
if ( !ok )
{
/*
* In case the conversion fails, a size of 100% allows a few cases to pass
* anyway (e.g. when it is the last partition of the layout)
*/
*unit = SizeUnit::Percent;
return 100.0L;
}
if ( unitString.length() > 0 )
{
if ( unitString.at(0) == '%' )
*unit = SizeUnit::Percent;
else if ( unitString.at(0).toUpper() == 'K' )
*unit = SizeUnit::KiB;
else if ( unitString.at(0).toUpper() == 'M' )
*unit = SizeUnit::MiB;
else if ( unitString.at(0).toUpper() == 'G' )
*unit = SizeUnit::GiB;
else
*unit = SizeUnit::Byte;
}
else
{
*unit = SizeUnit::Byte;
}
return value;
}
qint64
parseSizeString( const QString& sizeString, qint64 totalSize )
{
SizeUnit unit;
double value = parseSizeString( sizeString, &unit );
return sizeToBytes( value, unit, totalSize );
}
qint64
sizeToSectors( double size, SizeUnit unit, qint64 totalSectors, qint64 logicalSize )
{
qint64 bytes = sizeToBytes( size, unit, totalSectors * logicalSize );
return bytesToSectors( static_cast<unsigned long long>( bytes ), logicalSize );
}
} // nmamespace PartUtils
/* Implementation of methods for FstabEntry, from OsproberEntry.h */

View File

@ -171,22 +171,6 @@ bool isEfiBootable( const Partition* candidate );
*/
QString findFS( QString fsName, FileSystem::Type* fsType );
/**
* @brief Parse a partition size string and return its value and unit used.
* @param sizeString the string to parse.
* @param unit pointer to a SizeUnit variable for storing the parsed unit.
* @return the size value, as parsed from the input string.
*/
double parseSizeString( const QString& sizeString, SizeUnit* unit );
/**
* @brief Parse a partition size string and return its value in bytes.
* @param sizeString the string to parse.
* @param totalSize the size of the selected drive (used when the size is expressed in %)
* @return the size value in bytes.
*/
qint64 parseSizeString( const QString& sizeString, qint64 totalSize );
/**
* @brief Convert a partition size to a sectors count.
* @param size the partition size.

View File

@ -102,9 +102,14 @@ doAutopartition( PartitionCoreModule* core, Device* dev, Choices::AutoPartitionO
if ( isEfi )
{
if ( gs->contains( "efiSystemPartitionSize" ) )
uefisys_part_sizeB = PartUtils::parseSizeString( gs->value( "efiSystemPartitionSize" ).toString(), dev->capacity() );
{
PartUtils::PartSize part_size = PartUtils::PartSize( gs->value( "efiSystemPartitionSize" ).toString() );
uefisys_part_sizeB = part_size.toBytes( dev->capacity() );
}
else
{
uefisys_part_sizeB = 300_MiB;
}
}
// Since sectors count from 0, if the space is 2048 sectors in size,

View File

@ -2,7 +2,7 @@
*
* Copyright 2014-2017, Teo Mrnjavac <teo@kde.org>
* Copyright 2017-2018, Adriaan de Groot <groot@kde.org>
* Copyright 2018, Collabora Ltd
* Copyright 2018-2019, Collabora Ltd <arnaud.ferraris@collabora.com>
*
* Calamares is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -77,11 +77,11 @@ PartitionLayout::addEntry( PartitionLayout::PartitionEntry entry )
PartitionLayout::PartitionEntry::PartitionEntry( const QString& size, const QString& min, const QString& max )
{
partSize = PartUtils::parseSizeString( size , &partSizeUnit );
partSize = PartUtils::PartSize( size );
if ( !min.isEmpty() )
partMinSize = PartUtils::parseSizeString( min , &partMinSizeUnit );
partMinSize = PartUtils::PartSize( min );
if ( !max.isEmpty() )
partMaxSize = PartUtils::parseSizeString( max , &partMaxSizeUnit );
partMaxSize = PartUtils::PartSize( max );
}
void
@ -128,9 +128,9 @@ PartitionLayout::execute( Device *dev, qint64 firstSector,
Partition *currentPartition = nullptr;
// Calculate partition size
size = PartUtils::sizeToSectors( part.partSize, part.partSizeUnit, totalSize, dev->logicalSize() );
minSize = PartUtils::sizeToSectors( part.partMinSize, part.partMinSizeUnit, totalSize, dev->logicalSize() );
maxSize = PartUtils::sizeToSectors( part.partMaxSize, part.partMaxSizeUnit, totalSize, dev->logicalSize() );
size = part.partSize.toSectors( totalSize, dev->logicalSize() );
minSize = part.partMinSize.toSectors( totalSize, dev->logicalSize() );
maxSize = part.partMaxSize.toSectors( totalSize, dev->logicalSize() );
if ( size < minSize )
size = minSize;
if ( size > maxSize )

View File

@ -1,6 +1,6 @@
/* === This file is part of Calamares - <https://github.com/calamares> ===
*
* Copyright 2018, Collabora Ltd
* Copyright 2018-2019, Collabora Ltd <arnaud.ferraris@collabora.com>
* Copyright 2019, Adriaan de Groot <groot@kde.org>
*
* Calamares is free software: you can redistribute it and/or modify
@ -43,12 +43,9 @@ public:
QString partLabel;
QString partMountPoint;
FileSystem::Type partFileSystem = FileSystem::Unknown;
double partSize = 0.0L;
PartUtils::SizeUnit partSizeUnit = PartUtils::SizeUnit::Percent;
double partMinSize = 0.0L;
PartUtils::SizeUnit partMinSizeUnit = PartUtils::SizeUnit::Percent;
double partMaxSize = 100.0L;
PartUtils::SizeUnit partMaxSizeUnit = PartUtils::SizeUnit::Percent;
PartUtils::PartSize partSize = PartUtils::PartSize(0, PartUtils::SizeUnit::Percent);
PartUtils::PartSize partMinSize = PartUtils::PartSize(0, PartUtils::SizeUnit::Percent);
PartUtils::PartSize partMaxSize = PartUtils::PartSize(100, PartUtils::SizeUnit::Percent);
/// @brief All-zeroes PartitionEntry
PartitionEntry() {}