[PATCH 1/4] conf: use virXMLFormatElement for <iommu><driver>

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Use the new helper to simplify the code.
This also fixes the bug of not formatting 'eim' in the useless
case if it's the only enabled attribute.
---
 src/conf/domain_conf.c | 55 ++++++++++++++++++++++++++++----------------------
 1 file changed, 31 insertions(+), 24 deletions(-)

diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index d97aab483..8e555ad9d 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -25056,37 +25056,36 @@ virDomainDefIothreadShouldFormat(virDomainDefPtr def)
 }
 
 
-static void
+static int
 virDomainIOMMUDefFormat(virBufferPtr buf,
                         const virDomainIOMMUDef *iommu)
 {
     virBuffer childBuf = VIR_BUFFER_INITIALIZER;
+    virBuffer driverAttrBuf = VIR_BUFFER_INITIALIZER;
+    int ret = -1;
 
     virBufferSetChildIndent(&childBuf, buf);
 
-    if (iommu->intremap != VIR_TRISTATE_SWITCH_ABSENT ||
-        iommu->caching_mode != VIR_TRISTATE_SWITCH_ABSENT ||
-        iommu->iotlb != VIR_TRISTATE_SWITCH_ABSENT) {
-        virBufferAddLit(&childBuf, "<driver");
-        if (iommu->intremap != VIR_TRISTATE_SWITCH_ABSENT) {
-            virBufferAsprintf(&childBuf, " intremap='%s'",
-                              virTristateSwitchTypeToString(iommu->intremap));
-        }
-        if (iommu->caching_mode != VIR_TRISTATE_SWITCH_ABSENT) {
-            virBufferAsprintf(&childBuf, " caching_mode='%s'",
-                              virTristateSwitchTypeToString(iommu->caching_mode));
-        }
-        if (iommu->eim != VIR_TRISTATE_SWITCH_ABSENT) {
-            virBufferAsprintf(&childBuf, " eim='%s'",
-                              virTristateSwitchTypeToString(iommu->eim));
-        }
-        if (iommu->iotlb != VIR_TRISTATE_SWITCH_ABSENT) {
-            virBufferAsprintf(&childBuf, " iotlb='%s'",
-                              virTristateSwitchTypeToString(iommu->iotlb));
-        }
-        virBufferAddLit(&childBuf, "/>\n");
+    if (iommu->intremap != VIR_TRISTATE_SWITCH_ABSENT) {
+        virBufferAsprintf(&driverAttrBuf, " intremap='%s'",
+                          virTristateSwitchTypeToString(iommu->intremap));
+    }
+    if (iommu->caching_mode != VIR_TRISTATE_SWITCH_ABSENT) {
+        virBufferAsprintf(&driverAttrBuf, " caching_mode='%s'",
+                          virTristateSwitchTypeToString(iommu->caching_mode));
+    }
+    if (iommu->eim != VIR_TRISTATE_SWITCH_ABSENT) {
+        virBufferAsprintf(&driverAttrBuf, " eim='%s'",
+                          virTristateSwitchTypeToString(iommu->eim));
+    }
+    if (iommu->iotlb != VIR_TRISTATE_SWITCH_ABSENT) {
+        virBufferAsprintf(&driverAttrBuf, " iotlb='%s'",
+                          virTristateSwitchTypeToString(iommu->iotlb));
     }
 
+    if (virXMLFormatElement(&childBuf, "driver", &driverAttrBuf, NULL) < 0)
+        goto cleanup;
+
     virBufferAsprintf(buf, "<iommu model='%s'",
                       virDomainIOMMUModelTypeToString(iommu->model));
 
@@ -25097,6 +25096,13 @@ virDomainIOMMUDefFormat(virBufferPtr buf,
     } else {
         virBufferAddLit(buf, "/>\n");
     }
+
+    ret = 0;
+
+ cleanup:
+    virBufferFreeAndReset(&childBuf);
+    virBufferFreeAndReset(&driverAttrBuf);
+    return ret;
 }
 
 
@@ -25866,8 +25872,9 @@ virDomainDefFormatInternal(virDomainDefPtr def,
             goto error;
     }
 
-    if (def->iommu)
-        virDomainIOMMUDefFormat(buf, def->iommu);
+    if (def->iommu &&
+        virDomainIOMMUDefFormat(buf, def->iommu) < 0)
+        goto error;
 
     virBufferAdjustIndent(buf, -2);
     virBufferAddLit(buf, "</devices>\n");
-- 
2.13.0

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list



[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]
  Powered by Linux