2020-09-20 20:33:16 +01:00
/*
SPDX - FileCopyrightText : 2008 - 2010 Volker Lanz < vl @ fidra . de >
SPDX - FileCopyrightText : 2014 - 2016 Andrius Š tikonas < andrius @ stikonas . eu >
SPDX - FileCopyrightText : 2015 Teo Mrnjavac < teo @ kde . org >
SPDX - License - Identifier : GPL - 3.0 - or - later
*/
2015-06-04 01:29:22 +01:00
# include "core/partitionnode.h"
# include "core/partition.h"
# include "core/partitionrole.h"
# include "fs/filesystem.h"
/** Tries to find the predecessor for a Partition.
2015-07-13 15:16:36 +01:00
@ param p the Partition to find a predecessor for
2015-07-22 14:48:03 +01:00
@ return pointer to the predecessor or nullptr if none was found
2015-06-04 01:29:22 +01:00
*/
Partition * PartitionNode : : predecessor ( Partition & p )
{
2015-07-13 15:16:36 +01:00
Q_ASSERT ( p . parent ( ) ) ;
2015-06-04 01:29:22 +01:00
2015-07-13 15:16:36 +01:00
Partitions & plist = p . parent ( ) - > isRoot ( ) = = false ? p . parent ( ) - > children ( ) : children ( ) ;
2015-06-04 01:29:22 +01:00
2015-07-13 15:16:36 +01:00
for ( int idx = 1 ; idx < plist . size ( ) ; idx + + )
if ( plist [ idx ] = = & p )
return plist [ idx - 1 ] ;
2015-06-04 01:29:22 +01:00
2015-07-22 14:48:03 +01:00
return nullptr ;
2015-06-04 01:29:22 +01:00
}
/**
2015-07-13 15:16:36 +01:00
@ overload
2015-06-04 01:29:22 +01:00
*/
const Partition * PartitionNode : : predecessor ( const Partition & p ) const
{
2015-07-13 15:16:36 +01:00
Q_ASSERT ( p . parent ( ) ) ;
2015-06-04 01:29:22 +01:00
2015-07-13 15:16:36 +01:00
const Partitions & plist = p . parent ( ) - > isRoot ( ) = = false ? p . parent ( ) - > children ( ) : children ( ) ;
2015-06-04 01:29:22 +01:00
2015-07-13 15:16:36 +01:00
for ( int idx = 1 ; idx < plist . size ( ) ; idx + + )
if ( plist [ idx ] = = & p )
return plist [ idx - 1 ] ;
2015-06-04 01:29:22 +01:00
2015-07-22 14:48:03 +01:00
return nullptr ;
2015-06-04 01:29:22 +01:00
}
/** Tries to find the successor for a Partition.
2015-07-13 15:16:36 +01:00
@ param p the Partition to find a successor for
2015-07-22 14:48:03 +01:00
@ return pointer to the successor or nullptr if none was found
2015-06-04 01:29:22 +01:00
*/
Partition * PartitionNode : : successor ( Partition & p )
{
2015-07-13 15:16:36 +01:00
Q_ASSERT ( p . parent ( ) ) ;
2015-06-04 01:29:22 +01:00
2015-07-13 15:16:36 +01:00
Partitions & plist = p . parent ( ) - > isRoot ( ) = = false ? p . parent ( ) - > children ( ) : children ( ) ;
2015-06-04 01:29:22 +01:00
2015-07-13 15:16:36 +01:00
for ( int idx = plist . size ( ) - 2 ; idx > = 0 ; idx - - )
if ( plist [ idx ] = = & p )
return plist [ idx + 1 ] ;
2015-06-04 01:29:22 +01:00
2015-07-22 14:48:03 +01:00
return nullptr ;
2015-06-04 01:29:22 +01:00
}
/**
2015-07-13 15:16:36 +01:00
@ overload
2015-06-04 01:29:22 +01:00
*/
const Partition * PartitionNode : : successor ( const Partition & p ) const
{
2015-07-13 15:16:36 +01:00
Q_ASSERT ( p . parent ( ) ) ;
2015-06-04 01:29:22 +01:00
2015-07-13 15:16:36 +01:00
const Partitions & plist = p . parent ( ) - > isRoot ( ) = = false ? p . parent ( ) - > children ( ) : children ( ) ;
2015-06-04 01:29:22 +01:00
2015-07-13 15:16:36 +01:00
for ( int idx = plist . size ( ) - 2 ; idx > = 0 ; idx - - )
if ( plist [ idx ] = = & p )
return plist [ idx + 1 ] ;
2015-06-04 01:29:22 +01:00
2015-07-22 14:48:03 +01:00
return nullptr ;
2015-06-04 01:29:22 +01:00
}
/** Inserts a Partition into a PartitionNode's children
2015-07-22 14:48:03 +01:00
@ param p pointer to the Partition to insert . May be nullptr .
2015-07-13 15:16:36 +01:00
@ return true on success
2015-06-04 01:29:22 +01:00
*/
bool PartitionNode : : insert ( Partition * p )
{
2015-07-22 14:48:03 +01:00
if ( p = = nullptr )
2015-07-13 15:16:36 +01:00
return false ;
2015-06-04 01:29:22 +01:00
2015-07-13 15:16:36 +01:00
for ( int idx = 0 ; idx < children ( ) . size ( ) ; idx + + ) {
if ( children ( ) [ idx ] - > firstSector ( ) > p - > firstSector ( ) ) {
children ( ) . insert ( idx , p ) ;
return true ;
}
}
2015-06-04 01:29:22 +01:00
2015-07-13 15:16:36 +01:00
children ( ) . insert ( children ( ) . size ( ) , p ) ;
2015-06-04 01:29:22 +01:00
2015-07-13 15:16:36 +01:00
return true ;
2015-06-04 01:29:22 +01:00
}
/** Removes a Partition from the PartitionNode's children.
2015-07-22 14:48:03 +01:00
@ param p pointer to the Partition to remove . May be nullptr .
2015-07-13 15:16:36 +01:00
@ return true on success .
2015-06-04 01:29:22 +01:00
*/
bool PartitionNode : : remove ( Partition * p )
{
2015-07-22 14:48:03 +01:00
if ( p = = nullptr )
2015-07-13 15:16:36 +01:00
return false ;
2015-06-04 01:29:22 +01:00
2016-08-31 20:14:21 +01:00
return children ( ) . removeOne ( p ) ;
2015-06-04 01:29:22 +01:00
}
/** Deletes all children */
void PartitionNode : : clearChildren ( )
{
2015-07-13 15:16:36 +01:00
qDeleteAll ( children ( ) ) ;
children ( ) . clear ( ) ;
2015-06-04 01:29:22 +01:00
}
/** Finds a Partition by sector.
2015-07-13 15:16:36 +01:00
@ param s the sector the Partition is at
@ param role the PartitionRole the Partition is supposed to have
2015-07-22 14:48:03 +01:00
@ return pointer to the Partition found or nullptr if none was found
2015-06-04 01:29:22 +01:00
*/
Partition * PartitionNode : : findPartitionBySector ( qint64 s , const PartitionRole & role )
{
2016-08-11 14:26:54 +01:00
const auto partitions = children ( ) ;
for ( auto & p : partitions ) {
2015-07-13 15:16:36 +01:00
// (women and) children first. ;-)
2016-08-11 14:26:54 +01:00
const auto pChildren = p - > children ( ) ;
for ( auto & child : pChildren )
2016-08-08 18:37:21 +01:00
if ( ( child - > roles ( ) . roles ( ) & role . roles ( ) ) & & s > = child - > firstSector ( ) & & s < = child - > lastSector ( ) )
return child ;
2015-07-13 15:16:36 +01:00
if ( ( p - > roles ( ) . roles ( ) & role . roles ( ) ) & & s > = p - > firstSector ( ) & & s < = p - > lastSector ( ) )
return p ;
}
2015-07-22 14:48:03 +01:00
return nullptr ;
2015-06-04 01:29:22 +01:00
}
/**
2015-07-13 15:16:36 +01:00
@ overload
2015-06-04 01:29:22 +01:00
*/
const Partition * PartitionNode : : findPartitionBySector ( qint64 s , const PartitionRole & role ) const
{
2016-08-08 20:45:41 +01:00
for ( const auto * p : children ( ) ) {
2016-08-11 14:26:54 +01:00
for ( const auto & child : p - > children ( ) )
2016-08-08 18:37:21 +01:00
if ( ( child - > roles ( ) . roles ( ) & role . roles ( ) ) & & s > = child - > firstSector ( ) & & s < = child - > lastSector ( ) )
return child ;
2015-06-04 01:29:22 +01:00
2015-07-13 15:16:36 +01:00
if ( ( p - > roles ( ) . roles ( ) & role . roles ( ) ) & & s > = p - > firstSector ( ) & & s < = p - > lastSector ( ) )
return p ;
}
2015-06-04 01:29:22 +01:00
2015-07-22 14:48:03 +01:00
return nullptr ;
2015-06-04 01:29:22 +01:00
}
/** Reparents a Partition to this PartitionNode
2015-07-13 15:16:36 +01:00
@ param p the Partition to reparent
2015-06-04 01:29:22 +01:00
*/
void PartitionNode : : reparent ( Partition & p )
{
2015-07-13 15:16:36 +01:00
p . setParent ( this ) ;
if ( ! isRoot ( ) )
p . setRoles ( PartitionRole ( PartitionRole : : Logical ) ) ;
else if ( ! p . roles ( ) . has ( PartitionRole : : Extended ) )
p . setRoles ( PartitionRole ( PartitionRole : : Primary ) ) ;
else
p . setRoles ( PartitionRole ( PartitionRole : : Extended ) ) ;
2015-06-04 01:29:22 +01:00
}
/** @return the number of the highest mounted child, e.g. 7 if /dev/sdd7 is a child of this PartitionNode and mounted and /dev/sdd8 and /dev/sdd9 and so on aren't
*/
qint32 PartitionNode : : highestMountedChild ( ) const
{
2015-07-13 15:16:36 +01:00
qint32 result = - 1 ;
2015-06-04 01:29:22 +01:00
2016-08-08 20:45:41 +01:00
for ( const auto * p : children ( ) )
2016-08-08 18:37:21 +01:00
if ( p - > number ( ) > result & & p - > isMounted ( ) )
result = p - > number ( ) ;
2015-06-04 01:29:22 +01:00
2015-07-13 15:16:36 +01:00
return result ;
2015-06-04 01:29:22 +01:00
}
/** @return true if any of the partition's children are mounted */
bool PartitionNode : : isChildMounted ( ) const
{
2016-08-08 20:45:41 +01:00
for ( const auto * child : children ( ) )
2016-08-08 18:37:21 +01:00
if ( child - > isMounted ( ) | | ( child - > hasChildren ( ) & & child - > isChildMounted ( ) ) )
return true ;
2015-06-04 01:29:22 +01:00
2015-07-13 15:16:36 +01:00
return false ;
2015-06-04 01:29:22 +01:00
}