Create and use general find_extended_partition() function

The Operation class already provided find_index_extended() method and
 was used in the Operation and derived classes where required.  It
returns the index to the extended partition in the PartitionVector
object, or -1 when no extended partition exists.

There were several cases of the same functionality being open coded in
GParted_Core and Win_GParted.  Therefore move the implementation to
find_extended_partition() in PartitionVector compilation unit and use
this implementation everywhere.
This commit is contained in:
Mike Fleetwood 2016-08-21 11:56:51 +01:00 committed by Curtis Gedak
parent 786a53b43c
commit aa98107706
8 changed files with 27 additions and 56 deletions

View File

@ -66,7 +66,6 @@ public:
protected:
int find_index_original( const PartitionVector & partitions );
int find_index_new( const PartitionVector & partitions );
int find_index_extended( const PartitionVector & partitions );
void insert_unallocated( PartitionVector & partitions,
Sector start, Sector end, Byte_Value sector_size, bool inside_extended );
void substitute_new( PartitionVector & partitions );

View File

@ -77,6 +77,8 @@ private:
std::vector<Partition *> v;
};
int find_extended_partition( const PartitionVector & partitions );
} //GParted
#endif /* GPARTED_PARTITIONVECTOR_H */

View File

@ -380,17 +380,10 @@ bool GParted_Core::snap_to_mebibyte( const Device & device, Partition & partitio
// available for any following logical partition Extended Boot Record
if ( partition .type == TYPE_EXTENDED )
{
//Locate the extended partition that contains the logical partitions.
int index_extended = -1 ;
for ( unsigned int t = 0 ; t < device .partitions .size() ; t++ )
{
if ( device .partitions[ t ] .type == TYPE_EXTENDED )
index_extended = t ;
}
//If there is logical partition that starts less than 2 sectors
// from the start of this partition, then reserve a mebibyte for the EBR.
if ( index_extended != -1 )
int index_extended = find_extended_partition( device.partitions );
if ( index_extended >= 0 )
{
for ( unsigned int t = 0; t < device .partitions[ index_extended ] .logicals .size(); t++ )
{
@ -420,17 +413,10 @@ bool GParted_Core::snap_to_mebibyte( const Device & device, Partition & partitio
// required for a following logical partition Extended Boot Record
if ( partition .type == TYPE_LOGICAL )
{
//Locate the extended partition that contains the logical partitions.
int index_extended = -1 ;
for ( unsigned int t = 0 ; t < device .partitions .size() ; t++ )
{
if ( device .partitions[ t ] .type == TYPE_EXTENDED )
index_extended = t ;
}
//If there is a following logical partition that starts less than 2 sectors from
// the end of this partition, then reserve at least a mebibyte for the EBR.
if ( index_extended != -1 )
int index_extended = find_extended_partition( device.partitions );
if ( index_extended >= 0 )
{
for ( unsigned int t = 0; t < device .partitions[ index_extended ] .logicals .size(); t++ )
{

View File

@ -81,15 +81,6 @@ int Operation::find_index_new( const PartitionVector & partitions )
return -1;
}
int Operation::find_index_extended( const PartitionVector & partitions )
{
for ( unsigned int t = 0 ; t < partitions .size() ; t++ )
if ( partitions[ t ] .type == GParted::TYPE_EXTENDED )
return t ;
return -1 ;
}
void Operation::insert_unallocated( PartitionVector & partitions,
Sector start, Sector end, Byte_Value sector_size, bool inside_extended )
{
@ -110,7 +101,7 @@ void Operation::substitute_new( PartitionVector & partitions )
if ( partition_original->inside_extended )
{
index_extended = find_index_extended( partitions );
index_extended = find_extended_partition( partitions );
if ( index_extended >= 0 )
{
index = find_index_original( partitions[index_extended].logicals );
@ -146,7 +137,7 @@ void Operation::insert_new( PartitionVector & partitions )
if ( partition_new->inside_extended )
{
index_extended = find_index_extended( partitions );
index_extended = find_extended_partition( partitions );
if ( index_extended >= 0 )
{
index = find_index_new( partitions[index_extended].logicals );

View File

@ -61,8 +61,7 @@ void OperationDelete::apply_to_visual( PartitionVector & partitions )
if ( partition_original->inside_extended )
{
index_extended = find_index_extended( partitions ) ;
index_extended = find_extended_partition( partitions );
if ( index_extended >= 0 )
{
index = find_index_original( partitions[ index_extended ] .logicals ) ;

View File

@ -151,8 +151,7 @@ void OperationResizeMove::apply_normal_to_visual( PartitionVector & partitions )
if ( partition_original->inside_extended )
{
index_extended = find_index_extended( partitions ) ;
index_extended = find_extended_partition( partitions );
if ( index_extended >= 0 )
{
index = find_index_original( partitions[ index_extended ] .logicals ) ;
@ -191,13 +190,12 @@ void OperationResizeMove::apply_extended_to_visual( PartitionVector & partitions
int index_extended;
//stuff OUTSIDE extended partition
index_extended = find_index_extended( partitions ) ;
index_extended = find_extended_partition( partitions );
if ( index_extended >= 0 )
{
remove_adjacent_unallocated( partitions, index_extended ) ;
index_extended = find_index_extended( partitions ) ;
index_extended = find_extended_partition( partitions );
if ( index_extended >= 0 )
{
partitions[index_extended].sector_start = partition_new->sector_start;
@ -208,8 +206,7 @@ void OperationResizeMove::apply_extended_to_visual( PartitionVector & partitions
}
//stuff INSIDE extended partition
index_extended = find_index_extended( partitions ) ;
index_extended = find_extended_partition( partitions );
if ( index_extended >= 0 )
{
if ( partitions[ index_extended ] .logicals .size() > 0 &&

View File

@ -90,4 +90,13 @@ void PartitionVector::replace_at( size_type n, const Partition * partition )
v[n] = p;
}
// Return index of the extended partition or -1 when not found
int find_extended_partition( const PartitionVector & partitions )
{
for ( unsigned int i = 0 ; i < partitions.size() ; i ++ )
if ( partitions[i].type == TYPE_EXTENDED )
return (int)i;
return -1;
}
} //GParted

View File

@ -1783,13 +1783,9 @@ void Win_GParted::activate_resize()
g_assert( valid_display_partition_ptr( selected_partition_ptr ) ); // Bug: Not pointing at a valid display partition object
PartitionVector * display_partitions_ptr = &display_partitions;
if ( selected_partition_ptr->type == TYPE_LOGICAL )
{
unsigned int ext = 0 ;
while ( ext < display_partitions.size() && display_partitions[ext].type != TYPE_EXTENDED )
ext++;
display_partitions_ptr = &display_partitions[ext].logicals;
}
int index_extended = find_extended_partition( display_partitions );
if ( index_extended >= 0 )
display_partitions_ptr = &display_partitions[index_extended].logicals;
FS fs_cap = gparted_core.get_fs( selected_partition_ptr->get_filesystem_partition().filesystem );
Partition * working_ptn;
@ -2085,15 +2081,7 @@ void Win_GParted::activate_new()
// Check if an extended partition already exist; so that the dialog can
// decide whether to allow the creation of the only extended partition
// type or not.
bool any_extended = false;
for ( unsigned int i = 0 ; i < display_partitions.size() ; i ++ )
{
if ( display_partitions[i].type == TYPE_EXTENDED )
{
any_extended = true;
break;
}
}
bool any_extended = ( find_extended_partition( display_partitions ) >= 0 );
Dialog_Partition_New dialog( devices[current_device],
*selected_partition_ptr,
any_extended,