1 /*
2 SPDX-FileCopyrightText: 2008-2010 Volker Lanz <vl@fidra.de>
3 SPDX-FileCopyrightText: 2014-2017 Andrius Štikonas <andrius@stikonas.eu>
4
5 SPDX-License-Identifier: GPL-3.0-or-later
6 */
7
8 #include "jobs/movefilesystemjob.h"
9
10 #include "core/partition.h"
11 #include "core/device.h"
12 #include "core/copysourcedevice.h"
13 #include "core/copytargetdevice.h"
14
15 #include "util/report.h"
16
17 #include <KLocalizedString>
18
19 /** Creates a new MoveFileSystemJob
20 @param d the Device the Partition to move is on
21 @param p the Partition to move
22 @param newstart the new start sector for the Partition
23 */
MoveFileSystemJob(Device & d,Partition & p,qint64 newstart)24 MoveFileSystemJob::MoveFileSystemJob(Device& d, Partition& p, qint64 newstart) :
25 Job(),
26 m_Device(d),
27 m_Partition(p),
28 m_NewStart(newstart)
29 {
30 }
31
numSteps() const32 qint32 MoveFileSystemJob::numSteps() const
33 {
34 return 100;
35 }
36
run(Report & parent)37 bool MoveFileSystemJob::run(Report& parent)
38 {
39 bool rval = false;
40
41 Report* report = jobStarted(parent);
42
43 // A scope for moveSource and moveTarget, so CopyTargetDevice's dtor runs before we
44 // say we're finished: The CopyTargetDevice dtor asks the backend to close the device
45 // and that may take a while.
46 {
47 qint64 length = partition().fileSystem().lastByte() - partition().fileSystem().firstByte();
48 CopySourceDevice moveSource(device(), partition().fileSystem().firstByte(), partition().fileSystem().lastByte());
49 CopyTargetDevice moveTarget(device(), newStart() * device().logicalSize(), newStart() * device().logicalSize() + length);
50
51 if (!moveSource.open())
52 report->line() << xi18nc("@info:progress", "Could not open file system on partition <filename>%1</filename> for moving.", partition().deviceNode());
53 else if (!moveTarget.open())
54 report->line() << xi18nc("@info:progress", "Could not create target for moving file system on partition <filename>%1</filename>.", partition().deviceNode());
55 else {
56 rval = copyBlocks(*report, moveTarget, moveSource);
57
58 if (rval) {
59 const qint64 savedLength = partition().fileSystem().length() - 1;
60 partition().fileSystem().setFirstSector(newStart());
61 partition().fileSystem().setLastSector(newStart() + savedLength);
62 } else if (!rollbackCopyBlocks(*report, moveTarget, moveSource))
63 report->line() << xi18nc("@info:progress", "Rollback for file system on partition <filename>%1</filename> failed.", partition().deviceNode());
64
65 report->line() << xi18nc("@info:progress", "Closing device. This may take a few seconds.");
66 }
67 }
68
69 if (rval)
70 rval = partition().fileSystem().updateBootSector(*report, partition().deviceNode());
71
72 jobFinished(*report, rval);
73
74 return rval;
75 }
76
description() const77 QString MoveFileSystemJob::description() const
78 {
79 return xi18nc("@info:progress", "Move the file system on partition <filename>%1</filename> to sector %2", partition().deviceNode(), newStart());
80 }
81