[libvirt] [PATCH v5 1/7] qemuMonitorJSONGetDeviceProps: Separate props processing

Michal Privoznik mprivozn at redhat.com
Fri May 11 15:09:12 UTC 2018


The code that processes list of device properties is going to be
reused. Therefore put it into a separate function.

Signed-off-by: Michal Privoznik <mprivozn at redhat.com>
Reviewed-by: Ján Tomko <jtomko at redhat.com>
---
 src/qemu/qemu_monitor_json.c | 81 +++++++++++++++++++++++++-------------------
 1 file changed, 47 insertions(+), 34 deletions(-)

diff --git a/src/qemu/qemu_monitor_json.c b/src/qemu/qemu_monitor_json.c
index e0ea553c41..ce7bc9e8e3 100644
--- a/src/qemu/qemu_monitor_json.c
+++ b/src/qemu/qemu_monitor_json.c
@@ -6051,6 +6051,51 @@ int qemuMonitorJSONSetObjectProperty(qemuMonitorPtr mon,
 #undef MAKE_SET_CMD
 
 
+static int
+qemuMonitorJSONParsePropsList(virJSONValuePtr cmd,
+                              virJSONValuePtr reply,
+                              char ***props)
+{
+    virJSONValuePtr data;
+    char **proplist = NULL;
+    size_t n = 0;
+    size_t i;
+    int ret = -1;
+
+    if (qemuMonitorJSONCheckReply(cmd, reply, VIR_JSON_TYPE_ARRAY) < 0)
+        goto cleanup;
+
+    data = virJSONValueObjectGetArray(reply, "return");
+    n = virJSONValueArraySize(data);
+
+    /* null-terminated list */
+    if (VIR_ALLOC_N(proplist, n + 1) < 0)
+        goto cleanup;
+
+    for (i = 0; i < n; i++) {
+        virJSONValuePtr child = virJSONValueArrayGet(data, i);
+        const char *tmp;
+
+        if (!(tmp = virJSONValueObjectGetString(child, "name"))) {
+            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                           _("reply data was missing 'name'"));
+            goto cleanup;
+        }
+
+        if (VIR_STRDUP(proplist[i], tmp) < 0)
+            goto cleanup;
+    }
+
+    ret = n;
+    *props = proplist;
+    proplist = NULL;
+
+ cleanup:
+    virStringListFree(proplist);
+    return ret;
+}
+
+
 int qemuMonitorJSONGetDeviceProps(qemuMonitorPtr mon,
                                   const char *device,
                                   char ***props)
@@ -6058,10 +6103,6 @@ int qemuMonitorJSONGetDeviceProps(qemuMonitorPtr mon,
     int ret = -1;
     virJSONValuePtr cmd;
     virJSONValuePtr reply = NULL;
-    virJSONValuePtr data;
-    char **proplist = NULL;
-    size_t n = 0;
-    size_t i;
 
     *props = NULL;
 
@@ -6078,38 +6119,10 @@ int qemuMonitorJSONGetDeviceProps(qemuMonitorPtr mon,
         goto cleanup;
     }
 
-    if (qemuMonitorJSONCheckReply(cmd, reply, VIR_JSON_TYPE_ARRAY) < 0)
-        goto cleanup;
-
-    data = virJSONValueObjectGetArray(reply, "return");
-    n = virJSONValueArraySize(data);
-
-    /* null-terminated list */
-    if (VIR_ALLOC_N(proplist, n + 1) < 0)
-        goto cleanup;
-
-    for (i = 0; i < n; i++) {
-        virJSONValuePtr child = virJSONValueArrayGet(data, i);
-        const char *tmp;
-
-        if (!(tmp = virJSONValueObjectGetString(child, "name"))) {
-            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
-                           _("device-list-properties reply data was missing 'name'"));
-            goto cleanup;
-        }
-
-        if (VIR_STRDUP(proplist[i], tmp) < 0)
-            goto cleanup;
-    }
-
-    ret = n;
-    *props = proplist;
-    proplist = NULL;
-
+    ret = qemuMonitorJSONParsePropsList(cmd, reply, props);
  cleanup:
-    virStringListFree(proplist);
-    virJSONValueFree(cmd);
     virJSONValueFree(reply);
+    virJSONValueFree(cmd);
     return ret;
 }
 
-- 
2.16.1




More information about the libvir-list mailing list