]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
virxml: Avoid double indentation of <metadata/> element
authorMichal Privoznik <mprivozn@redhat.com>
Tue, 25 May 2021 09:32:37 +0000 (11:32 +0200)
committerMichal Privoznik <mprivozn@redhat.com>
Tue, 25 May 2021 11:17:22 +0000 (13:17 +0200)
There was a recent change in libxml2 that caused a trouble for
us. To us, <metadata/> in domain or network XMLs are just opaque
value where management application can store whatever data it
finds fit. At XML parser/formatter level, we just make a copy of
the element during parsing and then format it back. For
formatting we use xmlNodeDump() which allows caller to specify
level of indentation. Previously, the indentation was not
applied onto the very first line, but as of v2.9.12-2-g85b1792e
libxml2 is applying indentation also on the first line.

This does not work well with out virBuffer because as soon as we
call virBufferAsprintf() to append <metadata/> element,
virBufferAsprintf() will apply another level of indentation.

Instead of version checking, let's skip any indentation added by
libxml2 before virBufferAsprintf() is called.

Note, the problem is only when telling xmlNodeDump() to use
indentation, i.e. level argument is not zero. Therefore,
virXMLNodeToString() which also calls xmlNodeDump() is safe as it
passes zero.

Tested-by: Bjoern Walk <bwalk@linux.ibm.com>
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Pavel Hrdina <phrdina@redhat.com>
src/util/virxml.c

index 91c6f6b02eda2d88916223b91e97b1c633ed0c98..4360b1548632e7b7a55e4e834d6797e400fbd0fa 100644 (file)
@@ -1723,6 +1723,7 @@ virXMLFormatMetadata(virBuffer *buf,
                      xmlNodePtr metadata)
 {
     g_autoptr(xmlBuffer) xmlbuf = NULL;
+    const char *xmlbufContent = NULL;
     int oldIndentTreeOutput = xmlIndentTreeOutput;
 
     if (!metadata)
@@ -1745,7 +1746,12 @@ virXMLFormatMetadata(virBuffer *buf,
         return -1;
     }
 
-    virBufferAsprintf(buf, "%s\n", (char *) xmlBufferContent(xmlbuf));
+    /* After libxml2-v2.9.12-2-g85b1792e even the first line is indented.
+     * But virBufferAsprintf() also adds indentation. Skip one of them. */
+    xmlbufContent = (const char *) xmlBufferContent(xmlbuf);
+    virSkipSpaces(&xmlbufContent);
+
+    virBufferAsprintf(buf, "%s\n", xmlbufContent);
     xmlIndentTreeOutput = oldIndentTreeOutput;
 
     return 0;