This series lays the groundwork for replacing some recurring boilerplate code
in src/conf/ regarding the extraction of XML attribute values.
For an on / off attribute, the boilerplate code looks roughly like this,
g_autofree char *str = NULL;
if (str = virXMLPropString(node, "x")) {
int val;
if ((val = virTristateBoolTypeFromString(str)) <= 0) {
virReportError(...)
return -1;
}
def->x = val;
}
with some variations regarding how `str` is free'd in case of later re-use,
the exact error message for invalid values, whether or not
`VIR_TRISTATE_(SWITCH|BOOL)_ABSENT` is explicitly checked for (`val < 0` vs.
`val <= 0`), whether an intermediate variable is used or the value is assigned
directly, and in some cases the conditions in the two if-blocks are merged.
After the refactoring, the above code block looks like this:
if (virXMLPropTristateBool(node, "x", VIR_XML_PROP_OPTIONAL, &def->x)
< 0)
return -1;
Similar functions are introduced for integer valued attributes, unsigned
integer valued attributes and enum valued attributes.
Patches #6, #7, and #8 demonstrate the application of these function and stand
representative of more patches that I did not sent along yet as to not drown
the mailing list in spam. These patches remove a total of ~ 1000 lines of code
and fix some places, where e.g. `<foo bar="default"/>` would incorrectly
be
accepted as virXMLTristateBool.
As an added benefit, this refactoring makes the error messages for invalid
values in these XML attributes much more consistent:
$ git diff master | grep "^-.*_(\"" | wc -l
239
$ git diff master | grep "^+.*_(\"" | wc -l
21
V1:
https://listman.redhat.com/archives/libvir-list/2021-March/msg00853.html
V2:
https://listman.redhat.com/archives/libvir-list/2021-March/msg00994.html
V3:
https://listman.redhat.com/archives/libvir-list/2021-March/msg01066.html
V4:
https://listman.redhat.com/archives/libvir-list/2021-April/msg00209.html
Changes since V4:
* Added custom error message for virXMLProp(Int|UInt) when the attribute value
is 0 and VIR_XML_PROP_NONZERO is specified.
Cheers,
Tim
Tim Wiederhake (8):
virxml: Add virXMLPropTristateBool
virxml: Add virXMLPropTristateSwitch
virxml: Add virXMLPropInt
virxml: Add virXMLPropUInt
virxml: Add virXMLPropEnum
virNetworkForwardNatDefParseXML: Use virXMLProp*
virDomainIOThreadIDDefParseXML: Use virXMLProp*
virCPUDefParseXML: Use virXMLProp*
src/conf/cpu_conf.c | 14 +-
src/conf/domain_conf.c | 14 +-
src/conf/network_conf.c | 16 +--
src/libvirt_private.syms | 5 +
src/util/virxml.c | 267 +++++++++++++++++++++++++++++++++++++++
src/util/virxml.h | 31 +++++
6 files changed, 312 insertions(+), 35 deletions(-)
--
2.26.2