[gparted] Refactor set_partition_type() setting lp_partition earlier (#746204)



commit c882666e3af63951c6a2d55c09c75a5953650366
Author: Mike Fleetwood <mike fleetwood googlemail com>
Date:   Sat Mar 14 09:45:56 2015 +0000

    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

 src/GParted_Core.cc |   81 +++++++++++++++++++++++++--------------------------
 1 files changed, 40 insertions(+), 41 deletions(-)
---
diff --git a/src/GParted_Core.cc b/src/GParted_Core.cc
index 9ca0961..b207e4c 100644
--- a/src/GParted_Core.cc
+++ b/src/GParted_Core.cc
@@ -3167,55 +3167,54 @@ bool GParted_Core::set_partition_type( const Partition & partition, OperationDet
        PedDisk* lp_disk = NULL ;
        if ( get_device_and_disk( partition .device_path, lp_device, lp_disk ) )
        {
-               //Lookup libparted file system type using GParted's name, as most match
-               PedFileSystemType * fs_type = 
-                       ped_file_system_type_get( Utils::get_filesystem_string( partition .filesystem ) 
.c_str() ) ;
+               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 );
 
-               //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)" ) ;
+                       // Lookup libparted file system type using GParted's name, as most match
+                       PedFileSystemType * lp_fs_type = ped_file_system_type_get( fs_type.c_str() );
 
-               //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)" ) ;
+                       // 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)" );
 
-               //default is Linux (83)
-               if ( ! fs_type )
-                       fs_type = ped_file_system_type_get( "ext2" ) ;
+                       // 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)" );
 
-               if ( fs_type && partition .filesystem != FS_LVM2_PV )
-               {
-                       PedPartition* lp_partition = ped_disk_get_partition_by_sector( lp_disk, partition 
.get_sector() ) ;
+                       // default is Linux (83)
+                       if ( ! lp_fs_type )
+                               lp_fs_type = ped_file_system_type_get( "ext2" );
 
-                       //Also clear any libparted LVM flag so that it doesn't override the file system type
-                       if ( lp_partition                                                 &&
-                            ped_partition_set_flag( lp_partition, PED_PARTITION_LVM, 0 ) &&
-                            ped_partition_set_system( lp_partition, fs_type )            &&
-                            commit( lp_disk )                                               )
+                       if ( lp_fs_type && partition.filesystem != FS_LVM2_PV )
                        {
-                               operationdetail .get_last_child() .add_child( 
-                                       OperationDetail( String::ucompose( _("new partition type: %1"),
-                                                                          lp_partition ->fs_type ->name ),
-                                                        STATUS_NONE,
-                                                        FONT_ITALIC ) ) ;
-
-                               return_value = true ;
+                               // 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 )                                               )
+                               {
+                                       operationdetail.get_last_child().add_child(
+                                               OperationDetail( String::ucompose( _("new partition type: 
%1"),
+                                                                                  
lp_partition->fs_type->name ),
+                                                                STATUS_NONE,
+                                                                FONT_ITALIC ) );
+                                       return_value = true;
+                               }
                        }
-               }
-               else if ( partition .filesystem == FS_LVM2_PV )
-               {
-                       PedPartition* lp_partition = ped_disk_get_partition_by_sector( lp_disk, partition 
.get_sector() ) ;
-
-                       if ( lp_partition                                                 &&
-                            ped_partition_set_flag( lp_partition, PED_PARTITION_LVM, 1 ) &&
-                            commit( lp_disk )                                               )
+                       else if ( partition.filesystem == FS_LVM2_PV )
                        {
-                               operationdetail .get_last_child() .add_child(
-                                       OperationDetail( String::ucompose( _("new partition flag: %1"),
-                                                                          ped_partition_flag_get_name( 
PED_PARTITION_LVM ) ),
-                                                        STATUS_NONE,
-                                                        FONT_ITALIC ) ) ;
-                               return_value = true ;
+                               if ( ped_partition_set_flag( lp_partition, PED_PARTITION_LVM, 1 ) &&
+                                    commit( lp_disk )                                               )
+                               {
+                                       operationdetail.get_last_child().add_child(
+                                               OperationDetail( String::ucompose( _("new partition flag: 
%1"),
+                                                                                  
ped_partition_flag_get_name( PED_PARTITION_LVM ) ),
+                                                                STATUS_NONE,
+                                                                FONT_ITALIC ) );
+                                       return_value = true;
+                               }
                        }
                }
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]