1
/***************************************************************************
2
* Copyright (C) 2008 by Volker Lanz <vl@fidra.de> *
4
* This program is free software; you can redistribute it and/or modify *
5
* it under the terms of the GNU General Public License as published by *
6
* the Free Software Foundation; either version 2 of the License, or *
7
* (at your option) any later version. *
9
* This program is distributed in the hope that it will be useful, *
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12
* GNU General Public License for more details. *
14
* You should have received a copy of the GNU General Public License *
15
* along with this program; if not, write to the *
16
* Free Software Foundation, Inc., *
17
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18
***************************************************************************/
20
#include "jobs/restorefilesystemjob.h"
22
#include "core/partition.h"
23
#include "core/device.h"
24
#include "core/copysourcefile.h"
25
#include "core/copytargetdevice.h"
27
#include "fs/filesystem.h"
28
#include "fs/filesystemfactory.h"
30
#include "util/report.h"
35
/** Creates a new RestoreFileSystemJob
36
@param targetdevice the Device the FileSystem is to be restored to
37
@param targetpartition the Partition the FileSystem is to be restore to
38
@param filename the file name with the image file to restore
40
RestoreFileSystemJob::RestoreFileSystemJob(Device& targetdevice, Partition& targetpartition, const QString& filename) :
42
m_TargetDevice(targetdevice),
43
m_TargetPartition(targetpartition),
48
qint32 RestoreFileSystemJob::numSteps() const
53
bool RestoreFileSystemJob::run(Report& parent)
55
// Restoring is file system independent because we currently have no way of
56
// detecting the file system in a given image file. We cannot even find out if the
57
// file the user gave us is a valid image file or just some junk.
61
Report* report = jobStarted(parent);
63
// Again, a scope for copyTarget and copySource. See MoveFileSystemJob::run()
65
// FileSystems are restored to _partitions_, so don't use first and last sector of file system here
66
CopyTargetDevice copyTarget(targetDevice(), targetPartition().firstSector(), targetPartition().lastSector());
67
CopySourceFile copySource(fileName(), copyTarget.sectorSize());
69
if (!copySource.open())
70
report->line() << i18nc("@info/plain", "Could not open backup file <filename>%1</filename> to restore from.", fileName());
71
else if (!copyTarget.open())
72
report->line() << i18nc("@info/plain", "Could not open target partition <filename>%1</filename> to restore to.", targetPartition().deviceNode());
75
rval = copyBlocks(*report, copyTarget, copySource);
79
// create a new file system for what was restored with the length of the image file
80
const qint64 newLastSector = targetPartition().firstSector() + copySource.length() - 1;
81
FileSystem::Type t = detectFileSystemBySector(*report, targetDevice(), targetPartition().firstSector());
82
FileSystem* fs = FileSystemFactory::create(t, targetPartition().firstSector(), newLastSector);
84
targetPartition().deleteFileSystem();
85
targetPartition().setFileSystem(fs);
88
report->line() << i18nc("@info/plain", "Closing device. This may take a few seconds.");
92
jobFinished(*report, rval);
97
QString RestoreFileSystemJob::description() const
99
return i18nc("@info/plain", "Restore the file system from file <filename>%1</filename> to partition <filename>%2</filename>", fileName(), targetPartition().deviceNode());