Create a helper function which will do the primary work required to
find the specific device and get the disk name. Done in preparation for
some more code reuse.

Add new elements to _qemuMonitorJSONQueryBlockArgs in order to handle
the lookup of just one specific device from the query-block code.

Signed-off-by: John Ferlan <jfer...@redhat.com>
---
 src/qemu/qemu_monitor_json.c | 52 ++++++++++++++++++++++++++++++++++----------
 1 file changed, 41 insertions(+), 11 deletions(-)

diff --git a/src/qemu/qemu_monitor_json.c b/src/qemu/qemu_monitor_json.c
index db60ed4..24fb397 100644
--- a/src/qemu/qemu_monitor_json.c
+++ b/src/qemu/qemu_monitor_json.c
@@ -136,6 +136,10 @@ struct _qemuMonitorJSONQueryBlockArgs {
     virHashTablePtr table;
     const char *thisdev;
     bool backingChain;
+    const char *searchDevice;
+    virStorageSourcePtr top;
+    virStorageSourcePtr target;
+    char *foundDevice;
 };
 
 static int
@@ -4040,6 +4044,31 @@ qemuMonitorJSONDiskNameLookupOne(virJSONValuePtr image,
 }
 
 
+/* Taking a query block argument, if the current device (thisdev) is the
+ * one we're looking for (searchDevice), then call LookupDiskName. The caller
+ * will handle the case where the devices match, but the lookup call fails.
+ *
+ * Returns 0 on not found, 1 on found
+ */
+static int
+qemuMonitorJSONQueryBlockDiskNameLookup(qemuMonitorJSONQueryBlockArgsPtr args)
+{
+    virJSONValuePtr inserted;
+    virJSONValuePtr image;
+
+    if (STREQ(args->thisdev, args->searchDevice)) {
+        if ((inserted = virJSONValueObjectGetObject(args->dev, "inserted")) &&
+            (image = virJSONValueObjectGetObject(inserted, "image"))) {
+            args->foundDevice = qemuMonitorJSONDiskNameLookupOne(image,
+                                                                 args->top,
+                                                                 args->target);
+        }
+        return 1;
+    }
+    return 0;
+}
+
+
 char *
 qemuMonitorJSONDiskNameLookup(qemuMonitorPtr mon,
                               const char *device,
@@ -4061,28 +4090,29 @@ qemuMonitorJSONDiskNameLookup(qemuMonitorPtr mon,
     }
 
     for (i = 0; i < virJSONValueArraySize(devices); i++) {
-        virJSONValuePtr dev = virJSONValueArrayGet(devices, i);
-        virJSONValuePtr inserted;
-        virJSONValuePtr image;
-        const char *thisdev;
+        qemuMonitorJSONQueryBlockArgs args = {0};
+        int rc;
 
-        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"))) {
+        if (!(args.thisdev = virJSONValueObjectGetString(args.dev, "device"))) 
{
             virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                            _("block info device entry was not in expected 
format"));
             goto cleanup;
         }
 
-        if (STREQ(thisdev, device)) {
-            if ((inserted = virJSONValueObjectGetObject(dev, "inserted")) &&
-                (image = virJSONValueObjectGetObject(inserted, "image"))) {
-                ret = qemuMonitorJSONDiskNameLookupOne(image, top, target);
-            }
+        args.searchDevice = device;
+        args.top = top;
+        args.target = target;
+        if ((rc = qemuMonitorJSONQueryBlockDiskNameLookup(&args)) < 0)
+            goto cleanup;
+        if (rc == 1) {
+            ret = args.foundDevice;
             break;
         }
     }
-- 
2.7.4

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list

Reply via email to