mirror of
https://github.com/libvirt/libvirt.git
synced 2025-02-25 18:55:26 -06:00
virxml: Introduce and use virXMLFormatMetadata()
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@redhat.com> Reviewed-by: Pavel Hrdina <phrdina@redhat.com>
This commit is contained in:
parent
f772c1fd2a
commit
c380ae220e
@ -27806,27 +27806,8 @@ virDomainDefFormatInternalSetRootName(virDomainDef *def,
|
|||||||
virBufferEscapeString(buf, "<description>%s</description>\n",
|
virBufferEscapeString(buf, "<description>%s</description>\n",
|
||||||
def->description);
|
def->description);
|
||||||
|
|
||||||
if (def->metadata) {
|
if (virXMLFormatMetadata(buf, def->metadata) < 0)
|
||||||
g_autoptr(xmlBuffer) xmlbuf = NULL;
|
return -1;
|
||||||
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 (virDomainDefHasMemoryHotplug(def)) {
|
if (virDomainDefHasMemoryHotplug(def)) {
|
||||||
virBufferAsprintf(buf,
|
virBufferAsprintf(buf,
|
||||||
|
@ -2486,27 +2486,8 @@ virNetworkDefFormatBuf(virBuffer *buf,
|
|||||||
virUUIDFormat(uuid, uuidstr);
|
virUUIDFormat(uuid, uuidstr);
|
||||||
virBufferAsprintf(buf, "<uuid>%s</uuid>\n", uuidstr);
|
virBufferAsprintf(buf, "<uuid>%s</uuid>\n", uuidstr);
|
||||||
|
|
||||||
if (def->metadata) {
|
if (virXMLFormatMetadata(buf, def->metadata) < 0)
|
||||||
g_autoptr(xmlBuffer) xmlbuf = NULL;
|
return -1;
|
||||||
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 (def->forward.type != VIR_NETWORK_FORWARD_NONE) {
|
if (def->forward.type != VIR_NETWORK_FORWARD_NONE) {
|
||||||
const char *dev = NULL;
|
const char *dev = NULL;
|
||||||
|
@ -3555,6 +3555,7 @@ virXMLCheckIllegalChars;
|
|||||||
virXMLExtractNamespaceXML;
|
virXMLExtractNamespaceXML;
|
||||||
virXMLFormatElement;
|
virXMLFormatElement;
|
||||||
virXMLFormatElementEmpty;
|
virXMLFormatElementEmpty;
|
||||||
|
virXMLFormatMetadata;
|
||||||
virXMLNewNode;
|
virXMLNewNode;
|
||||||
virXMLNodeContentString;
|
virXMLNodeContentString;
|
||||||
virXMLNodeNameEqual;
|
virXMLNodeNameEqual;
|
||||||
|
@ -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
|
void
|
||||||
virXPathContextNodeRestore(virXPathContextNodeSave *save)
|
virXPathContextNodeRestore(virXPathContextNodeSave *save)
|
||||||
{
|
{
|
||||||
|
@ -333,6 +333,9 @@ virXMLFormatElementEmpty(virBuffer *buf,
|
|||||||
virBuffer *attrBuf,
|
virBuffer *attrBuf,
|
||||||
virBuffer *childBuf);
|
virBuffer *childBuf);
|
||||||
|
|
||||||
|
int
|
||||||
|
virXMLFormatMetadata(virBuffer *buf,
|
||||||
|
xmlNodePtr metadata);
|
||||||
|
|
||||||
struct _virXPathContextNodeSave {
|
struct _virXPathContextNodeSave {
|
||||||
xmlXPathContextPtr ctxt;
|
xmlXPathContextPtr ctxt;
|
||||||
|
Loading…
Reference in New Issue
Block a user