~ubuntu-branches/ubuntu/oneiric/partitionmanager/oneiric

« back to all changes in this revision

Viewing changes to src/jobs/restorefilesystemjob.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Mercatante
  • Date: 2009-01-23 17:57:36 UTC
  • Revision ID: james.westby@ubuntu.com-20090123175736-2ltrhgg3m55dokbm
Tags: upstream-1.0.0~beta1a
ImportĀ upstreamĀ versionĀ 1.0.0~beta1a

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *   Copyright (C) 2008 by Volker Lanz <vl@fidra.de>                       *
 
3
 *                                                                         *
 
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.                                   *
 
8
 *                                                                         *
 
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.                          *
 
13
 *                                                                         *
 
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
 ***************************************************************************/
 
19
 
 
20
#include "jobs/restorefilesystemjob.h"
 
21
 
 
22
#include "core/partition.h"
 
23
#include "core/device.h"
 
24
#include "core/copysourcefile.h"
 
25
#include "core/copytargetdevice.h"
 
26
 
 
27
#include "fs/filesystem.h"
 
28
#include "fs/filesystemfactory.h"
 
29
 
 
30
#include "util/report.h"
 
31
 
 
32
#include <klocale.h>
 
33
#include <kdebug.h>
 
34
 
 
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
 
39
*/
 
40
RestoreFileSystemJob::RestoreFileSystemJob(Device& targetdevice, Partition& targetpartition, const QString& filename) :
 
41
        Job(),
 
42
        m_TargetDevice(targetdevice),
 
43
        m_TargetPartition(targetpartition),
 
44
        m_FileName(filename)
 
45
{
 
46
}
 
47
 
 
48
qint32 RestoreFileSystemJob::numSteps() const
 
49
{
 
50
        return 100;
 
51
}
 
52
 
 
53
bool RestoreFileSystemJob::run(Report& parent)
 
54
{
 
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.
 
58
 
 
59
        bool rval = false;
 
60
        
 
61
        Report* report = jobStarted(parent);
 
62
 
 
63
        // Again, a scope for copyTarget and copySource. See MoveFileSystemJob::run()
 
64
        {
 
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());
 
68
 
 
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());
 
73
                else
 
74
                {
 
75
                        rval = copyBlocks(*report, copyTarget, copySource);
 
76
 
 
77
                        if (rval)
 
78
                        {
 
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);
 
83
 
 
84
                                targetPartition().deleteFileSystem();
 
85
                                targetPartition().setFileSystem(fs);
 
86
                        }
 
87
 
 
88
                        report->line() << i18nc("@info/plain", "Closing device. This may take a few seconds.");
 
89
                }
 
90
        }
 
91
        
 
92
        jobFinished(*report, rval);
 
93
 
 
94
        return rval;
 
95
}
 
96
 
 
97
QString RestoreFileSystemJob::description() const
 
98
{
 
99
        return i18nc("@info/plain", "Restore the file system from file <filename>%1</filename> to partition <filename>%2</filename>", fileName(), targetPartition().deviceNode());
 
100
}