File indexing completed on 2024-05-05 05:48:51
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 "ops/createpartitiontableoperation.h" 0011 0012 #include "core/device.h" 0013 #include "core/partitiontable.h" 0014 #include "core/partition.h" 0015 #include "core/raid/softwareraid.h" 0016 0017 #include "jobs/createpartitiontablejob.h" 0018 0019 #include <QString> 0020 0021 #include <KLocalizedString> 0022 0023 /** Creates a new CreatePartitionTableOperation. 0024 @param d the Device to create the new PartitionTable on 0025 @param t the type for the new PartitionTable 0026 */ 0027 CreatePartitionTableOperation::CreatePartitionTableOperation(Device& d, PartitionTable::TableType t) : 0028 Operation(), 0029 m_TargetDevice(d), 0030 m_OldPartitionTable(targetDevice().partitionTable()), 0031 m_PartitionTable(new PartitionTable(t, PartitionTable::defaultFirstUsable(d, t), PartitionTable::defaultLastUsable(d, t))), 0032 m_CreatePartitionTableJob(new CreatePartitionTableJob(targetDevice())) 0033 { 0034 addJob(createPartitionTableJob()); 0035 } 0036 0037 /** Creates a new CreatePartitionTableOperation. 0038 @param d the Device to create the new PartitionTable on 0039 @param ptable pointer to the new partition table object. the operation takes ownership. 0040 */ 0041 CreatePartitionTableOperation::CreatePartitionTableOperation(Device& d, PartitionTable* ptable) : 0042 Operation(), 0043 m_TargetDevice(d), 0044 m_OldPartitionTable(targetDevice().partitionTable()), 0045 m_PartitionTable(ptable), 0046 m_CreatePartitionTableJob(new CreatePartitionTableJob(targetDevice())) 0047 { 0048 addJob(createPartitionTableJob()); 0049 } 0050 0051 CreatePartitionTableOperation::~CreatePartitionTableOperation() 0052 { 0053 if (status() == StatusPending) 0054 delete m_PartitionTable; 0055 } 0056 0057 bool CreatePartitionTableOperation::targets(const Device& d) const 0058 { 0059 return d == targetDevice(); 0060 } 0061 0062 void CreatePartitionTableOperation::preview() 0063 { 0064 targetDevice().setPartitionTable(partitionTable()); 0065 targetDevice().partitionTable()->updateUnallocated(targetDevice()); 0066 } 0067 0068 void CreatePartitionTableOperation::undo() 0069 { 0070 targetDevice().setPartitionTable(oldPartitionTable()); 0071 0072 if (targetDevice().partitionTable()) 0073 targetDevice().partitionTable()->updateUnallocated(targetDevice()); 0074 } 0075 0076 bool CreatePartitionTableOperation::execute(Report& parent) 0077 { 0078 targetDevice().setPartitionTable(partitionTable()); 0079 return Operation::execute(parent); 0080 } 0081 0082 /** Can a new partition table be created on a device? 0083 @param device pointer to the device, can be nullptr 0084 @return true if a new partition table can be created on @p device 0085 */ 0086 bool CreatePartitionTableOperation::canCreate(const Device* device) 0087 { 0088 if (device == nullptr) 0089 return false; 0090 0091 if (device->type() == Device::Type::SoftwareRAID_Device) { 0092 const SoftwareRAID* raid = static_cast<const SoftwareRAID *>(device); 0093 0094 if (raid->status() == SoftwareRAID::Status::Inactive) 0095 return false; 0096 } 0097 0098 return (device->partitionTable() == nullptr || !device->partitionTable()->isChildMounted()) 0099 && (device->type() != Device::Type::LVM_Device); 0100 } 0101 0102 QString CreatePartitionTableOperation::description() const 0103 { 0104 return xi18nc("@info:status", "Create a new partition table (type: %1) on <filename>%2</filename>", partitionTable()->typeName(), targetDevice().deviceNode()); 0105 }