]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
bootctl: use output mode where "[]" is written instead for empty output
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 17 Nov 2022 14:40:54 +0000 (15:40 +0100)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 1 Dec 2022 17:14:52 +0000 (18:14 +0100)
It's easier for the caller if output is always a list, even if there are no
entries.

src/shared/bootspec.c
src/shared/json.c
src/shared/json.h

index 435fa4c1bb15976f53ca612244f4432227ef4a1c..8ae891e8a09dadc162b3addbd443ec2b1c043309 100644 (file)
@@ -1448,7 +1448,7 @@ int show_boot_entries(const BootConfig *config, JsonFormatFlags json_format) {
                                 return log_oom();
                 }
 
-                json_variant_dump(array, json_format, NULL, NULL);
+                json_variant_dump(array, json_format | JSON_FORMAT_EMPTY_ARRAY, NULL, NULL);
 
         } else {
                 for (size_t n = 0; n < config->n_entries; n++) {
index fe03f230469b78674acd10bb5e747ad52e044abe..b1ef0ed34992def5bbbae23b68e02317023c9055 100644 (file)
@@ -1810,8 +1810,12 @@ int json_variant_format(JsonVariant *v, JsonFormatFlags flags, char **ret) {
 }
 
 int json_variant_dump(JsonVariant *v, JsonFormatFlags flags, FILE *f, const char *prefix) {
-        if (!v)
-                return 0;
+        if (!v) {
+                if (flags & JSON_FORMAT_EMPTY_ARRAY)
+                        v = JSON_VARIANT_MAGIC_EMPTY_ARRAY;
+                else
+                        return 0;
+        }
 
         if (!f)
                 f = stdout;
index b1658b5f4bd1ab7f95af71c424f58837f6b57ad7..8d060e78777de8fa97145435a16a84047b460b8c 100644 (file)
@@ -194,7 +194,8 @@ typedef enum JsonFormatFlags {
         JSON_FORMAT_SSE         = 1 << 6, /* prefix/suffix with W3C server-sent events */
         JSON_FORMAT_SEQ         = 1 << 7, /* prefix/suffix with RFC 7464 application/json-seq */
         JSON_FORMAT_FLUSH       = 1 << 8, /* call fflush() after dumping JSON */
-        JSON_FORMAT_OFF         = 1 << 9, /* make json_variant_format() fail with -ENOEXEC */
+        JSON_FORMAT_EMPTY_ARRAY = 1 << 9, /* output "[]" for empty input */
+        JSON_FORMAT_OFF         = 1 << 10, /* make json_variant_format() fail with -ENOEXEC */
 } JsonFormatFlags;
 
 int json_variant_format(JsonVariant *v, JsonFormatFlags flags, char **ret);