[parted-devel] [PATCH 4/4] Fix resizepart when saying no to the shrink warning

Phillip Susi psusi at ubuntu.com
Mon May 7 18:55:54 BST 2018


Resizepart has to change the PedDisk to see if there are any problems
in doing so, then gives the warning about shirnking the partition.
If the user answers No ( do not change ), it did not commit the
changes to disk, but did keep them around in memory, so they could
later be committed by another command.  Fix it to discard the modified
PedDisk.
---
 NEWS                            |  3 +++
 parted/parted.c                 | 12 +++++++++---
 tests/t3200-resize-partition.sh | 36 +++++++++++++++++++++++++++++++++++-
 3 files changed, 47 insertions(+), 4 deletions(-)

diff --git a/NEWS b/NEWS
index 659119c..9fe2033 100644
--- a/NEWS
+++ b/NEWS
@@ -6,6 +6,9 @@ GNU parted NEWS                                    -*- outline -*-
 
 ** Bug Fixes
 
+  Fix resizepart to discard changes after getting the warning about
+  shirnking a partition and answering No.
+
   Fix resizepart to adjust the end to be -1 sector when using iec
   power of 2 units so that the next partition can start immediately
   following the new end, just like mkpart does.
diff --git a/parted/parted.c b/parted/parted.c
index 1d8ceeb..5afb94f 100644
--- a/parted/parted.c
+++ b/parted/parted.c
@@ -1542,6 +1542,7 @@ static int
 do_resizepart (PedDevice** dev, PedDisk** diskp)
 {
         PedDisk                 *disk = *diskp;
+        PedDisk                 *newdisk = NULL;
         PedPartition            *part = NULL;
         PedSector               start, end, oldend;
         PedGeometry             *range_end = NULL;
@@ -1555,12 +1556,13 @@ do_resizepart (PedDevice** dev, PedDisk** diskp)
         if (!disk)
                 goto error;
 
+        newdisk = ped_disk_duplicate (disk);
         if (ped_disk_is_flag_available(disk, PED_DISK_CYLINDER_ALIGNMENT))
                 if (!ped_disk_set_flag(disk, PED_DISK_CYLINDER_ALIGNMENT,
                                        alignment == ALIGNMENT_CYLINDER))
                         goto error;
 
-        if (!command_line_get_partition (_("Partition number?"), disk, &part))
+        if (!command_line_get_partition (_("Partition number?"), newdisk, &part))
                 goto error;
         if (!_partition_warn_busy (part))
                 goto error;
@@ -1574,7 +1576,7 @@ do_resizepart (PedDevice** dev, PedDisk** diskp)
         free(end_input);
         /* Do not move start of the partition */
         constraint = constraint_from_start_end_fixed_start (*dev, start, range_end);
-        if (!ped_disk_set_partition_geom (disk, part, constraint,
+        if (!ped_disk_set_partition_geom (newdisk, part, constraint,
                                           start, end))
                 goto error_destroy_constraint;
         /* warn when shrinking partition - might lose data */
@@ -1585,7 +1587,9 @@ do_resizepart (PedDevice** dev, PedDisk** diskp)
                             _("Shrinking a partition can cause data loss, " \
                               "are you sure you want to continue?")) != PED_EXCEPTION_YES)
                         goto error_destroy_constraint;
-        ped_disk_commit (disk);
+        ped_disk_commit (newdisk);
+        *diskp = newdisk;
+        newdisk = NULL;
 
         if ((*dev)->type != PED_DEVICE_FILE)
                 disk_is_modified = 1;
@@ -1597,6 +1601,8 @@ error_destroy_constraint:
 error:
         if (range_end != NULL)
                 ped_geometry_destroy (range_end);
+        if (newdisk)
+                ped_disk_destroy (newdisk);
         return rc;
 }
 
diff --git a/tests/t3200-resize-partition.sh b/tests/t3200-resize-partition.sh
index 251b545..ee71292 100755
--- a/tests/t3200-resize-partition.sh
+++ b/tests/t3200-resize-partition.sh
@@ -32,7 +32,6 @@ scsi_debug_setup_ dev_size_mb=5 > dev-name ||
   skip_ 'failed to create scsi_debug device'
 dev=$(cat dev-name)
 
-# TODO test simple shrink
 # TODO test expand past end of the disk
 # TODO test expand past begin of next partition
 # TODO test shrink before start
@@ -86,4 +85,39 @@ parted -s $dev mklabel msdos > out 2>&1 || fail=1
 # expect no output
 compare /dev/null out || fail=1
 
+# Create a partition
+parted -s $dev mkpart primary 1m 5m > out 2>&1 || fail=1
+
+# Expect no output
+compare /dev/null out || fail=1
+
+# Now shrink it
+cat > input <<EOF
+resizepart 1 4m
+No
+print
+EOF
+
+parted ---pretend-input-tty $dev > out 2>&1 < input || fail=1
+cat > out.exp <<EOF
+GNU Parted 3.2.99-14605-dirty
+Using $dev
+Welcome to GNU Parted! Type 'help' to view a list of commands.
+
                                                                          
(parted) resizepart 1 4m
+Warning: Shrinking a partition can cause data loss, are you sure you want to continue?
+
                                                                          

                                                                          
Yes/No? No
+
                                                                          
(parted) print
+Model: Linux scsi_debug (scsi)
+Disk /dev/sda: 5243kB
+Sector size (logical/physical): 512B/512B
+Partition Table: msdos
+Disk Flags: 
+
+Number  Start   End     Size    Type     File system  Flags
+ 1      1049kB  5243kB  4194kB  primary
+
+
                                                                          
(parted) 
+EOF
+compare out.exp out || fail=1
+
 Exit $fail
-- 
2.7.4




More information about the parted-devel mailing list