[libcalamares] Move partition-finding into libcalamares

- Moved from KPMHelpers to the partition service
 - The is-partition and find-partition methods that make sense
   in general, are moved to libcalamares.
This commit is contained in:
Adriaan de Groot 2019-06-13 14:41:33 +02:00
parent 3930826e93
commit 4b3bb54320
14 changed files with 221 additions and 94 deletions

View File

@ -117,6 +117,7 @@ if ( KPMcore_FOUND )
include_directories( ${KPMCORE_INCLUDE_DIR} )
list( APPEND libSources
partition/PartitionIterator.cpp
partition/PartitionQuery.cpp
)
list( APPEND OPTIONAL_PRIVATE_LIBRARIES kpmcore )
endif()

View File

@ -0,0 +1,95 @@
/* === This file is part of Calamares - <https://github.com/calamares> ===
*
* Copyright 2014, Aurélien Gâteau <agateau@kde.org>
* Copyright 2015-2016, Teo Mrnjavac <teo@kde.org>
* Copyright 2018-2019 Adriaan de Groot <groot@kde.org>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include "PartitionQuery.h"
#include "PartitionIterator.h"
#include <kpmcore/core/device.h>
#include <kpmcore/core/partition.h>
namespace CalamaresUtils
{
namespace Partition
{
// Types from KPMCore
using ::Device;
using ::Partition;
bool
isPartitionFreeSpace( Partition* partition )
{
return partition->roles().has( PartitionRole::Unallocated );
}
bool
isPartitionNew( Partition* partition )
{
#if defined( WITH_KPMCORE4API )
constexpr auto NewState = Partition::State::New;
#else
constexpr auto NewState = Partition::StateNew;
#endif
return partition->state() == NewState;
}
Partition*
findPartitionByCurrentMountPoint( const QList< Device* >& devices, const QString& mountPoint )
{
for ( auto device : devices )
for ( auto it = PartitionIterator::begin( device ); it != PartitionIterator::end( device ); ++it )
if ( (*it)->mountPoint() == mountPoint )
return *it;
return nullptr;
}
Partition*
findPartitionByPath( const QList< Device* >& devices, const QString& path )
{
if ( path.simplified().isEmpty() )
return nullptr;
for ( auto device : devices )
for ( auto it = PartitionIterator::begin( device ); it != PartitionIterator::end( device ); ++it )
if ( ( *it )->partitionPath() == path.simplified() )
return *it;
return nullptr;
}
QList< Partition* >
findPartitions( const QList< Device* >& devices,
std::function< bool ( Partition* ) > criterionFunction )
{
QList< Partition* > results;
for ( auto device : devices )
for ( auto it = PartitionIterator::begin( device ); it != PartitionIterator::end( device ); ++it )
if ( criterionFunction( *it ) )
results.append( *it );
return results;
}
}
} // namespace

View File

@ -0,0 +1,70 @@
/* === This file is part of Calamares - <https://github.com/calamares> ===
*
* Copyright 2014, Aurélien Gâteau <agateau@kde.org>
* Copyright 2015-2016, Teo Mrnjavac <teo@kde.org>
* Copyright 2019, Adriaan de Groot <groot@kde.org>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#ifndef PARTITION_PARTITIONQUERY_H
#define PARTITION_PARTITIONQUERY_H
#include <kpmcore/fs/filesystem.h>
#include <QList>
#include <functional>
class Device;
class Partition;
namespace CalamaresUtils
{
namespace Partition
{
/** @brief Is this a free-space area? */
bool isPartitionFreeSpace( ::Partition* );
/** @brief Is this partition newly-to-be-created?
*
* Returns true if the partition is planned to be created by the installer as
* opposed to already existing on the disk.
*/
bool isPartitionNew( ::Partition* );
/**
* Iterates on all devices and return the first partition which is (already)
* mounted on @p mountPoint.
*/
::Partition* findPartitionByCurrentMountPoint( const QList< ::Device* >& devices, const QString& mountPoint );
// TODO: add this distinction
// ::Partition* findPartitionByIntendedMountPoint( const QList< ::Device* >& devices, const QString& mountPoint );
/**
* Iterates on all devices and partitions and returns a pointer to the Partition object
* for the given path, or nullptr if a Partition for the given path cannot be found.
*/
::Partition* findPartitionByPath( const QList< ::Device* >& devices, const QString& path );
/**
* Iterates on all devices and partitions and returns a list of pointers to the Partition
* objects that satisfy the conditions defined in the criterion function.
*/
QList< ::Partition* > findPartitions( const QList< ::Device* >& devices,
std::function< bool ( ::Partition* ) > criterionFunction );
}
}
#endif // PARTITION_PARTITIONQUERY_H

