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

0001 /*
0002     SPDX-FileCopyrightText: 2008-2011 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     SPDX-FileCopyrightText: 2020 Arnaud Ferraris <arnaud.ferraris@collabora.com>
0007 
0008     SPDX-License-Identifier: GPL-3.0-or-later
0009 */
0010 
0011 #include "jobs/createfilesystemjob.h"
0012 
0013 #include "backend/corebackend.h"
0014 #include "backend/corebackendmanager.h"
0015 #include "backend/corebackenddevice.h"
0016 #include "backend/corebackendpartitiontable.h"
0017 
0018 #include "core/device.h"
0019 #include "core/partition.h"
0020 
0021 #include "fs/filesystem.h"
0022 
0023 #include "util/report.h"
0024 
0025 #include <KLocalizedString>
0026 
0027 /** Creates a new CreateFileSystemJob
0028     @param p the Partition the FileSystem to create is on
0029 */
0030 CreateFileSystemJob::CreateFileSystemJob(Device& d, Partition& p, const QString& label) :
0031     Job(),
0032     m_Device(d),
0033     m_Partition(p),
0034     m_Label(label)
0035 {
0036 }
0037 
0038 bool CreateFileSystemJob::run(Report& parent)
0039 {
0040     bool rval = false;
0041 
0042     Report* report = jobStarted(parent);
0043 
0044     if (partition().fileSystem().type() == FileSystem::Type::Unformatted)
0045         return true;
0046 
0047     bool createResult;
0048     if (partition().fileSystem().supportCreate() == FileSystem::cmdSupportFileSystem) {
0049         if (partition().fileSystem().supportCreateWithLabel() == FileSystem::cmdSupportFileSystem) {
0050             createResult = partition().fileSystem().createWithLabel(*report, partition().deviceNode(), m_Label);
0051         } else {
0052             createResult = partition().fileSystem().create(*report, partition().deviceNode());
0053         }
0054         if (createResult) {
0055             if (device().type() == Device::Type::Disk_Device || device().type() == Device::Type::SoftwareRAID_Device) {
0056                 std::unique_ptr<CoreBackendDevice> backendDevice = CoreBackendManager::self()->backend()->openDevice(device());
0057 
0058                 if (backendDevice) {
0059                     std::unique_ptr<CoreBackendPartitionTable> backendPartitionTable = backendDevice->openPartitionTable();
0060 
0061                     if (backendPartitionTable) {
0062                         if (backendPartitionTable->setPartitionSystemType(*report, partition())) {
0063                             rval = true;
0064                             backendPartitionTable->commit();
0065                         } else
0066                             report->line() << xi18nc("@info:progress", "Failed to set the system type for the file system on partition <filename>%1</filename>.", partition().deviceNode());
0067                     } else
0068                         report->line() << xi18nc("@info:progress", "Could not open partition table on device <filename>%1</filename> to set the system type for partition <filename>%2</filename>.", device().deviceNode(), partition().deviceNode());
0069                 } else
0070                     report->line() << xi18nc("@info:progress", "Could not open device <filename>%1</filename> to set the system type for partition <filename>%2</filename>.", device().deviceNode(), partition().deviceNode());
0071             } else if (device().type() == Device::Type::LVM_Device) {
0072                 rval = true;
0073             }
0074         }
0075     }
0076 
0077     jobFinished(*report, rval);
0078 
0079     return rval;
0080 }
0081 
0082 QString CreateFileSystemJob::description() const
0083 {
0084     return xi18nc("@info:progress", "Create file system <filename>%1</filename> on partition <filename>%2</filename>", partition().fileSystem().name(), partition().deviceNode());
0085 }