Refactor set_partition_type() setting lp_partition earlier (#746204)

Refactor GParted_Core::set_partition_type().

1) Set lp_partition variable earlier and use a single if lp_partition
   set condition, rather than in both if conditions for the normal file
   system case and the LVM2 Physical Volume case.

2) Stop calling Utils::get_filesystem_string() multiple times, instead
   save the result in a local variable.

Tidies the code a little and reorders it in preparation for the
following fix to only set the lvm partition flag when support, making
that code change simpler.

Bug 746204 - Creating partitions on pc98 table fails with lvm flag not
             available
This commit is contained in:
Mike Fleetwood 2015-03-14 09:45:56 +00:00 committed by Curtis Gedak
parent 9f6110ce4c
commit c882666e3a
1 changed files with 43 additions and 44 deletions

View File

@ -3167,55 +3167,54 @@ bool GParted_Core::set_partition_type( const Partition & partition, OperationDet
PedDisk* lp_disk = NULL ; PedDisk* lp_disk = NULL ;
if ( get_device_and_disk( partition .device_path, lp_device, lp_disk ) ) if ( get_device_and_disk( partition .device_path, lp_device, lp_disk ) )
{ {
//Lookup libparted file system type using GParted's name, as most match PedPartition* lp_partition = ped_disk_get_partition_by_sector( lp_disk, partition.get_sector() );
PedFileSystemType * fs_type = if ( lp_partition )
ped_file_system_type_get( Utils::get_filesystem_string( partition .filesystem ) .c_str() ) ;
//If not found, and FS is linux-swap, then try linux-swap(v1)
if ( ! fs_type && Utils::get_filesystem_string( partition .filesystem ) == "linux-swap" )
fs_type = ped_file_system_type_get( "linux-swap(v1)" ) ;
//If not found, and FS is linux-swap, then try linux-swap(new)
if ( ! fs_type && Utils::get_filesystem_string( partition .filesystem ) == "linux-swap" )
fs_type = ped_file_system_type_get( "linux-swap(new)" ) ;
//default is Linux (83)
if ( ! fs_type )
fs_type = ped_file_system_type_get( "ext2" ) ;
if ( fs_type && partition .filesystem != FS_LVM2_PV )
{ {
PedPartition* lp_partition = ped_disk_get_partition_by_sector( lp_disk, partition .get_sector() ) ; Glib::ustring fs_type = Utils::get_filesystem_string( partition.filesystem );
//Also clear any libparted LVM flag so that it doesn't override the file system type // Lookup libparted file system type using GParted's name, as most match
if ( lp_partition && PedFileSystemType * lp_fs_type = ped_file_system_type_get( fs_type.c_str() );
ped_partition_set_flag( lp_partition, PED_PARTITION_LVM, 0 ) &&
ped_partition_set_system( lp_partition, fs_type ) && // If not found, and FS is linux-swap, then try linux-swap(v1)
if ( ! lp_fs_type && fs_type == "linux-swap" )
lp_fs_type = ped_file_system_type_get( "linux-swap(v1)" );
// If not found, and FS is linux-swap, then try linux-swap(new)
if ( ! lp_fs_type && fs_type == "linux-swap" )
lp_fs_type = ped_file_system_type_get( "linux-swap(new)" );
// default is Linux (83)
if ( ! lp_fs_type )
lp_fs_type = ped_file_system_type_get( "ext2" );
if ( lp_fs_type && partition.filesystem != FS_LVM2_PV )
{
// Also clear any libparted LVM flag so that it doesn't
// override the file system type
if ( ped_partition_set_flag( lp_partition, PED_PARTITION_LVM, 0 ) &&
ped_partition_set_system( lp_partition, lp_fs_type ) &&
commit( lp_disk ) ) commit( lp_disk ) )
{ {
operationdetail .get_last_child() .add_child( operationdetail.get_last_child().add_child(
OperationDetail( String::ucompose( _("new partition type: %1"), OperationDetail( String::ucompose( _("new partition type: %1"),
lp_partition ->fs_type ->name ), lp_partition->fs_type->name ),
STATUS_NONE, STATUS_NONE,
FONT_ITALIC ) ) ; FONT_ITALIC ) );
return_value = true;
return_value = true ;
} }
} }
else if ( partition .filesystem == FS_LVM2_PV ) else if ( partition.filesystem == FS_LVM2_PV )
{ {
PedPartition* lp_partition = ped_disk_get_partition_by_sector( lp_disk, partition .get_sector() ) ; if ( ped_partition_set_flag( lp_partition, PED_PARTITION_LVM, 1 ) &&
if ( lp_partition &&
ped_partition_set_flag( lp_partition, PED_PARTITION_LVM, 1 ) &&
commit( lp_disk ) ) commit( lp_disk ) )
{ {
operationdetail .get_last_child() .add_child( operationdetail.get_last_child().add_child(
OperationDetail( String::ucompose( _("new partition flag: %1"), OperationDetail( String::ucompose( _("new partition flag: %1"),
ped_partition_flag_get_name( PED_PARTITION_LVM ) ), ped_partition_flag_get_name( PED_PARTITION_LVM ) ),
STATUS_NONE, STATUS_NONE,
FONT_ITALIC ) ) ; FONT_ITALIC ) );
return_value = true ; return_value = true;
}
} }
} }