File indexing completed on 2024-04-28 05:45:53

0001 /*
0002     SPDX-FileCopyrightText: 2008-2010 Volker Lanz <vl@fidra.de>
0003     SPDX-FileCopyrightText: 2014-2018 Andrius Štikonas <andrius@stikonas.eu>
0004     SPDX-FileCopyrightText: 2016 Chantara Tith <tith.chantara@gmail.com>
0005     SPDX-FileCopyrightText: 2018 Caio Jordão Carvalho <caiojcarvalho@gmail.com>
0006 
0007     SPDX-License-Identifier: GPL-3.0-or-later
0008 */
0009 
0010 #include "jobs/deletepartitionjob.h"
0011 
0012 #include "backend/corebackend.h"
0013 #include "backend/corebackendmanager.h"
0014 #include "backend/corebackenddevice.h"
0015 #include "backend/corebackendpartitiontable.h"
0016 
0017 #include "core/partition.h"
0018 #include "core/device.h"
0019 #include "core/lvmdevice.h"
0020 
0021 #include "util/report.h"
0022 
0023 #include <QDebug>
0024 
0025 #include <KLocalizedString>
0026 
0027 /** Creates a new DeletePartitionJob
0028     @param d the Device the Partition to delete is on
0029     @param p the Partition to delete
0030 */
0031 DeletePartitionJob::DeletePartitionJob(Device& d, Partition& p) :
0032     Job(),
0033     m_Device(d),
0034     m_Partition(p)
0035 {
0036 }
0037 
0038 bool DeletePartitionJob::run(Report& parent)
0039 {
0040     Q_ASSERT(device().deviceNode() == partition().devicePath());
0041 
0042     if (device().deviceNode() != partition().devicePath()) {
0043         qWarning() << "deviceNode: " << device().deviceNode() << ", partition path: " << partition().devicePath();
0044         return false;
0045     }
0046 
0047     bool rval = false;
0048 
0049     Report* report = jobStarted(parent);
0050 
0051     if (device().type() == Device::Type::Disk_Device || device().type() == Device::Type::SoftwareRAID_Device) {
0052         std::unique_ptr<CoreBackendDevice> backendDevice = CoreBackendManager::self()->backend()->openDevice(device());
0053 
0054         if (backendDevice) {
0055             std::unique_ptr<CoreBackendPartitionTable> backendPartitionTable = backendDevice->openPartitionTable();
0056 
0057             if (backendPartitionTable) {
0058                 rval = backendPartitionTable->deletePartition(*report, partition());
0059 
0060                 if (!rval)
0061                     report->line() << xi18nc("@info:progress", "Could not delete partition <filename>%1</filename>.", partition().deviceNode());
0062                 else
0063                     backendPartitionTable->commit();
0064             } else
0065                 report->line() << xi18nc("@info:progress", "Could not open partition table on device <filename>%1</filename> to delete partition <filename>%2</filename>.", device().deviceNode(), partition().deviceNode());
0066         } else
0067             report->line() << xi18nc("@info:progress", "Deleting partition failed: Could not open device <filename>%1</filename>.", device().deviceNode());
0068     } else if (device().type() == Device::Type::LVM_Device) {
0069         LvmDevice& dev = dynamic_cast<LvmDevice&>(device());
0070         rval = LvmDevice::removeLV(*report, dev, partition());
0071     }
0072 
0073     jobFinished(*report, rval);
0074 
0075     return rval;
0076 }
0077 
0078 QString DeletePartitionJob::description() const
0079 {
0080     return xi18nc("@info:progress", "Delete the partition <filename>%1</filename>", partition().deviceNode());
0081 }