[PATCH 2/3] vmx: convert to typesafe virConf accessors

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

 



Signed-off-by: Fabiano Fidêncio <fabiano@xxxxxxxxxxxx>
---
 src/vmx/vmx.c | 197 +++++++++++++++++++-------------------------------
 1 file changed, 73 insertions(+), 124 deletions(-)

diff --git a/src/vmx/vmx.c b/src/vmx/vmx.c
index df6a58a47..6247fa938 100644
--- a/src/vmx/vmx.c
+++ b/src/vmx/vmx.c
@@ -719,42 +719,36 @@ virVMXConvertToUTF8(const char *encoding, const char *string)
     return result;
 }
 
-
-
 static int
-virVMXGetConfigString(virConfPtr conf, const char *name, char **string,
-                      bool optional)
+virVMXGetConfigStringHelper(virConfPtr conf, const char *name, char **string,
+                            bool optional)
 {
-    virConfValuePtr value;
-
+    int result;
     *string = NULL;
-    value = virConfGetValue(conf, name);
 
-    if (value == NULL) {
-        if (optional)
-            return 0;
+    result = virConfGetValueString(conf, name, string);
+    if (result == 1 && *string != NULL)
+        return 1;
 
-        virReportError(VIR_ERR_INTERNAL_ERROR,
-                       _("Missing essential config entry '%s'"), name);
-        return -1;
-    }
+    if (optional)
+        return 0;
 
-    if (value->type != VIR_CONF_STRING) {
-        virReportError(VIR_ERR_INTERNAL_ERROR,
-                       _("Config entry '%s' must be a string"), name);
-        return -1;
-    }
+    virReportError(VIR_ERR_INTERNAL_ERROR,
+                   _("Missing essential config entry '%s'"), name);
+    return -1;
+}
 
-    if (value->str == NULL) {
-        if (optional)
-            return 0;
 
-        virReportError(VIR_ERR_INTERNAL_ERROR,
-                       _("Missing essential config entry '%s'"), name);
+static int
+virVMXGetConfigString(virConfPtr conf, const char *name, char **string,
+                      bool optional)
+{
+    *string = NULL;
+
+    if (virVMXGetConfigStringHelper(conf, name, string, optional) < 0)
         return -1;
-    }
 
-    return VIR_STRDUP(*string, value->str);
+    return 0;
 }
 
 
@@ -763,43 +757,26 @@ static int
 virVMXGetConfigUUID(virConfPtr conf, const char *name, unsigned char *uuid,
                     bool optional)
 {
-    virConfValuePtr value;
+    char *string = NULL;
+    int result;
 
-    value = virConfGetValue(conf, name);
+    result = virVMXGetConfigStringHelper(conf, name, &string, optional);
+    if (result <= 0)
+        return result;
 
-    if (value == NULL) {
-        if (optional) {
-            return 0;
-        } else {
-            virReportError(VIR_ERR_INTERNAL_ERROR,
-                           _("Missing essential config entry '%s'"), name);
-            return -1;
-        }
-    }
-
-    if (value->type != VIR_CONF_STRING) {
+    if (virUUIDParse(string, uuid) < 0) {
         virReportError(VIR_ERR_INTERNAL_ERROR,
-                       _("Config entry '%s' must be a string"), name);
-        return -1;
+                       _("Could not parse UUID from string '%s'"), string);
+        result = -1;
+        goto cleanup;
     }
 
-    if (value->str == NULL) {
-        if (optional) {
-            return 0;
-        } else {
-            virReportError(VIR_ERR_INTERNAL_ERROR,
-                           _("Missing essential config entry '%s'"), name);
-            return -1;
-        }
-    }
+    result = 0;
 
-    if (virUUIDParse(value->str, uuid) < 0) {
-        virReportError(VIR_ERR_INTERNAL_ERROR,
-                       _("Could not parse UUID from string '%s'"), value->str);
-        return -1;
-    }
+  cleanup:
+    VIR_FREE(string);
 
-    return 0;
+    return result;
 }
 
 
