]> xenbits.xensource.com Git - people/liuw/libxenctrl-split/libvirt.git/commitdiff
util: improve the sysinfo element XML format
authorLuyao Huang <lhuang@redhat.com>
Fri, 22 May 2015 09:26:28 +0000 (17:26 +0800)
committerJohn Ferlan <jferlan@redhat.com>
Tue, 26 May 2015 23:57:15 +0000 (19:57 -0400)
If the <sysinfo type='smbios'...> ends up not formatting any sub-elements,
then rather than formatting as:

  <sysinfo type='smbios'>
  </sysinfo>

Just format it more cleanly as:

  <sysinfo type='smbios'/>

Signed-off-by: Luyao Huang <lhuang@redhat.com>
src/util/virsysinfo.c

index 40390abaa91893696a67e4e66be8aed209cc757e..7b0d80d62fcabebd89c7caca1179773f060141e8 100644 (file)
@@ -1040,31 +1040,42 @@ virSysinfoMemoryFormat(virBufferPtr buf, virSysinfoDefPtr def)
 int
 virSysinfoFormat(virBufferPtr buf, virSysinfoDefPtr def)
 {
+    virBuffer childrenBuf = VIR_BUFFER_INITIALIZER;
     const char *type = virSysinfoTypeToString(def->type);
+    int indent = virBufferGetIndent(buf, false);
+    int ret = -1;
 
     if (!type) {
         virReportError(VIR_ERR_INTERNAL_ERROR,
                        _("unexpected sysinfo type model %d"),
                        def->type);
         virBufferFreeAndReset(buf);
-        return -1;
+        goto cleanup;
     }
 
-    virBufferAsprintf(buf, "<sysinfo type='%s'>\n", type);
-    virBufferAdjustIndent(buf, 2);
+    virBufferAdjustIndent(&childrenBuf, indent + 2);
 
-    virSysinfoBIOSFormat(buf, def);
-    virSysinfoSystemFormat(buf, def);
-    virSysinfoProcessorFormat(buf, def);
-    virSysinfoMemoryFormat(buf, def);
+    virSysinfoBIOSFormat(&childrenBuf, def);
+    virSysinfoSystemFormat(&childrenBuf, def);
+    virSysinfoProcessorFormat(&childrenBuf, def);
+    virSysinfoMemoryFormat(&childrenBuf, def);
 
-    virBufferAdjustIndent(buf, -2);
-    virBufferAddLit(buf, "</sysinfo>\n");
+    virBufferAsprintf(buf, "<sysinfo type='%s'", type);
+    if (virBufferUse(&childrenBuf)) {
+        virBufferAddLit(buf, ">\n");
+        virBufferAddBuffer(buf, &childrenBuf);
+        virBufferAddLit(buf, "</sysinfo>\n");
+    } else {
+        virBufferAddLit(buf, "/>\n");
+    }
 
     if (virBufferCheckError(buf) < 0)
-        return -1;
+        goto cleanup;
 
-    return 0;
+    ret = 0;
+ cleanup:
+    virBufferFreeAndReset(&childrenBuf);
+    return ret;
 }
 
 bool virSysinfoIsEqual(virSysinfoDefPtr src,