[partition] move parseSizeString() function to PartUtils
In order to be able to parse partition size strings using the same functions across the partition module, the parseSizeString() function is exported to the PartUtils namespace. Signed-off-by: Arnaud Ferraris <arnaud.ferraris@collabora.com>
This commit is contained in:
parent
d289b1bed4
commit
90eb6afd52
@ -2,6 +2,7 @@
|
||||
*
|
||||
* Copyright 2015-2016, Teo Mrnjavac <teo@kde.org>
|
||||
* Copyright 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
|
||||
@ -117,7 +118,7 @@ canBeResized( Partition* candidate )
|
||||
|
||||
if ( table->numPrimaries() >= table->maxPrimaries() )
|
||||
{
|
||||
cDebug() << " .. partition table already has"
|
||||
cDebug() << " .. partition table already has"
|
||||
<< table->maxPrimaries() << "primary partitions.";
|
||||
return false;
|
||||
}
|
||||
@ -198,7 +199,7 @@ lookForFstabEntries( const QString& partitionPath )
|
||||
mountOptions.append( "noload" );
|
||||
}
|
||||
|
||||
cDebug() << "Checking device" << partitionPath
|
||||
cDebug() << "Checking device" << partitionPath
|
||||
<< "for fstab (fs=" << r.getOutput() << ')';
|
||||
|
||||
FstabEntryList fstabEntries;
|
||||
@ -209,9 +210,9 @@ lookForFstabEntries( const QString& partitionPath )
|
||||
if ( !exit ) // if all is well
|
||||
{
|
||||
QFile fstabFile( mountsDir.path() + "/etc/fstab" );
|
||||
|
||||
|
||||
cDebug() << " .. reading" << fstabFile.fileName();
|
||||
|
||||
|
||||
if ( fstabFile.open( QIODevice::ReadOnly | QIODevice::Text ) )
|
||||
{
|
||||
const QStringList fstabLines = QString::fromLocal8Bit( fstabFile.readAll() )
|
||||
@ -458,6 +459,56 @@ findFS( QString fsName, FileSystem::Type* fsType )
|
||||
return fsName;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
} // nmamespace PartUtils
|
||||
|
||||
/* Implementation of methods for FstabEntry, from OsproberEntry.h */
|
||||
|
@ -2,6 +2,7 @@
|
||||
*
|
||||
* Copyright 2015-2016, Teo Mrnjavac <teo@kde.org>
|
||||
* Copyright 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
|
||||
@ -33,6 +34,14 @@ class Partition;
|
||||
|
||||
namespace PartUtils
|
||||
{
|
||||
enum SizeUnit
|
||||
{
|
||||
Percent = 0,
|
||||
Byte,
|
||||
KiB,
|
||||
MiB,
|
||||
GiB
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief canBeReplaced checks whether the given Partition satisfies the criteria
|
||||
@ -86,6 +95,14 @@ bool isEfiBootable( const Partition* candidate );
|
||||
* its value is FileSystem::Unknown if @p fsName is not recognized.
|
||||
*/
|
||||
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 );
|
||||
}
|
||||
|
||||
#endif // PARTUTILS_H
|
||||
|
@ -75,61 +75,11 @@ PartitionLayout::addEntry( PartitionLayout::PartitionEntry entry )
|
||||
m_partLayout.append( entry );
|
||||
}
|
||||
|
||||
static double
|
||||
parseSizeString( const QString& sizeString, PartitionLayout::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 = PartitionLayout::SizeUnit::Percent;
|
||||
return 100;
|
||||
}
|
||||
|
||||
if ( unitString.length() > 0 )
|
||||
{
|
||||
if ( unitString.at(0) == '%' )
|
||||
*unit = PartitionLayout::SizeUnit::Percent;
|
||||
else if ( unitString.at(0).toUpper() == 'K' )
|
||||
*unit = PartitionLayout::SizeUnit::KiB;
|
||||
else if ( unitString.at(0).toUpper() == 'M' )
|
||||
*unit = PartitionLayout::SizeUnit::MiB;
|
||||
else if ( unitString.at(0).toUpper() == 'G' )
|
||||
*unit = PartitionLayout::SizeUnit::GiB;
|
||||
else
|
||||
*unit = PartitionLayout::SizeUnit::Byte;
|
||||
}
|
||||
else
|
||||
{
|
||||
*unit = PartitionLayout::SizeUnit::Byte;
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
PartitionLayout::PartitionEntry::PartitionEntry(const QString& size, const QString& min)
|
||||
{
|
||||
partSize = parseSizeString( size , &partSizeUnit );
|
||||
partSize = PartUtils::parseSizeString( size , &partSizeUnit );
|
||||
if ( !min.isEmpty() )
|
||||
partMinSize = parseSizeString( min , &partMinSizeUnit );
|
||||
partMinSize = PartUtils::parseSizeString( min , &partMinSizeUnit );
|
||||
}
|
||||
|
||||
void
|
||||
|
@ -20,6 +20,8 @@
|
||||
#ifndef PARTITIONLAYOUT_H
|
||||
#define PARTITIONLAYOUT_H
|
||||
|
||||
#include "core/PartUtils.h"
|
||||
|
||||
#include "Typedefs.h"
|
||||
|
||||
// KPMcore
|
||||
@ -36,24 +38,15 @@ class PartitionLayout
|
||||
{
|
||||
public:
|
||||
|
||||
enum SizeUnit
|
||||
{
|
||||
Percent = 0,
|
||||
Byte,
|
||||
KiB,
|
||||
MiB,
|
||||
GiB
|
||||
};
|
||||
|
||||
struct PartitionEntry
|
||||
{
|
||||
QString partLabel;
|
||||
QString partMountPoint;
|
||||
FileSystem::Type partFileSystem = FileSystem::Unknown;
|
||||
double partSize = 0.0L;
|
||||
SizeUnit partSizeUnit = Percent;
|
||||
PartUtils::SizeUnit partSizeUnit = PartUtils::SizeUnit::Percent;
|
||||
double partMinSize = 0.0L;
|
||||
SizeUnit partMinSizeUnit = Percent;
|
||||
PartUtils::SizeUnit partMinSizeUnit = PartUtils::SizeUnit::Percent;
|
||||
|
||||
/// @brief All-zeroes PartitionEntry
|
||||
PartitionEntry() {};
|
||||
|
Loading…
Reference in New Issue
Block a user