So far, we have to places where we format <metadata/> into XMLs:
domain and network. Bot places share the same code. Move it into
a helper function and just call it from those places.
Signed-off-by: Michal Privoznik <mprivozn(a)redhat.com>
---
src/conf/domain_conf.c | 23 ++-------------------
src/conf/network_conf.c | 23 ++-------------------
src/libvirt_private.syms | 1 +
src/util/virxml.c | 43 ++++++++++++++++++++++++++++++++++++++++
src/util/virxml.h | 3 +++
5 files changed, 51 insertions(+), 42 deletions(-)
diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index 4f78b7b43d..413c44ac61 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -27806,27 +27806,8 @@ virDomainDefFormatInternalSetRootName(virDomainDef *def,
virBufferEscapeString(buf, "<description>%s</description>\n",
def->description);
- if (def->metadata) {
- g_autoptr(xmlBuffer) xmlbuf = NULL;
- int oldIndentTreeOutput = xmlIndentTreeOutput;
-
- /* Indentation on output requires that we previously set
- * xmlKeepBlanksDefault to 0 when parsing; also, libxml does 2
- * spaces per level of indentation of intermediate elements,
- * but no leading indentation before the starting element.
- * Thankfully, libxml maps what looks like globals into
- * thread-local uses, so we are thread-safe. */
- xmlIndentTreeOutput = 1;
- xmlbuf = virXMLBufferCreate();
-
- if (xmlNodeDump(xmlbuf, def->metadata->doc, def->metadata,
- virBufferGetIndent(buf) / 2, 1) < 0) {
- xmlIndentTreeOutput = oldIndentTreeOutput;
- return -1;
- }
- virBufferAsprintf(buf, "%s\n", (char *) xmlBufferContent(xmlbuf));
- xmlIndentTreeOutput = oldIndentTreeOutput;
- }
+ if (virXMLFormatMetadata(buf, def->metadata) < 0)
+ return -1;
if (virDomainDefHasMemoryHotplug(def)) {
virBufferAsprintf(buf,
diff --git a/src/conf/network_conf.c b/src/conf/network_conf.c
index a9eadff29c..b10ff5c7a8 100644
--- a/src/conf/network_conf.c
+++ b/src/conf/network_conf.c
@@ -2486,27 +2486,8 @@ virNetworkDefFormatBuf(virBuffer *buf,
virUUIDFormat(uuid, uuidstr);
virBufferAsprintf(buf, "<uuid>%s</uuid>\n", uuidstr);
- if (def->metadata) {
- g_autoptr(xmlBuffer) xmlbuf = NULL;
- int oldIndentTreeOutput = xmlIndentTreeOutput;
-
- /* Indentation on output requires that we previously set
- * xmlKeepBlanksDefault to 0 when parsing; also, libxml does 2
- * spaces per level of indentation of intermediate elements,
- * but no leading indentation before the starting element.
- * Thankfully, libxml maps what looks like globals into
- * thread-local uses, so we are thread-safe. */
- xmlIndentTreeOutput = 1;
- xmlbuf = virXMLBufferCreate();
-
- if (xmlNodeDump(xmlbuf, def->metadata->doc, def->metadata,
- virBufferGetIndent(buf) / 2, 1) < 0) {
- xmlIndentTreeOutput = oldIndentTreeOutput;
- return -1;
- }
- virBufferAsprintf(buf, "%s\n", (char *) xmlBufferContent(xmlbuf));
- xmlIndentTreeOutput = oldIndentTreeOutput;
- }
+ if (virXMLFormatMetadata(buf, def->metadata) < 0)
+ return -1;
if (def->forward.type != VIR_NETWORK_FORWARD_NONE) {
const char *dev = NULL;
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index e1aef5267e..37515f80ec 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -3555,6 +3555,7 @@ virXMLCheckIllegalChars;
virXMLExtractNamespaceXML;
virXMLFormatElement;
virXMLFormatElementEmpty;
+virXMLFormatMetadata;
virXMLNewNode;
virXMLNodeContentString;
virXMLNodeNameEqual;
diff --git a/src/util/virxml.c b/src/util/virxml.c
index 8dcece704a..062a5402f6 100644
--- a/src/util/virxml.c
+++ b/src/util/virxml.c
@@ -1707,6 +1707,49 @@ virXMLFormatElement(virBuffer *buf,
}
+/**
+ * virXMLFormatMetadata:
+ * @buf: the parent buffer where the element will be placed
+ * @metadata: pointer to metadata node
+ *
+ * Helper to format metadata element. If @metadata is NULL then
+ * this function is a NOP.
+ *
+ * Returns: 0 on success,
+ * -1 otherwise.
+ */
+int
+virXMLFormatMetadata(virBuffer *buf,
+ xmlNodePtr metadata)
+{
+ g_autoptr(xmlBuffer) xmlbuf = NULL;
+ int oldIndentTreeOutput = xmlIndentTreeOutput;
+
+ if (!metadata)
+ return 0;
+
+ /* Indentation on output requires that we previously set
+ * xmlKeepBlanksDefault to 0 when parsing; also, libxml does 2
+ * spaces per level of indentation of intermediate elements,
+ * but no leading indentation before the starting element.
+ * Thankfully, libxml maps what looks like globals into
+ * thread-local uses, so we are thread-safe. */
+ xmlIndentTreeOutput = 1;
+ xmlbuf = virXMLBufferCreate();
+
+ if (xmlNodeDump(xmlbuf, metadata->doc, metadata,
+ virBufferGetIndent(buf) / 2, 1) < 0) {
+ xmlIndentTreeOutput = oldIndentTreeOutput;
+ return -1;
+ }
+
+ virBufferAsprintf(buf, "%s\n", (char *) xmlBufferContent(xmlbuf));
+ xmlIndentTreeOutput = oldIndentTreeOutput;
+
+ return 0;
+}
+
+
void
virXPathContextNodeRestore(virXPathContextNodeSave *save)
{
diff --git a/src/util/virxml.h b/src/util/virxml.h
index ed02abd2e9..0bb0d1c118 100644
--- a/src/util/virxml.h
+++ b/src/util/virxml.h
@@ -333,6 +333,9 @@ virXMLFormatElementEmpty(virBuffer *buf,
virBuffer *attrBuf,
virBuffer *childBuf);
+int
+virXMLFormatMetadata(virBuffer *buf,
+ xmlNodePtr metadata);
struct _virXPathContextNodeSave {
xmlXPathContextPtr ctxt;
--
2.26.3