1
/***************************************************************************
2
* Copyright (C) 2008 by Volker Lanz <vl@fidra.de> *
4
* This program is free software; you can redistribute it and/or modify *
5
* it under the terms of the GNU General Public License as published by *
6
* the Free Software Foundation; either version 2 of the License, or *
7
* (at your option) any later version. *
9
* This program is distributed in the hope that it will be useful, *
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12
* GNU General Public License for more details. *
14
* You should have received a copy of the GNU General Public License *
15
* along with this program; if not, write to the *
16
* Free Software Foundation, Inc., *
17
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18
***************************************************************************/
20
#include "core/partitionnode.h"
22
#include "core/partition.h"
23
#include "core/partitionrole.h"
25
#include "fs/filesystem.h"
30
/** Tries to find the predecessor for a Partition.
31
@param p the Partition to find a predecessor for
32
@return pointer to the predecessor or NULL if none was found
34
Partition* PartitionNode::predecessor(Partition& p)
38
Partitions& plist = p.parent()->isRoot() == false ? p.parent()->children() : children();
40
for (int idx = 1; idx < plist.size(); idx++)
42
return plist[idx - 1];
50
const Partition* PartitionNode::predecessor(const Partition& p) const
54
const Partitions& plist = p.parent()->isRoot() == false ? p.parent()->children() : children();
56
for (int idx = 1; idx < plist.size(); idx++)
58
return plist[idx - 1];
63
/** Tries to find the successor for a Partition.
64
@param p the Partition to find a successor for
65
@return pointer to the successor or NULL if none was found
67
Partition* PartitionNode::successor(Partition& p)
71
Partitions& plist = p.parent()->isRoot() == false ? p.parent()->children() : children();
73
for (int idx = plist.size() - 2; idx >= 0; idx--)
75
return plist[idx + 1];
83
const Partition* PartitionNode::successor(const Partition& p) const
87
const Partitions& plist = p.parent()->isRoot() == false ? p.parent()->children() : children();
89
for (int idx = plist.size() - 2; idx >= 0; idx--)
91
return plist[idx + 1];
96
/** Inserts a Partition into a PartitionNode's children
97
@param p pointer to the Partition to insert. May be NULL.
98
@return true on success
100
bool PartitionNode::insert(Partition* p)
105
for (int idx = 0; idx < children().size(); idx++)
107
if (children()[idx]->firstSector() > p->firstSector())
109
children().insert(idx, p);
114
children().insert(children().size(), p);
119
/** Removes a Partition from the PartitionNode's children.
120
@param p pointer to the Partition to remove. May be NULL.
121
@return true on success.
123
bool PartitionNode::remove(Partition* p)
128
if (children().removeOne(p))
134
/** Deletes all children */
135
void PartitionNode::clearChildren()
137
qDeleteAll(children());
141
/** Finds a Partition by sector.
142
@param s the sector the Partition is at
143
@param role the PartitionRole the Partition is supposed to have
144
@return pointer to the Partition found or NULL if none was found
146
Partition* PartitionNode::findPartitionBySector(qint64 s, const PartitionRole& role)
148
foreach (Partition* p, children())
150
// (women and) children first. ;-)
151
foreach (Partition* child, p->children())
152
if ((child->roles().roles() & role.roles()) && s >= child->firstSector() && s <= child->lastSector())
155
if ((p->roles().roles() & role.roles()) && s >= p->firstSector() && s <= p->lastSector())
165
const Partition* PartitionNode::findPartitionBySector(qint64 s, const PartitionRole& role) const
167
foreach (const Partition* p, children())
169
foreach (const Partition* child, p->children())
170
if ((child->roles().roles() & role.roles()) && s >= child->firstSector() && s <= child->lastSector())
173
if ((p->roles().roles() & role.roles()) && s >= p->firstSector() && s <= p->lastSector())
180
/** Reparents a Partition to this PartitionNode
181
@param p the Partition to reparent
183
void PartitionNode::reparent(Partition& p)
188
p.setRoles(PartitionRole(PartitionRole::Logical));
189
else if (!p.roles().has(PartitionRole::Extended))
190
p.setRoles(PartitionRole(PartitionRole::Primary));
192
p.setRoles(PartitionRole(PartitionRole::Extended));
195
/** @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
197
qint32 PartitionNode::highestMountedChild() const
201
foreach (const Partition* p, children())
202
if (p->number() > result && p->isMounted())
203
result = p->number();
208
/** @return true if any of the partition's children are mounted */
209
bool PartitionNode::isChildMounted() const
211
foreach (const Partition* child, children())
212
if (child->isMounted() || (child->hasChildren() && child->isChildMounted()))