[libvirt] [PATCH 3/8] Split out virQEMUCapsSetFromNodes

Ján Tomko posted 8 patches 6 years, 11 months ago
[libvirt] [PATCH 3/8] Split out virQEMUCapsSetFromNodes
Posted by Ján Tomko 6 years, 11 months ago
Add a function that will convert the "name" attributes from the passed
nodes to QEMU capabilities.

Signed-off-by: Ján Tomko <jtomko@redhat.com>
---
 src/qemu/qemu_capabilities.c | 51 +++++++++++++++++++++++++-----------
 1 file changed, 35 insertions(+), 16 deletions(-)

diff --git a/src/qemu/qemu_capabilities.c b/src/qemu/qemu_capabilities.c
index 7160860ab4..a355ee2e37 100644
--- a/src/qemu/qemu_capabilities.c
+++ b/src/qemu/qemu_capabilities.c
@@ -3442,6 +3442,38 @@ virQEMUCapsParseSEVInfo(virQEMUCapsPtr qemuCaps, xmlXPathContextPtr ctxt)
 }
 
 
+static int
+virQEMUCapsSetFromNodes(virQEMUCapsPtr qemuCaps,
+                        xmlNodePtr *nodes,
+                        size_t n)
+{
+    size_t i;
+    char *str = NULL;
+    int ret = -1;
+
+    for (i = 0; i < n; i++) {
+        int flag;
+        if (!(str = virXMLPropString(nodes[i], "name"))) {
+            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                           _("missing flag name in QEMU capabilities cache"));
+            goto cleanup;
+        }
+        flag = virQEMUCapsTypeFromString(str);
+        if (flag < 0) {
+            virReportError(VIR_ERR_INTERNAL_ERROR,
+                           _("Unknown qemu capabilities flag %s"), str);
+            goto cleanup;
+        }
+        VIR_FREE(str);
+        virQEMUCapsSet(qemuCaps, flag);
+    }
+    ret = 0;
+ cleanup:
+    VIR_FREE(str);
+    return ret;
+}
+
+
 /*
  * Parsing a doc that looks like
  *
@@ -3515,22 +3547,9 @@ virQEMUCapsLoadCache(virArch hostArch,
         goto cleanup;
     }
     VIR_DEBUG("Got flags %d", n);
-    for (i = 0; i < n; i++) {
-        int flag;
-        if (!(str = virXMLPropString(nodes[i], "name"))) {
-            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
-                           _("missing flag name in QEMU capabilities cache"));
-            goto cleanup;
-        }
-        flag = virQEMUCapsTypeFromString(str);
-        if (flag < 0) {
-            virReportError(VIR_ERR_INTERNAL_ERROR,
-                           _("Unknown qemu capabilities flag %s"), str);
-            goto cleanup;
-        }
-        VIR_FREE(str);
-        virQEMUCapsSet(qemuCaps, flag);
-    }
+
+    if (virQEMUCapsSetFromNodes(qemuCaps, nodes, n) < 0)
+        goto cleanup;
     VIR_FREE(nodes);
 
     if (virXPathUInt("string(./version)", ctxt, &qemuCaps->version) < 0) {
-- 
2.19.2

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH 3/8] Split out virQEMUCapsSetFromNodes
Posted by Michal Privoznik 6 years, 11 months ago
On 2/21/19 4:42 PM, Ján Tomko wrote:
> Add a function that will convert the "name" attributes from the passed
> nodes to QEMU capabilities.
> 
> Signed-off-by: Ján Tomko <jtomko@redhat.com>
> ---
>   src/qemu/qemu_capabilities.c | 51 +++++++++++++++++++++++++-----------
>   1 file changed, 35 insertions(+), 16 deletions(-)
> 
> diff --git a/src/qemu/qemu_capabilities.c b/src/qemu/qemu_capabilities.c
> index 7160860ab4..a355ee2e37 100644
> --- a/src/qemu/qemu_capabilities.c
> +++ b/src/qemu/qemu_capabilities.c
> @@ -3442,6 +3442,38 @@ virQEMUCapsParseSEVInfo(virQEMUCapsPtr qemuCaps, xmlXPathContextPtr ctxt)
>   }
>   
>   
> +static int
> +virQEMUCapsSetFromNodes(virQEMUCapsPtr qemuCaps,
> +                        xmlNodePtr *nodes,
> +                        size_t n)
> +{
> +    size_t i;
> +    char *str = NULL;

VIR_AUTOFREE()

And if you declare the variable only in the loop body then ..

> +    int ret = -1;
> +
> +    for (i = 0; i < n; i++) {
> +        int flag;
> +        if (!(str = virXMLPropString(nodes[i], "name"))) {
> +            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
> +                           _("missing flag name in QEMU capabilities cache"));
> +            goto cleanup;
> +        }
> +        flag = virQEMUCapsTypeFromString(str);
> +        if (flag < 0) {
> +            virReportError(VIR_ERR_INTERNAL_ERROR,
> +                           _("Unknown qemu capabilities flag %s"), str);
> +            goto cleanup;
> +        }
> +        VIR_FREE(str);

.. this can be dropped too.

> +        virQEMUCapsSet(qemuCaps, flag);
> +    }
> +    ret = 0;
> + cleanup:
> +    VIR_FREE(str);
> +    return ret;
> +}
> +

Michal

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