mirror of
https://github.com/parchlinux/calamares.git
synced 2025-07-01 11:25:36 -04:00
[partition] Simplify the method execute
- Rename the "size" locals using "sectors" in their name. Size may be confusing or not enough specific as it can be interpreted a size in Byte. partSizeMap -> partSectorsMap, totalSize -> totalSectors, availablesize -> availableSectors, size -> sectors, minSize -> minSectors maxSize -> maxSectors - Create a the new local currentSector to iterate over the sectors; instead of using the parameter firstSector. - Remove the variable end that does not help much; too many variable already. Expand its expression instead.
This commit is contained in:
parent
81bec68b3d
commit
3016b93c8f
1 changed files with 37 additions and 74 deletions
|
@ -176,11 +176,12 @@ PartitionLayout::execute( Device* dev,
|
||||||
{
|
{
|
||||||
QList< Partition* > partList;
|
QList< Partition* > partList;
|
||||||
// Map each partition entry to its requested size (0 when calculated later)
|
// Map each partition entry to its requested size (0 when calculated later)
|
||||||
QMap< const PartitionLayout::PartitionEntry*, qint64 > partSizeMap;
|
QMap< const PartitionLayout::PartitionEntry*, qint64 > partSectorsMap;
|
||||||
qint64 totalSize = lastSector - firstSector + 1;
|
const qint64 totalSectors = lastSector - firstSector + 1;
|
||||||
qint64 availableSize = totalSize;
|
qint64 currentSector, availableSectors = totalSectors;
|
||||||
|
|
||||||
// Let's check if we have enough space for each partSize
|
// Let's check if we have enough space for each partitions, using the size
|
||||||
|
// propery or the min-size property if unit is in percentage.
|
||||||
for ( const auto& part : qAsConst( m_partLayout ) )
|
for ( const auto& part : qAsConst( m_partLayout ) )
|
||||||
{
|
{
|
||||||
if ( !part.partSize.isValid() )
|
if ( !part.partSize.isValid() )
|
||||||
|
@ -191,118 +192,80 @@ PartitionLayout::execute( Device* dev,
|
||||||
|
|
||||||
// Calculate partition size: Rely on "possibly uninitialized use"
|
// Calculate partition size: Rely on "possibly uninitialized use"
|
||||||
// warnings to ensure that all the cases are covered below.
|
// warnings to ensure that all the cases are covered below.
|
||||||
qint64 size;
|
|
||||||
// We need to ignore the percent-defined until later
|
// We need to ignore the percent-defined until later
|
||||||
|
qint64 sectors = 0;
|
||||||
if ( part.partSize.unit() != CalamaresUtils::Partition::SizeUnit::Percent )
|
if ( part.partSize.unit() != CalamaresUtils::Partition::SizeUnit::Percent )
|
||||||
{
|
{
|
||||||
size = part.partSize.toSectors( totalSize, dev->logicalSize() );
|
sectors = part.partSize.toSectors( totalSectors, dev->logicalSize() );
|
||||||
}
|
}
|
||||||
else
|
else if ( part.partMinSize.isValid() )
|
||||||
{
|
{
|
||||||
if ( part.partMinSize.isValid() )
|
sectors = part.partMinSize.toSectors( totalSectors, dev->logicalSize() );
|
||||||
{
|
|
||||||
size = part.partMinSize.toSectors( totalSize, dev->logicalSize() );
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
size = 0;
|
|
||||||
}
|
}
|
||||||
|
partSectorsMap.insert( &part, sectors );
|
||||||
|
availableSectors -= sectors;
|
||||||
}
|
}
|
||||||
|
|
||||||
partSizeMap.insert( &part, size );
|
// There is not enough space for all partitions, use the min-size property
|
||||||
availableSize -= size;
|
// and see if we can do better afterward.
|
||||||
}
|
if ( availableSectors < 0 )
|
||||||
|
|
||||||
// Use partMinSize and see if we can do better afterward.
|
|
||||||
if ( availableSize < 0 )
|
|
||||||
{
|
{
|
||||||
availableSize = totalSize;
|
availableSectors = totalSectors;
|
||||||
for ( const auto& part : qAsConst( m_partLayout ) )
|
for ( const auto& part : qAsConst( m_partLayout ) )
|
||||||
{
|
{
|
||||||
qint64 size;
|
qint64 sectors = partSectorsMap.value( &part );
|
||||||
|
|
||||||
if ( part.partMinSize.isValid() )
|
if ( part.partMinSize.isValid() )
|
||||||
{
|
{
|
||||||
size = part.partMinSize.toSectors( totalSize, dev->logicalSize() );
|
sectors = part.partMinSize.toSectors( totalSectors, dev->logicalSize() );
|
||||||
|
partSectorsMap.insert( &part, sectors );
|
||||||
}
|
}
|
||||||
else if ( part.partSize.isValid() )
|
availableSectors -= sectors;
|
||||||
{
|
|
||||||
if ( part.partSize.unit() != CalamaresUtils::Partition::SizeUnit::Percent )
|
|
||||||
{
|
|
||||||
size = part.partSize.toSectors( totalSize, dev->logicalSize() );
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
size = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
size = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
partSizeMap.insert( &part, size );
|
|
||||||
availableSize -= size;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Assign size for percentage-defined partitions
|
// Assign sectors for percentage-defined partitions.
|
||||||
for ( const auto& part : qAsConst( m_partLayout ) )
|
for ( const auto& part : qAsConst( m_partLayout ) )
|
||||||
{
|
{
|
||||||
if ( part.partSize.unit() == CalamaresUtils::Partition::SizeUnit::Percent )
|
if ( part.partSize.unit() == CalamaresUtils::Partition::SizeUnit::Percent )
|
||||||
{
|
{
|
||||||
qint64 size = partSizeMap.value( &part );
|
qint64 sectors = part.partSize.toSectors( availableSectors + partSectorsMap.value( &part ),
|
||||||
size = part.partSize.toSectors( availableSize + size, dev->logicalSize() );
|
dev->logicalSize() );
|
||||||
if ( part.partMinSize.isValid() )
|
if ( part.partMinSize.isValid() )
|
||||||
{
|
{
|
||||||
qint64 minSize = part.partMinSize.toSectors( totalSize, dev->logicalSize() );
|
sectors = std::max( sectors, part.partMinSize.toSectors( totalSectors, dev->logicalSize() ) );
|
||||||
if ( minSize > size )
|
|
||||||
{
|
|
||||||
size = minSize;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if ( part.partMaxSize.isValid() )
|
if ( part.partMaxSize.isValid() )
|
||||||
{
|
{
|
||||||
qint64 maxSize = part.partMaxSize.toSectors( totalSize, dev->logicalSize() );
|
sectors = std::min( sectors, part.partMaxSize.toSectors( totalSectors, dev->logicalSize() ) );
|
||||||
if ( maxSize < size )
|
}
|
||||||
{
|
partSectorsMap.insert( &part, sectors );
|
||||||
size = maxSize;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
partSizeMap.insert( &part, size );
|
// Create the partitions.
|
||||||
}
|
currentSector = firstSector;
|
||||||
}
|
availableSectors = totalSectors;
|
||||||
|
|
||||||
availableSize = totalSize;
|
|
||||||
|
|
||||||
// TODO: Refine partition sizes to make sure there is room for every partition
|
|
||||||
// Use a default (200-500M ?) minimum size for partition without minSize
|
|
||||||
|
|
||||||
for ( const auto& part : qAsConst( m_partLayout ) )
|
for ( const auto& part : qAsConst( m_partLayout ) )
|
||||||
{
|
{
|
||||||
qint64 size, end;
|
|
||||||
Partition* currentPartition = nullptr;
|
Partition* currentPartition = nullptr;
|
||||||
|
|
||||||
size = partSizeMap.value( &part );
|
// Adjust partition size based on available space.
|
||||||
|
qint64 sectors = partSectorsMap.value( &part );
|
||||||
// Adjust partition size based on available space
|
sectors = std::min( sectors, availableSectors );
|
||||||
if ( size > availableSize )
|
if ( sectors == 0 )
|
||||||
{
|
{
|
||||||
size = availableSize;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
end = firstSector + std::max( size - 1, Q_INT64_C( 0 ) );
|
|
||||||
|
|
||||||
if ( luksPassphrase.isEmpty() )
|
if ( luksPassphrase.isEmpty() )
|
||||||
{
|
{
|
||||||
currentPartition = KPMHelpers::createNewPartition(
|
currentPartition = KPMHelpers::createNewPartition(
|
||||||
parent, *dev, role, part.partFileSystem, firstSector, end, KPM_PARTITION_FLAG( None ) );
|
parent, *dev, role, part.partFileSystem, currentSector, currentSector + sectors - 1, KPM_PARTITION_FLAG( None ) );
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
currentPartition = KPMHelpers::createNewEncryptedPartition(
|
currentPartition = KPMHelpers::createNewEncryptedPartition(
|
||||||
parent, *dev, role, part.partFileSystem, firstSector, end, luksPassphrase, KPM_PARTITION_FLAG( None ) );
|
parent, *dev, role, part.partFileSystem, currentSector, currentSector + sectors - 1, luksPassphrase, KPM_PARTITION_FLAG( None ) );
|
||||||
}
|
}
|
||||||
PartitionInfo::setFormat( currentPartition, true );
|
PartitionInfo::setFormat( currentPartition, true );
|
||||||
PartitionInfo::setMountPoint( currentPartition, part.partMountPoint );
|
PartitionInfo::setMountPoint( currentPartition, part.partMountPoint );
|
||||||
|
@ -345,8 +308,8 @@ PartitionLayout::execute( Device* dev,
|
||||||
// Some buggy (legacy) BIOSes test if the bootflag of at least one partition is set.
|
// Some buggy (legacy) BIOSes test if the bootflag of at least one partition is set.
|
||||||
// Otherwise they ignore the device in boot-order, so add it here.
|
// Otherwise they ignore the device in boot-order, so add it here.
|
||||||
partList.append( currentPartition );
|
partList.append( currentPartition );
|
||||||
firstSector = end + 1;
|
currentSector += sectors;
|
||||||
availableSize -= size;
|
availableSectors -= sectors;
|
||||||
}
|
}
|
||||||
|
|
||||||
return partList;
|
return partList;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue