[parted-devel] [PATCH] Fix the script mode for mkpart and mkpartfs.

Joel Andres Granados jgranado at redhat.com
Thu Jun 19 15:47:15 UTC 2008


In scripting mode, parted used to ask the user for confirmation
when the values to be used where not the ones specified by the user.
* parted/parted.c (do_mkpart, do_mkpartfs): if opt_script_mode is.
  set fail, if it's not, warn and ask for intervention.
* tests/Makefile.am : include the new test in the TEST list.
* tests/t7000-scripting.sh : Distribute new test case.
---
 parted/parted.c          |   37 +++++++++++++++++++++++++++----------
 tests/Makefile.am        |    3 ++-
 tests/t7000-scripting.sh |   45 +++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 74 insertions(+), 11 deletions(-)
 create mode 100755 tests/t7000-scripting.sh

diff --git a/parted/parted.c b/parted/parted.c
index 9f79ea4..3b5c07a 100644
--- a/parted/parted.c
+++ b/parted/parted.c
@@ -776,14 +776,25 @@ do_mkpart (PedDevice** dev)
                         start_sol = ped_unit_format (*dev, part->geom.start);
                         end_sol   = ped_unit_format (*dev, part->geom.end);
 
+                        /* In script mode failure to use specified values is fatal.
+                         * However, in interactive mode, it merely elicits a warning
+                         * and a prompt for whether to proceed.  The same appies for
+                         * do_mkpartfs function.
+                         */
                         switch (ped_exception_throw (
-                                PED_EXCEPTION_WARNING,
-                                PED_EXCEPTION_YES_NO,
+                                (opt_script_mode
+                                 ? PED_EXCEPTION_ERROR
+                                 : PED_EXCEPTION_WARNING),
+                                (opt_script_mode 
+                                 ? PED_EXCEPTION_CANCEL
+                                 : PED_EXCEPTION_YES_NO),
                                 _("You requested a partition from %s to %s.\n"
                                   "The closest location we can manage is "
-                                  "%s to %s.  "
-                                  "Is this still acceptable to you?"),
-                                start_usr, end_usr, start_sol, end_sol))
+                                  "%s to %s."),
+                                start_usr, end_usr, start_sol, end_sol,
+                                (opt_script_mode ? ""
+                                 : _("  Is this still acceptable to you?"))))
+
                         {
                                 case PED_EXCEPTION_YES:
                                         /* all is well in this state */
@@ -937,13 +948,19 @@ do_mkpartfs (PedDevice** dev)
                         end_sol   = ped_unit_format (*dev, part->geom.end);
 
                         switch (ped_exception_throw (
-                                PED_EXCEPTION_WARNING,
-                                PED_EXCEPTION_YES_NO,
+                                (opt_script_mode
+                                 ? PED_EXCEPTION_ERROR
+                                 : PED_EXCEPTION_WARNING),
+                                (opt_script_mode 
+                                 ? PED_EXCEPTION_CANCEL
+                                 : PED_EXCEPTION_YES_NO),
                                 _("You requested a partition from %s to %s.\n"
                                   "The closest location we can manage is "
-                                  "%s to %s.  "
-                                  "Is this still acceptable to you?"),
-                                start_usr, end_usr, start_sol, end_sol)) {
+                                  "%s to %s."),
+                                start_usr, end_usr, start_sol, end_sol,
+                                (opt_script_mode ? ""
+                                 : _("  Is this still acceptable to you?"))))
+                         {
                                 case PED_EXCEPTION_YES:
                                         /* all is well in this state */
                                         break;
diff --git a/tests/Makefile.am b/tests/Makefile.am
index ebebf49..fbcad7c 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -12,7 +12,8 @@ TESTS = \
   t4100-msdos-partition-limits.sh \
   t4100-dvh-partition-limits.sh \
   t5000-tags.sh \
-  t6000-dm.sh
+  t6000-dm.sh \
+  t7000-scripting.sh
 
 EXTRA_DIST = \
   $(TESTS) test-lib.sh lvm-utils.sh
diff --git a/tests/t7000-scripting.sh b/tests/t7000-scripting.sh
new file mode 100755
index 0000000..2916223
--- /dev/null
+++ b/tests/t7000-scripting.sh
@@ -0,0 +1,45 @@
+#!/bin/sh
+
+# Copyright (C) 2008 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+test_description='Make sure the scripting option works (-s) properly.'
+
+: ${srcdir=.}
+. $srcdir/test-lib.sh
+
+# The failure message.
+cat << EOF >> err || fail=1
+Error: You requested a partition from 512B to 50.7kB.
+The closest location we can manage is 17.4kB to 33.8kB.
+EOF
+
+test_expect_success \
+    'Create the test file' \
+    'dd if=/dev/zero of=testfile bs=512 count=100 2> /dev/null'
+
+test_expect_failure \
+    'Try running parted in scriptmode' \
+    'parted -s testfile "mklabel gpt mkpart primary ext3 1s -1s" 1> out'
+
+test_expect_success \
+    'Compare the real error and the expected one' \
+    '$compare out err'
+
+test_expect_failure \
+    'Test the interactive mode' \
+    'echo n | parted ---pretend-input-tty testfile "mklabel gpt mkpart primary ext3 1s -1s"'
+
+test_done
-- 
1.5.4.1




More information about the parted-devel mailing list