2020-08-25 16:05:56 +02:00
|
|
|
/* === This file is part of Calamares - <https://calamares.io> ===
|
2020-08-20 22:42:19 +02:00
|
|
|
*
|
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>
|
2020-08-20 22:42:19 +02:00
|
|
|
* SPDX-License-Identifier: GPL-3.0-or-later
|
2014-07-22 14:25:21 +02:00
|
|
|
*
|
2020-08-25 16:05:56 +02:00
|
|
|
* Calamares is Free Software: see the License-Identifier above.
|
2014-07-22 14:25:21 +02:00
|
|
|
*
|
2020-05-30 16:15:03 +02:00
|
|
|
*
|
2014-07-22 14:25:21 +02:00
|
|
|
*/
|
|
|
|
|
2018-09-25 12:34:03 +02:00
|
|
|
#include "PartitionIterator.h"
|
2014-07-22 14:25:21 +02:00
|
|
|
|
2015-07-02 13:49: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
|
|
|
|
2019-06-13 12:12:47 +02:00
|
|
|
namespace CalamaresUtils
|
|
|
|
{
|
|
|
|
namespace Partition
|
|
|
|
{
|
|
|
|
|
|
|
|
using Partition = ::Partition;
|
|
|
|
|
2015-04-24 16:51:34 +02:00
|
|
|
PartitionIterator::PartitionIterator( PartitionTable* table )
|
|
|
|
: m_table( table )
|
2014-07-22 14:25:21 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-06-13 12:35:32 +02:00
|
|
|
Partition* PartitionIterator::operator*() const
|
|
|
|
{
|
|
|
|
return m_current;
|
|
|
|
}
|
2019-06-13 12:12:47 +02:00
|
|
|
|
2014-07-22 14:25:21 +02:00
|
|
|
void
|
|
|
|
PartitionIterator::operator++()
|
|
|
|
{
|
|
|
|
if ( !m_current )
|
2019-06-13 12:12:47 +02:00
|
|
|
{
|
2014-07-22 14:25:21 +02:00
|
|
|
return;
|
2019-06-13 12:12:47 +02:00
|
|
|
}
|
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
|
|
|
|
{
|
2015-04-24 16:51:34 +02:00
|
|
|
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
|
|
|
|
{
|
2019-06-13 12:12:47 +02:00
|
|
|
return !( *this == other );
|
2014-07-22 14:25:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
PartitionIterator
|
|
|
|
PartitionIterator::begin( Device* device )
|
|
|
|
{
|
2017-06-14 17:39:53 +02:00
|
|
|
if ( !device )
|
2019-06-13 12:12:47 +02:00
|
|
|
{
|
2017-06-14 17:39:53 +02:00
|
|
|
return PartitionIterator( nullptr );
|
2019-06-13 12:12:47 +02:00
|
|
|
}
|
2014-07-22 14:25:21 +02:00
|
|
|
PartitionTable* table = device->partitionTable();
|
|
|
|
if ( !table )
|
2019-06-13 12:12:47 +02:00
|
|
|
{
|
2015-04-24 16:51:34 +02:00
|
|
|
return PartitionIterator( nullptr );
|
2019-06-13 12:12:47 +02:00
|
|
|
}
|
2015-04-24 16:51:34 +02:00
|
|
|
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();
|
2019-05-09 14:00:23 +02:00
|
|
|
// 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() )
|
2019-06-13 12:12:47 +02:00
|
|
|
{
|
2014-07-22 14:25:21 +02:00
|
|
|
return it;
|
2019-06-13 12:12:47 +02:00
|
|
|
}
|
2014-07-22 14:25:21 +02:00
|
|
|
it.m_current = children.first();
|
|
|
|
return it;
|
|
|
|
}
|
|
|
|
|
|
|
|
PartitionIterator
|
|
|
|
PartitionIterator::end( Device* device )
|
|
|
|
{
|
2017-06-14 17:39:53 +02:00
|
|
|
if ( !device )
|
2019-06-13 12:12:47 +02:00
|
|
|
{
|
2017-06-14 17:39:53 +02:00
|
|
|
return PartitionIterator( nullptr );
|
2019-06-13 12:12:47 +02:00
|
|
|
}
|
2015-04-24 16:51:34 +02:00
|
|
|
PartitionTable* table = device->partitionTable();
|
|
|
|
if ( !table )
|
2019-06-13 12:12:47 +02:00
|
|
|
{
|
2015-04-24 16:51:34 +02:00
|
|
|
return PartitionIterator( nullptr );
|
2019-06-13 12:12:47 +02:00
|
|
|
}
|
2015-04-24 16:51:34 +02:00
|
|
|
|
|
|
|
return PartitionIterator::end( table );
|
|
|
|
}
|
|
|
|
|
|
|
|
PartitionIterator
|
|
|
|
PartitionIterator::end( PartitionTable* table )
|
|
|
|
{
|
|
|
|
return PartitionIterator( table );
|
2014-07-22 14:25:21 +02:00
|
|
|
}
|
2019-06-13 12:12:47 +02:00
|
|
|
|
|
|
|
} // namespace Partition
|
|
|
|
} // namespace CalamaresUtils
|