[libvirt] [PATCHv3 01/11] Use separate buffer for <input> subelements

Ján Tomko jtomko at redhat.com
Wed Aug 10 13:27:12 UTC 2016


Instead of figuring out upfront whether <input> will be a single
or a pair element, format the subelements into a separate buffer
and close <input/> early if this buffer is empty.
---
 src/conf/domain_conf.c | 19 ++++++++++---------
 1 file changed, 10 insertions(+), 9 deletions(-)

diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index 82876f3..5dbeb1a 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -21844,6 +21844,7 @@ virDomainInputDefFormat(virBufferPtr buf,
 {
     const char *type = virDomainInputTypeToString(def->type);
     const char *bus = virDomainInputBusTypeToString(def->bus);
+    virBuffer childbuf = VIR_BUFFER_INITIALIZER;
 
     /* don't format keyboard into migratable XML for backward compatibility */
     if (flags & VIR_DOMAIN_DEF_FORMAT_MIGRATABLE &&
@@ -21866,17 +21867,17 @@ virDomainInputDefFormat(virBufferPtr buf,
     virBufferAsprintf(buf, "<input type='%s' bus='%s'",
                       type, bus);
 
-    if (virDomainDeviceInfoNeedsFormat(&def->info, flags) ||
-        def->type == VIR_DOMAIN_INPUT_TYPE_PASSTHROUGH) {
+    virBufferAdjustIndent(&childbuf, virBufferGetIndent(buf, false) + 2);
+    virBufferEscapeString(&childbuf, "<source evdev='%s'/>\n", def->source.evdev);
+    if (virDomainDeviceInfoFormat(&childbuf, &def->info, flags) < 0)
+        return -1;
+
+    if (!virBufferUse(&childbuf)) {
+        virBufferAddLit(buf, "/>\n");
+    } else {
         virBufferAddLit(buf, ">\n");
-        virBufferAdjustIndent(buf, 2);
-        virBufferEscapeString(buf, "<source evdev='%s'/>\n", def->source.evdev);
-        if (virDomainDeviceInfoFormat(buf, &def->info, flags) < 0)
-            return -1;
-        virBufferAdjustIndent(buf, -2);
+        virBufferAddBuffer(buf, &childbuf);
         virBufferAddLit(buf, "</input>\n");
-    } else {
-        virBufferAddLit(buf, "/>\n");
     }
 
     return 0;
-- 
2.7.3




More information about the libvir-list mailing list