2020-09-29 23:55:11 +01:00
/*
SPDX - FileCopyrightText : 2008 - 2010 Volker Lanz < vl @ fidra . de >
SPDX - FileCopyrightText : 2009 Andrew Coles < andrew . i . coles @ googlemail . com >
SPDX - FileCopyrightText : 2014 - 2018 Andrius Š tikonas < andrius @ stikonas . eu >
SPDX - FileCopyrightText : 2016 Chantara Tith < tith . chantara @ gmail . com >
SPDX - License - Identifier : GPL - 3.0 - or - later
*/
2015-06-04 01:29:22 +01:00
# include "jobs/resizefilesystemjob.h"
# include "core/partition.h"
# include "core/device.h"
# include "backend/corebackend.h"
# include "backend/corebackendmanager.h"
# include "backend/corebackenddevice.h"
# include "backend/corebackendpartitiontable.h"
# include "fs/filesystem.h"
# include "util/report.h"
# include "util/capacity.h"
2018-03-31 14:44:40 +01:00
# include <memory>
2015-06-04 01:29:22 +01:00
# include <QDebug>
# include <KLocalizedString>
/** Creates a new ResizeFileSystemJob
2015-07-13 15:16:36 +01:00
@ param d the Device the FileSystem to be resized is on
@ param p the Partition the FileSystem to be resized is on
@ param newlength the new length for the FileSystem ; if - 1 , the FileSystem will be resized to fill the entire Partition
2015-06-04 01:29:22 +01:00
*/
ResizeFileSystemJob : : ResizeFileSystemJob ( Device & d , Partition & p , qint64 newlength ) :
2015-07-13 15:16:36 +01:00
Job ( ) ,
m_Device ( d ) ,
m_Partition ( p ) ,
m_Maximize ( newlength = = - 1 ) ,
m_NewLength ( isMaximizing ( ) ? partition ( ) . length ( ) : newlength )
2015-06-04 01:29:22 +01:00
{
}
qint32 ResizeFileSystemJob : : numSteps ( ) const
{
2015-07-13 15:16:36 +01:00
return 100 ;
2015-06-04 01:29:22 +01:00
}
bool ResizeFileSystemJob : : run ( Report & parent )
{
2015-07-13 15:16:36 +01:00
Q_ASSERT ( partition ( ) . fileSystem ( ) . firstSector ( ) ! = - 1 ) ;
Q_ASSERT ( partition ( ) . fileSystem ( ) . lastSector ( ) ! = - 1 ) ;
Q_ASSERT ( newLength ( ) < = partition ( ) . length ( ) ) ;
if ( partition ( ) . fileSystem ( ) . firstSector ( ) = = - 1 | | partition ( ) . fileSystem ( ) . lastSector ( ) = = - 1 | | newLength ( ) > partition ( ) . length ( ) ) {
qWarning ( ) < < " file system first sector: " < < partition ( ) . fileSystem ( ) . firstSector ( ) < < " , last sector: " < < partition ( ) . fileSystem ( ) . lastSector ( ) < < " , new length: " < < newLength ( ) < < " , partition length: " < < partition ( ) . length ( ) ;
return false ;
}
bool rval = false ;
Report * report = jobStarted ( parent ) ;
if ( partition ( ) . fileSystem ( ) . length ( ) = = newLength ( ) ) {
2016-07-17 23:41:00 +01:00
report - > line ( ) < < xi18ncp ( " @info:progress " , " The file system on partition <filename>%2</filename> already has the requested length of 1 sector. " , " The file system on partition <filename>%2</filename> already has the requested length of %1 sectors. " , newLength ( ) , partition ( ) . deviceNode ( ) ) ;
2015-07-13 15:16:36 +01:00
rval = true ;
} else {
2016-07-17 23:41:00 +01:00
report - > line ( ) < < i18nc ( " @info:progress " , " Resizing file system from %1 to %2 sectors. " , partition ( ) . fileSystem ( ) . length ( ) , newLength ( ) ) ;
2015-07-13 15:16:36 +01:00
FileSystem : : CommandSupportType support = ( newLength ( ) < partition ( ) . fileSystem ( ) . length ( ) ) ? partition ( ) . fileSystem ( ) . supportShrink ( ) : partition ( ) . fileSystem ( ) . supportGrow ( ) ;
switch ( support ) {
case FileSystem : : cmdSupportBackend : {
Report * childReport = report - > newChild ( ) ;
2016-07-17 23:41:00 +01:00
childReport - > line ( ) < < xi18nc ( " @info:progress " , " Resizing a %1 file system using internal backend functions. " , partition ( ) . fileSystem ( ) . name ( ) ) ;
2015-07-13 15:16:36 +01:00
rval = resizeFileSystemBackend ( * childReport ) ;
break ;
}
case FileSystem : : cmdSupportFileSystem : {
2018-04-09 15:14:34 +01:00
const qint64 newLengthInByte = Capacity ( newLength ( ) * device ( ) . logicalSize ( ) ) . toInt ( Capacity : : Unit : : Byte ) ;
2016-08-08 02:01:35 +01:00
if ( partition ( ) . isMounted ( ) )
rval = partition ( ) . fileSystem ( ) . resizeOnline ( * report , partition ( ) . deviceNode ( ) , partition ( ) . mountPoint ( ) , newLengthInByte ) ;
else
rval = partition ( ) . fileSystem ( ) . resize ( * report , partition ( ) . deviceNode ( ) , newLengthInByte ) ;
2015-07-13 15:16:36 +01:00
break ;
}
default :
2016-07-17 23:41:00 +01:00
report - > line ( ) < < xi18nc ( " @info:progress " , " The file system on partition <filename>%1</filename> cannot be resized because there is no support for it. " , partition ( ) . deviceNode ( ) ) ;
2015-07-13 15:16:36 +01:00
break ;
}
if ( rval )
partition ( ) . fileSystem ( ) . setLastSector ( partition ( ) . fileSystem ( ) . firstSector ( ) + newLength ( ) - 1 ) ;
}
jobFinished ( * report , rval ) ;
return rval ;
2015-06-04 01:29:22 +01:00
}
bool ResizeFileSystemJob : : resizeFileSystemBackend ( Report & report )
{
2015-07-13 15:16:36 +01:00
bool rval = false ;
2015-06-04 01:29:22 +01:00
2018-03-31 14:44:40 +01:00
std : : unique_ptr < CoreBackendDevice > backendDevice = CoreBackendManager : : self ( ) - > backend ( ) - > openDevice ( device ( ) ) ;
2015-06-04 01:29:22 +01:00
2015-07-13 15:16:36 +01:00
if ( backendDevice ) {
2018-03-31 21:49:20 +01:00
std : : unique_ptr < CoreBackendPartitionTable > backendPartitionTable = backendDevice - > openPartitionTable ( ) ;
2015-06-04 01:29:22 +01:00
2015-07-13 15:16:36 +01:00
if ( backendPartitionTable ) {
2016-05-24 01:37:15 +01:00
connect ( CoreBackendManager : : self ( ) - > backend ( ) , & CoreBackend : : progress , this , & ResizeFileSystemJob : : progress ) ;
2015-07-13 15:16:36 +01:00
rval = backendPartitionTable - > resizeFileSystem ( report , partition ( ) , newLength ( ) ) ;
2016-05-24 01:37:15 +01:00
disconnect ( CoreBackendManager : : self ( ) - > backend ( ) , & CoreBackend : : progress , this , & ResizeFileSystemJob : : progress ) ;
2015-06-04 01:29:22 +01:00
2015-07-13 15:16:36 +01:00
if ( rval ) {
2016-07-17 23:41:00 +01:00
report . line ( ) < < xi18nc ( " @info:progress " , " Successfully resized file system using internal backend functions. " ) ;
2015-07-13 15:16:36 +01:00
backendPartitionTable - > commit ( ) ;
}
} else
2016-07-17 23:41:00 +01:00
report . line ( ) < < xi18nc ( " @info:progress " , " Could not open partition <filename>%1</filename> while trying to resize the file system. " , partition ( ) . deviceNode ( ) ) ;
2015-06-04 01:29:22 +01:00
2015-07-13 15:16:36 +01:00
} else
2016-07-17 23:41:00 +01:00
report . line ( ) < < xi18nc ( " @info:progress " , " Could not read geometry for partition <filename>%1</filename> while trying to resize the file system. " , partition ( ) . deviceNode ( ) ) ;
2015-06-04 01:29:22 +01:00
2015-07-13 15:16:36 +01:00
return rval ;
2015-06-04 01:29:22 +01:00
}
QString ResizeFileSystemJob : : description ( ) const
{
2015-07-13 15:16:36 +01:00
if ( isMaximizing ( ) )
2016-07-17 23:41:00 +01:00
return xi18nc ( " @info:progress " , " Maximize file system on <filename>%1</filename> to fill the partition " , partition ( ) . deviceNode ( ) ) ;
2015-06-04 01:29:22 +01:00
2016-07-17 23:41:00 +01:00
return xi18ncp ( " @info:progress " , " Resize file system on partition <filename>%2</filename> to 1 sector " , " Resize file system on partition <filename>%2</filename> to %1 sectors " , newLength ( ) , partition ( ) . deviceNode ( ) ) ;
2015-06-04 01:29:22 +01:00
}