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

0001 /*
0002     SPDX-FileCopyrightText: 2008-2010 Volker Lanz <vl@fidra.de>
0003     SPDX-FileCopyrightText: 2014-2018 Andrius Štikonas <andrius@stikonas.eu>
0004 
0005     SPDX-License-Identifier: GPL-3.0-or-later
0006 */
0007 
0008 #include "jobs/setfilesystemlabeljob.h"
0009 
0010 #include "backend/corebackend.h"
0011 #include "backend/corebackendmanager.h"
0012 #include "backend/corebackenddevice.h"
0013 #include "backend/corebackendpartitiontable.h"
0014 
0015 #include "core/device_p.h"
0016 #include "core/operationstack.h"
0017 #include "core/partition.h"
0018 
0019 #include "fs/filesystem.h"
0020 
0021 #include "util/report.h"
0022 
0023 #include <KLocalizedString>
0024 
0025 #include <memory>
0026 
0027 /** Creates a new SetFileSystemLabelJob
0028     @param p the Partition the FileSystem whose label is to be set is on
0029     @param newlabel the new label
0030 */
0031 SetFileSystemLabelJob::SetFileSystemLabelJob(Partition& p, const QString& newlabel) :
0032     Job(),
0033     m_Partition(p),
0034     m_Label(newlabel)
0035 {
0036 }
0037 
0038 bool SetFileSystemLabelJob::run(Report& parent)
0039 {
0040     bool rval = true;
0041 
0042     Report* report = jobStarted(parent);
0043 
0044     // If there's no support for file system label setting for this file system,
0045     // just ignore the request and say all is well. This helps in operations because
0046     // we don't have to check for support to avoid having a failed job.
0047     if (partition().fileSystem().supportSetLabel() == FileSystem::cmdSupportNone)
0048         report->line() << xi18nc("@info:progress", "File system on partition <filename>%1</filename> does not support setting labels. Job ignored.", partition().deviceNode());
0049     else if (partition().fileSystem().supportSetLabel() == FileSystem::cmdSupportFileSystem && !partition().isMounted()) {
0050         rval = partition().fileSystem().writeLabel(*report, partition().deviceNode(), label());
0051 
0052         if (rval)
0053             partition().fileSystem().setLabel(label());
0054     }
0055     else if (partition().fileSystem().supportSetLabelOnline() == FileSystem::cmdSupportFileSystem && partition().isMounted()) {
0056         rval = partition().fileSystem().writeLabelOnline(*report, partition().deviceNode(), partition().mountPoint(), label());
0057 
0058         if (rval)
0059             partition().fileSystem().setLabel(label());
0060     }
0061     else
0062         rval = false;
0063 
0064     // A hack to reread partition table (commit() should be called even on non DiskDevices)
0065     Device dev(std::make_shared<DevicePrivate>(), QString(), QString(), 0, 0, QString(), Device::Type::Unknown_Device);
0066     std::unique_ptr<CoreBackendDevice> backendDevice = CoreBackendManager::self()->backend()->openDevice(dev);
0067     if (backendDevice) {
0068         std::unique_ptr<CoreBackendPartitionTable> backendPartitionTable = backendDevice->openPartitionTable();
0069 
0070         if (backendPartitionTable)
0071             backendPartitionTable->commit();
0072     }
0073 
0074     jobFinished(*report, rval);
0075 
0076     return rval;
0077 }
0078 
0079 QString SetFileSystemLabelJob::description() const
0080 {
0081     return xi18nc("@info:progress", "Set the file system label on partition <filename>%1</filename> to \"%2\"", partition().deviceNode(), label());
0082 }