diff --git a/src/Dialog_Base_Partition.cc b/src/Dialog_Base_Partition.cc index 77d88644..bfe6b403 100644 --- a/src/Dialog_Base_Partition.cc +++ b/src/Dialog_Base_Partition.cc @@ -265,47 +265,46 @@ void Dialog_Base_Partition::snap_to_cylinder(const Device& device, Partition& pa { 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; - 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; - if ( partition.type == TYPE_LOGICAL || - partition.sector_start == device .sectors - ) + if (partition.type == TYPE_LOGICAL || + 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 // logical partition (usually 63 sectors), and // (B) the Master Boot Record sector and the next track of the first - // primary partition - diff = (partition .sector_start - device .sectors) % device .cylsize ; + // primary partition. + 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 - // partition at sector 34 - diff = (partition .sector_start - 34 ) % device .cylsize ; + // partition at sector 34. + diff = (partition.sector_start - 34) % device.cylsize; } 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 ) - partition .sector_start -= diff ; + if (diff < device.cylsize / 2 || less_than_half_cylinder) + partition.sector_start -= diff; else - partition .sector_start += (device .cylsize - diff ) ; + partition.sector_start += device.cylsize - diff; } - diff = (partition .sector_end +1) % device .cylsize ; - if ( diff ) + diff = (partition.sector_end + 1) % device.cylsize; + if (diff) { - if ( diff < ( device .cylsize / 2 ) && ! less_than_half_cylinder ) - partition .sector_end -= diff ; + if (diff < device.cylsize / 2 && ! less_than_half_cylinder) + partition.sector_end -= diff; 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) { 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 - // (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 this partition is not simply queued for a reformat then - // add space minimum to force alignment to next mebibyte. - if ( (! partition .strict_start) - && (partition .free_space_before == 0) - && ( partition .status != STAT_FORMATTED) - ) + // If strict_start is set then do not adjust sector start. + // If this partition is not simply queued for a reformat then + // add space minimum to force alignment to next mebibyte. + if (! partition.strict_start && + partition.free_space_before == 0 && + partition.status != STAT_FORMATTED ) { - //Unless specifically told otherwise, the Linux kernel considers extended - // boot records to be two sectors long, in order to "leave room for LILO". - partition .sector_start += 2 ; + // Unless specifically told otherwise, the Linux kernel considers extended + // boot records to be two sectors long, in order to "leave room for LILO". + partition.sector_start += 2; } } - //Calculate difference offset from Mebibyte boundary - diff = Sector(partition .sector_start % ( MEBIBYTE / partition .sector_size )); + // Calculate difference offset from Mebibyte boundary. + diff = Sector(partition.sector_start % (MEBIBYTE / partition.sector_size)); - //Align start sector only if permitted to change start sector - if ( diff && ( (! partition .strict_start) - || ( partition .strict_start - && ( partition .status == STAT_NEW - || partition .status == STAT_COPY - ) - ) - ) - ) + // Align start sector only if permitted to change start sector. + if (diff && (! partition.strict_start || + (partition.strict_start && (partition.status == STAT_NEW || + 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 - // available for any following logical partition Extended Boot Record - if ( partition .type == TYPE_EXTENDED ) + // If this is an extended partition then check to see if sufficient space is + // available for any following logical partition Extended Boot Record. + if (partition.type == TYPE_EXTENDED) { - //If there is logical partition that starts less than 2 sectors - // from the start of this partition, then reserve a mebibyte for the EBR. - int index_extended = find_extended_partition( device.partitions ); - if ( index_extended >= 0 ) + // If there is logical partition that starts less than 2 sectors from + // the start of this partition, then reserve a mebibyte for the EBR. + 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++ ) + for (unsigned int i = 0; i < device.partitions[index_extended].logicals.size(); i++) { - if ( ( device .partitions[ index_extended ] .logicals[ t ] .type == TYPE_LOGICAL ) - && ( ( ( device .partitions[ index_extended ] .logicals[ t ] .sector_start ) - - ( partition .sector_start ) - ) - //Unless specifically told otherwise, the Linux kernel considers extended - // boot records to be two sectors long, in order to "leave room for LILO". - < 2 - ) - ) + if (device.partitions[index_extended].logicals[i].type == TYPE_LOGICAL && + // Unless specifically told otherwise, the Linux kernel considers extended + // boot records to be two sectors long, in order to "leave room for LILO". + device.partitions[index_extended].logicals[i].sector_start + - partition.sector_start < 2 ) { - partition .sector_start -= (MEBIBYTE / partition .sector_size) ; + partition.sector_start -= MEBIBYTE / partition.sector_size; } } } } } - //Align end sector - diff = (partition .sector_end + 1) % ( MEBIBYTE / partition .sector_size); - if ( diff ) - partition .sector_end -= diff ; + // Align end sector. + diff = (partition.sector_end + 1) % (MEBIBYTE / partition.sector_size); + if (diff) + partition.sector_end -= diff; - //If this is a logical partition not at end of drive then check to see if space is - // required for a following logical partition Extended Boot Record - if ( partition .type == TYPE_LOGICAL ) + // If this is a logical partition not at end of drive then check to see if space + // is required for a following logical partition Extended Boot Record. + if (partition.type == TYPE_LOGICAL) { - //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. - int index_extended = find_extended_partition( device.partitions ); - if ( index_extended >= 0 ) + // 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. + 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++ ) + for (unsigned int i = 0; i < device.partitions[index_extended].logicals.size(); i++) { - if ( ( device .partitions[ index_extended ] .logicals[ t ] .type == TYPE_LOGICAL ) - && ( device .partitions[ index_extended ] .logicals[ t ] .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 - // boot records to be two sectors long, in order to "leave room for LILO". - < 2 - ) - ) - partition .sector_end -= ( MEBIBYTE / partition .sector_size ) ; + if (device.partitions[index_extended].logicals[i].type == TYPE_LOGICAL && + device.partitions[index_extended].logicals[i].sector_start > partition.sector_end && + // Unless specifically told otherwise, the Linux kernel considers extended + // boot records to be two sectors long, in order to "leave room for LILO". + device.partitions[index_extended].logicals[i].sector_start + - partition.sector_end < 2 ) + { + partition.sector_end -= MEBIBYTE / partition.sector_size; + } } } - //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. - if ( ( index_extended != -1 ) - && ( 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 the logical partition end is beyond the end of the extended partition + // then reduce logical partition end by a mebibyte to address the overlap. + if (index_extended != -1 && + partition.sector_end > device.partitions[index_extended].sector_end ) { - if ( ( device .partitions[ t ] .type == TYPE_PRIMARY - || 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 ); + partition.sector_end -= MEBIBYTE / partition.sector_size; } } - //If this is an extended partition then check to see if the end of the - // extended partition encompasses the end of the last logical partition. - if ( partition .type == TYPE_EXTENDED ) + // 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) { - //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 t = 0; t < partition .logicals .size(); t++ ) + for (unsigned int i = 0; i < device.partitions.size(); i++) { - if ( ( partition .logicals[ t ] .type == TYPE_LOGICAL ) - && ( ( partition .logicals[ t ] .sector_end ) - > ( partition .sector_end ) - ) - ) + if ((device.partitions[i].type == TYPE_PRIMARY || + device.partitions[i].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[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 - // 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 ) - ) + // If this is an extended partition then check to see if the end of the + // extended partition encompasses the end of the last logical partition. + if (partition.type == TYPE_EXTENDED) { - 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; }