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@xxxxxxxxxx> --- 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 fc7fcfb0c6..65a110b16f 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -10808,75 +10808,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; -} /** @@ -10913,8 +10844,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. */ @@ -10956,9 +10887,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; @@ -11286,9 +11217,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) @@ -11529,14 +11460,14 @@ virDomainFSDefParseXML(virDomainXMLOptionPtr xmlopt, def->multidevs = VIR_DOMAIN_FS_MULTIDEVS_DEFAULT; } - 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; @@ -15388,8 +15319,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))) { @@ -19432,13 +19363,13 @@ virDomainFeaturesDefParse(virDomainDefPtr def, 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")); @@ -19782,13 +19713,13 @@ virDomainFeaturesDefParse(virDomainDefPtr def, } 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; @@ -20510,9 +20441,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) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 487eea4149..9f2abc1d8d 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -3484,6 +3484,7 @@ virVsockSetGuestCid; # util/virxml.h +virParseScaledValue; virXMLCheckIllegalChars; virXMLChildElementCount; virXMLExtractNamespaceXML; diff --git a/src/util/virxml.c b/src/util/virxml.c index 02b59ea2f8..e9429cd4ac 100644 --- a/src/util/virxml.c +++ b/src/util/virxml.c @@ -32,6 +32,7 @@ #include "viralloc.h" #include "virfile.h" #include "virstring.h" +#include "virutil.h" #define VIR_FROM_THIS VIR_FROM_XML @@ -1405,3 +1406,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 ed178105f6..886d1e025f 100644 --- a/src/util/virxml.h +++ b/src/util/virxml.h @@ -270,3 +270,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.26.2