diff --git a/src/libcalamares/utils/Units.h b/src/libcalamares/utils/Units.h new file mode 100644 index 000000000..81e2a9bca --- /dev/null +++ b/src/libcalamares/utils/Units.h @@ -0,0 +1,60 @@ +/* === This file is part of Calamares - === + * + * Copyright 2017, Adriaan de Groot + * + * Calamares is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Calamares is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Calamares. If not, see . + */ + +#ifndef LIBCALAMARES_UTILS_UNITS_H +#define LIBCALAMARES_UTILS_UNITS_H + +#include + +namespace CalamaresUtils +{ + +/** User defined literals, 1_MiB is 1 MibiByte (= 2^20 bytes) */ +constexpr qint64 operator ""_MiB( unsigned long long m ) +{ + return qint64(m) * 1024 * 1024; +} + +/** User defined literals, 1_GiB is 1 GibiByte (= 2^30 bytes) */ +constexpr qint64 operator ""_GiB( unsigned long long m ) +{ + return operator ""_MiB(m) * 1024; +} + +constexpr qint64 MiBtoBytes( unsigned long long m ) +{ + return operator ""_MiB( m ); +} + +constexpr qint64 GiBtoBytes( unsigned long long m ) +{ + return operator ""_GiB( m ); +} + +constexpr qint64 MiBToBytes( double m ) +{ + return qint64(m * 1024 * 1024); +} + +constexpr qint64 GiBtoBytes( double m ) +{ + return qint64(m * 1024 * 1024 * 1024); +} + +} // namespace +#endif diff --git a/src/modules/partition/core/PartitionActions.cpp b/src/modules/partition/core/PartitionActions.cpp index 14e7bf010..9567661b2 100644 --- a/src/modules/partition/core/PartitionActions.cpp +++ b/src/modules/partition/core/PartitionActions.cpp @@ -25,6 +25,7 @@ #include "core/PartUtils.h" #include "utils/CalamaresUtilsSystem.h" +#include "utils/Units.h" #include "JobQueue.h" #include "utils/Logger.h" #include "GlobalStorage.h" @@ -36,25 +37,10 @@ namespace PartitionActions { -constexpr qint64 operator ""_MiB( unsigned long long m ) -{ - return m * static_cast< qint64 >( 1024 ) * 1024; -} - -constexpr qint64 operator ""_GiB( unsigned long long m ) -{ - return operator ""_MiB(m) * static_cast< qint64 >( 1024 ); -} - -constexpr qint64 toMiB( unsigned long long m ) -{ - return operator ""_MiB( m ); -} - -constexpr qint64 toGiB( unsigned long long m ) -{ - return operator ""_GiB( m ); -} +using CalamaresUtils::GiBtoBytes; +using CalamaresUtils::MiBtoBytes; +using CalamaresUtils::operator""_GiB; +using CalamaresUtils::operator""_MiB; qint64 swapSuggestion( const qint64 availableSpaceB ) @@ -140,11 +126,11 @@ doAutopartition( PartitionCoreModule* core, Device* dev, const QString& luksPass empty_space_size = 1; } - qint64 firstFreeSector = toMiB(empty_space_size) / dev->logicalSize() + 1; + qint64 firstFreeSector = MiBtoBytes(empty_space_size) / dev->logicalSize() + 1; if ( isEfi ) { - qint64 lastSector = firstFreeSector + ( toMiB(uefisys_part_size) / dev->logicalSize() ); + qint64 lastSector = firstFreeSector + ( MiBtoBytes(uefisys_part_size) / dev->logicalSize() ); core->createPartitionTable( dev, PartitionTable::gpt ); Partition* efiPartition = KPMHelpers::createNewPartition( dev->partitionTable(), @@ -175,7 +161,7 @@ doAutopartition( PartitionCoreModule* core, Device* dev, const QString& luksPass qint64 availableSpaceB = ( dev->totalLogical() - firstFreeSector ) * dev->logicalSize(); suggestedSwapSizeB = swapSuggestion( availableSpaceB ); qint64 requiredSpaceB = - toGiB( gs->value( "requiredStorageGB" ).toDouble() + 0.1 + 2.0 ) + + GiBtoBytes( gs->value( "requiredStorageGB" ).toDouble() + 0.1 + 2.0 ) + suggestedSwapSizeB; // If there is enough room for ESP + root + swap, create swap, otherwise don't. diff --git a/src/modules/partition/tests/PartitionJobTests.cpp b/src/modules/partition/tests/PartitionJobTests.cpp index fe869232b..9f3a33c26 100644 --- a/src/modules/partition/tests/PartitionJobTests.cpp +++ b/src/modules/partition/tests/PartitionJobTests.cpp @@ -19,6 +19,8 @@ #include +#include "utils/Units.h" + #include #include #include @@ -36,9 +38,8 @@ QTEST_GUILESS_MAIN( PartitionJobTests ) -static const qint64 MB = 1024 * 1024; - using namespace Calamares; +using CalamaresUtils::operator""_MiB; class PartitionMounter { @@ -247,7 +248,7 @@ PartitionJobTests::testCreatePartition() freePartition = firstFreePartition( m_device->partitionTable() ); QVERIFY( freePartition ); - job = newCreatePartitionJob( freePartition, PartitionRole( PartitionRole::Primary ), FileSystem::Ext4, 1 * MB); + job = newCreatePartitionJob( freePartition, PartitionRole( PartitionRole::Primary ), FileSystem::Ext4, 1_MiB); Partition* partition1 = job->partition(); QVERIFY( partition1 ); job->updatePreview(); @@ -255,7 +256,7 @@ PartitionJobTests::testCreatePartition() freePartition = firstFreePartition( m_device->partitionTable() ); QVERIFY( freePartition ); - job = newCreatePartitionJob( freePartition, PartitionRole( PartitionRole::Primary ), FileSystem::LinuxSwap, 1 * MB); + job = newCreatePartitionJob( freePartition, PartitionRole( PartitionRole::Primary ), FileSystem::LinuxSwap, 1_MiB); Partition* partition2 = job->partition(); QVERIFY( partition2 ); job->updatePreview(); @@ -263,7 +264,7 @@ PartitionJobTests::testCreatePartition() freePartition = firstFreePartition( m_device->partitionTable() ); QVERIFY( freePartition ); - job = newCreatePartitionJob( freePartition, PartitionRole( PartitionRole::Primary ), FileSystem::Fat32, 1 * MB); + job = newCreatePartitionJob( freePartition, PartitionRole( PartitionRole::Primary ), FileSystem::Fat32, 1_MiB); Partition* partition3 = job->partition(); QVERIFY( partition3 ); job->updatePreview(); @@ -288,7 +289,7 @@ PartitionJobTests::testCreatePartitionExtended() freePartition = firstFreePartition( m_device->partitionTable() ); QVERIFY( freePartition ); - job = newCreatePartitionJob( freePartition, PartitionRole( PartitionRole::Primary ), FileSystem::Ext4, 10 * MB); + job = newCreatePartitionJob( freePartition, PartitionRole( PartitionRole::Primary ), FileSystem::Ext4, 10_MiB); Partition* partition1 = job->partition(); QVERIFY( partition1 ); job->updatePreview(); @@ -296,7 +297,7 @@ PartitionJobTests::testCreatePartitionExtended() freePartition = firstFreePartition( m_device->partitionTable() ); QVERIFY( freePartition ); - job = newCreatePartitionJob( freePartition, PartitionRole( PartitionRole::Extended ), FileSystem::Extended, 10 * MB); + job = newCreatePartitionJob( freePartition, PartitionRole( PartitionRole::Extended ), FileSystem::Extended, 10_MiB); job->updatePreview(); m_queue.enqueue( job_ptr( job ) ); Partition* extendedPartition = job->partition(); @@ -341,7 +342,7 @@ PartitionJobTests::testResizePartition() QFETCH( int, newStartMB ); QFETCH( int, newSizeMB ); - const qint64 sectorForMB = MB / m_device->logicalSize(); + const qint64 sectorForMB = 1_MiB / m_device->logicalSize(); qint64 oldFirst = sectorForMB * oldStartMB; qint64 oldLast = oldFirst + sectorForMB * oldSizeMB - 1; @@ -350,7 +351,7 @@ PartitionJobTests::testResizePartition() // Make the test data file smaller than the full size of the partition to // accomodate for the file system overhead - const QByteArray testData = generateTestData( ( qMin( oldSizeMB, newSizeMB ) ) * MB * 3 / 4 ); + const QByteArray testData = generateTestData( CalamaresUtils::MiBtoBytes( qMin( oldSizeMB, newSizeMB ) ) * 3 / 4 ); const QString testName = "test.data"; // Setup: create the test partition diff --git a/src/modules/welcome/checker/RequirementsChecker.cpp b/src/modules/welcome/checker/RequirementsChecker.cpp index 29112de84..f85f70872 100644 --- a/src/modules/welcome/checker/RequirementsChecker.cpp +++ b/src/modules/welcome/checker/RequirementsChecker.cpp @@ -27,6 +27,8 @@ #include "utils/Logger.h" #include "utils/Retranslator.h" #include "utils/CalamaresUtilsSystem.h" +#include "utils/Units.h" + #include "JobQueue.h" #include "GlobalStorage.h" @@ -77,12 +79,12 @@ RequirementsChecker::RequirementsChecker( QObject* parent ) bool isRoot = false; bool enoughScreen = (availableSize.width() >= CalamaresUtils::windowPreferredWidth) && (availableSize.height() >= CalamaresUtils::windowPreferredHeight); - qint64 requiredStorageB = m_requiredStorageGB * 1073741824L; /*powers of 2*/ + qint64 requiredStorageB = CalamaresUtils::GiBtoBytes(m_requiredStorageGB); cDebug() << "Need at least storage bytes:" << requiredStorageB; if ( m_entriesToCheck.contains( "storage" ) ) enoughStorage = checkEnoughStorage( requiredStorageB ); - qint64 requiredRamB = m_requiredRamGB * 1073741824L; /*powers of 2*/ + qint64 requiredRamB = CalamaresUtils::GiBtoBytes(m_requiredRamGB); cDebug() << "Need at least ram bytes:" << requiredRamB; if ( m_entriesToCheck.contains( "ram" ) ) enoughRam = checkEnoughRam( requiredRamB );