calamares/src/libcalamares/partition/PartitionSize.h

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

120 lines
3.3 KiB
C
Raw Normal View History

/* === This file is part of Calamares - <https://calamares.io> ===
*
[libcalamares] Update SPDX identifiers. Update CppJob.h Update CalamaresConfig.h.in Update DllMacro.h Update GlobalStorage.cpp Update GlobalStorage.h Update Job.cpp Update Job.h Update JobExample.cpp Update JobExample.h Update JobQueue.cpp Update CalamaresConfig.h.in Update CppJob.cpp Update CppJob.h Update DllMacro.h Update GlobalStorage.cpp Update GlobalStorage.h Update Job.cpp Update Job.h Update JobExample.cpp Update JobExample.h Update JobQueue.h Update ProcessJob.cpp Update ProcessJob.h Update PythonHelper.cpp Update PythonJob.cpp Update PythonJob.h Update PythonHelper.h Update PythonJobApi.cpp Update PythonJobApi.h Update Settings.cpp Update Settings.h Update GeoIPJSON.cpp Update GeoIPJSON.h Update GeoIPTests.cpp Update GeoIPTests.h Update GeoIPXML.cpp Update GeoIPXML.h Update Handler.cpp Update Handler.h Update Interface.cpp Update Interface.h Update test_geoip.cpp Update CountryData_p.cpp Update Label.cpp Update Label.h Update LabelModel.cpp Update LabelModel.h Update CountryData_p.cpp Update CountryData_p.cpp Update Lookup.cpp Update Lookup.h Update Tests.cpp Update Tests.h Update TimeZone.cpp Update TimeZone.h Update TranslatableConfiguration.cpp Update TranslatableConfiguration.h Update ZoneData_p.cxxtr Update cldr-extractor.py Update zone-extractor.py Update Actions.h Update Actions.h Update Descriptor.h Update InstanceKey.cpp Update Module.cpp Update Module.h Update Requirement.cpp Update RequirementsChecker.h Update RequirementsModel.cpp Update RequirementsModel.h Update Tests.cpp Update Manager.cpp Update Manager.h Update Tests.cpp Update FileSystem.cpp Update FileSystem.h Update KPMManager.cpp Update KPMManager.h Update KPMTests.cpp Update FileSystem.cpp Update FileSystem.cpp Update FileSystem.h Update KPMManager.cpp Update KPMManager.h Update Mount.cpp Update Mount.h Update PartitionIterator.cpp Update PartitionIterator.h Update PartitionIterator.h Update PartitionQuery.cpp Update PartitionQuery.h Update PartitionSize.cpp Update PartitionSize.h Update Sync.cpp Update Sync.h Update Tests.cpp Update Tests.h Update BoostPython.h Update CalamaresUtilsSystem.cpp Update CalamaresUtilsSystem.h Update CommandList.cpp Update CommandList.h Update Dirs.cpp Update Dirs.h Update Entropy.cpp Update Entropy.h Update Entropy.cpp Update Logger.cpp Update Logger.h Update NamedEnum.h Update NamedSuffix.h Update PluginFactory.cpp Update PluginFactory.h Update RAII.h Update RAII.h Update Retranslator.cpp Update Retranslator.h Update String.cpp Update String.h Update TestPaths.cpp Update Tests.cpp Update Tests.h Update UMask.cpp Update UMask.h Update Units.h Update Variant.cpp Update Variant.h Update Yaml.cpp Update Yaml.h Update moc-warnings.h
2020-05-30 16:15:03 +02:00
* SPDX-FileCopyrightText: 2019 Collabora Ltd <arnaud.ferraris@collabora.com>
* SPDX-FileCopyrightText: 2019 Adriaan de Groot <groot@kde.org>
* SPDX-License-Identifier: GPL-3.0-or-later
*
* Calamares is Free Software: see the License-Identifier above.
*
[libcalamares] Update SPDX identifiers. Update CppJob.h Update CalamaresConfig.h.in Update DllMacro.h Update GlobalStorage.cpp Update GlobalStorage.h Update Job.cpp Update Job.h Update JobExample.cpp Update JobExample.h Update JobQueue.cpp Update CalamaresConfig.h.in Update CppJob.cpp Update CppJob.h Update DllMacro.h Update GlobalStorage.cpp Update GlobalStorage.h Update Job.cpp Update Job.h Update JobExample.cpp Update JobExample.h Update JobQueue.h Update ProcessJob.cpp Update ProcessJob.h Update PythonHelper.cpp Update PythonJob.cpp Update PythonJob.h Update PythonHelper.h Update PythonJobApi.cpp Update PythonJobApi.h Update Settings.cpp Update Settings.h Update GeoIPJSON.cpp Update GeoIPJSON.h Update GeoIPTests.cpp Update GeoIPTests.h Update GeoIPXML.cpp Update GeoIPXML.h Update Handler.cpp Update Handler.h Update Interface.cpp Update Interface.h Update test_geoip.cpp Update CountryData_p.cpp Update Label.cpp Update Label.h Update LabelModel.cpp Update LabelModel.h Update CountryData_p.cpp Update CountryData_p.cpp Update Lookup.cpp Update Lookup.h Update Tests.cpp Update Tests.h Update TimeZone.cpp Update TimeZone.h Update TranslatableConfiguration.cpp Update TranslatableConfiguration.h Update ZoneData_p.cxxtr Update cldr-extractor.py Update zone-extractor.py Update Actions.h Update Actions.h Update Descriptor.h Update InstanceKey.cpp Update Module.cpp Update Module.h Update Requirement.cpp Update RequirementsChecker.h Update RequirementsModel.cpp Update RequirementsModel.h Update Tests.cpp Update Manager.cpp Update Manager.h Update Tests.cpp Update FileSystem.cpp Update FileSystem.h Update KPMManager.cpp Update KPMManager.h Update KPMTests.cpp Update FileSystem.cpp Update FileSystem.cpp Update FileSystem.h Update KPMManager.cpp Update KPMManager.h Update Mount.cpp Update Mount.h Update PartitionIterator.cpp Update PartitionIterator.h Update PartitionIterator.h Update PartitionQuery.cpp Update PartitionQuery.h Update PartitionSize.cpp Update PartitionSize.h Update Sync.cpp Update Sync.h Update Tests.cpp Update Tests.h Update BoostPython.h Update CalamaresUtilsSystem.cpp Update CalamaresUtilsSystem.h Update CommandList.cpp Update CommandList.h Update Dirs.cpp Update Dirs.h Update Entropy.cpp Update Entropy.h Update Entropy.cpp Update Logger.cpp Update Logger.h Update NamedEnum.h Update NamedSuffix.h Update PluginFactory.cpp Update PluginFactory.h Update RAII.h Update RAII.h Update Retranslator.cpp Update Retranslator.h Update String.cpp Update String.h Update TestPaths.cpp Update Tests.cpp Update Tests.h Update UMask.cpp Update UMask.h Update Units.h Update Variant.cpp Update Variant.h Update Yaml.cpp Update Yaml.h Update moc-warnings.h
2020-05-30 16:15:03 +02:00
*
*/
#ifndef PARTITION_PARTITIONSIZE_H
#define PARTITION_PARTITIONSIZE_H
#include "utils/NamedSuffix.h"
#include "utils/Units.h"
// Qt
#include <QString>
namespace CalamaresUtils
{
namespace Partition
{
enum class SizeUnit
{
None,
Percent,
Byte,
KB,
KiB,
MB,
MiB,
GB,
GiB
};
/** @brief Partition size expressions
*
* Sizes can be specified in bytes, KiB, MiB, GiB or percent (of
* the available drive space are on). This class handles parsing
* of such strings from the config file.
*/
class PartitionSize : public NamedSuffix< SizeUnit, SizeUnit::None >
{
public:
PartitionSize()
: NamedSuffix()
{
}
PartitionSize( int v, SizeUnit u )
: NamedSuffix( v, u )
{
}
PartitionSize( const QString& );
bool isValid() const { return ( unit() != SizeUnit::None ) && ( value() > 0 ); }
bool operator<( const PartitionSize& other ) const;
bool operator>( const PartitionSize& other ) const;
bool operator==( const PartitionSize& other ) const;
/** @brief Convert the size to the number of sectors @p totalSectors .
*
* Each sector has size @p sectorSize, for converting sizes in Bytes,
* KiB, MiB or GiB to sector counts.
*
* @return the number of sectors needed, or -1 for invalid sizes.
*/
qint64 toSectors( qint64 totalSectors, qint64 sectorSize ) const;
/** @brief Convert the size to bytes.
*
* The device's sectors count @p totalSectors and sector size
* @p sectoreSize are used to calculated the total size, which
* is then used to calculate the size when using Percent.
*
* @return the size in bytes, or -1 for invalid sizes.
*/
qint64 toBytes( qint64 totalSectors, qint64 sectorSize ) const;
/** @brief Convert the size to bytes.
*
* Total size @p totalBytes is needed for sizes in Percent. This
* parameter is unused in any other case.
*
* @return the size in bytes, or -1 for invalid sizes.
*/
qint64 toBytes( qint64 totalBytes ) const;
/** @brief Convert the size to bytes.
*
* This method is only valid for sizes in Bytes, KiB, MiB or GiB.
* It will return -1 in any other case. Note that 0KiB and 0MiB and
* 0GiB are considered **invalid** sizes and return -1.
*
* @return the size in bytes, or -1 if it cannot be calculated.
*/
qint64 toBytes() const;
/** @brief Are the units comparable?
*
* None units cannot be compared with anything. Percentages can
* be compared with each other, and all the explicit sizes (KiB, ...)
* can be compared with each other.
*/
static constexpr bool unitsComparable( const SizeUnit u1, const SizeUnit u2 )
{
return !( ( u1 == SizeUnit::None || u2 == SizeUnit::None )
|| ( u1 == SizeUnit::Percent && u2 != SizeUnit::Percent )
|| ( u1 != SizeUnit::Percent && u2 == SizeUnit::Percent ) );
}
};
} // namespace Partition
} // namespace CalamaresUtils
#endif // PARTITION_PARTITIONSIZE_H