From: Peter Krempa Date: Wed, 30 Sep 2020 13:30:20 +0000 (+0200) Subject: qemuMigrationCookieNetworkXMLFormat: Refactor XML formatting X-Git-Tag: v6.9.0-rc1~343 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=84589ac004955301702f1ccd17a6df05a60740ec;p=thirdparty%2Flibvirt.git qemuMigrationCookieNetworkXMLFormat: Refactor XML formatting Use 'virXMLFormatElement' both for formating the whole element but also for formatting the subelements. This alows to remove the crazy logic which was determining which element was already formatted. Additional simplification is achieved by switching to skipping the loop using 'continue' rather than putting everything in a giant block. Signed-off-by: Peter Krempa Reviewed-by: Michal Privoznik --- diff --git a/src/qemu/qemu_migration_cookie.c b/src/qemu/qemu_migration_cookie.c index 7f8772968f..442731933f 100644 --- a/src/qemu/qemu_migration_cookie.c +++ b/src/qemu/qemu_migration_cookie.c @@ -598,35 +598,27 @@ static void qemuMigrationCookieNetworkXMLFormat(virBufferPtr buf, qemuMigrationCookieNetworkPtr optr) { + g_auto(virBuffer) interfaceBuf = VIR_BUFFER_INIT_CHILD(buf); size_t i; - bool empty = true; for (i = 0; i < optr->nnets; i++) { + g_auto(virBuffer) attrBuf = VIR_BUFFER_INITIALIZER; + g_auto(virBuffer) childBuf = VIR_BUFFER_INIT_CHILD(&interfaceBuf); + /* If optr->net[i].vporttype is not set, there is nothing to transfer */ - if (optr->net[i].vporttype != VIR_NETDEV_VPORT_PROFILE_NONE) { - if (empty) { - virBufferAddLit(buf, "\n"); - virBufferAdjustIndent(buf, 2); - empty = false; - } - virBufferAsprintf(buf, "net[i].vporttype)); - if (optr->net[i].portdata) { - virBufferAddLit(buf, ">\n"); - virBufferAdjustIndent(buf, 2); - virBufferEscapeString(buf, "%s\n", - optr->net[i].portdata); - virBufferAdjustIndent(buf, -2); - virBufferAddLit(buf, "\n"); - } else { - virBufferAddLit(buf, "/>\n"); - } - } - } - if (!empty) { - virBufferAdjustIndent(buf, -2); - virBufferAddLit(buf, "\n"); + if (optr->net[i].vporttype == VIR_NETDEV_VPORT_PROFILE_NONE) + continue; + + virBufferAsprintf(&attrBuf, " index='%zu' vporttype='%s'", + i, virNetDevVPortTypeToString(optr->net[i].vporttype)); + + virBufferEscapeString(&childBuf, "%s\n", + optr->net[i].portdata); + + virXMLFormatElement(&interfaceBuf, "interface", &attrBuf, &childBuf); } + + virXMLFormatElement(buf, "network", NULL, &interfaceBuf); }