From a01726e9cf426e8cbe553139c3cee888de63c1f2 Mon Sep 17 00:00:00 2001 From: Peter Krempa Date: Thu, 7 Jan 2021 15:03:57 +0100 Subject: [PATCH] virDomainSnapshotDiskDefFormat: Use virXMLFormatElement MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Refactor the code to use modern XML formatting approach. Signed-off-by: Peter Krempa Reviewed-by: Ján Tomko --- src/conf/snapshot_conf.c | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/src/conf/snapshot_conf.c b/src/conf/snapshot_conf.c index f896fd1cf2..757af681cd 100644 --- a/src/conf/snapshot_conf.c +++ b/src/conf/snapshot_conf.c @@ -772,33 +772,30 @@ virDomainSnapshotDiskDefFormat(virBufferPtr buf, virDomainSnapshotDiskDefPtr disk, virDomainXMLOptionPtr xmlopt) { - int type = disk->src->type; + g_auto(virBuffer) attrBuf = VIR_BUFFER_INITIALIZER; + g_auto(virBuffer) childBuf = VIR_BUFFER_INIT_CHILD(buf); if (!disk->name) return 0; - virBufferEscapeString(buf, "name); + virBufferEscapeString(&attrBuf, " name='%s'", disk->name); if (disk->snapshot > 0) - virBufferAsprintf(buf, " snapshot='%s'", + virBufferAsprintf(&attrBuf, " snapshot='%s'", virDomainSnapshotLocationTypeToString(disk->snapshot)); - if (!disk->src->path && disk->src->format == 0) { - virBufferAddLit(buf, "/>\n"); - return 0; + if (disk->src->path || disk->src->format != 0) { + virBufferAsprintf(&attrBuf, " type='%s'", virStorageTypeToString(disk->src->type)); + + if (disk->src->format > 0) + virBufferEscapeString(&childBuf, "\n", + virStorageFileFormatTypeToString(disk->src->format)); + + if (virDomainDiskSourceFormat(&childBuf, disk->src, "source", 0, false, 0, + false, false, xmlopt) < 0) + return -1; } - virBufferAsprintf(buf, " type='%s'>\n", virStorageTypeToString(type)); - virBufferAdjustIndent(buf, 2); - - if (disk->src->format > 0) - virBufferEscapeString(buf, "\n", - virStorageFileFormatTypeToString(disk->src->format)); - if (virDomainDiskSourceFormat(buf, disk->src, "source", 0, false, 0, - false, false, xmlopt) < 0) - return -1; - - virBufferAdjustIndent(buf, -2); - virBufferAddLit(buf, "\n"); + virXMLFormatElement(buf, "disk", &attrBuf, &childBuf); return 0; }