mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2024-08-28 19:41:16 +00:00
conf: Move and rename virDomainParseScaledValue()
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>
This commit is contained in:
parent
afb1ea6776
commit
04bd77a19f
@ -10808,75 +10808,6 @@ virDomainDiskDefParseXML(virDomainXMLOptionPtr xmlopt,
|
|||||||
goto cleanup;
|
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);
|
max = virMemoryMaxValue(capped);
|
||||||
|
|
||||||
if (virDomainParseScaledValue(xpath, units_xpath, ctxt,
|
if (virParseScaledValue(xpath, units_xpath, ctxt,
|
||||||
&bytes, 1024, max, required) < 0)
|
&bytes, 1024, max, required) < 0)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
/* Yes, we really do use kibibytes for our internal sizing. */
|
/* Yes, we really do use kibibytes for our internal sizing. */
|
||||||
@ -10956,9 +10887,9 @@ virDomainParseMemoryLimit(const char *xpath,
|
|||||||
int ret;
|
int ret;
|
||||||
unsigned long long bytes;
|
unsigned long long bytes;
|
||||||
|
|
||||||
ret = virDomainParseScaledValue(xpath, units_xpath, ctxt, &bytes, 1024,
|
ret = virParseScaledValue(xpath, units_xpath, ctxt, &bytes, 1024,
|
||||||
VIR_DOMAIN_MEMORY_PARAM_UNLIMITED << 10,
|
VIR_DOMAIN_MEMORY_PARAM_UNLIMITED << 10,
|
||||||
false);
|
false);
|
||||||
|
|
||||||
if (ret < 0)
|
if (ret < 0)
|
||||||
return -1;
|
return -1;
|
||||||
@ -11286,9 +11217,9 @@ virDomainControllerDefParseXML(virDomainXMLOptionPtr xmlopt,
|
|||||||
"have an address"));
|
"have an address"));
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
if ((rc = virDomainParseScaledValue("./pcihole64", NULL,
|
if ((rc = virParseScaledValue("./pcihole64", NULL,
|
||||||
ctxt, &bytes, 1024,
|
ctxt, &bytes, 1024,
|
||||||
1024ULL * ULONG_MAX, false)) < 0)
|
1024ULL * ULONG_MAX, false)) < 0)
|
||||||
goto error;
|
goto error;
|
||||||
|
|
||||||
if (rc == 1)
|
if (rc == 1)
|
||||||
@ -11529,14 +11460,14 @@ virDomainFSDefParseXML(virDomainXMLOptionPtr xmlopt,
|
|||||||
def->multidevs = VIR_DOMAIN_FS_MULTIDEVS_DEFAULT;
|
def->multidevs = VIR_DOMAIN_FS_MULTIDEVS_DEFAULT;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (virDomainParseScaledValue("./space_hard_limit[1]",
|
if (virParseScaledValue("./space_hard_limit[1]",
|
||||||
NULL, ctxt, &def->space_hard_limit,
|
NULL, ctxt, &def->space_hard_limit,
|
||||||
1, ULLONG_MAX, false) < 0)
|
1, ULLONG_MAX, false) < 0)
|
||||||
goto error;
|
goto error;
|
||||||
|
|
||||||
if (virDomainParseScaledValue("./space_soft_limit[1]",
|
if (virParseScaledValue("./space_soft_limit[1]",
|
||||||
NULL, ctxt, &def->space_soft_limit,
|
NULL, ctxt, &def->space_soft_limit,
|
||||||
1, ULLONG_MAX, false) < 0)
|
1, ULLONG_MAX, false) < 0)
|
||||||
goto error;
|
goto error;
|
||||||
|
|
||||||
cur = node->children;
|
cur = node->children;
|
||||||
@ -15388,8 +15319,8 @@ virDomainShmemDefParseXML(virDomainXMLOptionPtr xmlopt,
|
|||||||
goto cleanup;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (virDomainParseScaledValue("./size[1]", NULL, ctxt,
|
if (virParseScaledValue("./size[1]", NULL, ctxt,
|
||||||
&def->size, 1, ULLONG_MAX, false) < 0)
|
&def->size, 1, ULLONG_MAX, false) < 0)
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
|
|
||||||
if ((server = virXPathNode("./server[1]", ctxt))) {
|
if ((server = virXPathNode("./server[1]", ctxt))) {
|
||||||
@ -19432,13 +19363,13 @@ virDomainFeaturesDefParse(virDomainDefPtr def,
|
|||||||
VIR_FREE(tmp);
|
VIR_FREE(tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (virDomainParseScaledValue("./features/hpt/maxpagesize",
|
if (virParseScaledValue("./features/hpt/maxpagesize",
|
||||||
NULL,
|
NULL,
|
||||||
ctxt,
|
ctxt,
|
||||||
&def->hpt_maxpagesize,
|
&def->hpt_maxpagesize,
|
||||||
1024,
|
1024,
|
||||||
ULLONG_MAX,
|
ULLONG_MAX,
|
||||||
false) < 0) {
|
false) < 0) {
|
||||||
virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
|
virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
|
||||||
"%s",
|
"%s",
|
||||||
_("Unable to parse HPT maxpagesize setting"));
|
_("Unable to parse HPT maxpagesize setting"));
|
||||||
@ -19782,13 +19713,13 @@ virDomainFeaturesDefParse(virDomainDefPtr def,
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (def->features[VIR_DOMAIN_FEATURE_SMM] == VIR_TRISTATE_SWITCH_ON) {
|
if (def->features[VIR_DOMAIN_FEATURE_SMM] == VIR_TRISTATE_SWITCH_ON) {
|
||||||
int rv = virDomainParseScaledValue("string(./features/smm/tseg)",
|
int rv = virParseScaledValue("string(./features/smm/tseg)",
|
||||||
"string(./features/smm/tseg/@unit)",
|
"string(./features/smm/tseg/@unit)",
|
||||||
ctxt,
|
ctxt,
|
||||||
&def->tseg_size,
|
&def->tseg_size,
|
||||||
1024 * 1024, /* Defaults to mebibytes */
|
1024 * 1024, /* Defaults to mebibytes */
|
||||||
ULLONG_MAX,
|
ULLONG_MAX,
|
||||||
false);
|
false);
|
||||||
if (rv < 0)
|
if (rv < 0)
|
||||||
return -1;
|
return -1;
|
||||||
def->tseg_specified = rv;
|
def->tseg_specified = rv;
|
||||||
@ -20507,9 +20438,9 @@ virDomainCachetuneDefParseCache(xmlXPathContextPtr ctxt,
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (virDomainParseScaledValue("./@size", "./@unit",
|
if (virParseScaledValue("./@size", "./@unit",
|
||||||
ctxt, &size, 1024,
|
ctxt, &size, 1024,
|
||||||
ULLONG_MAX, true) < 0)
|
ULLONG_MAX, true) < 0)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
if (virResctrlAllocSetCacheSize(alloc, level, type, cache, size) < 0)
|
if (virResctrlAllocSetCacheSize(alloc, level, type, cache, size) < 0)
|
||||||
|
@ -3484,6 +3484,7 @@ virVsockSetGuestCid;
|
|||||||
|
|
||||||
|
|
||||||
# util/virxml.h
|
# util/virxml.h
|
||||||
|
virParseScaledValue;
|
||||||
virXMLCheckIllegalChars;
|
virXMLCheckIllegalChars;
|
||||||
virXMLChildElementCount;
|
virXMLChildElementCount;
|
||||||
virXMLExtractNamespaceXML;
|
virXMLExtractNamespaceXML;
|
||||||
|
@ -32,6 +32,7 @@
|
|||||||
#include "viralloc.h"
|
#include "viralloc.h"
|
||||||
#include "virfile.h"
|
#include "virfile.h"
|
||||||
#include "virstring.h"
|
#include "virstring.h"
|
||||||
|
#include "virutil.h"
|
||||||
|
|
||||||
#define VIR_FROM_THIS VIR_FROM_XML
|
#define VIR_FROM_THIS VIR_FROM_XML
|
||||||
|
|
||||||
@ -1399,3 +1400,74 @@ virXMLNamespaceRegister(xmlXPathContextPtr ctxt,
|
|||||||
|
|
||||||
return 0;
|
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;
|
||||||
|
}
|
||||||
|
@ -270,3 +270,11 @@ virXMLNamespaceFormatNS(virBufferPtr buf,
|
|||||||
int
|
int
|
||||||
virXMLNamespaceRegister(xmlXPathContextPtr ctxt,
|
virXMLNamespaceRegister(xmlXPathContextPtr ctxt,
|
||||||
virXMLNamespace const *ns);
|
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);
|
||||||
|
Loading…
Reference in New Issue
Block a user