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

0001 /*
0002     SPDX-FileCopyrightText: 2008-2012 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/copyfilesystemjob.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 "fs/filesystem.h"
0016 
0017 #include "util/report.h"
0018 
0019 #include <KLocalizedString>
0020 
0021 /** Creates a new CopyFileSystemJob
0022     @param targetdevice the Device the FileSystem is to be copied to
0023     @param targetpartition the Partition the FileSystem is to be copied to
0024     @param sourcedevice the Device the source FileSystem is on
0025     @param sourcepartition the Partition the source FileSystem is on
0026 */
0027 CopyFileSystemJob::CopyFileSystemJob(Device& targetdevice, Partition& targetpartition, Device& sourcedevice, Partition& sourcepartition) :
0028     Job(),
0029     m_TargetDevice(targetdevice),
0030     m_TargetPartition(targetpartition),
0031     m_SourceDevice(sourcedevice),
0032     m_SourcePartition(sourcepartition)
0033 {
0034 }
0035 
0036 qint32 CopyFileSystemJob::numSteps() const
0037 {
0038     return 100;
0039 }
0040 
0041 bool CopyFileSystemJob::run(Report& parent)
0042 {
0043     bool rval = false;
0044 
0045     Report* report = jobStarted(parent);
0046 
0047     if (targetPartition().fileSystem().length() < sourcePartition().fileSystem().length())
0048         report->line() << xi18nc("@info:progress", "Cannot copy file system: File system on target partition <filename>%1</filename> is smaller than the file system on source partition <filename>%2</filename>.", targetPartition().deviceNode(), sourcePartition().deviceNode());
0049     else if (sourcePartition().fileSystem().supportCopy() == FileSystem::cmdSupportFileSystem)
0050         rval = sourcePartition().fileSystem().copy(*report, targetPartition().deviceNode(), sourcePartition().deviceNode());
0051     else if (sourcePartition().fileSystem().supportCopy() == FileSystem::cmdSupportCore) {
0052         CopySourceDevice copySource(sourceDevice(), sourcePartition().fileSystem().firstByte(), sourcePartition().fileSystem().lastByte());
0053         CopyTargetDevice copyTarget(targetDevice(), targetPartition().fileSystem().firstByte(), targetPartition().fileSystem().lastByte());
0054 
0055         if (!copySource.open())
0056             report->line() << xi18nc("@info:progress", "Could not open file system on source partition <filename>%1</filename> for copying.", sourcePartition().deviceNode());
0057         else if (!copyTarget.open())
0058             report->line() << xi18nc("@info:progress", "Could not open file system on target partition <filename>%1</filename> for copying.", targetPartition().deviceNode());
0059         else {
0060             rval = copyBlocks(*report, copyTarget, copySource);
0061             report->line() << xi18nc("@info:progress", "Closing device. This may take a while, especially on slow devices like Memory Sticks.");
0062         }
0063     }
0064 
0065     if (rval) {
0066         // set the target file system to the length of the source
0067         const qint64 newLastSector = targetPartition().fileSystem().firstSector() + sourcePartition().fileSystem().length() - 1;
0068 
0069         targetPartition().fileSystem().setLastSector(newLastSector);
0070 
0071         // and set a new UUID, if the target filesystem supports UUIDs
0072         if (targetPartition().fileSystem().supportUpdateUUID() == FileSystem::cmdSupportFileSystem) {
0073             targetPartition().fileSystem().updateUUID(*report, targetPartition().deviceNode());
0074             targetPartition().fileSystem().setUUID(targetPartition().fileSystem().readUUID(targetPartition().deviceNode()));
0075         }
0076     }
0077 
0078     if (rval)
0079         rval = targetPartition().fileSystem().updateBootSector(*report, targetPartition().deviceNode());
0080 
0081     jobFinished(*report, rval);
0082 
0083     return rval;
0084 }
0085 
0086 QString CopyFileSystemJob::description() const
0087 {
0088     return xi18nc("@info:progress", "Copy file system on partition <filename>%1</filename> to partition <filename>%2</filename>", sourcePartition().deviceNode(), targetPartition().deviceNode());
0089 }