From 90eb6afd528da0f7abf8c4dac3a3ea324b28d0a0 Mon Sep 17 00:00:00 2001 From: Arnaud Ferraris Date: Thu, 28 Feb 2019 13:18:02 +0100 Subject: [PATCH] [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 --- src/modules/partition/core/PartUtils.cpp | 59 +++++++++++++++++-- src/modules/partition/core/PartUtils.h | 17 ++++++ .../partition/core/PartitionLayout.cpp | 54 +---------------- src/modules/partition/core/PartitionLayout.h | 15 ++--- 4 files changed, 78 insertions(+), 67 deletions(-) diff --git a/src/modules/partition/core/PartUtils.cpp b/src/modules/partition/core/PartUtils.cpp index d09bcd149..856af4799 100644 --- a/src/modules/partition/core/PartUtils.cpp +++ b/src/modules/partition/core/PartUtils.cpp @@ -2,6 +2,7 @@ * * Copyright 2015-2016, Teo Mrnjavac * Copyright 2018, Adriaan de Groot + * Copyright 2019, Collabora Ltd * * 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 */ diff --git a/src/modules/partition/core/PartUtils.h b/src/modules/partition/core/PartUtils.h index c7da86c06..35c78f061 100644 --- a/src/modules/partition/core/PartUtils.h +++ b/src/modules/partition/core/PartUtils.h @@ -2,6 +2,7 @@ * * Copyright 2015-2016, Teo Mrnjavac * Copyright 2018, Adriaan de Groot + * Copyright 2019, Collabora Ltd * * 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 diff --git a/src/modules/partition/core/PartitionLayout.cpp b/src/modules/partition/core/PartitionLayout.cpp index 39341fddd..287c19e41 100644 --- a/src/modules/partition/core/PartitionLayout.cpp +++ b/src/modules/partition/core/PartitionLayout.cpp @@ -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 diff --git a/src/modules/partition/core/PartitionLayout.h b/src/modules/partition/core/PartitionLayout.h index 999e10425..ab597734c 100644 --- a/src/modules/partition/core/PartitionLayout.h +++ b/src/modules/partition/core/PartitionLayout.h @@ -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() {};