[parted-devel] [PATCH] escape colon in machine output

Arvin Schnell aschnell at suse.com
Thu Jul 29 11:22:35 BST 2021


Hi,

in the machine readable output of parted the fields are separated
by colons. Several fields them self can contain colons, the
device path, the device model and the partition name. The
attached patch (already submitted 2017-11-07 in a bit different
form) protects colons and backslashes using backslashes.

In general supporting a standard format like JSON might also be
interesting since it would allow to add new fields without
risking to break existing parsers. E.g. in 2018 adding the
partition type was suggested. I also have a patch to display
max_primary_partition_count (which is not always 128 for GPT).
If there is interest I could work on that.

ciao
  Arvin

---
 parted/parted.c | 42 ++++++++++++++++++++++++++++++++++++++----
 1 file changed, 38 insertions(+), 4 deletions(-)

diff --git a/parted/parted.c b/parted/parted.c
index 22b5818..65b5ab2 100644
--- a/parted/parted.c
+++ b/parted/parted.c
@@ -985,6 +985,32 @@ _print_disk_geometry (const PedDevice *dev)
         free (cyl_size);
 }
 
+static char *
+_escape_machine_string (const char *str)
+{
+        size_t i, j;
+        char *dest;
+
+        dest = ped_malloc (2 * strlen(str) + 1);
+        if (!dest)
+                return NULL;
+
+        for (i = 0, j = 0; str[i] != '\0'; i++, j++) {
+             switch (str[i]) {
+                 case ':':
+                 case '\\':
+                     dest[j++] = '\\';
+                     /* fallthrough */
+                 default:
+                     dest[j] = str[i];
+                     break;
+             }
+        }
+        dest[j] = '\0';
+
+        return dest;
+}
+
 static void
 _print_disk_info (const PedDevice *dev, const PedDisk *diskp)
 {
@@ -1005,6 +1031,9 @@ _print_disk_info (const PedDevice *dev, const PedDisk *diskp)
         char *disk_flags = disk_print_flags (diskp);
 
         if (opt_machine_mode) {
+            char *escaped_path = _escape_machine_string (dev->path);
+            char *escaped_model = _escape_machine_string (dev->model);
+
             switch (default_unit) {
                 case PED_UNIT_CHS:      puts ("CHS;");
                                         break;
@@ -1015,9 +1044,11 @@ _print_disk_info (const PedDevice *dev, const PedDisk *diskp)
 
             }
             printf ("%s:%s:%s:%lld:%lld:%s:%s:%s;\n",
-                    dev->path, end, transport[dev->type],
+                    escaped_path, end, transport[dev->type],
                     dev->sector_size, dev->phys_sector_size,
-                    pt_name, dev->model, disk_flags);
+                    pt_name, escaped_model, disk_flags);
+            free (escaped_path);
+            free (escaped_model);
         } else {
             printf (_("Model: %s (%s)\n"),
                     dev->model, transport[dev->type]);
@@ -1289,8 +1320,11 @@ do_print (PedDevice** dev, PedDisk** diskp)
                         putchar (':');
 
                     if (has_name)
-                        printf ("%s:", ped_partition_get_name (part));
-                    else
+                    {
+                        char *escaped_name = _escape_machine_string (ped_partition_get_name (part));
+                        printf ("%s:", escaped_name);
+                        free (escaped_name);
+                    } else
                         putchar (':');
 
                     char *flags = partition_print_flags (part);
-- 
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