On 12/02/2011 04:58 PM, Phillip Susi wrote:
> I removed some unused locals from your patch as well as the special
> treatment of extended partitions, and added the warning when
> shrinking partitions. Would you care to write the commit message?
> Attaching my complete series.

I have renamed your resize command to "resizepart".  This allows it to be back 
ported to older versions of parted, and helps to avoid mixup with the old 
command.  I have added a new stub "resize" command to make sure that any old 
scripts still trying to call it get an error explaining that it was removed 
instead of accidentally running the new resizepart command.

I still can't figure out why the help command refuses to print the detailed 
message for the resize stub, in case someone tries to call that.

From 60117a0706c786c8e7ea624e2436eeb1682f05df Mon Sep 17 00:00:00 2001
From: Petr Uzel <[email protected]>
Date: Mon, 26 Sep 2011 17:21:01 +0200
Subject: [PATCH 1/6] parted: resize command

TODO
---
 parted/parted.c |   90 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 90 insertions(+), 0 deletions(-)

diff --git a/parted/parted.c b/parted/parted.c
index 66beba6..636fa3e 100644
--- a/parted/parted.c
+++ b/parted/parted.c
@@ -152,6 +152,9 @@ static const char* fs_type_msg_start = N_("FS-TYPE is one of: ");
 static const char* start_end_msg =    N_("START and END are disk locations, such as "
                 "4GB or 10%.  Negative values count from the end of the disk.  "
                 "For example, -1s specifies exactly the last sector.\n");
+static const char* end_msg =          N_("END is disk location, such as "
+                "4GB or 10%.  Negative value counts from the end of the disk.  "
+                "For example, -1s specifies exactly the last sector.\n");
 static const char* state_msg =        N_("STATE is one of: on, off\n");
 static const char* device_msg =       N_("DEVICE is usually /dev/hda or /dev/sda\n");
 static const char* name_msg =         N_("NAME is any word you want\n");
@@ -435,6 +438,21 @@ constraint_from_start_end (PedDevice* dev, PedGeometry* range_start,
                 range_start, range_end, 1, dev->length);
 }
 
+
+static PedConstraint*
+constraint_from_start_end_fixed_start (PedDevice* dev, PedSector start_sector,
+                           PedGeometry* range_end)
+{
+        PedGeometry range_start;
+        range_start.dev = dev;
+        range_start.start = start_sector;
+        range_start.end = start_sector;
+        range_start.length = 1;
+
+        return ped_constraint_new (ped_alignment_any, ped_alignment_any,
+                &range_start, range_end, 1, dev->length);
+}
+
 void
 help_on (char* topic)
 {
@@ -1456,6 +1474,69 @@ error:
         return 0;
 }
 