View File

@ -22,6 +22,7 @@
#include "core/KPMHelpers.h"
#include "partition/PartitionIterator.h"
#include "partition/PartitionQuery.h"
#include "utils/Logger.h"
// KPMcore
@ -33,6 +34,8 @@
#include <QMap>
using CalamaresUtils::Partition::PartitionIterator;
using CalamaresUtils::Partition::isPartitionFreeSpace;
using CalamaresUtils::Partition::isPartitionNew;
static const int NUM_PARTITION_COLORS = 5;
static const int NUM_NEW_PARTITION_COLORS = 4;
@ -91,7 +94,7 @@ colorForPartition( Partition* partition )
return FREE_SPACE_COLOR;
}
if ( KPMHelpers::isPartitionFreeSpace( partition ) )
if ( isPartitionFreeSpace( partition ) )
return FREE_SPACE_COLOR;
if ( partition->roles().has( PartitionRole::Extended ) )
return EXTENDED_COLOR;
@ -126,16 +129,16 @@ colorForPartition( Partition* partition )
Partition* child = *it;
if ( child == partition )
break;
if ( !KPMHelpers::isPartitionFreeSpace( child ) &&
if ( !isPartitionFreeSpace( child ) &&
!child->hasChildren() )
{
if ( KPMHelpers::isPartitionNew( child ) )
if ( isPartitionNew( child ) )
++newColorIdx;
++colorIdx;
}
}
if ( KPMHelpers::isPartitionNew( partition ) )
if ( isPartitionNew( partition ) )
return NEW_PARTITION_COLORS[ newColorIdx % NUM_NEW_PARTITION_COLORS ];
if ( partition->fileSystem().supportGetUUID() != FileSystem::cmdSupportNone &&
@ -172,9 +175,9 @@ colorForPartitionInFreeSpace( Partition* partition )
Partition* child = *it;
if ( child == partition )
break;
if ( !KPMHelpers::isPartitionFreeSpace( child ) &&
if ( !isPartitionFreeSpace( child ) &&
!child->hasChildren() &&
KPMHelpers::isPartitionNew( child ) )
isPartitionNew( child ) )
++newColorIdx;
}
return NEW_PARTITION_COLORS[ newColorIdx % NUM_NEW_PARTITION_COLORS ];

View File

