2020-09-29 23:55:11 +01:00
/*
SPDX - FileCopyrightText : 2008 - 2010 Volker Lanz < vl @ fidra . de >
SPDX - FileCopyrightText : 2014 - 2017 Andrius Š tikonas < andrius @ stikonas . eu >
SPDX - License - Identifier : GPL - 3.0 - or - later
*/
2015-06-04 01:29:22 +01:00
# include "jobs/movefilesystemjob.h"
# include "core/partition.h"
# include "core/device.h"
# include "core/copysourcedevice.h"
# include "core/copytargetdevice.h"
# include "util/report.h"
# include <KLocalizedString>
/** Creates a new MoveFileSystemJob
2015-07-13 15:16:36 +01:00
@ param d the Device the Partition to move is on
@ param p the Partition to move
@ param newstart the new start sector for the Partition
2015-06-04 01:29:22 +01:00
*/
MoveFileSystemJob : : MoveFileSystemJob ( Device & d , Partition & p , qint64 newstart ) :
2015-07-13 15:16:36 +01:00
Job ( ) ,
m_Device ( d ) ,
m_Partition ( p ) ,
m_NewStart ( newstart )
2015-06-04 01:29:22 +01:00
{
}
qint32 MoveFileSystemJob : : numSteps ( ) const
{
2015-07-13 15:16:36 +01:00
return 100 ;
2015-06-04 01:29:22 +01:00
}
bool MoveFileSystemJob : : run ( Report & parent )
{
2015-07-13 15:16:36 +01:00
bool rval = false ;
Report * report = jobStarted ( parent ) ;
// A scope for moveSource and moveTarget, so CopyTargetDevice's dtor runs before we
// say we're finished: The CopyTargetDevice dtor asks the backend to close the device
// and that may take a while.
{
2017-10-26 16:20:21 +01:00
qint64 length = partition ( ) . fileSystem ( ) . lastByte ( ) - partition ( ) . fileSystem ( ) . firstByte ( ) ;
CopySourceDevice moveSource ( device ( ) , partition ( ) . fileSystem ( ) . firstByte ( ) , partition ( ) . fileSystem ( ) . lastByte ( ) ) ;
CopyTargetDevice moveTarget ( device ( ) , newStart ( ) * device ( ) . logicalSize ( ) , newStart ( ) * device ( ) . logicalSize ( ) + length ) ;
2015-07-13 15:16:36 +01:00
if ( ! moveSource . open ( ) )
2016-07-17 23:41:00 +01:00
report - > line ( ) < < xi18nc ( " @info:progress " , " Could not open file system on partition <filename>%1</filename> for moving. " , partition ( ) . deviceNode ( ) ) ;
2015-07-13 15:16:36 +01:00
else if ( ! moveTarget . open ( ) )
2016-07-17 23:41:00 +01:00
report - > line ( ) < < xi18nc ( " @info:progress " , " Could not create target for moving file system on partition <filename>%1</filename>. " , partition ( ) . deviceNode ( ) ) ;
2015-07-13 15:16:36 +01:00
else {
rval = copyBlocks ( * report , moveTarget , moveSource ) ;
if ( rval ) {
const qint64 savedLength = partition ( ) . fileSystem ( ) . length ( ) - 1 ;
partition ( ) . fileSystem ( ) . setFirstSector ( newStart ( ) ) ;
partition ( ) . fileSystem ( ) . setLastSector ( newStart ( ) + savedLength ) ;
} else if ( ! rollbackCopyBlocks ( * report , moveTarget , moveSource ) )
2016-07-17 23:41:00 +01:00
report - > line ( ) < < xi18nc ( " @info:progress " , " Rollback for file system on partition <filename>%1</filename> failed. " , partition ( ) . deviceNode ( ) ) ;
2015-07-13 15:16:36 +01:00
2016-07-17 23:41:00 +01:00
report - > line ( ) < < xi18nc ( " @info:progress " , " Closing device. This may take a few seconds. " ) ;
2015-07-13 15:16:36 +01:00
}
}
if ( rval )
rval = partition ( ) . fileSystem ( ) . updateBootSector ( * report , partition ( ) . deviceNode ( ) ) ;
jobFinished ( * report , rval ) ;
return rval ;
2015-06-04 01:29:22 +01:00
}
QString MoveFileSystemJob : : description ( ) const
{
2016-07-17 23:41:00 +01:00
return xi18nc ( " @info:progress " , " Move the file system on partition <filename>%1</filename> to sector %2 " , partition ( ) . deviceNode ( ) , newStart ( ) ) ;
2015-06-04 01:29:22 +01:00
}