Blob Blame History Raw
From 0cee78aa69f5e3317b5e4853454a108e597228e5 Mon Sep 17 00:00:00 2001
Message-Id: <0cee78aa69f5e3317b5e4853454a108e597228e5@dist-git>
From: Michal Privoznik <mprivozn@redhat.com>
Date: Wed, 7 Oct 2020 18:45:33 +0200
Subject: [PATCH] conf: Move and rename virDomainParseScaledValue()
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

There is nothing domain specific about the function, thus it
should not have virDomain prefix. Also, the fact that it is a
static function makes it impossible to use from other files.
Move the function to virxml.c and drop the 'Domain' infix.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Daniel Henrique Barboza <danielhb413@gmail.com>
(cherry picked from commit 04bd77a19f8312493151ce377da40577b1470a0b)

Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1749518

Conflicts:
- src/conf/domain_conf.c: Some context mismatch, and some areas
the original commit changes don't exist in this old libvirt yet
or the calls are in other places because of refactors.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Message-Id: <26a847deef5941fd90f892cf5fe1443cf3fc90ca.1602087923.git.mprivozn@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
---
 src/conf/domain_conf.c   | 135 ++++++++++-----------------------------
 src/libvirt_private.syms |   1 +
 src/util/virxml.c        |  72 +++++++++++++++++++++
 src/util/virxml.h        |   8 +++
 4 files changed, 114 insertions(+), 102 deletions(-)

diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index 306926b64c..484f3b4352 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -10644,75 +10644,6 @@ virDomainDiskDefParseXML(virDomainXMLOptionPtr xmlopt,
     goto cleanup;
 }
 
-/**
- * virDomainParseScaledValue:
- * @xpath: XPath to memory amount
- * @units_xpath: XPath to units attribute
- * @ctxt: XPath context
- * @val: scaled value is stored here
- * @scale: default scale for @val
- * @max: maximal @val allowed
- * @required: is the value required?
- *
- * Parse a value located at @xpath within @ctxt, and store the
- * result into @val. The value is scaled by units located at
- * @units_xpath (or the 'unit' attribute under @xpath if
- * @units_xpath is NULL). If units are not present, the default
- * @scale is used. If @required is set, then the value must
- * exist; otherwise, the value is optional. The resulting value
- * is in bytes.
- *
- * Returns 1 on success,
- *         0 if the value was not present and !@required,
- *         -1 on failure after issuing error.
- */
-static int
-virDomainParseScaledValue(const char *xpath,
-                          const char *units_xpath,
-                          xmlXPathContextPtr ctxt,
-                          unsigned long long *val,
-                          unsigned long long scale,
-                          unsigned long long max,
-                          bool required)
-{
-    unsigned long long bytes;
-    g_autofree char *xpath_full = NULL;
-    g_autofree char *unit = NULL;
-    g_autofree char *bytes_str = NULL;
-
-    *val = 0;
-    xpath_full = g_strdup_printf("string(%s)", xpath);
-
-    bytes_str = virXPathString(xpath_full, ctxt);
-    if (!bytes_str) {
-        if (!required)
-            return 0;
-        virReportError(VIR_ERR_XML_ERROR,
-                       _("missing element or attribute '%s'"),
-                       xpath);
-        return -1;
-    }
-    VIR_FREE(xpath_full);
-
-    if (virStrToLong_ullp(bytes_str, NULL, 10, &bytes) < 0) {
-        virReportError(VIR_ERR_XML_ERROR,
-                       _("Invalid value '%s' for element or attribute '%s'"),
-                       bytes_str, xpath);
-        return -1;
-    }
-
-    if (units_xpath)
-         xpath_full = g_strdup_printf("string(%s)", units_xpath);
-    else
-         xpath_full = g_strdup_printf("string(%s/@unit)", xpath);
-    unit = virXPathString(xpath_full, ctxt);
-
-    if (virScaleInteger(&bytes, unit, scale, max) < 0)
-        return -1;
-
-    *val = bytes;
-    return 1;
-}
 
 
 /**
@@ -10749,8 +10680,8 @@ virDomainParseMemory(const char *xpath,
 
     max = virMemoryMaxValue(capped);
 
-    if (virDomainParseScaledValue(xpath, units_xpath, ctxt,
-                                  &bytes, 1024, max, required) < 0)
+    if (virParseScaledValue(xpath, units_xpath, ctxt,
+                            &bytes, 1024, max, required) < 0)
         return -1;
 
     /* Yes, we really do use kibibytes for our internal sizing.  */
