2015-06-04 01:29:22 +01:00
|
|
|
/*************************************************************************
|
|
|
|
* Copyright (C) 2008, 2010 by Volker Lanz <vl@fidra.de> *
|
2016-03-02 19:00:31 +00:00
|
|
|
* Copyright (C) 2016 by Andrius Štikonas <andrius@stikonas.eu> *
|
2015-06-04 01:29:22 +01:00
|
|
|
* *
|
|
|
|
* This program 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. *
|
|
|
|
* *
|
|
|
|
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.*
|
|
|
|
*************************************************************************/
|
|
|
|
|
|
|
|
#include "ops/deleteoperation.h"
|
|
|
|
|
|
|
|
#include "core/partition.h"
|
|
|
|
#include "core/device.h"
|
|
|
|
#include "core/partitiontable.h"
|
2016-04-26 10:50:44 +01:00
|
|
|
#include "fs/luks.h"
|
2015-06-04 01:29:22 +01:00
|
|
|
|
|
|
|
#include "jobs/deletepartitionjob.h"
|
|
|
|
#include "jobs/deletefilesystemjob.h"
|
|
|
|
#include "jobs/shredfilesystemjob.h"
|
|
|
|
|
|
|
|
#include "util/capacity.h"
|
|
|
|
|
|
|
|
#include <QString>
|
|
|
|
|
|
|
|
#include <KLocalizedString>
|
|
|
|
|
|
|
|
/** Creates a new DeleteOperation
|
2015-07-13 15:16:36 +01:00
|
|
|
@param d the Device to delete a Partition on
|
2015-07-22 14:48:03 +01:00
|
|
|
@param p pointer to the Partition to delete. May not be nullptr
|
2015-06-04 01:29:22 +01:00
|
|
|
*/
|
2015-07-10 14:56:12 +01:00
|
|
|
DeleteOperation::DeleteOperation(Device& d, Partition* p, ShredAction shred) :
|
2015-07-13 15:16:36 +01:00
|
|
|
Operation(),
|
|
|
|
m_TargetDevice(d),
|
|
|
|
m_DeletedPartition(p),
|
2015-07-10 14:56:12 +01:00
|
|
|
m_ShredAction(shred),
|
2015-07-13 15:16:36 +01:00
|
|
|
m_DeletePartitionJob(new DeletePartitionJob(targetDevice(), deletedPartition()))
|
2015-06-04 01:29:22 +01:00
|
|
|
{
|
2015-07-13 15:16:36 +01:00
|
|
|
switch (shredAction()) {
|
2015-07-10 14:56:12 +01:00
|
|
|
case NoShred:
|
|
|
|
m_DeleteFileSystemJob = static_cast<Job*>(new DeleteFileSystemJob(targetDevice(), deletedPartition()));
|
2015-07-23 16:36:55 +01:00
|
|
|
break;
|
2015-07-10 14:56:12 +01:00
|
|
|
case ZeroShred:
|
|
|
|
m_DeleteFileSystemJob = static_cast<Job*>(new ShredFileSystemJob(targetDevice(), deletedPartition(), false));
|
2015-07-23 16:36:55 +01:00
|
|
|
break;
|
2015-07-10 14:56:12 +01:00
|
|
|
case RandomShred:
|
|
|
|
m_DeleteFileSystemJob = static_cast<Job*>(new ShredFileSystemJob(targetDevice(), deletedPartition(), true));
|
|
|
|
}
|
|
|
|
|
2015-07-13 15:16:36 +01:00
|
|
|
addJob(deleteFileSystemJob());
|
|
|
|
addJob(deletePartitionJob());
|
2015-06-04 01:29:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
DeleteOperation::~DeleteOperation()
|
|
|
|
{
|
2015-07-13 15:16:36 +01:00
|
|
|
if (status() != StatusPending && status() != StatusNone) // don't delete the partition if we're being merged or undone
|
|
|
|
delete m_DeletedPartition;
|
2015-06-04 01:29:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool DeleteOperation::targets(const Device& d) const
|
|
|
|
{
|
2015-07-13 15:16:36 +01:00
|
|
|
return d == targetDevice();
|
2015-06-04 01:29:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool DeleteOperation::targets(const Partition& p) const
|
|
|
|
{
|
2015-07-13 15:16:36 +01:00
|
|
|
return p == deletedPartition();
|
2015-06-04 01:29:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void DeleteOperation::preview()
|
|
|
|
{
|
2015-07-13 15:16:36 +01:00
|
|
|
removePreviewPartition(targetDevice(), deletedPartition());
|
|
|
|
checkAdjustLogicalNumbers(deletedPartition(), false);
|
2015-06-04 01:29:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void DeleteOperation::undo()
|
|
|
|
{
|
2015-07-13 15:16:36 +01:00
|
|
|
checkAdjustLogicalNumbers(deletedPartition(), true);
|
|
|
|
insertPreviewPartition(targetDevice(), deletedPartition());
|
2015-06-04 01:29:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QString DeleteOperation::description() const
|
|
|
|
{
|
2015-07-10 14:56:12 +01:00
|
|
|
if (shredAction() != NoShred)
|
2016-07-17 23:41:00 +01:00
|
|
|
return xi18nc("@info:status", "Shred partition <filename>%1</filename> (%2, %3)", deletedPartition().deviceNode(), Capacity::formatByteSize(deletedPartition().capacity()), deletedPartition().fileSystem().name());
|
2015-07-13 15:16:36 +01:00
|
|
|
else
|
2016-07-17 23:41:00 +01:00
|
|
|
return xi18nc("@info:status", "Delete partition <filename>%1</filename> (%2, %3)", deletedPartition().deviceNode(), Capacity::formatByteSize(deletedPartition().capacity()), deletedPartition().fileSystem().name());
|
2015-06-04 01:29:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void DeleteOperation::checkAdjustLogicalNumbers(Partition& p, bool undo)
|
|
|
|
{
|
2015-07-13 15:16:36 +01:00
|
|
|
// If the deleted partition is a logical one, we need to adjust the numbers of the
|
|
|
|
// other logical partitions in the extended one, if there are any, because the OS
|
|
|
|
// will do that, too: Logicals must be numbered without gaps, i.e., a numbering like
|
|
|
|
// sda5, sda6, sda8 (after sda7 is deleted) will become sda5, sda6, sda7
|
|
|
|
Partition* parentPartition = dynamic_cast<Partition*>(p.parent());
|
|
|
|
if (parentPartition && parentPartition->roles().has(PartitionRole::Extended))
|
|
|
|
parentPartition->adjustLogicalNumbers(undo ? -1 : p.number(), undo ? p.number() : -1);
|
2015-06-04 01:29:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Can a Partition be deleted?
|
2015-07-22 14:48:03 +01:00
|
|
|
@param p the Partition in question, may be nullptr.
|
2015-07-13 15:16:36 +01:00
|
|
|
@return true if @p p can be deleted.
|
2015-06-04 01:29:22 +01:00
|
|
|
*/
|
|
|
|
bool DeleteOperation::canDelete(const 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
|
|
|
if (p->isMounted())
|
|
|
|
return false;
|
2015-06-04 01:29:22 +01:00
|
|
|
|
2015-07-13 15:16:36 +01:00
|
|
|
if (p->roles().has(PartitionRole::Unallocated))
|
|
|
|
return false;
|
2015-06-04 01:29:22 +01:00
|
|
|
|
2015-07-13 15:16:36 +01:00
|
|
|
if (p->roles().has(PartitionRole::Extended))
|
|
|
|
return p->children().size() == 1 && p->children()[0]->roles().has(PartitionRole::Unallocated);
|
2015-06-04 01:29:22 +01:00
|
|
|
|
2016-04-26 10:50:44 +01:00
|
|
|
if (p->roles().has(PartitionRole::Luks))
|
|
|
|
{
|
|
|
|
const FileSystem& fsRef = p->fileSystem();
|
|
|
|
const FS::luks* luksFs = dynamic_cast<const FS::luks*>(&fsRef);
|
|
|
|
if (!luksFs)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (luksFs->isCryptOpen() || luksFs->isMounted())
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-07-13 15:16:36 +01:00
|
|
|
return true;
|
2015-06-04 01:29:22 +01:00
|
|
|
}
|