]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
qemuMigrationCookieNetworkXMLFormat: Refactor XML formatting
authorPeter Krempa <pkrempa@redhat.com>
Wed, 30 Sep 2020 13:30:20 +0000 (15:30 +0200)
committerPeter Krempa <pkrempa@redhat.com>
Mon, 5 Oct 2020 13:58:52 +0000 (15:58 +0200)
Use 'virXMLFormatElement' both for formating the whole <network> element
but also for formatting the <interface> 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 <pkrempa@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
src/qemu/qemu_migration_cookie.c

index 7f8772968f4d993055a0a3ead72cb8c74d00ca8c..442731933f46b5eb24058355279f02239d6f9b3b 100644 (file)
@@ -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, "<network>\n");
-                virBufferAdjustIndent(buf, 2);
-                empty = false;
-            }
-            virBufferAsprintf(buf, "<interface index='%zu' vporttype='%s'",
-                              i, virNetDevVPortTypeToString(optr->net[i].vporttype));
-            if (optr->net[i].portdata) {
-                virBufferAddLit(buf, ">\n");
-                virBufferAdjustIndent(buf, 2);
-                virBufferEscapeString(buf, "<portdata>%s</portdata>\n",
-                                      optr->net[i].portdata);
-                virBufferAdjustIndent(buf, -2);
-                virBufferAddLit(buf, "</interface>\n");
-            } else {
-                virBufferAddLit(buf, "/>\n");
-            }
-        }
-    }
-    if (!empty) {
-        virBufferAdjustIndent(buf, -2);
-        virBufferAddLit(buf, "</network>\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, "<portdata>%s</portdata>\n",
+                              optr->net[i].portdata);
+
+        virXMLFormatElement(&interfaceBuf, "interface", &attrBuf, &childBuf);
     }
+
+    virXMLFormatElement(buf, "network", NULL, &interfaceBuf);
 }