2015-06-04 01:29:22 +01:00
/*************************************************************************
* Copyright ( C ) 2008 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/restorefilesystemjob.h"
# include "backend/corebackend.h"
# include "backend/corebackendmanager.h"
# include "backend/corebackenddevice.h"
# include "backend/corebackendpartitiontable.h"
# include "core/partition.h"
# include "core/device.h"
# include "core/copysourcefile.h"
# include "core/copytargetdevice.h"
# include "fs/filesystem.h"
# include "fs/filesystemfactory.h"
# include "util/report.h"
# include <KLocalizedString>
/** Creates a new RestoreFileSystemJob
2015-07-13 15:16:36 +01:00
@ param targetdevice the Device the FileSystem is to be restored to
@ param targetpartition the Partition the FileSystem is to be restore to
@ param filename the file name with the image file to restore
2015-06-04 01:29:22 +01:00
*/
RestoreFileSystemJob : : RestoreFileSystemJob ( Device & targetdevice , Partition & targetpartition , const QString & filename ) :
2015-07-13 15:16:36 +01:00
Job ( ) ,
m_TargetDevice ( targetdevice ) ,
m_TargetPartition ( targetpartition ) ,
m_FileName ( filename )
2015-06-04 01:29:22 +01:00
{
}
qint32 RestoreFileSystemJob : : numSteps ( ) const
{
2015-07-13 15:16:36 +01:00
return 100 ;
2015-06-04 01:29:22 +01:00
}
bool RestoreFileSystemJob : : run ( Report & parent )
{
2015-07-13 15:16:36 +01:00
// Restoring is file system independent because we currently have no way of
// detecting the file system in a given image file. We cannot even find out if the
// file the user gave us is a valid image file or just some junk.
2015-06-04 01:29:22 +01:00
2015-07-13 15:16:36 +01:00
bool rval = false ;
2015-06-04 01:29:22 +01:00
2015-07-13 15:16:36 +01:00
Report * report = jobStarted ( parent ) ;
2015-06-04 01:29:22 +01:00
2015-07-13 15:16:36 +01:00
// Again, a scope for copyTarget and copySource. See MoveFileSystemJob::run()
{
// FileSystems are restored to _partitions_, so don't use first and last sector of file system here
CopyTargetDevice copyTarget ( targetDevice ( ) , targetPartition ( ) . firstSector ( ) , targetPartition ( ) . lastSector ( ) ) ;
CopySourceFile copySource ( fileName ( ) , copyTarget . sectorSize ( ) ) ;
2015-06-04 01:29:22 +01:00
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 backup file <filename>%1</filename> to restore from. " , fileName ( ) ) ;
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 target partition <filename>%1</filename> to restore to. " , targetPartition ( ) . deviceNode ( ) ) ;
2015-07-13 15:16:36 +01:00
else {
rval = copyBlocks ( * report , copyTarget , copySource ) ;
2015-06-04 01:29:22 +01:00
2015-07-13 15:16:36 +01:00
if ( rval ) {
// create a new file system for what was restored with the length of the image file
const qint64 newLastSector = targetPartition ( ) . firstSector ( ) + copySource . length ( ) - 1 ;
2015-06-04 01:29:22 +01:00
2015-07-13 15:16:36 +01:00
CoreBackendDevice * backendDevice = CoreBackendManager : : self ( ) - > backend ( ) - > openDevice ( targetDevice ( ) . deviceNode ( ) ) ;
2015-06-04 01:29:22 +01:00
2015-07-13 15:16:36 +01:00
FileSystem : : Type t = FileSystem : : Unknown ;
2015-06-04 01:29:22 +01:00
2015-07-13 15:16:36 +01:00
if ( backendDevice ) {
CoreBackendPartitionTable * backendPartitionTable = backendDevice - > openPartitionTable ( ) ;
2015-06-04 01:29:22 +01:00
2015-07-13 15:16:36 +01:00
if ( backendPartitionTable )
t = backendPartitionTable - > detectFileSystemBySector ( * report , targetDevice ( ) , targetPartition ( ) . firstSector ( ) ) ;
}
2015-06-04 01:29:22 +01:00
2017-09-03 15:37:14 +01:00
FileSystem * fs = FileSystemFactory : : create ( t , targetPartition ( ) . firstSector ( ) , newLastSector , targetPartition ( ) . sectorSize ( ) ) ;
2015-06-04 01:29:22 +01:00
2015-07-13 15:16:36 +01:00
targetPartition ( ) . deleteFileSystem ( ) ;
targetPartition ( ) . setFileSystem ( fs ) ;
}
2015-06-04 01:29:22 +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
}
}
2015-06-04 01:29:22 +01:00
2015-07-13 15:16:36 +01:00
jobFinished ( * report , rval ) ;
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 RestoreFileSystemJob : : description ( ) const
{
2016-07-17 23:41:00 +01:00
return xi18nc ( " @info:progress " , " Restore the file system from file <filename>%1</filename> to partition <filename>%2</filename> " , fileName ( ) , targetPartition ( ) . deviceNode ( ) ) ;
2015-06-04 01:29:22 +01:00
}