[libvirt] [PATCH 03/11] caps: Use an enum internally for ostype value

Cole Robinson crobinso at redhat.com
Sat Apr 18 01:45:13 UTC 2015


But the internal API stays the same, and we just convert the value as
needed. Not useful yet, but this is the beginning step of using an enum
for ostype throughout the code.
---
 src/conf/capabilities.c | 74 ++++++++++++++++++++++++++++++++++++++-----------
 src/conf/capabilities.h |  2 +-
 src/conf/domain_conf.c  |  8 ++++++
 src/conf/domain_conf.h  | 13 +++++++++
 src/xenconfig/xen_xl.c  |  2 +-
 5 files changed, 81 insertions(+), 18 deletions(-)

diff --git a/src/conf/capabilities.c b/src/conf/capabilities.c
index b66c6dd..acae41a 100644
--- a/src/conf/capabilities.c
+++ b/src/conf/capabilities.c
@@ -32,6 +32,7 @@
 #include "cpu_conf.h"
 #include "virerror.h"
 #include "virstring.h"
+#include "domain_conf.h"
 
 #define VIR_FROM_THIS VIR_FROM_CAPABILITIES
 
@@ -155,8 +156,6 @@ virCapabilitiesFreeGuest(virCapsGuestPtr guest)
     if (guest == NULL)
         return;
 
-    VIR_FREE(guest->ostype);
-
     VIR_FREE(guest->arch.defaultInfo.emulator);
     VIR_FREE(guest->arch.defaultInfo.loader);
     for (i = 0; i < guest->arch.defaultInfo.nmachines; i++)
