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

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 Huzaifa Faruqui <huzaifafaruqui@gmail.com>
0006 
0007     SPDX-License-Identifier: GPL-3.0-or-later
0008 */
0009 
0010 #include "jobs/backupfilesystemjob.h"
0011 
0012 #include "core/partition.h"
0013 #include "core/device.h"
0014 #include "core/copysourcedevice.h"
0015 #include "core/copytargetfile.h"
0016 
0017 #include "fs/filesystem.h"
0018 #include "util/externalcommand.h"
0019 #include "util/report.h"
0020 
0021 #include <KLocalizedString>
0022 
0023 /** Creates a new BackupFileSystemJob
0024     @param sourcedevice the device the FileSystem to back up is on
0025     @param sourcepartition the Partition the FileSystem to back up is on
0026     @param filename name of the file to backup to
0027 */
0028 BackupFileSystemJob::BackupFileSystemJob(Device& sourcedevice, Partition& sourcepartition, const QString& filename) :
0029     Job(),
0030     m_SourceDevice(sourcedevice),
0031     m_SourcePartition(sourcepartition),
0032     m_FileName(filename)
0033 {
0034 }
0035 
0036 qint32 BackupFileSystemJob::numSteps() const
0037 {
0038     return 100;
0039 }
0040 
0041 bool BackupFileSystemJob::run(Report& parent)
0042 {
0043     bool rval = false;
0044 
0045     Report* report = jobStarted(parent);
0046 
0047     if (sourcePartition().fileSystem().supportBackup() == FileSystem::cmdSupportFileSystem)
0048         rval = sourcePartition().fileSystem().backup(*report, sourceDevice(), sourcePartition().deviceNode(), fileName());
0049     else if (sourcePartition().fileSystem().supportBackup() == FileSystem::cmdSupportCore) {
0050         CopySourceDevice copySource(sourceDevice(), sourcePartition().fileSystem().firstByte(), sourcePartition().fileSystem().lastByte());
0051         CopyTargetFile copyTarget(fileName());
0052 
0053         if (!copySource.open())
0054             report->line() << xi18nc("@info:progress", "Could not open file system on source partition <filename>%1</filename> for backup.", sourcePartition().deviceNode());
0055         else if (!copyTarget.open())
0056             report->line() << xi18nc("@info:progress", "Could not create backup file <filename>%1</filename>.", fileName());
0057         else
0058             rval = copyBlocks(*report, copyTarget, copySource);
0059     }
0060 
0061     jobFinished(*report, rval);
0062 
0063     return rval;
0064 }
0065 
0066 QString BackupFileSystemJob::description() const
0067 {
0068     return xi18nc("@info:progress", "Back up file system on partition <filename>%1</filename> to <filename>%2</filename>", sourcePartition().deviceNode(), fileName());
0069 }