[libvirt] [PATCH 2/5] qemu: Split out filling of JSONGetBlockInfo data

John Ferlan jferlan at redhat.com
Fri Sep 30 12:53:51 UTC 2016


Create a helper function which will read the device data and fill in a
hash table entry with that data.  In preparation for some more code reuse.

The helper function will take as an argument a new local static structure
in order to define the arguments. This will make it possible to combine
more code in later patches.

NB: Adjust error reporting to not use 'removable' or 'locked' as an
    argument to reporting error - rather just inline that in the message.

Signed-off-by: John Ferlan <jferlan at redhat.com>
---
 src/qemu/qemu_monitor_json.c | 117 +++++++++++++++++++++++++++----------------
 1 file changed, 74 insertions(+), 43 deletions(-)

diff --git a/src/qemu/qemu_monitor_json.c b/src/qemu/qemu_monitor_json.c
index 4af98cc..58e6105 100644
--- a/src/qemu/qemu_monitor_json.c
+++ b/src/qemu/qemu_monitor_json.c
@@ -129,6 +129,14 @@ static qemuEventHandler eventHandlers[] = {
     /* We use bsearch, so keep this list sorted.  */
 };
 
+typedef struct _qemuMonitorJSONQueryBlockArgs qemuMonitorJSONQueryBlockArgs;
+typedef qemuMonitorJSONQueryBlockArgs *qemuMonitorJSONQueryBlockArgsPtr;
+struct _qemuMonitorJSONQueryBlockArgs {
+    virJSONValuePtr dev;
+    virHashTablePtr table;
+    const char *thisdev;
+};
+
 static int
 qemuMonitorEventCompare(const void *key, const void *elt)
 {
@@ -1800,6 +1808,66 @@ qemuMonitorJSONQueryBlock(qemuMonitorPtr mon)
 }
 
 
+/* Taking a query block argument, allocate a qemuDomainDiskInfo structure,
+ * place it into the args.table hash table, and then fill in the various
+ * fields of the info structure that are pertinent.
+ *
+ * Returns -1 on failure, 0 on success
+ */
+static int
+qemuMonitorJSONQueryBlockFillBlockInfoTable(qemuMonitorJSONQueryBlockArgsPtr args)
+{
+    int ret = -1;
+    struct qemuDomainDiskInfo *info;
+    const char *status;
+    const char *thisdev;
+
+    thisdev = qemuAliasDiskDriveSkipPrefix(args->thisdev);
+
+    if (VIR_ALLOC(info) < 0)
+        goto cleanup;
+
+    if (virHashAddEntry(args->table, thisdev, info) < 0) {
+        VIR_FREE(info);
+        goto cleanup;
+    }
+
+    if (virJSONValueObjectGetBoolean(args->dev, "removable",
+                                     &info->removable) < 0) {
+        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                       _("cannot read 'removable' value"));
+        goto cleanup;
+    }
+
+    if (virJSONValueObjectGetBoolean(args->dev, "locked", &info->locked) < 0) {
+        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                       _("cannot read 'locked' value"));
+        goto cleanup;
+    }
+
+    /* 'tray_open' is present only if the device has a tray */
+    if (virJSONValueObjectGetBoolean(args->dev, "tray_open",
+                                     &info->tray_open) == 0)
+        info->tray = true;
+
+    /* presence of 'inserted' notifies that a medium is in the device */
+    if (!virJSONValueObjectGetObject(args->dev, "inserted"))
+        info->empty = true;
+
+    /* Missing io-status indicates no error */
+    if ((status = virJSONValueObjectGetString(args->dev, "io-status"))) {
+        info->io_status = qemuMonitorBlockIOStatusToError(status);
+        if (info->io_status < 0)
+            goto cleanup;
+    }
+
+    ret = 0;
+
+ cleanup:
+    return ret;
+}
+
+
 int qemuMonitorJSONGetBlockInfo(qemuMonitorPtr mon,
                                 virHashTablePtr table)
 {
@@ -1819,61 +1887,24 @@ int qemuMonitorJSONGetBlockInfo(qemuMonitorPtr mon,
     }
 
     for (i = 0; i < virJSONValueArraySize(devices); i++) {
-        virJSONValuePtr dev = virJSONValueArrayGet(devices, i);
-        struct qemuDomainDiskInfo *info;
-        const char *thisdev;
-        const char *status;
+        qemuMonitorJSONQueryBlockArgs args = {0};
 
-        if (!dev || dev->type != VIR_JSON_TYPE_OBJECT) {
+        args.dev = virJSONValueArrayGet(devices, i);
+        if (!args.dev || args.dev->type != VIR_JSON_TYPE_OBJECT) {
             virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                            _("block info device entry was not in expected format"));
             goto cleanup;
         }
 
-        if ((thisdev = virJSONValueObjectGetString(dev, "device")) == NULL) {
+        if (!(args.thisdev = virJSONValueObjectGetString(args.dev, "device"))) {
             virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                            _("block info device entry was not in expected format"));
             goto cleanup;
         }
 
-        thisdev = qemuAliasDiskDriveSkipPrefix(thisdev);
-
-        if (VIR_ALLOC(info) < 0)
-            goto cleanup;
-
-        if (virHashAddEntry(table, thisdev, info) < 0) {
-            VIR_FREE(info);
-            goto cleanup;
-        }
-
-        if (virJSONValueObjectGetBoolean(dev, "removable", &info->removable) < 0) {
-            virReportError(VIR_ERR_INTERNAL_ERROR,
-                           _("cannot read %s value"),
-                           "removable");
-            goto cleanup;
-        }
-
-        if (virJSONValueObjectGetBoolean(dev, "locked", &info->locked) < 0) {
-            virReportError(VIR_ERR_INTERNAL_ERROR,
-                           _("cannot read %s value"),
-                           "locked");
+        args.table = table;
+        if (qemuMonitorJSONQueryBlockFillBlockInfoTable(&args) < 0)
             goto cleanup;
-        }
-
-        /* 'tray_open' is present only if the device has a tray */
-        if (virJSONValueObjectGetBoolean(dev, "tray_open", &info->tray_open) == 0)
-            info->tray = true;
-
-        /* presence of 'inserted' notifies that a medium is in the device */
-        if (!virJSONValueObjectGetObject(dev, "inserted"))
-            info->empty = true;
-
-        /* Missing io-status indicates no error */
-        if ((status = virJSONValueObjectGetString(dev, "io-status"))) {
-            info->io_status = qemuMonitorBlockIOStatusToError(status);
-            if (info->io_status < 0)
-                goto cleanup;
-        }
     }
 
     ret = 0;
-- 
2.7.4




More information about the libvir-list mailing list