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

[libvirt] [PATCH 04/14] snapshot: indent domain xml when nesting, round 4



Focusing just on sysinfo output.  The new virBufferIndent*
functions allow us to shave off quite a few lines of code.

* src/util/sysinfo.h (virSysinfoFormat): Alter signature.
* src/util/sysinfo.c (virSysinfoFormat, virSysinfoBIOSFormat)
(virSysinfoSystemFormat, virSysinfoProcessorFormat)
(virSysinfoMemoryFormat): Change indentation parameter.
* src/conf/domain_conf.c (virDomainSysinfoDefFormat)
(virDomainDefFormatInternal): Adjust callers.
* src/qemu/qemu_driver.c (qemuGetSysinfo): Likewise.
---
 src/conf/domain_conf.c |    7 +-
 src/qemu/qemu_driver.c |    2 +-
 src/util/sysinfo.c     |  407 ++++++++++++++++++------------------------------
 src/util/sysinfo.h     |    4 +-
 4 files changed, 155 insertions(+), 265 deletions(-)

diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index 64bc82d..9b6b9ca 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -9874,9 +9874,10 @@ virDomainMemballoonDefFormat(virBufferPtr buf,

 static int
 virDomainSysinfoDefFormat(virBufferPtr buf,
-                          virSysinfoDefPtr def)
+                          virSysinfoDefPtr def,
+                          int indent)
 {
-    char *format = virSysinfoFormat(def, "  ");
+    char *format = virSysinfoFormat(def, indent);

     if (!format)
         return -1;
@@ -10616,7 +10617,7 @@ virDomainDefFormatInternal(virDomainDefPtr def,
     }

     if (def->sysinfo)
-        virDomainSysinfoDefFormat(buf, def->sysinfo); /* XXX indent */
+        virDomainSysinfoDefFormat(buf, def->sysinfo, indent);

     if (def->os.bootloader) {
         virBufferIndentEscapeString(buf, indent,
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index 0d0bea2..08310b4 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -972,7 +972,7 @@ qemuGetSysinfo(virConnectPtr conn, unsigned int flags)
         return NULL;
     }

-    return virSysinfoFormat(driver->hostsysinfo, "");
+    return virSysinfoFormat(driver->hostsysinfo, 0);
 }

 static int qemudGetMaxVCPUs(virConnectPtr conn ATTRIBUTE_UNUSED, const char *type) {
diff --git a/src/util/sysinfo.c b/src/util/sysinfo.c
index 6625cae..203a936 100644
--- a/src/util/sysinfo.c
+++ b/src/util/sysinfo.c
@@ -515,298 +515,187 @@ no_memory:
 #endif /* !WIN32 && x86 */

 static void
-virSysinfoBIOSFormat(virSysinfoDefPtr def, const char *prefix,
-                     virBufferPtr buf)
+virSysinfoBIOSFormat(virSysinfoDefPtr def, int indent, virBufferPtr buf)
 {
-    int len = strlen(prefix);
-
-    if ((def->bios_vendor != NULL) || (def->bios_version != NULL) ||
-        (def->bios_date != NULL) || (def->bios_release != NULL)) {
-        virBufferAsprintf(buf, "%s  <bios>\n", prefix);
-        if (def->bios_vendor != NULL) {
-            virBufferAdd(buf, prefix, len);
-            virBufferEscapeString(buf,
-                                  "    <entry name='vendor'>%s</entry>\n",
-                                  def->bios_vendor);
-        }
-        if (def->bios_version != NULL) {
-            virBufferAdd(buf, prefix, len);
-            virBufferEscapeString(buf,
-                                  "    <entry name='version'>%s</entry>\n",
-                                  def->bios_version);
-        }
-        if (def->bios_date != NULL) {
-            virBufferAdd(buf, prefix, len);
-            virBufferEscapeString(buf,
-                                  "    <entry name='date'>%s</entry>\n",
-                                  def->bios_date);
-        }
-        if (def->bios_release != NULL) {
-            virBufferAdd(buf, prefix, len);
-            virBufferEscapeString(buf,
-                                  "    <entry name='release'>%s</entry>\n",
-                                  def->bios_release);
-        }
-        virBufferAsprintf(buf, "%s  </bios>\n", prefix);
-    }
+    if (!def->bios_vendor && !def->bios_version &&
+        !def->bios_date && !def->bios_release)
+        return;

-    return;
+    virBufferIndentAddLit(buf, indent, "<bios>\n");
+    virBufferIndentEscapeString(buf, indent + 2,
+                                "<entry name='vendor'>%s</entry>\n",
+                                def->bios_vendor);
+    virBufferIndentEscapeString(buf, indent + 2,
+                                "<entry name='version'>%s</entry>\n",
+                                def->bios_version);
+    virBufferIndentEscapeString(buf, indent + 2,
+                                "<entry name='date'>%s</entry>\n",
+                                def->bios_date);
+    virBufferIndentEscapeString(buf, indent + 2,
+                                "<entry name='release'>%s</entry>\n",
+                                def->bios_release);
+    virBufferIndentAddLit(buf, indent, "</bios>\n");
 }

 static void
-virSysinfoSystemFormat(virSysinfoDefPtr def, const char *prefix,
-                       virBufferPtr buf)
+virSysinfoSystemFormat(virSysinfoDefPtr def, int indent, virBufferPtr buf)
 {
-    int len = strlen(prefix);
-
-    if ((def->system_manufacturer != NULL) || (def->system_product != NULL) ||
-        (def->system_version != NULL) || (def->system_serial != NULL) ||
-        (def->system_uuid != NULL) || (def->system_sku != NULL) ||
-        (def->system_family != NULL)) {
-        virBufferAsprintf(buf, "%s  <system>\n", prefix);
-        if (def->system_manufacturer != NULL) {
-            virBufferAdd(buf, prefix, len);
-            virBufferEscapeString(buf,
-                                  "    <entry name='manufacturer'>%s</entry>\n",
-                                  def->system_manufacturer);
-        }
-        if (def->system_product != NULL) {
-            virBufferAdd(buf, prefix, len);
-            virBufferEscapeString(buf,
-                                  "    <entry name='product'>%s</entry>\n",
-                                  def->system_product);
-        }
-        if (def->system_version != NULL) {
-            virBufferAdd(buf, prefix, len);
-            virBufferEscapeString(buf,
-                                  "    <entry name='version'>%s</entry>\n",
-                                  def->system_version);
-        }
-        if (def->system_serial != NULL) {
-            virBufferAdd(buf, prefix, len);
-            virBufferEscapeString(buf,
-                                  "    <entry name='serial'>%s</entry>\n",
-                                  def->system_serial);
-        }
-        if (def->system_uuid != NULL) {
-            virBufferAdd(buf, prefix, len);
-            virBufferEscapeString(buf,
-                                  "    <entry name='uuid'>%s</entry>\n",
-                                  def->system_uuid);
-        }
-        if (def->system_sku != NULL) {
-            virBufferAdd(buf, prefix, len);
-            virBufferEscapeString(buf,
-                                  "    <entry name='sku'>%s</entry>\n",
-                                  def->system_sku);
-        }
-        if (def->system_family != NULL) {
-            virBufferAdd(buf, prefix, len);
-            virBufferEscapeString(buf,
-                                  "    <entry name='family'>%s</entry>\n",
-                                  def->system_family);
-        }
-        virBufferAsprintf(buf, "%s  </system>\n", prefix);
-    }
+    if (!def->system_manufacturer && !def->system_product &&
+        !def->system_version && !def->system_serial &&
+        !def->system_uuid && !def->system_sku && !def->system_family)
+        return;

-    return;
+    virBufferIndentAddLit(buf, indent, "<system>\n");
+    virBufferIndentEscapeString(buf, indent + 2,
+                                "<entry name='manufacturer'>%s</entry>\n",
+                                def->system_manufacturer);
+    virBufferIndentEscapeString(buf, indent + 2,
+                                "<entry name='product'>%s</entry>\n",
+                                def->system_product);
+    virBufferIndentEscapeString(buf, indent + 2,
+                                "<entry name='version'>%s</entry>\n",
+                                def->system_version);
+    virBufferIndentEscapeString(buf, indent + 2,
+                                "<entry name='serial'>%s</entry>\n",
+                                def->system_serial);
+    virBufferIndentEscapeString(buf, indent + 2,
+                                "<entry name='uuid'>%s</entry>\n",
+                                def->system_uuid);
+    virBufferIndentEscapeString(buf, indent + 2,
+                                "<entry name='sku'>%s</entry>\n",
+                                def->system_sku);
+    virBufferIndentEscapeString(buf, indent + 2,
+                                "<entry name='family'>%s</entry>\n",
+                                def->system_family);
+    virBufferIndentAddLit(buf, indent, "</system>\n");
 }

 static void
-virSysinfoProcessorFormat(virSysinfoDefPtr def, const char *prefix,
-                          virBufferPtr buf)
+virSysinfoProcessorFormat(virSysinfoDefPtr def, int indent, virBufferPtr buf)
 {
     int i;
-    int len = strlen(prefix);
     virSysinfoProcessorDefPtr processor;

     for (i = 0; i < def->nprocessor; i++) {
         processor = &def->processor[i];

-        if ((processor->processor_socket_destination != NULL) ||
-            (processor->processor_type != NULL) ||
-            (processor->processor_family != NULL) ||
-            (processor->processor_manufacturer != NULL) ||
-            (processor->processor_signature != NULL) ||
-            (processor->processor_version != NULL) ||
-            (processor->processor_external_clock != NULL) ||
-            (processor->processor_max_speed != NULL) ||
-            (processor->processor_status != NULL) ||
-            (processor->processor_serial_number != NULL) ||
-            (processor->processor_part_number != NULL)) {
-            virBufferAsprintf(buf, "%s  <processor>\n", prefix);
-            if (processor->processor_socket_destination != NULL) {
-                virBufferAdd(buf, prefix, len);
-                virBufferEscapeString(buf,
-                                      "    <entry name='socket_destination'>%s</entry>\n",
-                                      processor->processor_socket_destination);
-            }
-            if (processor->processor_type != NULL) {
-                virBufferAdd(buf, prefix, len);
-                virBufferEscapeString(buf,
-                                      "    <entry name='type'>%s</entry>\n",
-                                      processor->processor_type);
-            }
-            if (processor->processor_family != NULL) {
-                virBufferAdd(buf, prefix, len);
-                virBufferEscapeString(buf,
-                                      "    <entry name='family'>%s</entry>\n",
-                                      processor->processor_family);
-            }
-            if (processor->processor_manufacturer != NULL) {
-                virBufferAdd(buf, prefix, len);
-                virBufferEscapeString(buf,
-                                      "    <entry name='manufacturer'>%s</entry>\n",
-                                      processor->processor_manufacturer);
-            }
-            if (processor->processor_signature != NULL) {
-                virBufferAdd(buf, prefix, len);
-                virBufferEscapeString(buf,
-                                      "    <entry name='signature'>%s</entry>\n",
-                                      processor->processor_signature);
-            }
-            if (processor->processor_version != NULL) {
-                virBufferAdd(buf, prefix, len);
-                virBufferEscapeString(buf,
-                                      "    <entry name='version'>%s</entry>\n",
-                                      processor->processor_version);
-            }
-            if (processor->processor_external_clock != NULL) {
-                virBufferAdd(buf, prefix, len);
-                virBufferEscapeString(buf,
-                                      "    <entry name='external_clock'>%s</entry>\n",
-                                      processor->processor_external_clock);
-            }
-            if (processor->processor_max_speed != NULL) {
-                virBufferAdd(buf, prefix, len);
-                virBufferEscapeString(buf,
-                                      "    <entry name='max_speed'>%s</entry>\n",
-                                      processor->processor_max_speed);
-            }
-            if (processor->processor_status != NULL) {
-                virBufferAdd(buf, prefix, len);
-                virBufferEscapeString(buf,
-                                      "    <entry name='status'>%s</entry>\n",
-                                      processor->processor_status);
-            }
-            if (processor->processor_serial_number != NULL) {
-                virBufferAdd(buf, prefix, len);
-                virBufferEscapeString(buf,
-                                      "    <entry name='serial_number'>%s</entry>\n",
-                                      processor->processor_serial_number);
-            }
-            if (processor->processor_part_number != NULL) {
-                virBufferAdd(buf, prefix, len);
-                virBufferEscapeString(buf,
-                                      "    <entry name='part_number'>%s</entry>\n",
-                                      processor->processor_part_number);
-            }
-            virBufferAsprintf(buf, "%s  </processor>\n", prefix);
-        }
+        if (!processor->processor_socket_destination &&
+            !processor->processor_type &&
+            !processor->processor_family &&
+            !processor->processor_manufacturer &&
+            !processor->processor_signature &&
+            !processor->processor_version &&
+            !processor->processor_external_clock &&
+            !processor->processor_max_speed &&
+            !processor->processor_status &&
+            !processor->processor_serial_number &&
+            !processor->processor_part_number)
+            continue;
+
+        virBufferIndentAddLit(buf, indent, "<processor>\n");
+        virBufferIndentEscapeString(buf, indent + 2,
+                                    "<entry name='socket_destination'>%s</entry>\n",
+                                    processor->processor_socket_destination);
+        virBufferIndentEscapeString(buf, indent + 2,
+                                    "<entry name='type'>%s</entry>\n",
+                                    processor->processor_type);
+        virBufferIndentEscapeString(buf, indent + 2,
+                                    "<entry name='family'>%s</entry>\n",
+                                    processor->processor_family);
+        virBufferIndentEscapeString(buf, indent + 2,
+                                    "<entry name='manufacturer'>%s</entry>\n",
+                                    processor->processor_manufacturer);
+        virBufferIndentEscapeString(buf, indent + 2,
+                                    "<entry name='signature'>%s</entry>\n",
+                                    processor->processor_signature);
+        virBufferIndentEscapeString(buf, indent + 2,
+                                    "<entry name='version'>%s</entry>\n",
+                                    processor->processor_version);
+        virBufferIndentEscapeString(buf, indent + 2,
+                                    "<entry name='external_clock'>%s</entry>\n",
+                                    processor->processor_external_clock);
+        virBufferIndentEscapeString(buf, indent + 2,
+                                    "<entry name='max_speed'>%s</entry>\n",
+                                    processor->processor_max_speed);
+        virBufferIndentEscapeString(buf, indent + 2,
+                                    "<entry name='status'>%s</entry>\n",
+                                    processor->processor_status);
+        virBufferIndentEscapeString(buf, indent + 2,
+                                    "<entry name='serial_number'>%s</entry>\n",
+                                    processor->processor_serial_number);
+        virBufferIndentEscapeString(buf, indent + 2,
+                                    "<entry name='part_number'>%s</entry>\n",
+                                    processor->processor_part_number);
+        virBufferIndentAddLit(buf, indent, "</processor>\n");
     }
-
-    return;
 }

 static void
-virSysinfoMemoryFormat(virSysinfoDefPtr def, const char *prefix,
-                             virBufferPtr buf)
+virSysinfoMemoryFormat(virSysinfoDefPtr def, int indent, virBufferPtr buf)
 {
     int i;
-    int len = strlen(prefix);
     virSysinfoMemoryDefPtr memory;

     for (i = 0; i < def->nmemory; i++) {
         memory = &def->memory[i];

-        if ((memory->memory_size != NULL) ||
-            (memory->memory_form_factor != NULL) ||
-            (memory->memory_locator != NULL) ||
-            (memory->memory_bank_locator != NULL) ||
-            (memory->memory_type != NULL) ||
-            (memory->memory_type_detail != NULL) ||
-            (memory->memory_speed != NULL) ||
-            (memory->memory_manufacturer != NULL) ||
-            (memory->memory_serial_number != NULL) ||
-            (memory->memory_part_number != NULL)) {
-            virBufferAsprintf(buf, "%s  <memory_device>\n", prefix);
-            if (memory->memory_size != NULL) {
-                virBufferAdd(buf, prefix, len);
-                virBufferEscapeString(buf,
-                                      "    <entry name='size'>%s</entry>\n",
-                                      memory->memory_size);
-            }
-            if (memory->memory_form_factor != NULL) {
-                virBufferAdd(buf, prefix, len);
-                virBufferEscapeString(buf,
-                                      "    <entry name='form_factor'>%s</entry>\n",
-                                      memory->memory_form_factor);
-            }
-            if (memory->memory_locator != NULL) {
-                virBufferAdd(buf, prefix, len);
-                virBufferEscapeString(buf,
-                                      "    <entry name='locator'>%s</entry>\n",
-                                      memory->memory_locator);
-            }
-            if (memory->memory_bank_locator != NULL) {
-                virBufferAdd(buf, prefix, len);
-                virBufferEscapeString(buf,
-                                      "    <entry name='bank_locator'>%s</entry>\n",
-                                      memory->memory_bank_locator);
-            }
-            if (memory->memory_type != NULL) {
-                virBufferAdd(buf, prefix, len);
-                virBufferEscapeString(buf,
-                                      "    <entry name='type'>%s</entry>\n",
-                                      memory->memory_type);
-            }
-            if (memory->memory_type_detail != NULL) {
-                virBufferAdd(buf, prefix, len);
-                virBufferEscapeString(buf,
-                                      "    <entry name='type_detail'>%s</entry>\n",
-                                      memory->memory_type_detail);
-            }
-            if (memory->memory_speed != NULL) {
-                virBufferAdd(buf, prefix, len);
-                virBufferEscapeString(buf,
-                                      "    <entry name='speed'>%s</entry>\n",
-                                      memory->memory_speed);
-            }
-            if (memory->memory_manufacturer != NULL) {
-                virBufferAdd(buf, prefix, len);
-                virBufferEscapeString(buf,
-                                      "    <entry name='manufacturer'>%s</entry>\n",
-                                      memory->memory_manufacturer);
-            }
-            if (memory->memory_serial_number != NULL) {
-                virBufferAdd(buf, prefix, len);
-                virBufferEscapeString(buf,
-                                      "    <entry name='serial_number'>%s</entry>\n",
-                                      memory->memory_serial_number);
-            }
-            if (memory->memory_part_number != NULL) {
-                virBufferAdd(buf, prefix, len);
-                virBufferEscapeString(buf,
-                                      "    <entry name='part_number'>%s</entry>\n",
-                                      memory->memory_part_number);
-            }
-            virBufferAsprintf(buf, "%s  </memory_device>\n", prefix);
-        }
+        if (!memory->memory_size &&
+            !memory->memory_form_factor &&
+            !memory->memory_locator &&
+            !memory->memory_bank_locator &&
+            !memory->memory_type &&
+            !memory->memory_type_detail &&
+            !memory->memory_speed &&
+            !memory->memory_manufacturer &&
+            !memory->memory_serial_number &&
+            !memory->memory_part_number)
+            continue;
+
+        virBufferIndentAddLit(buf, indent, "<memory_device>\n");
+        virBufferIndentEscapeString(buf, indent + 2,
+                                    "<entry name='size'>%s</entry>\n",
+                                    memory->memory_size);
+        virBufferIndentEscapeString(buf, indent + 2,
+                                    "<entry name='form_factor'>%s</entry>\n",
+                                    memory->memory_form_factor);
+        virBufferIndentEscapeString(buf, indent + 2,
+                                    "<entry name='locator'>%s</entry>\n",
+                                    memory->memory_locator);
+        virBufferIndentEscapeString(buf, indent + 2,
+                                    "<entry name='bank_locator'>%s</entry>\n",
+                                    memory->memory_bank_locator);
+        virBufferIndentEscapeString(buf, indent + 2,
+                                    "<entry name='type'>%s</entry>\n",
+                                    memory->memory_type);
+        virBufferIndentEscapeString(buf, indent + 2,
+                                    "<entry name='type_detail'>%s</entry>\n",
+                                    memory->memory_type_detail);
+        virBufferIndentEscapeString(buf, indent + 2,
+                                    "<entry name='speed'>%s</entry>\n",
+                                    memory->memory_speed);
+        virBufferIndentEscapeString(buf, indent + 2,
+                                    "<entry name='manufacturer'>%s</entry>\n",
+                                    memory->memory_manufacturer);
+        virBufferIndentEscapeString(buf, indent + 2,
+                                    "<entry name='serial_number'>%s</entry>\n",
+                                    memory->memory_serial_number);
+        virBufferIndentEscapeString(buf, indent + 2,
+                                    "<entry name='part_number'>%s</entry>\n",
+                                    memory->memory_part_number);
+        virBufferIndentAddLit(buf, indent, "</memory_device>\n");
     }
-
-    return;
 }

 /**
  * virSysinfoFormat:
  * @def: structure to convert to xml string
- * @prefix: string to prefix before each line of xml
+ * @indent: number of spaces to output before each line of xml
  *
  * This returns the XML description of the sysinfo, or NULL after
  * generating an error message.
  */
 char *
-virSysinfoFormat(virSysinfoDefPtr def, const char *prefix)
+virSysinfoFormat(virSysinfoDefPtr def, int indent)
 {
     const char *type = virSysinfoTypeToString(def->type);
     virBuffer buf = VIR_BUFFER_INITIALIZER;
@@ -818,14 +707,14 @@ virSysinfoFormat(virSysinfoDefPtr def, const char *prefix)
         return NULL;
     }

-    virBufferAsprintf(&buf, "%s<sysinfo type='%s'>\n", prefix, type);
+    virBufferAsprintf(&buf, "%*s<sysinfo type='%s'>\n", indent, "", type);

-    virSysinfoBIOSFormat(def, prefix, &buf);
-    virSysinfoSystemFormat(def, prefix, &buf);
-    virSysinfoProcessorFormat(def, prefix, &buf);
-    virSysinfoMemoryFormat(def, prefix, &buf);
+    virSysinfoBIOSFormat(def, indent + 2, &buf);
+    virSysinfoSystemFormat(def, indent + 2, &buf);
+    virSysinfoProcessorFormat(def, indent + 2, &buf);
+    virSysinfoMemoryFormat(def, indent + 2, &buf);

-    virBufferAsprintf(&buf, "%s</sysinfo>\n", prefix);
+    virBufferIndentAddLit(&buf, indent, "</sysinfo>\n");

     if (virBufferError(&buf)) {
         virReportOOMError();
diff --git a/src/util/sysinfo.h b/src/util/sysinfo.h
index 86fd20f..3f17152 100644
--- a/src/util/sysinfo.h
+++ b/src/util/sysinfo.h
@@ -93,8 +93,8 @@ virSysinfoDefPtr virSysinfoRead(void);

 void virSysinfoDefFree(virSysinfoDefPtr def);

-char *virSysinfoFormat(virSysinfoDefPtr def, const char *prefix)
-    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
+char *virSysinfoFormat(virSysinfoDefPtr def, int indent)
+    ATTRIBUTE_NONNULL(1);

 bool virSysinfoIsEqual(virSysinfoDefPtr src,
                        virSysinfoDefPtr dst);
-- 
1.7.4.4


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