2015-06-04 01:29:22 +01:00
/*************************************************************************
* Copyright ( C ) 2010 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 "plugins/libparted/libparteddevice.h"
# include "plugins/libparted/libpartedpartitiontable.h"
# include "core/partitiontable.h"
# include "util/globallog.h"
# include "util/report.h"
# include <KLocalizedString>
2016-07-29 14:17:36 +01:00
LibPartedDevice : : LibPartedDevice ( const QString & deviceNode ) :
CoreBackendDevice ( deviceNode ) ,
2015-07-22 14:48:03 +01:00
m_PedDevice ( nullptr )
2015-06-04 01:29:22 +01:00
{
}
LibPartedDevice : : ~ LibPartedDevice ( )
{
2015-07-13 15:16:36 +01:00
if ( pedDevice ( ) )
close ( ) ;
2015-06-04 01:29:22 +01:00
}
bool LibPartedDevice : : open ( )
{
2015-07-22 14:48:03 +01:00
Q_ASSERT ( pedDevice ( ) = = nullptr ) ;
2015-06-04 01:29:22 +01:00
2015-07-13 15:16:36 +01:00
if ( pedDevice ( ) )
return false ;
2015-06-04 01:29:22 +01:00
2017-12-03 12:37:21 +00:00
m_PedDevice = ped_device_get ( deviceNode ( ) . toLocal8Bit ( ) . constData ( ) ) ;
2015-06-04 01:29:22 +01:00
2015-07-22 14:48:03 +01:00
return m_PedDevice ! = nullptr ;
2015-06-04 01:29:22 +01:00
}
bool LibPartedDevice : : openExclusive ( )
{
2015-07-13 15:16:36 +01:00
bool rval = open ( ) & & ped_device_open ( pedDevice ( ) ) ;
2015-06-04 01:29:22 +01:00
2015-07-13 15:16:36 +01:00
if ( rval )
setExclusive ( true ) ;
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
}
bool LibPartedDevice : : close ( )
{
2015-07-13 15:16:36 +01:00
Q_ASSERT ( pedDevice ( ) ) ;
2015-06-04 01:29:22 +01:00
2015-07-13 15:16:36 +01:00
if ( pedDevice ( ) & & isExclusive ( ) ) {
ped_device_close ( pedDevice ( ) ) ;
setExclusive ( false ) ;
}
2015-06-04 01:29:22 +01:00
2015-07-22 14:48:03 +01:00
m_PedDevice = nullptr ;
2015-07-13 15:16:36 +01:00
return true ;
2015-06-04 01:29:22 +01:00
}
CoreBackendPartitionTable * LibPartedDevice : : openPartitionTable ( )
{
2015-07-13 15:16:36 +01:00
CoreBackendPartitionTable * ptable = new LibPartedPartitionTable ( pedDevice ( ) ) ;
2015-06-04 01:29:22 +01:00
2015-07-22 14:48:03 +01:00
if ( ptable = = nullptr | | ! ptable - > open ( ) ) {
2015-07-13 15:16:36 +01:00
delete ptable ;
2015-07-22 14:48:03 +01:00
ptable = nullptr ;
2015-07-13 15:16:36 +01:00
}
2015-06-04 01:29:22 +01:00
2015-07-13 15:16:36 +01:00
return ptable ;
2015-06-04 01:29:22 +01:00
}
bool LibPartedDevice : : createPartitionTable ( Report & report , const PartitionTable & ptable )
{
2017-12-03 12:37:21 +00:00
PedDiskType * pedDiskType = ped_disk_type_get ( ptable . typeName ( ) . toLocal8Bit ( ) . constData ( ) ) ;
2015-06-04 01:29:22 +01:00
2015-07-22 14:48:03 +01:00
if ( pedDiskType = = nullptr ) {
2016-07-17 23:41:00 +01:00
report . line ( ) < < xi18nc ( " @info:progress " , " Creating partition table failed: Could not retrieve partition table type \" %1 \" for <filename>%2</filename>. " , ptable . typeName ( ) , deviceNode ( ) ) ;
2015-07-13 15:16:36 +01:00
return false ;
}
2015-06-04 01:29:22 +01:00
2017-12-03 12:37:21 +00:00
PedDevice * dev = ped_device_get ( deviceNode ( ) . toLocal8Bit ( ) . constData ( ) ) ;
2015-06-04 01:29:22 +01:00
2015-07-22 14:48:03 +01:00
if ( dev = = nullptr ) {
2016-07-17 23:41:00 +01:00
report . line ( ) < < xi18nc ( " @info:progress " , " Creating partition table failed: Could not open backend device <filename>%1</filename>. " , deviceNode ( ) ) ;
2015-07-13 15:16:36 +01:00
return false ;
}
2015-06-04 01:29:22 +01:00
2015-07-13 15:16:36 +01:00
PedDisk * disk = ped_disk_new_fresh ( dev , pedDiskType ) ;
2015-06-04 01:29:22 +01:00
2015-07-22 14:48:03 +01:00
if ( disk = = nullptr ) {
2016-07-17 23:41:00 +01:00
report . line ( ) < < xi18nc ( " @info:progress " , " Creating partition table failed: Could not create a new partition table in the backend for device <filename>%1</filename>. " , deviceNode ( ) ) ;
2015-07-13 15:16:36 +01:00
return false ;
}
2015-06-04 01:29:22 +01:00
2015-07-13 15:16:36 +01:00
return LibPartedPartitionTable : : commit ( disk ) ;
2015-06-04 01:29:22 +01:00
}