White space layout update in snap_to_mebibyte/cylinder() (#48)

The previous commit moved the code from GParted_Core to
Dialog_Base_Partition class without making a single formatting change to
ensure the code was guaranteed to work the same within that larger
commit.  Now reformat the code to current layout standards.  Making it a
separate commit simplifies the effort for both changes and improves
bisectability.

Additionally to be sure there were no code changes,
Dialog_Base_Partition.cc was compiled to assembler code with and without
this change applied and the resultant assembler code compared.  There
were no differences in the generated assembler code.

Start with the make generated g++ command for compiling
Dialog_Base_Partition.o; (1) remove the '-g' flag as inserted debugging
directives do differ; and (2) replace '-c -o Dialog_Base_Partition.o'
with '-S -o Dialog_Base_Partition.s' to produce assembler output instead
of object code.

Closes #48 - Error when moving locked LUKS-encrypted partition
This commit is contained in:
Mike Fleetwood 2019-05-22 17:21:41 +01:00 committed by Curtis Gedak
parent 7c94b7d920
commit e3b0558f62

View file

@ -265,47 +265,46 @@ void Dialog_Base_Partition::snap_to_cylinder(const Device& device, Partition& pa
{ {
Sector diff = 0; Sector diff = 0;
//Determine if partition size is less than half a disk cylinder // Determine if partition size is less than half a disk cylinder.
bool less_than_half_cylinder = false; bool less_than_half_cylinder = false;
if ( ( partition .sector_end - partition .sector_start ) < ( device .cylsize / 2 ) ) if (partition.sector_end - partition.sector_start < device.cylsize / 2)
less_than_half_cylinder = true; less_than_half_cylinder = true;
if ( partition.type == TYPE_LOGICAL || if (partition.type == TYPE_LOGICAL ||
partition.sector_start == device .sectors partition.sector_start == device.sectors )
)
{ {
//Must account the relative offset between: // Must account the relative offset between:
// (A) the Extended Boot Record sector and the next track of the // (A) the Extended Boot Record sector and the next track of the
// logical partition (usually 63 sectors), and // logical partition (usually 63 sectors), and
// (B) the Master Boot Record sector and the next track of the first // (B) the Master Boot Record sector and the next track of the first
// primary partition // primary partition.
diff = (partition .sector_start - device .sectors) % device .cylsize ; diff = (partition.sector_start - device.sectors) % device.cylsize;
} }
else if ( partition.sector_start == 34 ) else if (partition.sector_start == 34)
{ {
// (C) the GUID Partition Table (GPT) and the start of the data // (C) the GUID Partition Table (GPT) and the start of the data
// partition at sector 34 // partition at sector 34.
diff = (partition .sector_start - 34 ) % device .cylsize ; diff = (partition.sector_start - 34) % device.cylsize;
} }
else else
{ {
diff = partition .sector_start % device .cylsize ; diff = partition.sector_start % device.cylsize;
} }
if ( diff && ! partition .strict_start ) if (diff && ! partition.strict_start)
{ {
if ( diff < ( device .cylsize / 2 ) || less_than_half_cylinder ) if (diff < device.cylsize / 2 || less_than_half_cylinder)
partition .sector_start -= diff ; partition.sector_start -= diff;
else else
partition .sector_start += (device .cylsize - diff ) ; partition.sector_start += device.cylsize - diff;
} }
diff = (partition .sector_end +1) % device .cylsize ; diff = (partition.sector_end + 1) % device.cylsize;
if ( diff ) if (diff)
{ {
if ( diff < ( device .cylsize / 2 ) && ! less_than_half_cylinder ) if (diff < device.cylsize / 2 && ! less_than_half_cylinder)
partition .sector_end -= diff ; partition.sector_end -= diff;
else else
partition .sector_end += (device .cylsize - diff ) ; partition.sector_end += device.cylsize - diff;
} }
} }
@ -313,157 +312,140 @@ void Dialog_Base_Partition::snap_to_cylinder(const Device& device, Partition& pa
void Dialog_Base_Partition::snap_to_mebibyte(const Device& device, Partition& partition) void Dialog_Base_Partition::snap_to_mebibyte(const Device& device, Partition& partition)
{ {
Sector diff = 0; Sector diff = 0;
if ( partition .sector_start < 2 || partition .type == TYPE_LOGICAL ) if (partition.sector_start < 2 || partition.type == TYPE_LOGICAL)
{ {
//Must account the relative offset between: // Must account the relative offset between:
// (A) the Master Boot Record sector and the first primary/extended partition, and // (A) the Master Boot Record sector and the first primary/extended partition, and
// (B) the Extended Boot Record sector and the logical partition // (B) the Extended Boot Record sector and the logical partition.
//If strict_start is set then do not adjust sector start. // If strict_start is set then do not adjust sector start.
//If this partition is not simply queued for a reformat then // If this partition is not simply queued for a reformat then
// add space minimum to force alignment to next mebibyte. // add space minimum to force alignment to next mebibyte.
if ( (! partition .strict_start) if (! partition.strict_start &&
&& (partition .free_space_before == 0) partition.free_space_before == 0 &&
&& ( partition .status != STAT_FORMATTED) partition.status != STAT_FORMATTED )
)
{ {
//Unless specifically told otherwise, the Linux kernel considers extended // Unless specifically told otherwise, the Linux kernel considers extended
// boot records to be two sectors long, in order to "leave room for LILO". // boot records to be two sectors long, in order to "leave room for LILO".
partition .sector_start += 2 ; partition.sector_start += 2;
} }
} }
//Calculate difference offset from Mebibyte boundary // Calculate difference offset from Mebibyte boundary.
diff = Sector(partition .sector_start % ( MEBIBYTE / partition .sector_size )); diff = Sector(partition.sector_start % (MEBIBYTE / partition.sector_size));
//Align start sector only if permitted to change start sector // Align start sector only if permitted to change start sector.
if ( diff && ( (! partition .strict_start) if (diff && (! partition.strict_start ||
|| ( partition .strict_start (partition.strict_start && (partition.status == STAT_NEW ||
&& ( partition .status == STAT_NEW partition.status == STAT_COPY )) ))
|| partition .status == STAT_COPY
)
)
)
)
{ {
partition .sector_start += ( (MEBIBYTE / partition .sector_size) - diff) ; partition.sector_start += MEBIBYTE / partition.sector_size - diff;
//If this is an extended partition then check to see if sufficient space is // If this is an extended partition then check to see if sufficient space is
// available for any following logical partition Extended Boot Record // available for any following logical partition Extended Boot Record.
if ( partition .type == TYPE_EXTENDED ) if (partition.type == TYPE_EXTENDED)
{ {
//If there is logical partition that starts less than 2 sectors // If there is logical partition that starts less than 2 sectors from
// from the start of this partition, then reserve a mebibyte for the EBR. // the start of this partition, then reserve a mebibyte for the EBR.
int index_extended = find_extended_partition( device.partitions ); int index_extended = find_extended_partition(device.partitions);
if ( index_extended >= 0 ) if (index_extended >= 0)
{ {
for ( unsigned int t = 0; t < device .partitions[ index_extended ] .logicals .size(); t++ ) for (unsigned int i = 0; i < device.partitions[index_extended].logicals.size(); i++)
{ {
if ( ( device .partitions[ index_extended ] .logicals[ t ] .type == TYPE_LOGICAL ) if (device.partitions[index_extended].logicals[i].type == TYPE_LOGICAL &&
&& ( ( ( device .partitions[ index_extended ] .logicals[ t ] .sector_start ) // Unless specifically told otherwise, the Linux kernel considers extended
- ( partition .sector_start ) // boot records to be two sectors long, in order to "leave room for LILO".
) device.partitions[index_extended].logicals[i].sector_start
//Unless specifically told otherwise, the Linux kernel considers extended - partition.sector_start < 2 )
// boot records to be two sectors long, in order to "leave room for LILO".
< 2
)
)
{ {
partition .sector_start -= (MEBIBYTE / partition .sector_size) ; partition.sector_start -= MEBIBYTE / partition.sector_size;
} }
} }
} }
} }
} }
//Align end sector // Align end sector.
diff = (partition .sector_end + 1) % ( MEBIBYTE / partition .sector_size); diff = (partition.sector_end + 1) % (MEBIBYTE / partition.sector_size);
if ( diff ) if (diff)
partition .sector_end -= diff ; partition.sector_end -= diff;
//If this is a logical partition not at end of drive then check to see if space is // If this is a logical partition not at end of drive then check to see if space
// required for a following logical partition Extended Boot Record // is required for a following logical partition Extended Boot Record.
if ( partition .type == TYPE_LOGICAL ) if (partition.type == TYPE_LOGICAL)
{ {
//If there is a following logical partition that starts less than 2 sectors from // If there is a following logical partition that starts less than 2 sectors
// the end of this partition, then reserve at least a mebibyte for the EBR. // from the end of this partition, then reserve at least a mebibyte for the EBR.
int index_extended = find_extended_partition( device.partitions ); int index_extended = find_extended_partition(device.partitions);
if ( index_extended >= 0 ) if (index_extended >= 0)
{ {
for ( unsigned int t = 0; t < device .partitions[ index_extended ] .logicals .size(); t++ ) for (unsigned int i = 0; i < device.partitions[index_extended].logicals.size(); i++)
{ {
if ( ( device .partitions[ index_extended ] .logicals[ t ] .type == TYPE_LOGICAL ) if (device.partitions[index_extended].logicals[i].type == TYPE_LOGICAL &&
&& ( device .partitions[ index_extended ] .logicals[ t ] .sector_start > partition .sector_end ) device.partitions[index_extended].logicals[i].sector_start > partition.sector_end &&
&& ( ( device .partitions[ index_extended ] .logicals[ t ] .sector_start - partition .sector_end ) // Unless specifically told otherwise, the Linux kernel considers extended
//Unless specifically told otherwise, the Linux kernel considers extended // boot records to be two sectors long, in order to "leave room for LILO".
// boot records to be two sectors long, in order to "leave room for LILO". device.partitions[index_extended].logicals[i].sector_start
< 2 - partition.sector_end < 2 )
) {
) partition.sector_end -= MEBIBYTE / partition.sector_size;
partition .sector_end -= ( MEBIBYTE / partition .sector_size ) ; }
} }
} }
//If the logical partition end is beyond the end of the extended partition // If the logical partition end is beyond the end of the extended partition
// then reduce logical partition end by a mebibyte to address the overlap. // then reduce logical partition end by a mebibyte to address the overlap.
if ( ( index_extended != -1 ) if (index_extended != -1 &&
&& ( partition .sector_end > device .partitions[ index_extended ] .sector_end ) partition.sector_end > device.partitions[index_extended].sector_end )
)
partition .sector_end -= ( MEBIBYTE / partition .sector_size ) ;
}
//If this is a primary or an extended partition and the partition overlaps
// the start of the next primary or extended partition then subtract a
// mebibyte from the end of the partition to address the overlap.
if ( partition .type == TYPE_PRIMARY || partition .type == TYPE_EXTENDED )
{
for ( unsigned int t = 0 ; t < device .partitions .size() ; t++ )
{ {
if ( ( device .partitions[ t ] .type == TYPE_PRIMARY partition.sector_end -= MEBIBYTE / partition.sector_size;
|| device .partitions[ t ] .type == TYPE_EXTENDED
)
&& ( //For a change to an existing partition, (e.g., move or resize)
// skip comparing to original partition and
// only compare to other existing partitions
partition .status == STAT_REAL
&& partition .partition_number != device. partitions[ t ] .partition_number
)
&& ( device .partitions[ t ] .sector_start > partition .sector_start )
&& ( device .partitions[ t ] .sector_start <= partition .sector_end )
)
partition .sector_end -= ( MEBIBYTE / partition .sector_size );
} }
} }
//If this is an extended partition then check to see if the end of the // If this is a primary or an extended partition and the partition overlaps
// extended partition encompasses the end of the last logical partition. // the start of the next primary or extended partition then subtract a
if ( partition .type == TYPE_EXTENDED ) // mebibyte from the end of the partition to address the overlap.
if (partition.type == TYPE_PRIMARY || partition.type == TYPE_EXTENDED)
{ {
//If there is logical partition that has an end sector beyond the for (unsigned int i = 0; i < device.partitions.size(); i++)
// end of the extended partition, then set the extended partition
// end sector to be the same as the end of the logical partition.
for ( unsigned int t = 0; t < partition .logicals .size(); t++ )
{ {
if ( ( partition .logicals[ t ] .type == TYPE_LOGICAL ) if ((device.partitions[i].type == TYPE_PRIMARY ||
&& ( ( partition .logicals[ t ] .sector_end ) device.partitions[i].type == TYPE_EXTENDED ) &&
> ( partition .sector_end ) // For a change to an existing partition, (e.g., move or resize)
) // skip comparing to original partition and only compare to
) // other existing partitions.
partition.status == STAT_REAL &&
partition.partition_number != device.partitions[i].partition_number &&
device.partitions[i].sector_start > partition.sector_start &&
device.partitions[i].sector_start <= partition.sector_end )
{ {
partition .sector_end = partition .logicals[ t ] .sector_end ; partition.sector_end -= MEBIBYTE / partition.sector_size;
} }
} }
} }
//If this is a GPT partition table and the partition ends less than 34 sectors // If this is an extended partition then check to see if the end of the
// from the end of the device, then reserve at least a mebibyte for the // extended partition encompasses the end of the last logical partition.
// backup partition table if (partition.type == TYPE_EXTENDED)
if ( device .disktype == "gpt"
&& ( ( device .length - partition .sector_end ) < 34 )
)
{ {
partition .sector_end -= ( MEBIBYTE / partition .sector_size ) ; // If there is logical partition that has an end sector beyond the
// end of the extended partition, then set the extended partition
// end sector to be the same as the end of the logical partition.
for (unsigned int i = 0; i < partition.logicals.size(); i++)
{
if (partition.logicals[i].type == TYPE_LOGICAL &&
partition.logicals[i].sector_end > partition.sector_end )
{
partition.sector_end = partition.logicals[i].sector_end;
}
}
} }
// If this is a GPT partition table and the partition ends less than 34 sectors
// from the end of the device, then reserve at least a mebibyte for the backup
// partition table.
if (device.disktype == "gpt" && device.length - partition.sector_end < 34)
partition.sector_end -= MEBIBYTE / partition.sector_size;
} }