@@ -10792,9 +10723,9 @@ virDomainParseMemoryLimit(const char *xpath,
     int ret;
     unsigned long long bytes;
 
-    ret = virDomainParseScaledValue(xpath, units_xpath, ctxt, &bytes, 1024,
-                                    VIR_DOMAIN_MEMORY_PARAM_UNLIMITED << 10,
-                                    false);
+    ret = virParseScaledValue(xpath, units_xpath, ctxt, &bytes, 1024,
+                              VIR_DOMAIN_MEMORY_PARAM_UNLIMITED << 10,
+                              false);
 
     if (ret < 0)
         return -1;
@@ -11125,9 +11056,9 @@ virDomainControllerDefParseXML(virDomainXMLOptionPtr xmlopt,
                                  "have an address"));
                 goto error;
             }
-            if ((rc = virDomainParseScaledValue("./pcihole64", NULL,
-                                                ctxt, &bytes, 1024,
-                                                1024ULL * ULONG_MAX, false)) < 0)
+            if ((rc = virParseScaledValue("./pcihole64", NULL,
+                                          ctxt, &bytes, 1024,
+                                          1024ULL * ULONG_MAX, false)) < 0)
                 goto error;
 
             if (rc == 1)
@@ -11349,14 +11280,14 @@ virDomainFSDefParseXML(virDomainXMLOptionPtr xmlopt,
         }
     }
 
-    if (virDomainParseScaledValue("./space_hard_limit[1]",
-                                  NULL, ctxt, &def->space_hard_limit,
-                                  1, ULLONG_MAX, false) < 0)
+    if (virParseScaledValue("./space_hard_limit[1]",
+                            NULL, ctxt, &def->space_hard_limit,
+                            1, ULLONG_MAX, false) < 0)
         goto error;
 
-    if (virDomainParseScaledValue("./space_soft_limit[1]",
-                                  NULL, ctxt, &def->space_soft_limit,
-                                  1, ULLONG_MAX, false) < 0)
+    if (virParseScaledValue("./space_soft_limit[1]",
+                            NULL, ctxt, &def->space_soft_limit,
+                            1, ULLONG_MAX, false) < 0)
         goto error;
 
     cur = node->children;
@@ -15205,8 +15136,8 @@ virDomainShmemDefParseXML(virDomainXMLOptionPtr xmlopt,
         goto cleanup;
     }
 
