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-2017 Andrius Štikonas <andrius@stikonas.eu>
0004 
0005     SPDX-License-Identifier: GPL-3.0-or-later
0006 */
0007 
0008 #include "jobs/movefilesystemjob.h"
0009 
0010 #include "core/partition.h"
0011 #include "core/device.h"
0012 #include "core/copysourcedevice.h"
0013 #include "core/copytargetdevice.h"
0014 
0015 #include "util/report.h"
0016 
0017 #include <KLocalizedString>
0018 
0019 /** Creates a new MoveFileSystemJob
0020     @param d the Device the Partition to move is on
0021     @param p the Partition to move
0022     @param newstart the new start sector for the Partition
0023 */
0024 MoveFileSystemJob::MoveFileSystemJob(Device& d, Partition& p, qint64 newstart) :
0025     Job(),
0026     m_Device(d),
0027     m_Partition(p),
0028     m_NewStart(newstart)
0029 {
0030 }
0031 
0032 qint32 MoveFileSystemJob::numSteps() const
0033 {
0034     return 100;
0035 }
0036 
0037 bool MoveFileSystemJob::run(Report& parent)
0038 {
0039     bool rval = false;
0040 
0041     Report* report = jobStarted(parent);
0042 
0043     // A scope for moveSource and moveTarget, so CopyTargetDevice's dtor runs before we
0044     // say we're finished: The CopyTargetDevice dtor asks the backend to close the device
0045     // and that may take a while.
0046     {
0047         qint64 length = partition().fileSystem().lastByte() - partition().fileSystem().firstByte();
0048         CopySourceDevice moveSource(device(), partition().fileSystem().firstByte(), partition().fileSystem().lastByte());
0049         CopyTargetDevice moveTarget(device(), newStart() * device().logicalSize(), newStart() * device().logicalSize() + length);
0050 
0051         if (!moveSource.open())
0052             report->line() << xi18nc("@info:progress", "Could not open file system on partition <filename>%1</filename> for moving.", partition().deviceNode());
0053         else if (!moveTarget.open())
0054             report->line() << xi18nc("@info:progress", "Could not create target for moving file system on partition <filename>%1</filename>.", partition().deviceNode());
0055         else {
0056             rval = copyBlocks(*report, moveTarget, moveSource);
0057 
0058             if (rval) {
0059                 const qint64 savedLength = partition().fileSystem().length() - 1;
0060                 partition().fileSystem().setFirstSector(newStart());
0061                 partition().fileSystem().setLastSector(newStart() + savedLength);
0062             } else if (!rollbackCopyBlocks(*report, moveTarget, moveSource))
0063                 report->line() << xi18nc("@info:progress", "Rollback for file system on partition <filename>%1</filename> failed.", partition().deviceNode());
0064 
0065             report->line() << xi18nc("@info:progress", "Closing device. This may take a few seconds.");
0066         }
0067     }
0068 
0069     if (rval)
0070         rval = partition().fileSystem().updateBootSector(*report, partition().deviceNode());
0071 
0072     jobFinished(*report, rval);
0073 
0074     return rval;
0075 }
0076 
0077 QString MoveFileSystemJob::description() const
0078 {
0079     return xi18nc("@info:progress", "Move the file system on partition <filename>%1</filename> to sector %2", partition().deviceNode(), newStart());
0080 }