]> xenbits.xensource.com Git - libvirt.git/commitdiff
conf: Refactor virDomainMemballoonDefFormat
authorPeter Krempa <pkrempa@redhat.com>
Tue, 5 Mar 2019 09:17:31 +0000 (10:17 +0100)
committerPeter Krempa <pkrempa@redhat.com>
Wed, 6 Mar 2019 14:53:51 +0000 (15:53 +0100)
Use virXMLFormatElement to format the internals along with simplifying
cleanup code paths.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
src/conf/domain_conf.c

index cbefa7749bcd6844120c098c7ac2d5fbd93d4f0e..fd3f19d6ceb3a2020bc0eea6f62f394f04b59edb 100644 (file)
@@ -25837,19 +25837,20 @@ virDomainMemballoonDefFormat(virBufferPtr buf,
                              unsigned int flags)
 {
     const char *model = virDomainMemballoonModelTypeToString(def->model);
-    virBuffer childrenBuf = VIR_BUFFER_INITIALIZER;
-    int ret = -1;
+    VIR_AUTOCLEAN(virBuffer) attrBuf = VIR_BUFFER_INITIALIZER;
+    VIR_AUTOCLEAN(virBuffer) childrenBuf = VIR_BUFFER_INITIALIZER;
+    VIR_AUTOCLEAN(virBuffer) driverAttrBuf = VIR_BUFFER_INITIALIZER;
 
     if (!model) {
         virReportError(VIR_ERR_INTERNAL_ERROR,
                        _("unexpected memballoon model %d"), def->model);
-        goto cleanup;
+        return -1;
     }
 
-    virBufferAsprintf(buf, "<memballoon model='%s'", model);
+    virBufferAsprintf(&attrBuf, " model='%s'", model);
 
     if (def->autodeflate != VIR_TRISTATE_SWITCH_ABSENT)
-        virBufferAsprintf(buf, " autodeflate='%s'",
+        virBufferAsprintf(&attrBuf, " autodeflate='%s'",
                           virTristateSwitchTypeToString(def->autodeflate));
 
     virBufferSetChildIndent(&childrenBuf, buf);
@@ -25858,40 +25859,14 @@ virDomainMemballoonDefFormat(virBufferPtr buf,
         virBufferAsprintf(&childrenBuf, "<stats period='%i'/>\n", def->period);
 
     if (virDomainDeviceInfoFormat(&childrenBuf, &def->info, flags) < 0)
-        goto cleanup;
-
-    if (def->virtio) {
-        virBuffer driverBuf = VIR_BUFFER_INITIALIZER;
-
-        virDomainVirtioOptionsFormat(&driverBuf, def->virtio);
-
-        if (virBufferCheckError(&driverBuf) < 0)
-            goto cleanup;
-
-        if (virBufferUse(&driverBuf)) {
-            virBufferAddLit(&childrenBuf, "<driver");
-            virBufferAddBuffer(&childrenBuf, &driverBuf);
-            virBufferAddLit(&childrenBuf, "/>\n");
-        }
-    }
-
-    if (virBufferCheckError(&childrenBuf) < 0)
-        goto cleanup;
-
-    if (!virBufferUse(&childrenBuf)) {
-        virBufferAddLit(buf, "/>\n");
-    } else {
-        virBufferAddLit(buf, ">\n");
-        virBufferAddBuffer(buf, &childrenBuf);
-        virBufferAddLit(buf, "</memballoon>\n");
-    }
+        return -1;
 
-    ret = 0;
+    virDomainVirtioOptionsFormat(&driverAttrBuf, def->virtio);
 
- cleanup:
-    virBufferFreeAndReset(&childrenBuf);
+    if (virXMLFormatElement(&childrenBuf, "driver", &driverAttrBuf, NULL) < 0)
+        return -1;
 
-    return ret;
+    return virXMLFormatElement(buf, "memballoon", &attrBuf, &childrenBuf);
 }
 
 static int