Allow creation of partition at sector 2048 when following another (#172)

[This commit message and test case is written assuming a drive with a
(logical) sector size of 512 bytes.  GParted equally well works with
other sector sizes because the limit is expressed as 1 MiB / sector
size.  Adjust the test case sector counts as needed when testing with
different sector sized drives.]

Prepare an MSDOS or GPT partitioned disk with the first partition within
the first 1 MiB.

For MSDOS use:
    dd if=/dev/zero bs=1M of=/dev/sdb
    echo 1,2047 | sfdisk -uS --force /dev/sdb

For GPT use:
    sgdisk --zap-all /dev/sdb
    sgdisk --set-alignment=1 --new 1:34:2047 /dev/sdb

In GParted create a new partition on /dev/sdb as near to the start of
the drive as possible.  GParted insists on added an extra 1 MiB of space
before the new partition, making it start at sector 4096, even though
sector 2048 is free and aligns to whole megabytes.

Delete the preceding partition.  Now GParted allows the new partition
to be created starting at sector 2048.

GParted only needs to add padding of 1 MiB to account for the partition
table at the start of the drive when the possible start is within the
first MiB, not already at the first MiB.  Fix this off by one error in
the sector comparison.

The reason this has bug has never occurred before is because it is very
unusual to have the first partition entirely within the first 1 MiB.
Normally either the (start of) the drive is free so GParted creates an
unallocated partition object starting at sector 0, so adding 1 MiB of
padding to preserve the partition table is correct; or the first
partition starts at 1 MiB so the possible start for a second partition
is much later in the drive.

Closes #172 - GParted doesn't allow creation of a partition starting at
              sector 2048 if there is a partition before it
This commit is contained in:
Mike Fleetwood 2021-10-19 19:06:20 +01:00 committed by Curtis Gedak
parent f31fbb986f
commit 0bfbac4f65
2 changed files with 2 additions and 2 deletions

View File

@ -510,7 +510,7 @@ int Dialog_Base_Partition::MB_Needed_for_Boot_Record( const Partition & partitio
)
|| ( partition .type == TYPE_LOGICAL )
/* Beginning of disk device */
|| ( partition .sector_start <= (MEBIBYTE / partition .sector_size) )
|| (partition.sector_start < (MEBIBYTE / partition.sector_size))
)
return 1 ;
else

View File

@ -1363,7 +1363,7 @@ void Win_GParted::set_valid_operations()
)
|| ( selected_partition_ptr->type == TYPE_LOGICAL )
/* Beginning of disk device */
|| ( selected_partition_ptr->sector_start <= (MEBIBYTE / selected_partition_ptr->sector_size) )
|| (selected_partition_ptr->sector_start < (MEBIBYTE / selected_partition_ptr->sector_size))
)
required_size += MEBIBYTE;