+
+static int
+do_resizepart (PedDevice** dev)
+{
+        PedDisk                 *disk;
+        PedPartition            *part = NULL;
+        PedSector               start, end, oldend;
+        PedGeometry             *range_end = NULL;
+        PedConstraint*          constraint;
+
+        disk = ped_disk_new (*dev);
+        if (!disk)
+                goto error;
+
+        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_destroy_disk;
+
+        if (!command_line_get_partition (_("Partition number?"), disk, &part))
+                goto error_destroy_disk;
+        if (!_partition_warn_busy (part))
+                goto error_destroy_disk;
+
+        start = part->geom.start;
+        end = oldend = part->geom.end;
+        if (!command_line_get_sector (_("End?"), *dev, &end, &range_end, NULL))
+                goto error_destroy_disk;
+        /* 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,
+                                          start, end))
+                goto error_destroy_constraint;
+        /* warn when shrinking partition - might lose data */
+        if (part->geom.end < oldend)
+                if (ped_exception_throw (
+                            PED_EXCEPTION_WARNING,
+                            PED_EXCEPTION_YES_NO,
+                            _("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_destroy (disk);
+        ped_constraint_destroy (constraint);
+        if (range_end != NULL)
+                ped_geometry_destroy (range_end);
+
+        if ((*dev)->type != PED_DEVICE_FILE)
+                disk_is_modified = 1;
+
+        return 1;
+
+error_destroy_constraint:
+        ped_constraint_destroy (constraint);
+error_destroy_disk:
+        ped_disk_destroy (disk);
+error:
+        if (range_end != NULL)
+                ped_geometry_destroy (range_end);
+        return 0;
+}
+
+
 static int
 do_rm (PedDevice** dev)
 {
@@ -1820,6 +1901,15 @@ _("rescue START END                         rescue a lost partition near "
 NULL),
         str_list_create (_(start_end_msg), NULL), 1));
 
+//XXX: mention that this command does never move start of the partition
+command_register (commands, command_create (
+        str_list_create_unique ("resizepart", _("resizepart"), NULL),
+        do_resizepart,
+        str_list_create (
+_("resizepart NUMBER END                    resize partition NUMBER"),
+NULL),
+        str_list_create (_(number_msg), _(end_msg), NULL), 1));
+
 command_register (commands, command_create (
         str_list_create_unique ("rm", _("rm"), NULL),
         do_rm,
-- 
1.7.5.4

From 741a1bba9cd4c0cfa5888ad209b7adde2e736e86 Mon Sep 17 00:00:00 2001
From: Phillip Susi <[email protected]>
Date: Mon, 5 Dec 2011 19:24:39 -0500
Subject: [PATCH 5/6] Allow undocumented commands

Have command_print_summary print nothing if the command summary is NULL.
This allows for a command to be registered, but not documented in the
output of help.
---
 parted/command.c |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)

diff --git a/parted/command.c b/parted/command.c
index 2c31efb..7137cb3 100644
--- a/parted/command.c
+++ b/parted/command.c
@@ -118,6 +118,8 @@ command_get_names (Command** list)
 void
 command_print_summary (Command* cmd)
 {
+        if (cmd->summary == NULL)
+                return;
         fputs ("  ", stdout);
 	str_list_print_wrap (cmd->summary, screen_width(), 2, 8, stdout);
 	putchar ('\n');
-- 
1.7.5.4

From 73591f3123836be1cf315d5d76982c3754645e55 Mon Sep 17 00:00:00 2001
From: Phillip Susi <[email protected]>
Date: Mon, 5 Dec 2011 19:26:51 -0500
Subject: [PATCH 6/6] Add stub resize command for backward compatibility

To make sure that older scripts trying to use the resize command do not
accidentally run the new resizepart command by mistake, this undocumented
stub command will throw an error if called.
---
 parted/parted.c |   15 +++++++++++++++
 1 files changed, 15 insertions(+), 0 deletions(-)

diff --git a/parted/parted.c b/parted/parted.c
index 6efa35d..6f3419e 100644
--- a/parted/parted.c
+++ b/parted/parted.c
@@ -1476,6 +1476,15 @@ error:
         return 0;
 }
 
+static int
+do_resize (PedDevice **dev)
+{
+        ped_exception_throw (
+                PED_EXCEPTION_ERROR,
+                PED_EXCEPTION_CANCEL,
+                _("The resize command has been removed in parted 3.0"));
+        return 0;
+}
 
 static int
 do_resizepart (PedDevice** dev)
@@ -1903,6 +1912,12 @@ _("rescue START END                         rescue a lost partition near "
 NULL),
         str_list_create (_(start_end_msg), NULL), 1));
 
+command_register (commands, command_create (
+        str_list_create_unique ("resize", _("resize"), NULL),
+        do_resize,
+        NULL,
+        str_list_create (_("This command was removed in parted 3.0"), NULL), 1));
+
 //XXX: mention that this command does never move start of the partition
 command_register (commands, command_create (
         str_list_create_unique ("resizepart", _("resizepart"), NULL),
-- 
1.7.5.4

Attachment: signature.asc
Description: OpenPGP digital signature

Reply via email to