~ubuntu-branches/ubuntu/karmic/partitionmanager/karmic

« back to all changes in this revision

Viewing changes to src/jobs/resizefilesystemjob.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/resizefilesystemjob.h"
 
21
 
 
22
#include "core/partition.h"
 
23
#include "core/device.h"
 
24
 
 
25
#include "fs/filesystem.h"
 
26
 
 
27
#include "util/report.h"
 
28
#include "util/capacity.h"
 
29
 
 
30
#include <klocale.h>
 
31
#include <kdebug.h>
 
32
 
 
33
#include <parted/parted.h>
 
34
 
 
35
/** Creates a new ResizeFileSystemJob
 
36
        @param d the Device the FileSystem to be resized is on
 
37
        @param p the Partition the FileSystem to be resized is on
 
38
        @param newlength the new length for the FileSystem; if -1, the FileSystem will be resized to fill the entire Partition
 
39
*/
 
40
ResizeFileSystemJob::ResizeFileSystemJob(Device& d, Partition& p, qint64 newlength) :
 
41
        Job(),
 
42
        m_Device(d),
 
43
        m_Partition(p),
 
44
        m_Maximize(newlength == -1),
 
45
        m_NewLength(isMaximizing() ? partition().length() : newlength)
 
46
{
 
47
}
 
48
 
 
49
qint32 ResizeFileSystemJob::numSteps() const
 
50
{
 
51
        return 100;
 
52
}
 
53
 
 
54
bool ResizeFileSystemJob::run(Report& parent)
 
55
{
 
56
        Q_ASSERT(partition().fileSystem().firstSector() != -1);
 
57
        Q_ASSERT(partition().fileSystem().lastSector() != -1);
 
58
        Q_ASSERT(newLength() <= partition().length());
 
59
 
 
60
        if (partition().fileSystem().firstSector() == -1 || partition().fileSystem().lastSector() == -1 || newLength() > partition().length())
 
61
        {
 
62
                kWarning() << "file system first sector: " << partition().fileSystem().firstSector() << ", last sector: " << partition().fileSystem().lastSector() << ", new length: " << newLength() << ", partition length: " << partition().length();
 
63
                return false;
 
64
        }
 
65
        
 
66
        bool rval = false;
 
67
        
 
68
        Report* report = jobStarted(parent);
 
69
        
 
70
        if (partition().fileSystem().length() == newLength())
 
71
        {
 
72
                report->line() << i18nc("@info/plain", "The file system on partition <filename>%1</filename> already has the requested length of %2 sectors.", partition().deviceNode(), newLength());
 
73
                rval = true;
 
74
        }
 
75
        else
 
76
        {
 
77
                report->line() << i18nc("@info/plain", "Resizing file system from %1 to %2 sectors.", partition().fileSystem().length(), newLength());
 
78
                
 
79
                FileSystem::SupportType support = (newLength() < partition().fileSystem().length()) ? partition().fileSystem().supportShrink() : partition().fileSystem().supportGrow();
 
80
 
 
81
                switch(support)
 
82
                {
 
83
                        case FileSystem::SupportLibParted:
 
84
                        {
 
85
                                Report* childReport = report->newChild();
 
86
                                childReport->line() << i18nc("@info/plain", "Resizing a %1 file system using LibParted internal functions.", partition().fileSystem().name());
 
87
                                rval = resizeFileSystemLibParted(*childReport);
 
88
                                break;
 
89
                        }
 
90
 
 
91
                        case FileSystem::SupportExternal:
 
92
                        {
 
93
                                const qint64 newLengthInByte = Capacity(newLength() * partition().sectorSize()).toInt(Capacity::Byte);
 
94
                                rval = partition().fileSystem().resize(*report, partition().deviceNode(), newLengthInByte);
 
95
                                break;
 
96
                        }
 
97
 
 
98
                        default:
 
99
                                report->line() << i18nc("@info/plain", "The file system on partition <filename>%1</filename> cannot be resized because there is no support for it.", partition().deviceNode());
 
100
                                break;
 
101
                }
 
102
                
 
103
                if (rval)
 
104
                        partition().fileSystem().setLastSector(partition().fileSystem().firstSector() + newLength() - 1);
 
105
        }
 
106
        
 
107
        jobFinished(*report, rval);
 
108
                
 
109
        return rval;
 
110
}
 
111
 
 
112
bool ResizeFileSystemJob::resizeFileSystemLibParted(Report& report)
 
113
{
 
114
        bool rval = false;
 
115
 
 
116
        if (!openPed(device().deviceNode()))
 
117
        {
 
118
                report.line() << i18nc("@info/plain", "Resizing file system on partition <filename>%1</filename>: Could not open device <filename>%2</filename>.", partition().deviceNode(), device().deviceNode());
 
119
                return false;
 
120
        }
 
121
 
 
122
        if (PedGeometry* originalGeometry = ped_geometry_new(pedDevice(), partition().fileSystem().firstSector(), partition().fileSystem().length()))
 
123
        {
 
124
                if (PedFileSystem* pedFileSystem = ped_file_system_open(originalGeometry))
 
125
                {
 
126
                        if (PedGeometry* resizedGeometry = ped_geometry_new(pedDevice(), partition().fileSystem().firstSector(), newLength()))
 
127
                        {
 
128
                                PedTimer* pedTimer = ped_timer_new(pedTimerHandler, this);
 
129
                                rval = ped_file_system_resize(pedFileSystem, resizedGeometry, pedTimer) && commit();
 
130
                                ped_timer_destroy(pedTimer);
 
131
 
 
132
                                if (rval)
 
133
                                        report.line() << i18nc("@info/plain", "Successfully resized file system using LibParted.");
 
134
                                else
 
135
                                        report.line() << i18nc("@info/plain", "Could not resize file system on partition <filename>%1</filename>.", partition().deviceNode());
 
136
                        }
 
137
                        else
 
138
                                report.line() << i18nc("@info/plain", "Could not get geometry for resized partition <filename>%1</filename> while trying to resize the file system.", partition().deviceNode());
 
139
 
 
140
                        ped_file_system_close(pedFileSystem);
 
141
                }
 
142
                else
 
143
                        report.line() << i18nc("@info/plain", "Could not open partition <filename>%1</filename> while trying to resize the file system.", partition().deviceNode());
 
144
        }
 
145
        else
 
146
                report.line() << i18nc("@info/plain", "Could not read geometry for partition <filename>%1</filename> while trying to resize the file system.", partition().deviceNode());
 
147
        
 
148
        closePed();
 
149
        
 
150
        return rval;
 
151
}
 
152
 
 
153
QString ResizeFileSystemJob::description() const
 
154
{
 
155
        if (isMaximizing())
 
156
                return i18nc("@info/plain", "Maximize file system on <filename>%1</filename> to fill the partition", partition().deviceNode());
 
157
                
 
158
        return i18nc("@info/plain", "Resize file system on partition <filename>%1</filename> to %2 sectors", partition().deviceNode(), newLength());
 
159
}