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/resizefilesystemjob.h"
22
#include "core/partition.h"
23
#include "core/device.h"
25
#include "fs/filesystem.h"
27
#include "util/report.h"
28
#include "util/capacity.h"
33
#include <parted/parted.h>
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
40
ResizeFileSystemJob::ResizeFileSystemJob(Device& d, Partition& p, qint64 newlength) :
44
m_Maximize(newlength == -1),
45
m_NewLength(isMaximizing() ? partition().length() : newlength)
49
qint32 ResizeFileSystemJob::numSteps() const
54
bool ResizeFileSystemJob::run(Report& parent)
56
Q_ASSERT(partition().fileSystem().firstSector() != -1);
57
Q_ASSERT(partition().fileSystem().lastSector() != -1);
58
Q_ASSERT(newLength() <= partition().length());
60
if (partition().fileSystem().firstSector() == -1 || partition().fileSystem().lastSector() == -1 || newLength() > partition().length())
62
kWarning() << "file system first sector: " << partition().fileSystem().firstSector() << ", last sector: " << partition().fileSystem().lastSector() << ", new length: " << newLength() << ", partition length: " << partition().length();
68
Report* report = jobStarted(parent);
70
if (partition().fileSystem().length() == newLength())
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());
77
report->line() << i18nc("@info/plain", "Resizing file system from %1 to %2 sectors.", partition().fileSystem().length(), newLength());
79
FileSystem::SupportType support = (newLength() < partition().fileSystem().length()) ? partition().fileSystem().supportShrink() : partition().fileSystem().supportGrow();
83
case FileSystem::SupportLibParted:
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);
91
case FileSystem::SupportExternal:
93
const qint64 newLengthInByte = Capacity(newLength() * partition().sectorSize()).toInt(Capacity::Byte);
94
rval = partition().fileSystem().resize(*report, partition().deviceNode(), newLengthInByte);
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());
104
partition().fileSystem().setLastSector(partition().fileSystem().firstSector() + newLength() - 1);
107
jobFinished(*report, rval);
112
bool ResizeFileSystemJob::resizeFileSystemLibParted(Report& report)
116
if (!openPed(device().deviceNode()))
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());
122
if (PedGeometry* originalGeometry = ped_geometry_new(pedDevice(), partition().fileSystem().firstSector(), partition().fileSystem().length()))
124
if (PedFileSystem* pedFileSystem = ped_file_system_open(originalGeometry))
126
if (PedGeometry* resizedGeometry = ped_geometry_new(pedDevice(), partition().fileSystem().firstSector(), newLength()))
128
PedTimer* pedTimer = ped_timer_new(pedTimerHandler, this);
129
rval = ped_file_system_resize(pedFileSystem, resizedGeometry, pedTimer) && commit();
130
ped_timer_destroy(pedTimer);
133
report.line() << i18nc("@info/plain", "Successfully resized file system using LibParted.");
135
report.line() << i18nc("@info/plain", "Could not resize file system on partition <filename>%1</filename>.", partition().deviceNode());
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());
140
ped_file_system_close(pedFileSystem);
143
report.line() << i18nc("@info/plain", "Could not open partition <filename>%1</filename> while trying to resize the file system.", partition().deviceNode());
146
report.line() << i18nc("@info/plain", "Could not read geometry for partition <filename>%1</filename> while trying to resize the file system.", partition().deviceNode());
153
QString ResizeFileSystemJob::description() const
156
return i18nc("@info/plain", "Maximize file system on <filename>%1</filename> to fill the partition", partition().deviceNode());
158
return i18nc("@info/plain", "Resize file system on partition <filename>%1</filename> to %2 sectors", partition().deviceNode(), newLength());