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,30 +3167,32 @@ 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 ) )
{ {
PedPartition* lp_partition = ped_disk_get_partition_by_sector( lp_disk, partition.get_sector() );
if ( lp_partition )
{
Glib::ustring fs_type = Utils::get_filesystem_string( partition.filesystem );
// Lookup libparted file system type using GParted's name, as most match // Lookup libparted file system type using GParted's name, as most match
PedFileSystemType * fs_type = PedFileSystemType * lp_fs_type = ped_file_system_type_get( fs_type.c_str() );
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 not found, and FS is linux-swap, then try linux-swap(v1)
if ( ! fs_type && Utils::get_filesystem_string( partition .filesystem ) == "linux-swap" ) if ( ! lp_fs_type && fs_type == "linux-swap" )
fs_type = ped_file_system_type_get( "linux-swap(v1)" ) ; 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 not found, and FS is linux-swap, then try linux-swap(new)
if ( ! fs_type && Utils::get_filesystem_string( partition .filesystem ) == "linux-swap" ) if ( ! lp_fs_type && fs_type == "linux-swap" )
fs_type = ped_file_system_type_get( "linux-swap(new)" ) ; lp_fs_type = ped_file_system_type_get( "linux-swap(new)" );
// default is Linux (83) // default is Linux (83)
if ( ! fs_type ) if ( ! lp_fs_type )
fs_type = ped_file_system_type_get( "ext2" ) ; lp_fs_type = ped_file_system_type_get( "ext2" );
if ( fs_type && partition .filesystem != FS_LVM2_PV ) if ( lp_fs_type && partition.filesystem != FS_LVM2_PV )
{ {
PedPartition* lp_partition = ped_disk_get_partition_by_sector( lp_disk, partition .get_sector() ) ; // Also clear any libparted LVM flag so that it doesn't
// override the file system type
//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 ) &&
if ( lp_partition && ped_partition_set_system( lp_partition, lp_fs_type ) &&
ped_partition_set_flag( lp_partition, PED_PARTITION_LVM, 0 ) &&
ped_partition_set_system( lp_partition, fs_type ) &&
commit( lp_disk ) ) commit( lp_disk ) )
{ {
operationdetail.get_last_child().add_child( operationdetail.get_last_child().add_child(
@ -3198,16 +3200,12 @@ bool GParted_Core::set_partition_type( const Partition & partition, OperationDet
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(
@ -3218,6 +3216,7 @@ bool GParted_Core::set_partition_type( const Partition & partition, OperationDet
return_value = true; return_value = true;
} }
} }
}
destroy_device_and_disk( lp_device, lp_disk ) ; destroy_device_and_disk( lp_device, lp_disk ) ;
} }