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

[libvirt] [PATCH 1/3] Cleanup virSysinfoRead()



[Cleanup] Separate BIOSInfo and SystemInfo part from virSysinfoRead()


Signed-off-by: Minoru Usui <usui mxm nes nec co jp>
---
 src/util/sysinfo.c |  210 ++++++++++++++++++++++++++++++++--------------------
 1 files changed, 130 insertions(+), 80 deletions(-)

diff --git a/src/util/sysinfo.c b/src/util/sysinfo.c
index 40ec2e3..53122f7 100644
--- a/src/util/sysinfo.c
+++ b/src/util/sysinfo.c
@@ -96,37 +96,10 @@ virSysinfoRead(void) {
 
 #else /* !WIN32 */
 
-virSysinfoDefPtr
-virSysinfoRead(void) {
-    char *path, *cur, *eol, *base;
-    virSysinfoDefPtr ret = NULL;
-    char *outbuf = NULL;
-    virCommandPtr cmd;
-
-    path = virFindFileInPath(SYSINFO_SMBIOS_DECODER);
-    if (path == NULL) {
-        virSmbiosReportError(VIR_ERR_INTERNAL_ERROR,
-                             _("Failed to find path for %s binary"),
-                             SYSINFO_SMBIOS_DECODER);
-        return NULL;
-    }
-
-    cmd = virCommandNewArgList(path, "-q", "-t", "0,1", NULL);
-    VIR_FREE(path);
-    virCommandSetOutputBuffer(cmd, &outbuf);
-    if (virCommandRun(cmd, NULL) < 0) {
-        virSmbiosReportError(VIR_ERR_INTERNAL_ERROR,
-                             _("Failed to execute command %s"),
-                             path);
-        goto cleanup;
-    }
-
-    if (VIR_ALLOC(ret) < 0)
-        goto no_memory;
-
-    ret->type = VIR_SYSINFO_SMBIOS;
-
-    base = outbuf;
+static char *
+parseBIOSInfo(char *base, virSysinfoDefPtr ret)
+{
+    char *cur, *eol;
 
     if ((cur = strstr(base, "Vendor: ")) != NULL) {
         cur += 8;
@@ -152,8 +125,21 @@ virSysinfoRead(void) {
         if ((eol) && ((ret->bios_release = strndup(cur, eol - cur)) == NULL))
             goto no_memory;
     }
-    if ((base = strstr(outbuf, "System Information")) == NULL)
-        goto cleanup;
+
+    return eol + 1;
+
+no_memory:
+    return NULL;
+}
+
+static char *
+parseSystemInfo(char *base, virSysinfoDefPtr ret)
+{
+    char *cur, *eol;
+
+    if ((base = strstr(base, "System Information")) == NULL)
+        return 0;
+
     if ((cur = strstr(base, "Manufacturer: ")) != NULL) {
         cur += 14;
         eol = strchr(cur, '\n');
@@ -198,6 +184,50 @@ virSysinfoRead(void) {
             goto no_memory;
     }
 
+    return eol + 1;
+
+no_memory:
+    return NULL;
+}
+
+virSysinfoDefPtr
+virSysinfoRead(void) {
+    char *path, *base;
+    virSysinfoDefPtr ret = NULL;
+    char *outbuf = NULL;
+    virCommandPtr cmd;
+
+    path = virFindFileInPath(SYSINFO_SMBIOS_DECODER);
+    if (path == NULL) {
+        virSmbiosReportError(VIR_ERR_INTERNAL_ERROR,
+                             _("Failed to find path for %s binary"),
+                             SYSINFO_SMBIOS_DECODER);
+        return NULL;
+    }
+
+    cmd = virCommandNewArgList(path, "-q", "-t", "0,1,4,17", NULL);
+    VIR_FREE(path);
+    virCommandSetOutputBuffer(cmd, &outbuf);
+    if (virCommandRun(cmd, NULL) < 0) {
+        virSmbiosReportError(VIR_ERR_INTERNAL_ERROR,
+                             _("Failed to execute command %s"),
+                             path);
+        goto cleanup;
+    }
+
+    if (VIR_ALLOC(ret) < 0)
+        goto no_memory;
+
+    ret->type = VIR_SYSINFO_SMBIOS;
+
+    base = outbuf;
+
+    if ((base = parseBIOSInfo(base, ret)) == NULL)
+        goto no_memory;
+
+    if ((base = parseSystemInfo(base, ret)) == NULL)
+        goto no_memory;
+
 cleanup:
     VIR_FREE(outbuf);
     virCommandFree(cmd);
@@ -213,108 +243,128 @@ no_memory:
 }
 #endif /* !WIN32 */
 
-/**
- * virSysinfoFormat:
- * @def: structure to convert to xml string
- * @prefix: string to prefix 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)
+static void
+BIOSInfoFormat(virSysinfoDefPtr def, const char *prefix, virBufferPtr buf)
 {
-    const char *type = virSysinfoTypeToString(def->type);
-    virBuffer buf = VIR_BUFFER_INITIALIZER;
-    size_t len = strlen(prefix);
-
-    if (!type) {
-        virSmbiosReportError(VIR_ERR_INTERNAL_ERROR,
-                             _("unexpected sysinfo type model %d"),
-                             def->type);
-        return NULL;
-    }
+    int len = strlen(prefix);
 
-    virBufferAsprintf(&buf, "%s<sysinfo type='%s'>\n", prefix, type);
     if ((def->bios_vendor != NULL) || (def->bios_version != NULL) ||
         (def->bios_date != NULL) || (def->bios_release != NULL)) {
-        virBufferAsprintf(&buf, "%s  <bios>\n", prefix);
+        virBufferAsprintf(buf, "%s  <bios>\n", prefix);
         if (def->bios_vendor != NULL) {
-            virBufferAdd(&buf, prefix, len);
-            virBufferEscapeString(&buf,
+            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,
+            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,
+            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,
+            virBufferAdd(buf, prefix, len);
+            virBufferEscapeString(buf,
                                   "    <entry name='release'>%s</entry>\n",
                                   def->bios_release);
         }
-        virBufferAsprintf(&buf, "%s  </bios>\n", prefix);
+        virBufferAsprintf(buf, "%s  </bios>\n", prefix);
     }
+
+    return;
+}
+
+static void
+SystemInfoFormat(virSysinfoDefPtr def, const char *prefix, 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);
+        virBufferAsprintf(buf, "%s  <system>\n", prefix);
         if (def->system_manufacturer != NULL) {
-            virBufferAdd(&buf, prefix, len);
-            virBufferEscapeString(&buf,
+            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,
+            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,
+            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,
+            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,
+            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,
+            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,
+            virBufferAdd(buf, prefix, len);
+            virBufferEscapeString(buf,
                                   "    <entry name='family'>%s</entry>\n",
                                   def->system_family);
         }
-        virBufferAsprintf(&buf, "%s  </system>\n", prefix);
+        virBufferAsprintf(buf, "%s  </system>\n", prefix);
+    }
+
+    return;
+}
+
+/**
+ * virSysinfoFormat:
+ * @def: structure to convert to xml string
+ * @prefix: string to prefix 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)
+{
+    const char *type = virSysinfoTypeToString(def->type);
+    virBuffer buf = VIR_BUFFER_INITIALIZER;
+
+    if (!type) {
+        virSmbiosReportError(VIR_ERR_INTERNAL_ERROR,
+                             _("unexpected sysinfo type model %d"),
+                             def->type);
+        return NULL;
     }
 
+    virBufferAsprintf(&buf, "%s<sysinfo type='%s'>\n", prefix, type);
+
+    BIOSInfoFormat(def, prefix, &buf);
+    SystemInfoFormat(def, prefix, &buf);
+
     virBufferAsprintf(&buf, "%s</sysinfo>\n", prefix);
 
     return virBufferContentAndReset(&buf);
-- 
1.7.1


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