@ -56,20 +56,6 @@ initKPMcore()
}
bool
isPartitionFreeSpace( Partition* partition )
{
return partition->roles().has( PartitionRole::Unallocated );
}
bool
isPartitionNew( Partition* partition )
{
return partition->state() == KPM_PARTITION_STATE(New);
}
Partition*
findPartitionByMountPoint( const QList< Device* >& devices, const QString& mountPoint )
{
@ -81,33 +67,6 @@ findPartitionByMountPoint( const QList< Device* >& devices, const QString& mount
}
Partition*
findPartitionByPath( const QList< Device* >& devices, const QString& path )
{
if ( path.simplified().isEmpty() )
return nullptr;
for ( auto device : devices )
for ( auto it = PartitionIterator::begin( device ); it != PartitionIterator::end( device ); ++it )
if ( ( *it )->partitionPath() == path.simplified() )
return *it;
return nullptr;
}
QList< Partition* >
findPartitions( const QList< Device* >& devices,
std::function< bool ( Partition* ) > criterionFunction )
{
QList< Partition* > results;
for ( auto device : devices )
for ( auto it = PartitionIterator::begin( device ); it != PartitionIterator::end( device ); ++it )
if ( criterionFunction( *it ) )
results.append( *it );
return results;
}
Partition*
createNewPartition( PartitionNode* parent,
const Device& device,

View File

@ -63,33 +63,12 @@ namespace KPMHelpers
*/
bool initKPMcore();
bool isPartitionFreeSpace( Partition* );
/**
* Returns true if the partition is planned to be created by the installer as
* opposed to already existing on the disk.
*/
bool isPartitionNew( Partition* );
/**
* Iterates on all devices and return the first partition which is associated
* with mountPoint. This uses PartitionInfo::mountPoint(), not Partition::mountPoint()
*/
Partition* findPartitionByMountPoint( const QList< Device* >& devices, const QString& mountPoint );
/**
* Iterates on all devices and partitions and returns a pointer to the Partition object
* for the given path, or nullptr if a Partition for the given path cannot be found.
*/
Partition* findPartitionByPath( const QList< Device* >& devices, const QString& path );
/**
* Iterates on all devices and partitions and returns a list of pointers to the Partition
* objects that satisfy the conditions defined in the criterion function.
*/
QList< Partition* > findPartitions( const QList< Device* >& devices,
std::function< bool ( Partition* ) > criterionFunction );
/**
* Helper function to create a new Partition object (does not create anything
* on the disk) associated with a FileSystem.

View File

@ -29,6 +29,7 @@
#include "GlobalStorage.h"
#include "JobQueue.h"
#include "partition/PartitionIterator.h"
#include "partition/PartitionQuery.h"
#include "utils/CalamaresUtilsSystem.h"
#include "utils/Logger.h"
@ -37,10 +38,12 @@
#include <kpmcore/core/device.h>
#include <kpmcore/core/partition.h>
#include <QProcess>
#include <QTemporaryDir>
using CalamaresUtils::Partition::isPartitionFreeSpace;
using CalamaresUtils::Partition::isPartitionNew;
namespace PartUtils
{
@ -133,7 +136,7 @@ canBeResized( Partition* candidate )
return false;
}
if ( KPMHelpers::isPartitionFreeSpace( candidate ) )
if ( isPartitionFreeSpace( candidate ) )
{
cDebug() << Logger::SubEntry << "NO, partition is free space";
return false;
@ -206,7 +209,7 @@ canBeResized( PartitionCoreModule* core, const QString& partitionPath )
for ( int i = 0; i < dm->rowCount(); ++i )
{
Device* dev = dm->deviceForIndex( dm->index( i ) );
Partition* candidate = KPMHelpers::findPartitionByPath( { dev }, partitionWithOs );
Partition* candidate = CalamaresUtils::Partition::findPartitionByPath( { dev }, partitionWithOs );
if ( candidate )
{
return canBeResized( candidate );

View File

@ -48,6 +48,7 @@
#include "JobExample.h"
#endif
#include "partition/PartitionIterator.h"
#include "partition/PartitionQuery.h"
#include "utils/Logger.h"
#include "utils/Variant.h"
@ -70,6 +71,8 @@
#include <QtConcurrent/QtConcurrent>
using CalamaresUtils::Partition::PartitionIterator;
using CalamaresUtils::Partition::isPartitionFreeSpace;
using CalamaresUtils::Partition::isPartitionNew;
PartitionCoreModule::RefreshHelper::RefreshHelper(PartitionCoreModule* module)
: m_module( module )
@ -395,7 +398,7 @@ PartitionCoreModule::deletePartition( Device* device, Partition* partition )
// deleting them, so let's play it safe and keep our own list.
QList< Partition* > lst;
for ( auto childPartition : partition->children() )
if ( !KPMHelpers::isPartitionFreeSpace( childPartition ) )
if ( !isPartitionFreeSpace( childPartition ) )
lst << childPartition;
for ( auto childPartition : lst )
@ -653,7 +656,7 @@ PartitionCoreModule::scanForEfiSystemPartitions()
}
QList< Partition* > efiSystemPartitions =
KPMHelpers::findPartitions( devices, PartUtils::isEfiBootable );
CalamaresUtils::Partition::findPartitions( devices, PartUtils::isEfiBootable );
if ( efiSystemPartitions.isEmpty() )
cWarning() << "system is EFI but no EFI system partitions found.";

View File

@ -22,6 +22,8 @@
#include "core/ColorUtils.h"
#include "core/PartitionInfo.h"
#include "core/KPMHelpers.h"
#include "partition/PartitionQuery.h"
#include "utils/Logger.h"
// CalaPM
@ -36,6 +38,9 @@
// Qt
#include <QColor>
using CalamaresUtils::Partition::isPartitionFreeSpace;
using CalamaresUtils::Partition::isPartitionNew;
//- ResetHelper --------------------------------------------
PartitionModel::ResetHelper::ResetHelper( PartitionModel* model )
: m_model( model )
@ -140,11 +145,11 @@ PartitionModel::data( const QModelIndex& index, int role ) const
int col = index.column();
if ( col == NameColumn )
{
if ( KPMHelpers::isPartitionFreeSpace( partition ) )
if ( isPartitionFreeSpace( partition ) )
return tr( "Free Space" );
else
{
return KPMHelpers::isPartitionNew( partition )
return isPartitionNew( partition )
? tr( "New partition" )
: partition->partitionPath();
}
@ -172,11 +177,11 @@ PartitionModel::data( const QModelIndex& index, int role ) const
QString name;
if ( col == NameColumn )
{
if ( KPMHelpers::isPartitionFreeSpace( partition ) )
if ( isPartitionFreeSpace( partition ) )
name = tr( "Free Space" );
else
{
name = KPMHelpers::isPartitionNew( partition )
name = isPartitionNew( partition )
? tr( "New partition" )
: partition->partitionPath();
}
@ -189,10 +194,10 @@ PartitionModel::data( const QModelIndex& index, int role ) const
case SizeRole:
return ( partition->lastSector() - partition->firstSector() + 1 ) * m_device->logicalSize();
case IsFreeSpaceRole:
return KPMHelpers::isPartitionFreeSpace( partition );
return isPartitionFreeSpace( partition );
case IsPartitionNewRole:
return KPMHelpers::isPartitionNew( partition );
return isPartitionNew( partition );
case FileSystemLabelRole:
if ( partition->fileSystem().supportGetLabel() != FileSystem::cmdSupportNone &&

View File

@ -42,6 +42,7 @@
#include "GlobalStorage.h"
#include "JobQueue.h"
#include "partition/PartitionIterator.h"
#include "partition/PartitionQuery.h"
#include "utils/Logger.h"
#include "utils/Retranslator.h"
#include "utils/Units.h"
@ -66,6 +67,8 @@
using PartitionActions::Choices::SwapChoice;
using CalamaresUtils::Partition::PartitionIterator;
using CalamaresUtils::Partition::isPartitionFreeSpace;
using CalamaresUtils::Partition::findPartitionByPath;
/** @brief Given a set of swap choices, return a sensible value from it.
*
@ -692,7 +695,7 @@ ChoicePage::doAlongsideApply()
for ( int i = 0; i < dm->rowCount(); ++i )
{
Device* dev = dm->deviceForIndex( dm->index( i ) );
Partition* candidate = KPMHelpers::findPartitionByPath( { dev }, path );
Partition* candidate = findPartitionByPath( { dev }, path );
if ( candidate )
{
qint64 firstSector = candidate->firstSector();
@ -755,7 +758,7 @@ ChoicePage::doReplaceSelectedPartition( const QModelIndex& current )
Partition* selectedPartition =
static_cast< Partition* >( current.data( PartitionModel::PartitionPtrRole )
.value< void* >() );
if ( KPMHelpers::isPartitionFreeSpace( selectedPartition ) )
if ( isPartitionFreeSpace( selectedPartition ) )
{
//NOTE: if the selected partition is free space, we don't deal with
// a separate /home partition at all because there's no existing
@ -769,7 +772,7 @@ ChoicePage::doReplaceSelectedPartition( const QModelIndex& current )
if ( parent && parent->roles().has( PartitionRole::Extended ) )
{
newRoles = PartitionRole( PartitionRole::Logical );
newParent = KPMHelpers::findPartitionByPath( { selectedDevice() }, parent->partitionPath() );
newParent = findPartitionByPath( { selectedDevice() }, parent->partitionPath() );
}
}
@ -783,7 +786,7 @@ ChoicePage::doReplaceSelectedPartition( const QModelIndex& current )
// We can't use the PartitionPtrRole because we need to make changes to the
// main DeviceModel, not the immutable copy.
QString partPath = current.data( PartitionModel::PartitionPathRole ).toString();
selectedPartition = KPMHelpers::findPartitionByPath( { selectedDevice() },
selectedPartition = findPartitionByPath( { selectedDevice() },
partPath );
if ( selectedPartition )
{
@ -806,7 +809,7 @@ ChoicePage::doReplaceSelectedPartition( const QModelIndex& current )
gs->value( "defaultFileSystemType" ).toString(),
m_encryptWidget->passphrase()
} );
Partition* homePartition = KPMHelpers::findPartitionByPath( { selectedDevice() },
Partition* homePartition = findPartitionByPath( { selectedDevice() },
*homePartitionPath );
if ( homePartition && doReuseHomePartition )

View File

@ -31,9 +31,10 @@
#include "ui_CreatePartitionDialog.h"
#include "utils/Logger.h"
#include "GlobalStorage.h"
#include "JobQueue.h"
#include "partition/PartitionQuery.h"
#include "utils/Logger.h"
// KPMcore
#include <kpmcore/core/device.h>
@ -272,7 +273,7 @@ CreatePartitionDialog::checkMountPointSelection()
void
CreatePartitionDialog::initPartResizerWidget( Partition* partition )
{
QColor color = KPMHelpers::isPartitionFreeSpace( partition )
QColor color = CalamaresUtils::Partition::isPartitionFreeSpace( partition )
? ColorUtils::colorForPartitionInFreeSpace( partition )
: ColorUtils::colorForPartition( partition );
m_partitionSizeController->init( m_device, partition, color );

View File

@ -40,11 +40,13 @@
#include "ui_PartitionPage.h"
#include "ui_CreatePartitionTableDialog.h"
#include "GlobalStorage.h"
#include "JobQueue.h"
#include "partition/PartitionQuery.h"
#include "utils/Logger.h"
#include "utils/Retranslator.h"
#include "Branding.h"
#include "JobQueue.h"
#include "GlobalStorage.h"
// KPMcore
#include <kpmcore/core/device.h>
@ -132,7 +134,7 @@ PartitionPage::updateButtons()
Q_ASSERT( model );
Partition* partition = model->partitionForIndex( index );
Q_ASSERT( partition );
bool isFree = KPMHelpers::isPartitionFreeSpace( partition );
bool isFree = CalamaresUtils::Partition::isPartitionFreeSpace( partition );
bool isExtended = partition->roles().has( PartitionRole::Extended );
bool isInVG = m_core->isInVG( partition );
@ -392,7 +394,7 @@ PartitionPage::onEditClicked()
Partition* partition = model->partitionForIndex( index );
Q_ASSERT( partition );
if ( KPMHelpers::isPartitionNew( partition ) )
if ( CalamaresUtils::Partition::isPartitionNew( partition ) )
updatePartitionToCreate( model->device(), partition );
else
editExistingPartition( model->device(), partition );
@ -452,7 +454,7 @@ PartitionPage::onPartitionViewActivated()
// but I don't expect there will be other occurences of triggering the same
// action from multiple UI elements in this page, so it does not feel worth
// the price.
if ( KPMHelpers::isPartitionFreeSpace( partition ) )
if ( CalamaresUtils::Partition::isPartitionFreeSpace( partition ) )
m_ui->createButton->click();
else
m_ui->editButton->click();

View File

@ -21,8 +21,9 @@
#include "core/ColorUtils.h"
#include "core/KPMHelpers.h"
#include "utils/Logger.h"
#include "partition/PartitionIterator.h"
#include "partition/PartitionQuery.h"
#include "utils/Logger.h"
#include "utils/CalamaresUtilsGui.h"
@ -69,7 +70,7 @@ PartitionSplitterWidget::init( Device* dev, bool drawNestedPartitions )
PartitionSplitterItem newItem = {
( *it )->partitionPath(),
ColorUtils::colorForPartition( *it ),
KPMHelpers::isPartitionFreeSpace( *it ),
CalamaresUtils::Partition::isPartitionFreeSpace( *it ),
( *it )->capacity(),
PartitionSplitterItem::Normal,
{}

View File

@ -19,6 +19,7 @@
#include <PartitionJobTests.h>
#include "partition/PartitionQuery.h"
#include "utils/Units.h"
#include <jobs/CreatePartitionJob.h>
@ -40,6 +41,7 @@ QTEST_GUILESS_MAIN( PartitionJobTests )
using namespace Calamares;
using CalamaresUtils::operator""_MiB;
using CalamaresUtils::Partition::isPartitionFreeSpace;
class PartitionMounter
{
@ -115,7 +117,7 @@ static Partition*
firstFreePartition( PartitionNode* parent )
{
for( auto child : parent->children() )
if ( KPMHelpers::isPartitionFreeSpace( child ) )
if ( isPartitionFreeSpace( child ) )
return child;
return nullptr;
}