Remove some code duplication.
Signed-off-by: Ján Tomko <jtomko(a)redhat.com>
---
src/qemu/qemu_capabilities.c | 2 +-
src/qemu/qemu_capabilities.h | 4 ++++
src/qemu/qemu_domain.c | 16 ++--------------
tests/qemucaps2xmltest.c | 17 +++--------------
4 files changed, 10 insertions(+), 29 deletions(-)
diff --git a/src/qemu/qemu_capabilities.c b/src/qemu/qemu_capabilities.c
index a355ee2e37..b12f5914aa 100644
--- a/src/qemu/qemu_capabilities.c
+++ b/src/qemu/qemu_capabilities.c
@@ -3442,7 +3442,7 @@ virQEMUCapsParseSEVInfo(virQEMUCapsPtr qemuCaps, xmlXPathContextPtr
ctxt)
}
-static int
+int
virQEMUCapsSetFromNodes(virQEMUCapsPtr qemuCaps,
xmlNodePtr *nodes,
size_t n)
diff --git a/src/qemu/qemu_capabilities.h b/src/qemu/qemu_capabilities.h
index 6fa402a846..d1303da3cf 100644
--- a/src/qemu/qemu_capabilities.h
+++ b/src/qemu/qemu_capabilities.h
@@ -516,6 +516,10 @@ virQEMUCapsPtr virQEMUCapsNew(void);
void virQEMUCapsSet(virQEMUCapsPtr qemuCaps,
virQEMUCapsFlags flag) ATTRIBUTE_NONNULL(1);
+int virQEMUCapsSetFromNodes(virQEMUCapsPtr qemuCaps,
+ xmlNodePtr *nodes,
+ size_t nnodes)
+ ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
void virQEMUCapsSetList(virQEMUCapsPtr qemuCaps, ...) ATTRIBUTE_NONNULL(1);
diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c
index bbd4a5efe8..ef44252cba 100644
--- a/src/qemu/qemu_domain.c
+++ b/src/qemu/qemu_domain.c
@@ -2995,20 +2995,8 @@ qemuDomainObjPrivateXMLParse(xmlXPathContextPtr ctxt,
if (!(qemuCaps = virQEMUCapsNew()))
goto error;
- for (i = 0; i < n; i++) {
- char *str = virXMLPropString(nodes[i], "name");
- if (str) {
- int flag = virQEMUCapsTypeFromString(str);
- if (flag < 0) {
- virReportError(VIR_ERR_INTERNAL_ERROR,
- _("Unknown qemu capabilities flag %s"),
str);
- VIR_FREE(str);
- goto error;
- }
- VIR_FREE(str);
- virQEMUCapsSet(qemuCaps, flag);
- }
- }
+ if (virQEMUCapsSetFromNodes(qemuCaps, nodes, n) < 0)
+ goto error;
VIR_STEAL_PTR(priv->qemuCaps, qemuCaps);
}
diff --git a/tests/qemucaps2xmltest.c b/tests/qemucaps2xmltest.c
index 0d9b4e679a..3e96c643da 100644
--- a/tests/qemucaps2xmltest.c
+++ b/tests/qemucaps2xmltest.c
@@ -38,7 +38,7 @@ testQemuGetCaps(char *caps)
virQEMUCapsPtr qemuCaps = NULL;
xmlDocPtr xml;
xmlXPathContextPtr ctxt = NULL;
- ssize_t i, n;
+ ssize_t n;
xmlNodePtr *nodes = NULL;
if (!(xml = virXMLParseStringCtxt(caps, "(test caps)", &ctxt)))
@@ -52,19 +52,8 @@ testQemuGetCaps(char *caps)
if (!(qemuCaps = virQEMUCapsNew()))
goto error;
- for (i = 0; i < n; i++) {
- char *str = virXMLPropString(nodes[i], "name");
- if (str) {
- int flag = virQEMUCapsTypeFromString(str);
- if (flag < 0) {
- fprintf(stderr, "Unknown qemu capabilities flag %s", str);
- VIR_FREE(str);
- goto error;
- }
- VIR_FREE(str);
- virQEMUCapsSet(qemuCaps, flag);
- }
- }
+ if (virQEMUCapsSetFromNodes(qemuCaps, nodes, n) < 0)
+ goto error;
VIR_FREE(nodes);
xmlFreeDoc(xml);
--
2.19.2