-    if (virDomainParseScaledValue("./size[1]", NULL, ctxt,
-                                  &def->size, 1, ULLONG_MAX, false) < 0)
+    if (virParseScaledValue("./size[1]", NULL, ctxt,
+                            &def->size, 1, ULLONG_MAX, false) < 0)
         goto cleanup;
 
     if ((server = virXPathNode("./server[1]", ctxt))) {
@@ -19603,9 +19534,9 @@ virDomainCachetuneDefParseCache(xmlXPathContextPtr ctxt,
         return -1;
     }
 
-    if (virDomainParseScaledValue("./@size", "./@unit",
-                                  ctxt, &size, 1024,
-                                  ULLONG_MAX, true) < 0)
+    if (virParseScaledValue("./@size", "./@unit",
+                            ctxt, &size, 1024,
+                            ULLONG_MAX, true) < 0)
         return -1;
 
     if (virResctrlAllocSetCacheSize(alloc, level, type, cache, size) < 0)
@@ -20712,13 +20643,13 @@ virDomainDefParseXML(xmlDocPtr xml,
                 VIR_FREE(tmp);
             }
 
-            if (virDomainParseScaledValue("./features/hpt/maxpagesize",
-                                          NULL,
-                                          ctxt,
-                                          &def->hpt_maxpagesize,
-                                          1024,
-                                          ULLONG_MAX,
-                                          false) < 0) {
+            if (virParseScaledValue("./features/hpt/maxpagesize",
+                                    NULL,
+                                    ctxt,
+                                    &def->hpt_maxpagesize,
+                                    1024,
+                                    ULLONG_MAX,
+                                    false) < 0) {
                 virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                                "%s",
                                _("Unable to parse HPT maxpagesize setting"));
@@ -20944,13 +20875,13 @@ virDomainDefParseXML(xmlDocPtr xml,
     }
 
     if (def->features[VIR_DOMAIN_FEATURE_SMM] == VIR_TRISTATE_SWITCH_ON) {
-        int rv = virDomainParseScaledValue("string(./features/smm/tseg)",
-                                           "string(./features/smm/tseg/@unit)",
-                                           ctxt,
-                                           &def->tseg_size,
-                                           1024 * 1024, /* Defaults to mebibytes */
-                                           ULLONG_MAX,
-                                           false);
+        int rv = virParseScaledValue("string(./features/smm/tseg)",
+                                     "string(./features/smm/tseg/@unit)",
+                                     ctxt,
+                                     &def->tseg_size,
+                                     1024 * 1024, /* Defaults to mebibytes */
+                                     ULLONG_MAX,
+                                     false);
         if (rv < 0)
             goto error;
         def->tseg_specified = rv;
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 130828706a..acb25eb8c8 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -3442,6 +3442,7 @@ virVsockSetGuestCid;
 
 
 # util/virxml.h
+virParseScaledValue;
 virXMLCheckIllegalChars;
 virXMLChildElementCount;
 virXMLExtractNamespaceXML;
diff --git a/src/util/virxml.c b/src/util/virxml.c
index 0e66d1623b..bae2e6aca5 100644
--- a/src/util/virxml.c
+++ b/src/util/virxml.c
@@ -33,6 +33,7 @@
 #include "viralloc.h"
 #include "virfile.h"
 #include "virstring.h"
+#include "virutil.h"
 
 #define VIR_FROM_THIS VIR_FROM_XML
 
@@ -1433,3 +1434,74 @@ virXMLNamespaceRegister(xmlXPathContextPtr ctxt,
 
     return 0;
 }
+
+
+/**
+ * virParseScaledValue:
+ * @xpath: XPath to memory amount
+ * @units_xpath: XPath to units attribute
+ * @ctxt: XPath context
+ * @val: scaled value is stored here
+ * @scale: default scale for @val
+ * @max: maximal @val allowed
+ * @required: is the value required?
+ *
+ * Parse a value located at @xpath within @ctxt, and store the
+ * result into @val. The value is scaled by units located at
+ * @units_xpath (or the 'unit' attribute under @xpath if
+ * @units_xpath is NULL). If units are not present, the default
+ * @scale is used. If @required is set, then the value must
+ * exist; otherwise, the value is optional. The resulting value
+ * is in bytes.
+ *
+ * Returns 1 on success,
+ *         0 if the value was not present and !@required,
+ *         -1 on failure after issuing error.
+ */
+int
+virParseScaledValue(const char *xpath,
+                    const char *units_xpath,
+                    xmlXPathContextPtr ctxt,
+                    unsigned long long *val,
+                    unsigned long long scale,
+                    unsigned long long max,
+                    bool required)
+{
+    unsigned long long bytes;
+    g_autofree char *xpath_full = NULL;
+    g_autofree char *unit = NULL;
+    g_autofree char *bytes_str = NULL;
+
+    *val = 0;
+    xpath_full = g_strdup_printf("string(%s)", xpath);
+
+    bytes_str = virXPathString(xpath_full, ctxt);
+    if (!bytes_str) {
+        if (!required)
+            return 0;
+        virReportError(VIR_ERR_XML_ERROR,
+                       _("missing element or attribute '%s'"),
+                       xpath);
+        return -1;
+    }
+    VIR_FREE(xpath_full);
+
+    if (virStrToLong_ullp(bytes_str, NULL, 10, &bytes) < 0) {
+        virReportError(VIR_ERR_XML_ERROR,
+                       _("Invalid value '%s' for element or attribute '%s'"),
+                       bytes_str, xpath);
+        return -1;
+    }
+
+    if (units_xpath)
+         xpath_full = g_strdup_printf("string(%s)", units_xpath);
+    else
+         xpath_full = g_strdup_printf("string(%s/@unit)", xpath);
+    unit = virXPathString(xpath_full, ctxt);
+
+    if (virScaleInteger(&bytes, unit, scale, max) < 0)
+        return -1;
+
+    *val = bytes;
+    return 1;
+}
diff --git a/src/util/virxml.h b/src/util/virxml.h
index 26ab9f9c2d..39b261687a 100644
--- a/src/util/virxml.h
+++ b/src/util/virxml.h
@@ -269,3 +269,11 @@ virXMLNamespaceFormatNS(virBufferPtr buf,
 int
 virXMLNamespaceRegister(xmlXPathContextPtr ctxt,
                         virXMLNamespace const *ns);
+
+int virParseScaledValue(const char *xpath,
+                        const char *units_xpath,
+                        xmlXPathContextPtr ctxt,
+                        unsigned long long *val,
+                        unsigned long long scale,
+                        unsigned long long max,
+                        bool required);
-- 
2.29.2