[parted-devel] [PATCH] allow to pass empty string in script mode

Arvin Schnell aschnell at suse.com
Fri Aug 13 14:02:27 BST 2021


Hi,

here is a repost of a patch from 2017 [1]. It makes it possible
to pass an empty string in script mode e.g. to set no partition
name (on GPT). I have extended the patch now to include a test.

Thanks,
  Arvin

[1] https://alioth-lists.debian.net/pipermail/parted-devel/2017-August/005100.html

---
 parted/ui.c             |  9 +++++++--
 tests/Makefile.am       |  1 +
 tests/t0290-gpt-name.sh | 41 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 49 insertions(+), 2 deletions(-)
 create mode 100755 tests/t0290-gpt-name.sh

diff --git a/parted/ui.c b/parted/ui.c
index b5948d3..25ad4f1 100644
--- a/parted/ui.c
+++ b/parted/ui.c
@@ -16,7 +16,6 @@
     You should have received a copy of the GNU General Public License
     along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
-#include <config.h>
 
 #include <config.h>
 
@@ -727,6 +726,7 @@ void
 command_line_push_line (const char* line, int multi_word)
 {
         int     quoted = 0;
+        int     quotes_empty = 0;
         char    quote_char = 0;
         char    this_word [256];
         int     i;
@@ -754,6 +754,9 @@ command_line_push_line (const char* line, int multi_word)
 
                         if (quoted && *line == quote_char) {
                                 quoted = 0;
+                                /* allow empty partition name in script mode */
+                                if (!i)
+                                        quotes_empty = 1;
                                 continue;
                         }
 
@@ -761,9 +764,11 @@ command_line_push_line (const char* line, int multi_word)
                         if (quoted && line[0] == '\\' && line[1])
                                 line++;
 
+                        quotes_empty = 0;
                         this_word [i++] = *line;
                 }
-                if (i || !multi_word) {
+                if (i || !multi_word || quotes_empty) {
+                        quotes_empty = 0;
                         this_word [i] = 0;
                         command_line_push_word (this_word);
                 }
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 3473e6b..9c4a79d 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -33,6 +33,7 @@ TESTS = \
   t0281-gpt-grow.sh \
   t0282-gpt-move-backup.sh \
   t0283-overlap-partitions.sh \
+  t0290-gpt-name.sh \
   t0300-dos-on-gpt.sh \
   t0301-overwrite-gpt-pmbr.sh \
   t0350-mac-PT-increases-sector-size.sh \
diff --git a/tests/t0290-gpt-name.sh b/tests/t0290-gpt-name.sh
new file mode 100755
index 0000000..26041b6
--- /dev/null
+++ b/tests/t0290-gpt-name.sh
@@ -0,0 +1,41 @@
+#!/bin/sh
+
+# Test setting empty GPT partition name in non-interactive mode
+
+# Copyright (C) 2021 SUSE LLC
+
+# 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/>.
+
+. "${srcdir=.}/init.sh"; path_prepend_ ../parted
+
+dev=loop-file
+
+truncate --size 50MiB "$dev" || fail=1
+
+# create partition with empty name
+parted --script "$dev" mklabel gpt mkpart '""' ext4 1MiB 49MiB > out 2>&1 || fail=1
+parted --script --machine "$dev" unit MiB print > out 2>&1 || fail=1
+grep 'MiB:::;' out || fail=1
+
+# set a non-empty name
+parted --script "$dev" name 1 "test" > out 2>&1 || fail=1
+parted --script --machine "$dev" unit MiB print > out 2>&1 || fail=1
+grep 'MiB::test:;' out || fail=1
+
+# set empty name
+parted --script "$dev" name 1 "''" > out 2>&1 || fail=1
+parted --script --machine "$dev" unit MiB print > out 2>&1 || fail=1
+grep 'MiB:::;' out || fail=1
+
+Exit $fail
-- 
Arvin Schnell, <aschnell at suse.com>
Senior Software Engineer, Research & Development

SUSE Software Solutions Germany GmbH
Maxfeldstraße 5
90409 Nürnberg
Germany

(HRB 36809, AG Nürnberg)

Geschäftsführer: Felix Imendörffer



More information about the parted-devel mailing list