]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libfdisk: improve JSON output
authorKarel Zak <kzak@redhat.com>
Thu, 5 Apr 2018 10:38:22 +0000 (12:38 +0200)
committerKarel Zak <kzak@redhat.com>
Thu, 5 Apr 2018 10:38:22 +0000 (12:38 +0200)
* use fputs_quoted_json() for all data
* fix use of variables separator

Signed-off-by: Karel Zak <kzak@redhat.com>
libfdisk/src/script.c

index 972631cda5243beeae73b1ba067b95b6ce523209..02ee7876cac1376a1c2e889b66737698a6b7e042 100644 (file)
@@ -475,6 +475,13 @@ static void fput_indent(int indent, FILE *f)
                fputs("   ", f);
 }
 
+static void fput_var_separator(int *nvars, FILE *f)
+{
+       if (*nvars > 0)
+               fputs(", ", f);
+       ++(*nvars);
+}
+
 static int write_file_json(struct fdisk_script *dp, FILE *f)
 {
        struct list_head *h;
@@ -510,12 +517,13 @@ static int write_file_json(struct fdisk_script *dp, FILE *f)
                        name = "id";
 
                fput_indent(indent, f);
-               fputs_quoted_lower(name, f);
-               fputs(": ", f);
+               fputs_quoted_json_lower(name, f);
+               fputs(":", f);
                if (!num)
-                       fputs_quoted(fi->data, f);
+                       fputs_quoted_json(fi->data, f);
                else
                        fputs(fi->data, f);
+
                if (!dp->table && fi == list_last_entry(&dp->headers, struct fdisk_scriptheader, headers))
                        fputc('\n', f);
                else
@@ -540,6 +548,7 @@ static int write_file_json(struct fdisk_script *dp, FILE *f)
        fdisk_reset_iter(&itr, FDISK_ITER_FORWARD);
        while (fdisk_table_next_partition(dp->table, &itr, &pa) == 0) {
                char *p = NULL;
+               int nvars = 0;
 
                ct++;
                fput_indent(indent, f);
@@ -548,36 +557,53 @@ static int write_file_json(struct fdisk_script *dp, FILE *f)
                        p = fdisk_partname(devname, pa->partno + 1);
                if (p) {
                        DBG(SCRIPT, ul_debugobj(dp, "write %s entry", p));
-                       fputs("\"node\": ", f);
-                       fputs_quoted(p, f);
+                       fputs("\"node\":", f);
+                       fputs_quoted_json(p, f);
+                       nvars++;
                }
 
-               if (fdisk_partition_has_start(pa))
-                       fprintf(f, ", \"start\": %ju", (uintmax_t)pa->start);
-               if (fdisk_partition_has_size(pa))
-                       fprintf(f, ", \"size\": %ju", (uintmax_t)pa->size);
-
-               if (pa->type && fdisk_parttype_get_string(pa->type))
-                       fprintf(f, ", \"type\": \"%s\"", fdisk_parttype_get_string(pa->type));
-               else if (pa->type)
-                       fprintf(f, ", \"type\": \"%x\"", fdisk_parttype_get_code(pa->type));
+               if (fdisk_partition_has_start(pa)) {
+                       fput_var_separator(&nvars, f);
+                       fprintf(f, "\"start\":%ju", (uintmax_t)pa->start);
+               }
+               if (fdisk_partition_has_size(pa)) {
+                       fput_var_separator(&nvars, f);
+                       fprintf(f, "\"size\":%ju", (uintmax_t)pa->size);
+               }
+               if (pa->type && fdisk_parttype_get_string(pa->type)) {
+                       fput_var_separator(&nvars, f);
+                       fputs("\"type\":", f);
+                       fputs_quoted_json(fdisk_parttype_get_string(pa->type), f);
+               } else if (pa->type) {
+                       fput_var_separator(&nvars, f);
+                       fprintf(f, "\"type\":\"%x\"", fdisk_parttype_get_code(pa->type));
+               }
 
-               if (pa->uuid)
-                       fprintf(f, ", \"uuid\": \"%s\"", pa->uuid);
+               if (pa->uuid) {
+                       fput_var_separator(&nvars, f);
+                       fputs("\"uuid\":", f);
+                       fputs_quoted_json(pa->uuid, f);
+               }
                if (pa->name && *pa->name) {
-                       fputs(", \"name\": ", f),
-                       fputs_quoted(pa->name, f);
+                       fput_var_separator(&nvars, f);
+                       fputs("\"name\":", f),
+                       fputs_quoted_json(pa->name, f);
                }
 
                /* for MBR attr=80 means bootable */
                if (pa->attrs) {
                        struct fdisk_label *lb = script_get_label(dp);
 
-                       if (!lb || fdisk_label_get_type(lb) != FDISK_DISKLABEL_DOS)
-                               fprintf(f, ", \"attrs\": \"%s\"", pa->attrs);
+                       if (!lb || fdisk_label_get_type(lb) != FDISK_DISKLABEL_DOS) {
+                               fput_var_separator(&nvars, f);
+                               fputs("\"attrs\":", f);
+                               fputs_quoted_json(pa->attrs, f);
+                       }
+               }
+               if (fdisk_partition_is_bootable(pa)) {
+                       fput_var_separator(&nvars, f);
+                       fprintf(f, "\"bootable\":true");
                }
-               if (fdisk_partition_is_bootable(pa))
-                       fprintf(f, ", \"bootable\": true");
 
                if ((size_t)ct < fdisk_table_get_nents(dp->table))
                        fputs("},\n", f);