[fsresizer] Apply sizes properly

- Distinguish size and atleast; in percentages they mean different
   things (atleast is a bit weird as a percentage, but hey).
 - Fix bug in percentage calculation.
 - Avoid percentage above 100.
 - Add documentation in config-file.
This commit is contained in:
Adriaan de Groot 2018-09-28 12:32:08 +02:00
parent c725f6b552
commit aaf27ac2ab
3 changed files with 48 additions and 10 deletions

View File

@ -68,31 +68,48 @@ ResizeFSJob::RelativeSize::RelativeSize( const QString& s)
{ {
matchUnitSuffix( s, "%", Percent, m_value, m_unit ); matchUnitSuffix( s, "%", Percent, m_value, m_unit );
matchUnitSuffix( s, "MiB", Absolute, m_value, m_unit ); matchUnitSuffix( s, "MiB", Absolute, m_value, m_unit );
if ( ( unit() == Percent ) && ( value() > 100 ) )
{
cDebug() << "Percent value" << value() << "is not valid.";
m_value = 0;
m_unit = None;
}
if ( !m_value ) if ( !m_value )
m_unit = None; m_unit = None;
} }
qint64 qint64
ResizeFSJob::RelativeSize::apply( Device* d ) ResizeFSJob::RelativeSize::apply( qint64 totalSectors , qint64 sectorSize )
{ {
if ( !isValid() ) if ( !isValid() )
return -1; return -1;
if ( sectorSize < 1 )
return -1;
switch( m_unit ) switch( m_unit )
{ {
case None: case None:
return -1; return -1;
case Absolute: case Absolute:
return CalamaresUtils::MiBtoBytes( value() ) / d->logicalSize(); return CalamaresUtils::MiBtoBytes( value() ) / sectorSize;
case Percent: case Percent:
return d->logicalSize() * value() / 100; if ( value() == 100 )
return totalSectors; // Common-case, avoid futzing around
else
return totalSectors * value() / 100;
} }
// notreached // notreached
return -1; return -1;
} }
qint64
ResizeFSJob::RelativeSize::apply( Device* d )
{
return apply( d->totalLogical(), d->logicalSize() );
}
ResizeFSJob::ResizeFSJob( QObject* parent ) ResizeFSJob::ResizeFSJob( QObject* parent )
: Calamares::CppJob( parent ) : Calamares::CppJob( parent )
@ -150,9 +167,11 @@ qint64
ResizeFSJob::findGrownEnd(ResizeFSJob::PartitionMatch m) ResizeFSJob::findGrownEnd(ResizeFSJob::PartitionMatch m)
{ {
if ( !m.first || !m.second ) if ( !m.first || !m.second )
return -1; return -1; // Missing device data
if ( !ResizeOperation::canGrow( m.second ) ) if ( !ResizeOperation::canGrow( m.second ) )
return -1; return -1; // Operation is doomed
if ( !m_size.isValid() )
return -1; // Must have a grow-size
cDebug() << "Containing device size" << m.first->totalLogical(); cDebug() << "Containing device size" << m.first->totalLogical();
qint64 last_available = m.first->totalLogical() - 1; // Numbered from 0 qint64 last_available = m.first->totalLogical() - 1; // Numbered from 0
@ -198,6 +217,13 @@ ResizeFSJob::findGrownEnd(ResizeFSJob::PartitionMatch m)
} }
} }
qint64 wanted = m_size.apply( expand, m.first->logicalSize() );
if ( wanted < expand )
{
cDebug() << ".. only growing by" << wanted << "instead of full" << expand;
last_available -= ( expand - wanted );
}
return last_available; return last_available;
} }

View File

@ -64,15 +64,23 @@ public:
return ( unit() != None ) && ( value() > 0 ); return ( unit() != None ) && ( value() > 0 );
} }
/** @brief Apply this size to the given device @p d /** @brief Apply this size to the number of sectors @p totalSectors .
* *
* Each sector has size @p sectorSize , for converting absolute
* sizes in MiB to sector counts.
*
* For invalid sizes, returns -1. * For invalid sizes, returns -1.
* For absolute sizes, returns the number of sectors needed. * For absolute sizes, returns the number of sectors needed.
* For percent sizes, returns the number of sectors matching * For percent sizes, returns that percent of the number of sectors.
* that percentage of the device size. */
qint64 apply( qint64 totalSectors, qint64 sectorSize );
/** @brief Apply this size to the given device.
*
* Equivalent to apply( d->totalLogical(), d->logicalSize() )
*/ */
qint64 apply( Device* d ); qint64 apply( Device* d );
private: private:
int m_value; int m_value;
Unit m_unit; Unit m_unit;

View File

@ -23,6 +23,8 @@ fs: /
# how big the card is), use MiB as suffix in that case. # how big the card is), use MiB as suffix in that case.
# If missing, then it's assumed to be 0, and no resizing # If missing, then it's assumed to be 0, and no resizing
# will happen. # will happen.
#
# Percentages apply to **available space**.
size: 100% size: 100%
# Resizing might not be worth it, though. Set the minimum # Resizing might not be worth it, though. Set the minimum
@ -30,4 +32,6 @@ size: 100%
# resizing is skipped. Can be in percentage or absolute # resizing is skipped. Can be in percentage or absolute
# size, as above. If missing, then it's assumed to be 0, # size, as above. If missing, then it's assumed to be 0,
# which means resizing is always worthwhile. # which means resizing is always worthwhile.
#
# Percentages apply to **total device size**.
atleast: 1000MiB atleast: 1000MiB