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     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 "jobs/setpartgeometryjob.h"
0011 
0012 #include "backend/corebackend.h"
0013 #include "backend/corebackendmanager.h"
0014 #include "backend/corebackenddevice.h"
0015 #include "backend/corebackendpartitiontable.h"
0016 
0017 #include "core/partition.h"
0018 #include "core/device.h"
0019 #include "core/lvmdevice.h"
0020 
0021 #include "util/report.h"
0022 
0023 #include <KLocalizedString>
0024 
0025 /** Creates a new SetPartGeometryJob
0026     @param d the Device the Partition whose geometry is to be set is on
0027     @param p the Partition whose geometry is to be set
0028     @param newstart the new start sector for the Partition
0029     @param newlength the new length for the Partition
0030 
0031     @todo Wouldn't it be better to have newfirst (new first sector) and newlast (new last sector) as args instead?
0032     Having a length here doesn't seem to be very consistent with the rest of the app, right?
0033 */
0034 SetPartGeometryJob::SetPartGeometryJob(Device& d, Partition& p, qint64 newstart, qint64 newlength) :
0035     Job(),
0036     m_Device(d),
0037     m_Partition(p),
0038     m_NewStart(newstart),
0039     m_NewLength(newlength)
0040 {
0041 }
0042 
0043 bool SetPartGeometryJob::run(Report& parent)
0044 {
0045     bool rval = false;
0046 
0047     Report* report = jobStarted(parent);
0048 
0049     if(device().type() == Device::Type::Disk_Device || device().type() == Device::Type::SoftwareRAID_Device) {
0050         std::unique_ptr<CoreBackendDevice> backendDevice = CoreBackendManager::self()->backend()->openDevice(device());
0051 
0052         if (backendDevice) {
0053             std::unique_ptr<CoreBackendPartitionTable> backendPartitionTable = backendDevice->openPartitionTable();
0054 
0055             if (backendPartitionTable) {
0056                 rval = backendPartitionTable->updateGeometry(*report, partition(), newStart(), newStart() + newLength() - 1);
0057 
0058                 if (rval) {
0059                     partition().setFirstSector(newStart());
0060                     partition().setLastSector(newStart() + newLength() - 1);
0061                     backendPartitionTable->commit();
0062                 }
0063             }
0064         } else
0065             report->line() << xi18nc("@info:progress", "Could not open device <filename>%1</filename> while trying to resize/move partition <filename>%2</filename>.", device().deviceNode(), partition().deviceNode());
0066     } else if (device().type() == Device::Type::LVM_Device) {
0067 
0068         partition().setFirstSector(newStart());
0069         partition().setLastSector(newStart() + newLength() - 1);
0070 
0071         rval = LvmDevice::resizeLV(*report, partition());
0072     }
0073 
0074     jobFinished(*report, rval);
0075 
0076     return rval;
0077 }
0078 
0079 QString SetPartGeometryJob::description() const
0080 {
0081     return xi18nc("@info:progress", "Set geometry of partition <filename>%1</filename>: Start sector: %2, length: %3", partition().deviceNode(), newStart(), newLength());
0082 }