]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libfdisk: use lib/jsonwrt.s for JSON formatting
authorKarel Zak <kzak@redhat.com>
Thu, 6 May 2021 13:02:14 +0000 (15:02 +0200)
committerKarel Zak <kzak@redhat.com>
Thu, 6 May 2021 13:02:14 +0000 (15:02 +0200)
Signed-off-by: Karel Zak <kzak@redhat.com>
libfdisk/src/script.c

index 19ad9f0b3dc66c7f0e97773c50627e859a6cf6ed..3005a3ccbbfd92f032bc7fe67e3d5362a5a56afb 100644 (file)
@@ -3,6 +3,7 @@
 #include "strutils.h"
 #include "carefulputc.h"
 #include "mangle.h"
+#include "jsonwrt.h"
 
 #ifdef FUZZ_TARGET
 #include "fuzz.h"
@@ -544,45 +545,30 @@ int fdisk_script_enable_json(struct fdisk_script *dp, int json)
        return 0;
 }
 
-static void fput_indent(int indent, FILE *f)
-{
-       int i;
-
-       for (i = 0; i <= indent; i++)
-               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;
        struct fdisk_partition *pa;
        struct fdisk_iter itr;
        const char *devname = NULL;
-       int ct = 0, indent = 0;
+       int ct = 0;
+       struct ul_jsonwrt json;
 
        assert(dp);
        assert(f);
 
        DBG(SCRIPT, ul_debugobj(dp, "writing json dump to file"));
 
-       fputs("{\n", f);
+       ul_jsonwrt_init(&json, f, 0);
+       ul_jsonwrt_root_open(&json);
 
-       fput_indent(indent, f);
-       fputs("\"partitiontable\": {\n", f);
-       indent++;
+       ul_jsonwrt_object_open(&json, "partitiontable");
 
        /* script headers */
        list_for_each(h, &dp->headers) {
                struct fdisk_scriptheader *fi = list_entry(h, struct fdisk_scriptheader, headers);
                const char *name = fi->name;
-               int num = 0;
+               int num = 0, islast = 0;
 
                if (strcmp(name, "first-lba") == 0) {
                        name = "firstlba";
@@ -596,18 +582,14 @@ static int write_file_json(struct fdisk_script *dp, FILE *f)
                } else if (strcmp(name, "label-id") == 0)
                        name = "id";
 
-               fput_indent(indent, f);
-               fputs_quoted_json_lower(name, f);
-               fputs(":", f);
-               if (!num)
-                       fputs_quoted_json(fi->data, f);
-               else
-                       fputs(fi->data, f);
+               if ((fi == list_last_entry(&dp->headers, struct fdisk_scriptheader, headers))
+                   && (!dp->table || fdisk_table_is_empty(dp->table)))
+                       islast = 1;
 
-               if ((fi == list_last_entry(&dp->headers, struct fdisk_scriptheader, headers)) && (!dp->table || fdisk_table_is_empty(dp->table)))
-                       fputc('\n', f);
+               if (num)
+                       ul_jsonwrt_value_raw(&json, name, fi->data, islast);
                else
-                       fputs(",\n", f);
+                       ul_jsonwrt_value_s(&json, name, fi->data, islast);
 
                if (strcmp(name, "device") == 0)
                        devname = fi->data;
@@ -621,84 +603,58 @@ static int write_file_json(struct fdisk_script *dp, FILE *f)
 
        DBG(SCRIPT, ul_debugobj(dp, "%zu entries", fdisk_table_get_nents(dp->table)));
 
-       fput_indent(indent, f);
-       fputs("\"partitions\": [\n", f);
-       indent++;
+       ul_jsonwrt_array_open(&json, "partitions");
 
        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);
-               fputc('{', f);
+               ul_jsonwrt_object_open(&json, NULL);
                if (devname)
                        p = fdisk_partname(devname, pa->partno + 1);
                if (p) {
                        DBG(SCRIPT, ul_debugobj(dp, "write %s entry", p));
-                       fputs("\"node\":", f);
-                       fputs_quoted_json(p, f);
-                       nvars++;
+                       ul_jsonwrt_value_s(&json, "node", p, 0);
                        free(p);
                }
 
-               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 (fdisk_partition_has_start(pa))
+                       ul_jsonwrt_value_u64(&json, "start", (uintmax_t)pa->start, 0);
 
-               if (pa->uuid) {
-                       fput_var_separator(&nvars, f);
-                       fputs("\"uuid\":", f);
-                       fputs_quoted_json(pa->uuid, f);
-               }
-               if (pa->name && *pa->name) {
-                       fput_var_separator(&nvars, f);
-                       fputs("\"name\":", f),
-                       fputs_quoted_json(pa->name, f);
+               if (fdisk_partition_has_size(pa))
+                       ul_jsonwrt_value_u64(&json, "size", (uintmax_t)pa->size, 0);
+
+               if (pa->type && fdisk_parttype_get_string(pa->type))
+                       ul_jsonwrt_value_s(&json, "type", fdisk_parttype_get_string(pa->type), 0);
+
+               else if (pa->type) {
+                       ul_jsonwrt_value_open(&json, "type");
+                       fprintf(f, "\"%x\"", fdisk_parttype_get_code(pa->type));
+                       ul_jsonwrt_value_close(&json, 0);
                }
 
+               if (pa->uuid)
+                       ul_jsonwrt_value_s(&json, "uuid", pa->uuid, 0);
+               if (pa->name && *pa->name)
+                       ul_jsonwrt_value_s(&json, "name", pa->name, 0);
+
                /* 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) {
-                               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 (!lb || fdisk_label_get_type(lb) != FDISK_DISKLABEL_DOS)
+                               ul_jsonwrt_value_s(&json, "attrs", pa->attrs, 0);
                }
 
-               if ((size_t)ct < fdisk_table_get_nents(dp->table))
-                       fputs("},\n", f);
-               else
-                       fputs("}\n", f);
+               ul_jsonwrt_value_boolean(&json, "bootable", fdisk_partition_is_bootable(pa), 1);
+               ul_jsonwrt_object_close(&json, (size_t)ct >= fdisk_table_get_nents(dp->table));
        }
 
-       indent--;
-       fput_indent(indent, f);
-       fputs("]\n", f);
+       ul_jsonwrt_array_close(&json, 1);
 done:
-       indent--;
-       fput_indent(indent, f);
-       fputs("}\n}\n", f);
+       ul_jsonwrt_object_close(&json, 1);
+       ul_jsonwrt_root_close(&json);
 
        DBG(SCRIPT, ul_debugobj(dp, "write script done"));
        return 0;