@@ -808,47 +785,35 @@ static int
 virVMXGetConfigLong(virConfPtr conf, const char *name, long long *number,
                     long long default_, bool optional)
 {
-    virConfValuePtr value;
+    char *string = NULL;
+    int result;
 
     *number = default_;
-    value = virConfGetValue(conf, name);
 
-    if (value == NULL) {
-        if (optional) {
-            return 0;
-        } else {
-            virReportError(VIR_ERR_INTERNAL_ERROR,
-                           _("Missing essential config entry '%s'"), name);
-            return -1;
-        }
-    }
+    result = virVMXGetConfigStringHelper(conf, name, &string, optional);
+    if (result <= 0)
+        return result;
 
-    if (value->type == VIR_CONF_STRING) {
-        if (value->str == NULL) {
-            if (optional) {
-                return 0;
-            } else {
-                virReportError(VIR_ERR_INTERNAL_ERROR,
-                               _("Missing essential config entry '%s'"), name);
-                return -1;
-            }
-        }
+    if (STRCASEEQ(string, "unlimited")) {
+        *number = -1;
+        result = 0;
+        goto cleanup;
+    }
 
-        if (STRCASEEQ(value->str, "unlimited")) {
-            *number = -1;
-        } else if (virStrToLong_ll(value->str, NULL, 10, number) < 0) {
-            virReportError(VIR_ERR_INTERNAL_ERROR,
-                           _("Config entry '%s' must represent an integer value"),
-                           name);
-            return -1;
-        }
-    } else {
+    if (virStrToLong_ll(string, NULL, 10, number) < 0) {
         virReportError(VIR_ERR_INTERNAL_ERROR,
-                       _("Config entry '%s' must be a string"), name);
-        return -1;
+                _("Config entry '%s' must represent an integer value"),
+                name);
+        result = -1;
+        goto cleanup;
     }
 
-    return 0;
+    result = 0;
+
+ cleanup:
+    VIR_FREE(string);
+
+    return result;
 }
 
 
@@ -857,49 +822,33 @@ static int
 virVMXGetConfigBoolean(virConfPtr conf, const char *name, bool *boolean_,
                        bool default_, bool optional)
 {
-    virConfValuePtr value;
+    char *string = NULL;
+    int result;
 
     *boolean_ = default_;
-    value = virConfGetValue(conf, name);
 
-    if (value == NULL) {
-        if (optional) {
-            return 0;
-        } else {
-            virReportError(VIR_ERR_INTERNAL_ERROR,
-                           _("Missing essential config entry '%s'"), name);
-            return -1;
-        }
-    }
+    result = virVMXGetConfigStringHelper(conf, name, &string, optional);
+    if (result <= 0)
+        return result;
 
-    if (value->type == VIR_CONF_STRING) {
-        if (value->str == NULL) {
-            if (optional) {
-                return 0;
-            } else {
-                virReportError(VIR_ERR_INTERNAL_ERROR,
-                               _("Missing essential config entry '%s'"), name);
-                return -1;
-            }
-        }
-
-        if (STRCASEEQ(value->str, "true")) {
-            *boolean_ = 1;
-        } else if (STRCASEEQ(value->str, "false")) {
-            *boolean_ = 0;
-        } else {
-            virReportError(VIR_ERR_INTERNAL_ERROR,
-                           _("Config entry '%s' must represent a boolean value "
-                             "(true|false)"), name);
-            return -1;
-        }
+    if (STRCASEEQ(string, "true")) {
+        *boolean_ = 1;
+    } else if (STRCASEEQ(string, "false")) {
+        *boolean_ = 0;
     } else {
         virReportError(VIR_ERR_INTERNAL_ERROR,
-                       _("Config entry '%s' must be a string"), name);
-        return -1;
+                       _("Config entry '%s' must represent a boolean value "
+                         "(true|false)"), name);
+        result = -1;
+        goto cleanup;
     }
 
-    return 0;
+    result = 0;
+
+ cleanup:
+    VIR_FREE(string);
+
+    return result;
 }
 
 
-- 
2.17.0

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]

  Powered by Linux