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

0001 /*
0002     SPDX-FileCopyrightText: 2020 Gaƫl PORTAY <gael.portay@collabora.com>
0003 
0004     SPDX-License-Identifier: GPL-3.0-or-later
0005 */
0006 
0007 #include "jobs/setpartitionuuidjob.h"
0008 
0009 #include "backend/corebackend.h"
0010 #include "backend/corebackendmanager.h"
0011 #include "backend/corebackenddevice.h"
0012 #include "backend/corebackendpartitiontable.h"
0013 
0014 #include "core/partition.h"
0015 #include "core/device.h"
0016 
0017 #include "util/report.h"
0018 
0019 #include <KLocalizedString>
0020 #include <memory>
0021 
0022 /** Creates a new SetPartitionUUIDJob (GPT only)
0023     @param d the Device the Partition to be created will be on
0024     @param p the Partition whose UUID is to be set is on
0025     @param newUUID the new UUID
0026 */
0027 SetPartitionUUIDJob::SetPartitionUUIDJob(Device& d, Partition& p, const QString& newUUID) :
0028     Job(),
0029     m_Device(d),
0030     m_Partition(p),
0031     m_UUID(newUUID)
0032 {
0033 }
0034 
0035 bool SetPartitionUUIDJob::run(Report& parent)
0036 {
0037     Q_ASSERT(partition().devicePath() == device().deviceNode());
0038 
0039     bool rval = true;
0040 
0041     Report* report = jobStarted(parent);
0042 
0043     // The UUID is supported by GPT only, if the partition table is not GPT, just ignore the
0044     // request and say all is well. This helps in operations because we don't have to check for
0045     // support to avoid having a failed job.
0046     if (m_Device.partitionTable()->type() != PartitionTable::gpt)
0047         report->line() << xi18nc("@info:progress", "Partition table of partition <filename>%1</filename> does not support setting UUIDs. Job ignored.", partition().deviceNode());
0048     else {
0049         std::unique_ptr<CoreBackendDevice> backendDevice = CoreBackendManager::self()->backend()->openDevice(m_Device);
0050         if (backendDevice) {
0051             std::unique_ptr<CoreBackendPartitionTable> backendPartitionTable = backendDevice->openPartitionTable();
0052 
0053             if (backendPartitionTable) {
0054                 if (backendPartitionTable->setPartitionUUID(*report, partition(), m_UUID)) {
0055                     rval = true;
0056                     partition().setUUID(m_UUID);
0057                     backendPartitionTable->commit();
0058                 } else
0059                     report->line() << xi18nc("@info:progress", "Failed to set the UUID for the partition <filename>%1</filename>.", partition().deviceNode());
0060             } else
0061                 report->line() << xi18nc("@info:progress", "Could not open partition table on device <filename>%1</filename> to set the UUID for the partition <filename>%2</filename>.", device().deviceNode(), partition().deviceNode());
0062         } else
0063                 report->line() << xi18nc("@info:progress", "Could not open device <filename>%1</filename> to set the UUID for partition <filename>%2</filename>.", device().deviceNode(), partition().deviceNode());
0064 
0065     }
0066 
0067     jobFinished(*report, rval);
0068 
0069     return rval;
0070 }
0071 
0072 QString SetPartitionUUIDJob::description() const
0073 {
0074     return xi18nc("@info:progress", "Set the UUID on partition <filename>%1</filename> to \"%2\"", partition().deviceNode(), uuid());
0075 }