calamares/src/libcalamares/partition/PartitionIterator.cpp

139 lines
3.1 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: 2014 Aurélien Gâteau <agateau@kde.org>
* SPDX-FileCopyrightText: 2015 Teo Mrnjavac <teo@kde.org>
* SPDX-FileCopyrightText: 2017-2019 Adriaan de Groot <groot@kde.org>
* SPDX-License-Identifier: GPL-3.0-or-later
2014-07-22 14:25:21 +02:00
*
* Calamares is Free Software: see the License-Identifier above.
2014-07-22 14:25:21 +02:00
*
[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
*
2014-07-22 14:25:21 +02:00
*/
#include "PartitionIterator.h"
2014-07-22 14:25:21 +02:00
// KPMcore
#include <kpmcore/core/device.h>
#include <kpmcore/core/partition.h>
#include <kpmcore/core/partitiontable.h>
2014-07-22 14:25:21 +02:00
namespace CalamaresUtils
{
namespace Partition
{
using Partition = ::Partition;
PartitionIterator::PartitionIterator( PartitionTable* table )
: m_table( table )
2014-07-22 14:25:21 +02:00
{
}
Partition* PartitionIterator::operator*() const
{
return m_current;
}
2014-07-22 14:25:21 +02:00
void
PartitionIterator::operator++()
{
if ( !m_current )
{
2014-07-22 14:25:21 +02:00
return;
}
2014-07-22 14:25:21 +02:00
if ( m_current->hasChildren() )
{
// Go to the first child
m_current = static_cast< Partition* >( m_current->children().first() );
return;
}
PartitionNode* parent = m_current->parent();
Partition* successor = parent->successor( *m_current );
if ( successor )
{
// Go to the next sibling
m_current = successor;
return;
}
if ( parent->isRoot() )
{
// We reached the end
m_current = nullptr;
return;
}
// Try to go to the next sibling of our parent
PartitionNode* grandParent = parent->parent();
Q_ASSERT( grandParent );
// If parent is not root, then it's not a PartitionTable but a
// Partition, we can static_cast it.
m_current = grandParent->successor( *static_cast< Partition* >( parent ) );
}
bool
PartitionIterator::operator==( const PartitionIterator& other ) const
{
return m_table == other.m_table && m_current == other.m_current;
2014-07-22 14:25:21 +02:00
}
bool
PartitionIterator::operator!=( const PartitionIterator& other ) const
{
return !( *this == other );
2014-07-22 14:25:21 +02:00
}
PartitionIterator
PartitionIterator::begin( Device* device )
{
if ( !device )
{
return PartitionIterator( nullptr );
}
2014-07-22 14:25:21 +02:00
PartitionTable* table = device->partitionTable();
if ( !table )
{
return PartitionIterator( nullptr );
}
return PartitionIterator::begin( table );
}
PartitionIterator
PartitionIterator::begin( PartitionTable* table )
{
auto it = PartitionIterator( table );
2014-07-22 14:25:21 +02:00
QList< Partition* > children = table->children();
// Does not usually happen, but it did happen on a tiny (10MiB) disk with an MBR
2014-07-22 14:25:21 +02:00
// partition table.
if ( children.isEmpty() )
{
2014-07-22 14:25:21 +02:00
return it;
}
2014-07-22 14:25:21 +02:00
it.m_current = children.first();
return it;
}
PartitionIterator
PartitionIterator::end( Device* device )
{
if ( !device )
{
return PartitionIterator( nullptr );
}
PartitionTable* table = device->partitionTable();
if ( !table )
{
return PartitionIterator( nullptr );
}
return PartitionIterator::end( table );
}
PartitionIterator
PartitionIterator::end( PartitionTable* table )
{
return PartitionIterator( table );
2014-07-22 14:25:21 +02:00
}
} // namespace Partition
} // namespace CalamaresUtils