thebeanogamer / rpms / qemu-kvm

Forked from rpms/qemu-kvm 5 months ago
Clone
ed5979
From 074c89b05dae971c7118cb769fd34e22135c8f4c Mon Sep 17 00:00:00 2001
ed5979
From: Hanna Reitz <hreitz@redhat.com>
ed5979
Date: Mon, 20 Jun 2022 18:26:53 +0200
ed5979
Subject: [PATCH 06/20] block: Improve empty format-specific info dump
ed5979
ed5979
RH-Author: Hanna Czenczek <hreitz@redhat.com>
ed5979
RH-MergeRequest: 145: Show protocol-level information in qemu-img info
ed5979
RH-Bugzilla: 1860292
ed5979
RH-Acked-by: Kevin Wolf <kwolf@redhat.com>
ed5979
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
ed5979
RH-Acked-by: Stefano Garzarella <sgarzare@redhat.com>
ed5979
RH-Commit: [1/12] be551e83f426e620e673302198b51368bfd324ce (hreitz/qemu-kvm-c-9-s)
ed5979
ed5979
When a block driver supports obtaining format-specific information, but
ed5979
that object only contains optional fields, it is possible that none of
ed5979
them are present, so that dump_qobject() (called by
ed5979
bdrv_image_info_specific_dump()) will not print anything.
ed5979
ed5979
The callers of bdrv_image_info_specific_dump() put a header above this
ed5979
information ("Format specific information:\n"), which will look strange
ed5979
when there is nothing below.  Modify bdrv_image_info_specific_dump() to
ed5979
print this header instead of its callers, and only if there is indeed
ed5979
something to be printed.
ed5979
ed5979
Signed-off-by: Hanna Reitz <hreitz@redhat.com>
ed5979
Message-Id: <20220620162704.80987-2-hreitz@redhat.com>
ed5979
Reviewed-by: Kevin Wolf <kwolf@redhat.com>
ed5979
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
ed5979
(cherry picked from commit 3716470b24f0f63090d59bcf28ad8fe6fb7835bd)
ed5979
Signed-off-by: Hanna Czenczek <hreitz@redhat.com>
ed5979
---
ed5979
 block/qapi.c         | 41 +++++++++++++++++++++++++++++++++++++----
ed5979
 include/block/qapi.h |  3 ++-
ed5979
 qemu-io-cmds.c       |  4 ++--
ed5979
 3 files changed, 41 insertions(+), 7 deletions(-)
ed5979
ed5979
diff --git a/block/qapi.c b/block/qapi.c
ed5979
index cf557e3aea..51202b470a 100644
ed5979
--- a/block/qapi.c
ed5979
+++ b/block/qapi.c
ed5979
@@ -777,7 +777,35 @@ static void dump_qdict(int indentation, QDict *dict)
ed5979
     }
ed5979
 }
ed5979
 
ed5979
-void bdrv_image_info_specific_dump(ImageInfoSpecific *info_spec)
ed5979
+/*
ed5979
+ * Return whether dumping the given QObject with dump_qobject() would
ed5979
+ * yield an empty dump, i.e. not print anything.
ed5979
+ */
ed5979
+static bool qobject_is_empty_dump(const QObject *obj)
ed5979
+{
ed5979
+    switch (qobject_type(obj)) {
ed5979
+    case QTYPE_QNUM:
ed5979
+    case QTYPE_QSTRING:
ed5979
+    case QTYPE_QBOOL:
ed5979
+        return false;
ed5979
+
ed5979
+    case QTYPE_QDICT:
ed5979
+        return qdict_size(qobject_to(QDict, obj)) == 0;
ed5979
+
ed5979
+    case QTYPE_QLIST:
ed5979
+        return qlist_empty(qobject_to(QList, obj));
ed5979
+
ed5979
+    default:
ed5979
+        abort();
ed5979
+    }
ed5979
+}
ed5979
+
ed5979
+/**
ed5979
+ * Dumps the given ImageInfoSpecific object in a human-readable form,
ed5979
+ * prepending an optional prefix if the dump is not empty.
ed5979
+ */
ed5979
+void bdrv_image_info_specific_dump(ImageInfoSpecific *info_spec,
ed5979
+                                   const char *prefix)
ed5979
 {
ed5979
     QObject *obj, *data;
ed5979
     Visitor *v = qobject_output_visitor_new(&obj);
ed5979
@@ -785,7 +813,12 @@ void bdrv_image_info_specific_dump(ImageInfoSpecific *info_spec)
ed5979
     visit_type_ImageInfoSpecific(v, NULL, &info_spec, &error_abort);
ed5979
     visit_complete(v, &obj);
ed5979
     data = qdict_get(qobject_to(QDict, obj), "data");
ed5979
-    dump_qobject(1, data);
ed5979
+    if (!qobject_is_empty_dump(data)) {
ed5979
+        if (prefix) {
ed5979
+            qemu_printf("%s", prefix);
ed5979
+        }
ed5979
+        dump_qobject(1, data);
ed5979
+    }
ed5979
     qobject_unref(obj);
ed5979
     visit_free(v);
ed5979
 }
ed5979
@@ -866,7 +899,7 @@ void bdrv_image_info_dump(ImageInfo *info)
ed5979
     }
ed5979
 
ed5979
     if (info->has_format_specific) {
ed5979
-        qemu_printf("Format specific information:\n");
ed5979
-        bdrv_image_info_specific_dump(info->format_specific);
ed5979
+        bdrv_image_info_specific_dump(info->format_specific,
ed5979
+                                      "Format specific information:\n");
ed5979
     }
ed5979
 }
ed5979
diff --git a/include/block/qapi.h b/include/block/qapi.h
ed5979
index 22c7807c89..c09859ea78 100644
ed5979
--- a/include/block/qapi.h
ed5979
+++ b/include/block/qapi.h
ed5979
@@ -40,6 +40,7 @@ void bdrv_query_image_info(BlockDriverState *bs,
ed5979
                            Error **errp);
ed5979
 
ed5979
 void bdrv_snapshot_dump(QEMUSnapshotInfo *sn);
ed5979
-void bdrv_image_info_specific_dump(ImageInfoSpecific *info_spec);
ed5979
+void bdrv_image_info_specific_dump(ImageInfoSpecific *info_spec,
ed5979
+                                   const char *prefix);
ed5979
 void bdrv_image_info_dump(ImageInfo *info);
ed5979
 #endif
ed5979
diff --git a/qemu-io-cmds.c b/qemu-io-cmds.c
ed5979
index 952dc940f1..f4a374528e 100644
ed5979
--- a/qemu-io-cmds.c
ed5979
+++ b/qemu-io-cmds.c
ed5979
@@ -1825,8 +1825,8 @@ static int info_f(BlockBackend *blk, int argc, char **argv)
ed5979
         return -EIO;
ed5979
     }
ed5979
     if (spec_info) {
ed5979
-        printf("Format specific information:\n");
ed5979
-        bdrv_image_info_specific_dump(spec_info);
ed5979
+        bdrv_image_info_specific_dump(spec_info,
ed5979
+                                      "Format specific information:\n");
ed5979
         qapi_free_ImageInfoSpecific(spec_info);
ed5979
     }
ed5979
 
ed5979
-- 
ed5979
2.31.1
ed5979