@@ -408,7 +407,7 @@ virCapabilitiesFreeMachines(virCapsGuestMachinePtr *machines,
  */
 virCapsGuestPtr
 virCapabilitiesAddGuest(virCapsPtr caps,
-                        const char *ostype,
+                        const char *ostypestr,
                         virArch arch,
                         const char *emulator,
                         const char *loader,
@@ -416,13 +415,18 @@ virCapabilitiesAddGuest(virCapsPtr caps,
                         virCapsGuestMachinePtr *machines)
 {
     virCapsGuestPtr guest;
+    int ostype;
 
     if (VIR_ALLOC(guest) < 0)
         goto error;
 
-    if (VIR_STRDUP(guest->ostype, ostype) < 0)
+    if ((ostype = virDomainOSTypeFromString(ostypestr)) < 0) {
+        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
+                       _("unknown OS type '%s'"), ostypestr);
         goto error;
+    }
 
+    guest->ostype = ostype;
     guest->arch.id = arch;
     guest->arch.wordsize = virArchGetWordSize(arch);
 
@@ -603,11 +607,19 @@ virCapabilitiesSupportsGuestArch(virCapsPtr caps,
  */
 extern int
 virCapabilitiesSupportsGuestOSType(virCapsPtr caps,
-                                   const char *ostype)
+                                   const char *ostypestr)
 {
     size_t i;
+    int ostype;
+
+    if ((ostype = virDomainOSTypeFromString(ostypestr)) < 0) {
+        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
+                       _("unknown OS type '%s'"), ostypestr);
+        return 0;
+    }
+
     for (i = 0; i < caps->nguests; i++) {
-        if (STREQ(caps->guests[i]->ostype, ostype))
+        if (caps->guests[i]->ostype == ostype)
             return 1;
     }
     return 0;
@@ -625,12 +637,20 @@ virCapabilitiesSupportsGuestOSType(virCapsPtr caps,
  */
 extern int
 virCapabilitiesSupportsGuestOSTypeArch(virCapsPtr caps,
-                                       const char *ostype,
+                                       const char *ostypestr,
                                        virArch arch)
 {
     size_t i;
+    int ostype;
+
+    if ((ostype = virDomainOSTypeFromString(ostypestr)) < 0) {
+        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
+                       _("unknown OS type '%s'"), ostypestr);
+        return 0;
+    }
+
     for (i = 0; i < caps->nguests; i++) {
-        if (STREQ(caps->guests[i]->ostype, ostype) &&
+        if (caps->guests[i]->ostype == ostype &&
             caps->guests[i]->arch.id == arch)
             return 1;
     }
@@ -648,14 +668,21 @@ virCapabilitiesSupportsGuestOSTypeArch(virCapsPtr caps,
  */
 extern virArch
 virCapabilitiesDefaultGuestArch(virCapsPtr caps,
-                                const char *ostype,
+                                const char *ostypestr,
                                 const char *domain)
 {
     size_t i, j;
+    int ostype;
+
+    if ((ostype = virDomainOSTypeFromString(ostypestr)) < 0) {
+        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
+                       _("unknown OS type '%s'"), ostypestr);
+        return VIR_ARCH_NONE;
+    }
 
     /* First try to find one matching host arch */
     for (i = 0; i < caps->nguests; i++) {
-        if (STREQ(caps->guests[i]->ostype, ostype)) {
+        if (caps->guests[i]->ostype == ostype) {
             for (j = 0; j < caps->guests[i]->arch.ndomains; j++) {
                 if (STREQ(caps->guests[i]->arch.domains[j]->type, domain) &&
                     caps->guests[i]->arch.id == caps->host.arch)
@@ -666,7 +693,7 @@ virCapabilitiesDefaultGuestArch(virCapsPtr caps,
 
     /* Otherwise find the first match */
     for (i = 0; i < caps->nguests; i++) {
-        if (STREQ(caps->guests[i]->ostype, ostype)) {
+        if (caps->guests[i]->ostype == ostype) {
             for (j = 0; j < caps->guests[i]->arch.ndomains; j++) {
                 if (STREQ(caps->guests[i]->arch.domains[j]->type, domain))
                     return caps->guests[i]->arch.id;
@@ -690,17 +717,24 @@ virCapabilitiesDefaultGuestArch(virCapsPtr caps,
  */
 extern const char *
 virCapabilitiesDefaultGuestMachine(virCapsPtr caps,
-                                   const char *ostype,
+                                   const char *ostypestr,
                                    virArch arch,
                                    const char *domain)
 {
     size_t i;
+    int ostype;
+
+    if ((ostype = virDomainOSTypeFromString(ostypestr)) < 0) {
+        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
+                       _("unknown OS type '%s'"), ostypestr);
+        return NULL;
+    }
 
     for (i = 0; i < caps->nguests; i++) {
         virCapsGuestPtr guest = caps->guests[i];
         size_t j;
 
-        if (!STREQ(guest->ostype, ostype) ||
+        if (guest->ostype != ostype ||
             guest->arch.id != arch)
             continue;
 
@@ -736,14 +770,22 @@ virCapabilitiesDefaultGuestMachine(virCapsPtr caps,
  */
 extern const char *
 virCapabilitiesDefaultGuestEmulator(virCapsPtr caps,
-                                    const char *ostype,
+                                    const char *ostypestr,
                                     virArch arch,
                                     const char *domain)
 {
     size_t i, j;
+    int ostype;
+
+    if ((ostype = virDomainOSTypeFromString(ostypestr)) < 0) {
+        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
+                       _("unknown OS type '%s'"), ostypestr);
+        return NULL;
+    }
+
     for (i = 0; i < caps->nguests; i++) {
         char *emulator;
-        if (STREQ(caps->guests[i]->ostype, ostype) &&
+        if (caps->guests[i]->ostype == ostype &&
             caps->guests[i]->arch.id == arch) {
             emulator = caps->guests[i]->arch.defaultInfo.emulator;
             for (j = 0; j < caps->guests[i]->arch.ndomains; j++) {
@@ -944,7 +986,7 @@ virCapabilitiesFormatXML(virCapsPtr caps)
         virBufferAddLit(&buf, "<guest>\n");
         virBufferAdjustIndent(&buf, 2);
         virBufferAsprintf(&buf, "<os_type>%s</os_type>\n",
-                          caps->guests[i]->ostype);
+                          virDomainOSTypeToString(caps->guests[i]->ostype));
         if (caps->guests[i]->arch.id)
             virBufferAsprintf(&buf, "<arch name='%s'>\n",
                               virArchToString(caps->guests[i]->arch.id));
diff --git a/src/conf/capabilities.h b/src/conf/capabilities.h
index 476deba..fc852ac 100644
--- a/src/conf/capabilities.h
+++ b/src/conf/capabilities.h
@@ -79,7 +79,7 @@ struct _virCapsGuestArch {
 typedef struct _virCapsGuest virCapsGuest;
 typedef virCapsGuest *virCapsGuestPtr;
 struct _virCapsGuest {
-    char *ostype;
+    int ostype;
     virCapsGuestArch arch;
     size_t nfeatures;
     size_t nfeatures_max;
diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index 860c950..8731fc1 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -122,6 +122,14 @@ VIR_ENUM_IMPL(virDomainVirt, VIR_DOMAIN_VIRT_LAST,
               "parallels",
               "bhyve")
 
+VIR_ENUM_IMPL(virDomainOS, VIR_DOMAIN_OSTYPE_LAST,
+              "hvm",
+              "xen",
+              "linux",
+              "exe",
+              "uml",
+              "aix")
+
 VIR_ENUM_IMPL(virDomainBoot, VIR_DOMAIN_BOOT_LAST,
               "fd",
               "cdrom",
diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h
index 3045652..6dc9918 100644
--- a/src/conf/domain_conf.h
+++ b/src/conf/domain_conf.h
@@ -228,6 +228,19 @@ typedef enum {
 } virDomainVirtType;
 
 typedef enum {
+    VIR_DOMAIN_OSTYPE_HVM,
+    VIR_DOMAIN_OSTYPE_XEN,
+    VIR_DOMAIN_OSTYPE_LINUX,
+    VIR_DOMAIN_OSTYPE_EXE,
+    VIR_DOMAIN_OSTYPE_UML,
+    VIR_DOMAIN_OSTYPE_AIX,
+
+    VIR_DOMAIN_OSTYPE_LAST
+} virDomainOSType;
+VIR_ENUM_DECL(virDomainOS)
+
+
+typedef enum {
     VIR_DOMAIN_DEVICE_ADDRESS_TYPE_NONE,
     VIR_DOMAIN_DEVICE_ADDRESS_TYPE_PCI,
     VIR_DOMAIN_DEVICE_ADDRESS_TYPE_DRIVE,
diff --git a/src/xenconfig/xen_xl.c b/src/xenconfig/xen_xl.c
index b572587..62284f7 100644
--- a/src/xenconfig/xen_xl.c
+++ b/src/xenconfig/xen_xl.c
@@ -68,7 +68,7 @@ xenParseXLOS(virConfPtr conf, virDomainDefPtr def, virCapsPtr caps)
         const char *boot;
 
         for (i = 0; i < caps->nguests; i++) {
-            if (STREQ(caps->guests[i]->ostype, "hvm") &&
+            if (caps->guests[i]->ostype == VIR_DOMAIN_OSTYPE_HVM &&
                 caps->guests[i]->arch.id == def->os.arch) {
                 if (VIR_ALLOC(def->os.loader) < 0 ||
                     VIR_STRDUP(def->os.loader->path,
-- 
2.3.5




More information about the libvir-list mailing list