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

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/setpartitionlabeljob.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 SetPartitionLabelJob (GPT only)
0023     @param d the Device the Partition to be created will be on
0024     @param p the Partition whose label is to be set is on
0025     @param newLabel the new label
0026 */
0027 SetPartitionLabelJob::SetPartitionLabelJob(Device& d, Partition& p, const QString& newLabel) :
0028     Job(),
0029     m_Device(d),
0030     m_Partition(p),
0031     m_Label(newLabel)
0032 {
0033 }
0034 
0035 bool SetPartitionLabelJob::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 label is supported by GPT only (as partition name), if the partition table is not GPT,
0044     // just ignore the request and say all is well. This helps in operations because
0045     // we don't have to check for 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 names. 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->setPartitionLabel(*report, partition(), m_Label)) {
0055                     rval = true;
0056                     partition().setLabel(m_Label);
0057                     backendPartitionTable->commit();
0058                 } else
0059                     report->line() << xi18nc("@info:progress", "Failed to set the name 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 name 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 name for partition <filename>%2</filename>.", device().deviceNode(), partition().deviceNode());
0064 
0065     }
0066 
0067     jobFinished(*report, rval);
0068 
0069     return rval;
0070 }
0071 
0072 QString SetPartitionLabelJob::description() const
0073 {
0074     return xi18nc("@info:progress", "Set the label on partition <filename>%1</filename> to \"%2\"", partition().deviceNode(), label());
0075 }