2015-06-04 01:29:22 +01:00
/*************************************************************************
* Copyright ( C ) 2008 , 2009 by Volker Lanz < vl @ fidra . de > *
2016-03-02 19:00:31 +00:00
* Copyright ( C ) 2016 by Andrius Š tikonas < andrius @ stikonas . eu > *
2015-06-04 01:29:22 +01:00
* *
* This program is free software ; you can redistribute it and / or *
* modify it under the terms of the GNU General Public License as *
* published by the Free Software Foundation ; either version 3 of *
* the License , or ( at your option ) any later version . *
* *
* This program is distributed in the hope that it will be useful , *
* but WITHOUT ANY WARRANTY ; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the *
* GNU General Public License for more details . *
* *
* You should have received a copy of the GNU General Public License *
* along with this program . If not , see < http : //www.gnu.org/licenses/>.*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
# include "jobs/copyfilesystemjob.h"
# include "core/partition.h"
# include "core/device.h"
# include "core/copysourcedevice.h"
# include "core/copytargetdevice.h"
# include "fs/filesystem.h"
# include "util/report.h"
# include <KLocalizedString>
/** Creates a new CopyFileSystemJob
2015-07-13 15:16:36 +01:00
@ param targetdevice the Device the FileSystem is to be copied to
@ param targetpartition the Partition the FileSystem is to be copied to
@ param sourcedevice the Device the source FileSystem is on
@ param sourcepartition the Partition the source FileSystem is on
2015-06-04 01:29:22 +01:00
*/
CopyFileSystemJob : : CopyFileSystemJob ( Device & targetdevice , Partition & targetpartition , Device & sourcedevice , Partition & sourcepartition ) :
2015-07-13 15:16:36 +01:00
Job ( ) ,
m_TargetDevice ( targetdevice ) ,
m_TargetPartition ( targetpartition ) ,
m_SourceDevice ( sourcedevice ) ,
m_SourcePartition ( sourcepartition )
2015-06-04 01:29:22 +01:00
{
}
qint32 CopyFileSystemJob : : numSteps ( ) const
{
2015-07-13 15:16:36 +01:00
return 100 ;
2015-06-04 01:29:22 +01:00
}
bool CopyFileSystemJob : : run ( Report & parent )
{
2015-07-13 15:16:36 +01:00
bool rval = false ;
Report * report = jobStarted ( parent ) ;
if ( targetPartition ( ) . fileSystem ( ) . length ( ) < sourcePartition ( ) . fileSystem ( ) . length ( ) )
2016-07-17 23:41:00 +01:00
report - > line ( ) < < xi18nc ( " @info:progress " , " Cannot copy file system: File system on target partition <filename>%1</filename> is smaller than the file system on source partition <filename>%2</filename>. " , targetPartition ( ) . deviceNode ( ) , sourcePartition ( ) . deviceNode ( ) ) ;
2015-07-13 15:16:36 +01:00
else if ( sourcePartition ( ) . fileSystem ( ) . supportCopy ( ) = = FileSystem : : cmdSupportFileSystem )
rval = sourcePartition ( ) . fileSystem ( ) . copy ( * report , targetPartition ( ) . deviceNode ( ) , sourcePartition ( ) . deviceNode ( ) ) ;
else if ( sourcePartition ( ) . fileSystem ( ) . supportCopy ( ) = = FileSystem : : cmdSupportCore ) {
2017-10-26 16:20:21 +01:00
CopySourceDevice copySource ( sourceDevice ( ) , sourcePartition ( ) . fileSystem ( ) . firstByte ( ) , sourcePartition ( ) . fileSystem ( ) . lastByte ( ) ) ;
CopyTargetDevice copyTarget ( targetDevice ( ) , targetPartition ( ) . fileSystem ( ) . firstByte ( ) , targetPartition ( ) . fileSystem ( ) . lastByte ( ) ) ;
2015-07-13 15:16:36 +01:00
if ( ! copySource . open ( ) )
2016-07-17 23:41:00 +01:00
report - > line ( ) < < xi18nc ( " @info:progress " , " Could not open file system on source partition <filename>%1</filename> for copying. " , sourcePartition ( ) . deviceNode ( ) ) ;
2015-07-13 15:16:36 +01:00
else if ( ! copyTarget . open ( ) )
2016-07-17 23:41:00 +01:00
report - > line ( ) < < xi18nc ( " @info:progress " , " Could not open file system on target partition <filename>%1</filename> for copying. " , targetPartition ( ) . deviceNode ( ) ) ;
2015-07-13 15:16:36 +01:00
else {
rval = copyBlocks ( * report , copyTarget , copySource ) ;
2016-07-17 23:41:00 +01:00
report - > line ( ) < < xi18nc ( " @info:progress " , " Closing device. This may take a while, especially on slow devices like Memory Sticks. " ) ;
2015-07-13 15:16:36 +01:00
}
}
if ( rval ) {
// set the target file system to the length of the source
const qint64 newLastSector = targetPartition ( ) . fileSystem ( ) . firstSector ( ) + sourcePartition ( ) . fileSystem ( ) . length ( ) - 1 ;
targetPartition ( ) . fileSystem ( ) . setLastSector ( newLastSector ) ;
// and set a new UUID, if the target filesystem supports UUIDs
if ( targetPartition ( ) . fileSystem ( ) . supportUpdateUUID ( ) = = FileSystem : : cmdSupportFileSystem ) {
targetPartition ( ) . fileSystem ( ) . updateUUID ( * report , targetPartition ( ) . deviceNode ( ) ) ;
targetPartition ( ) . fileSystem ( ) . setUUID ( targetPartition ( ) . fileSystem ( ) . readUUID ( targetPartition ( ) . deviceNode ( ) ) ) ;
}
}
if ( rval )
rval = targetPartition ( ) . fileSystem ( ) . updateBootSector ( * report , targetPartition ( ) . deviceNode ( ) ) ;
jobFinished ( * report , rval ) ;
return rval ;
2015-06-04 01:29:22 +01:00
}
QString CopyFileSystemJob : : description ( ) const
{
2016-07-17 23:41:00 +01:00
return xi18nc ( " @info:progress " , " Copy file system on partition <filename>%1</filename> to partition <filename>%2</filename> " , sourcePartition ( ) . deviceNode ( ) , targetPartition ( ) . deviceNode ( ) ) ;
2015-06-04 01:29:22 +01:00
}