[partition] move bytesToSectors() and sizeToSectors() to PartUtils

In order to allow the use of these functions across the whole partition
module and keep all partition size-related functions in the same
namespace, this commit moves them to PartUtils.

Signed-off-by: Arnaud Ferraris <arnaud.ferraris@collabora.com>
This commit is contained in:
Arnaud Ferraris 2019-02-28 13:36:29 +01:00
parent c3ccc0de0e
commit a5258f07a1
5 changed files with 67 additions and 53 deletions

View File

@ -459,6 +459,33 @@ 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 )
{
@ -509,6 +536,13 @@ parseSizeString( const QString& sizeString, SizeUnit* unit )
return value;
}
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

@ -22,6 +22,7 @@
#define PARTUTILS_H
#include "OsproberEntry.h"
#include "utils/Units.h"
// KPMcore
#include <kpmcore/fs/filesystem.h>
@ -34,6 +35,8 @@ class Partition;
namespace PartUtils
{
using CalamaresUtils::MiBtoBytes;
enum SizeUnit
{
Percent = 0,
@ -103,6 +106,31 @@ QString findFS( QString fsName, FileSystem::Type* fsType );
* @return the size value, as parsed from the input string.
*/
double parseSizeString( const QString& sizeString, SizeUnit* unit );
/**
* @brief Convert a partition size to a sectors count.
* @param size the partition size.
* @param unit the partition size unit.
* @param totalSectors the total number of sectors of the selected drive.
* @param logicalSize the sector size, in bytes.
* @return the number of sectors to be used for the given partition size.
*/
qint64 sizeToSectors( double size, SizeUnit unit, qint64 totalSectors, qint64 logicalSize );
constexpr qint64 alignBytesToBlockSize( qint64 bytes, qint64 blocksize )
{
qint64 blocks = bytes / blocksize;
if ( blocks * blocksize != bytes )
++blocks;
return blocks * blocksize;
}
constexpr qint64 bytesToSectors( qint64 bytes, qint64 blocksize )
{
return alignBytesToBlockSize( alignBytesToBlockSize( bytes, blocksize), MiBtoBytes(1ULL) ) / blocksize;
}
}
#endif // PARTUTILS_H

View File

@ -2,6 +2,7 @@
*
* Copyright 2014-2017, Teo Mrnjavac <teo@kde.org>
* Copyright 2017-2018, Adriaan de Groot <groot@kde.org>
* Copyright 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
@ -38,8 +39,6 @@
namespace PartitionActions
{
using CalamaresUtils::GiBtoBytes;
using CalamaresUtils::MiBtoBytes;
using CalamaresUtils::operator""_GiB;
using CalamaresUtils::operator""_MiB;
@ -82,22 +81,6 @@ swapSuggestion( const qint64 availableSpaceB, Choices::SwapChoice swap )
return suggestedSwapSizeB;
}
constexpr qint64
alignBytesToBlockSize( qint64 bytes, qint64 blocksize )
{
qint64 blocks = bytes / blocksize;
if ( blocks * blocksize != bytes )
++blocks;
return blocks * blocksize;
}
qint64
bytesToSectors( qint64 bytes, qint64 blocksize )
{
return alignBytesToBlockSize( alignBytesToBlockSize( bytes, blocksize), MiBtoBytes(1) ) / blocksize;
}
void
doAutopartition( PartitionCoreModule* core, Device* dev, Choices::AutoPartitionOptions o )
{
@ -117,11 +100,11 @@ doAutopartition( PartitionCoreModule* core, Device* dev, Choices::AutoPartitionO
// Since sectors count from 0, if the space is 2048 sectors in size,
// the first free sector has number 2048 (and there are 2048 sectors
// before that one, numbered 0..2047).
qint64 firstFreeSector = bytesToSectors( empty_space_sizeB, dev->logicalSize() );
qint64 firstFreeSector = PartUtils::bytesToSectors( empty_space_sizeB, dev->logicalSize() );
if ( isEfi )
{
qint64 efiSectorCount = bytesToSectors( uefisys_part_sizeB, dev->logicalSize() );
qint64 efiSectorCount = PartUtils::bytesToSectors( uefisys_part_sizeB, dev->logicalSize() );
Q_ASSERT( efiSectorCount > 0 );
// Since sectors count from 0, and this partition is created starting

View File

@ -75,8 +75,6 @@ namespace Choices
} // namespace Choices
qint64 bytesToSectors( qint64 bytes, qint64 blocksize );
/**
* @brief doAutopartition sets up an autopartitioning operation on the given Device.
* @param core a pointer to the PartitionCoreModule instance.

View File

@ -107,35 +107,6 @@ PartitionLayout::addEntry( const QString& label, const QString& mountPoint, cons
m_partLayout.append( entry );
}
static qint64
sizeToSectors( double size, PartitionLayout::SizeUnit unit, qint64 totalSize, qint64 logicalSize )
{
qint64 sectors;
double tmp;
if ( unit == PartitionLayout::SizeUnit::Percent )
{
tmp = static_cast<double>( totalSize ) * size / 100;
sectors = static_cast<qint64>( tmp );
}
else
{
tmp = size;
if ( unit >= PartitionLayout::SizeUnit::KiB )
tmp *= 1024;
if ( unit >= PartitionLayout::SizeUnit::MiB )
tmp *= 1024;
if ( unit >= PartitionLayout::SizeUnit::GiB )
tmp *= 1024;
sectors = PartitionActions::bytesToSectors( static_cast<unsigned long long>( tmp ),
logicalSize
);
}
return sectors;
}
QList< Partition* >
PartitionLayout::execute( Device *dev, qint64 firstSector,
qint64 lastSector, QString luksPassphrase,
@ -155,8 +126,8 @@ PartitionLayout::execute( Device *dev, qint64 firstSector,
Partition *currentPartition = nullptr;
// Calculate partition size
size = sizeToSectors( part.partSize, part.partSizeUnit, totalSize, dev->logicalSize() );
minSize = sizeToSectors( part.partMinSize, part.partMinSizeUnit, totalSize, dev->logicalSize() );
size = PartUtils::sizeToSectors( part.partSize, part.partSizeUnit, totalSize, dev->logicalSize() );
minSize = PartUtils::sizeToSectors( part.partMinSize, part.partMinSizeUnit, totalSize, dev->logicalSize() );
if ( size < minSize )
size = minSize;
if ( size > availableSize )