conf: refactor virDomainBlkioDeviceParseXML to reduce calls to xmlNodeGetContent

virDomainBlkioDeviceParseXML() calls xmlNodeGetContent() multiple
times in a loop, but can easily be refactored to call it once for all
element nodes, and then use the result of that one call in each of the
(mutually exclusive) blocks that previously each had their own call to
xmlNodeGetContent.

This is being done in order to reduce the number of changes needed in
an upcoming patch that will eliminate the lack of checking for NULL on
return from xmlNodeGetContent().

As part of the simplification, the while() loop has been changed into
a for() so that we can use "continue" without bypassing the
"node = node->next".

Signed-off-by: Laine Stump <laine@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
This commit is contained in:
Laine Stump 2020-06-18 21:52:38 -04:00
parent ed8d1385f7
commit b595f44525

View File

@ -1642,73 +1642,85 @@ virDomainBlkioDeviceParseXML(xmlNodePtr root,
virBlkioDevicePtr dev) virBlkioDevicePtr dev)
{ {
xmlNodePtr node; xmlNodePtr node;
g_autofree char *c = NULL; g_autofree char *path = NULL;
node = root->children; for (node = root->children; node != NULL; node = node->next) {
while (node) { g_autofree char *c = NULL;
if (node->type == XML_ELEMENT_NODE) {
if (virXMLNodeNameEqual(node, "path") && !dev->path) { if (node->type != XML_ELEMENT_NODE)
dev->path = (char *)xmlNodeGetContent(node); continue;
} else if (virXMLNodeNameEqual(node, "weight")) {
c = (char *)xmlNodeGetContent(node); c = (char *)xmlNodeGetContent(node);
if (virStrToLong_ui(c, NULL, 10, &dev->weight) < 0) {
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, if (virXMLNodeNameEqual(node, "path")) {
_("could not parse weight %s"), /* To avoid the need for explicit cleanup on failure,
c); * don't set dev->path until we're assured of
goto error; * success. Until then, store it in an autofree pointer.
} */
VIR_FREE(c); if (!path)
} else if (virXMLNodeNameEqual(node, "read_bytes_sec")) { path = g_steal_pointer(&c);
c = (char *)xmlNodeGetContent(node); continue;
if (virStrToLong_ull(c, NULL, 10, &dev->rbps) < 0) { }
virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
_("could not parse read bytes sec %s"), if (virXMLNodeNameEqual(node, "weight")) {
c); if (virStrToLong_ui(c, NULL, 10, &dev->weight) < 0) {
goto error; virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
} _("could not parse weight %s"),
VIR_FREE(c); c);
} else if (virXMLNodeNameEqual(node, "write_bytes_sec")) { return -1;
c = (char *)xmlNodeGetContent(node); }
if (virStrToLong_ull(c, NULL, 10, &dev->wbps) < 0) { continue;
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, }
_("could not parse write bytes sec %s"),
c); if (virXMLNodeNameEqual(node, "read_bytes_sec")) {
goto error; if (virStrToLong_ull(c, NULL, 10, &dev->rbps) < 0) {
} virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
VIR_FREE(c); _("could not parse read bytes sec %s"),
} else if (virXMLNodeNameEqual(node, "read_iops_sec")) { c);
c = (char *)xmlNodeGetContent(node); return -1;
if (virStrToLong_ui(c, NULL, 10, &dev->riops) < 0) { }
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, continue;
_("could not parse read iops sec %s"), }
c);
goto error; if (virXMLNodeNameEqual(node, "write_bytes_sec")) {
} if (virStrToLong_ull(c, NULL, 10, &dev->wbps) < 0) {
VIR_FREE(c); virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
} else if (virXMLNodeNameEqual(node, "write_iops_sec")) { _("could not parse write bytes sec %s"),
c = (char *)xmlNodeGetContent(node); c);
if (virStrToLong_ui(c, NULL, 10, &dev->wiops) < 0) { return -1;
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, }
_("could not parse write iops sec %s"), continue;
c); }
goto error;
} if (virXMLNodeNameEqual(node, "read_iops_sec")) {
VIR_FREE(c); if (virStrToLong_ui(c, NULL, 10, &dev->riops) < 0) {
} virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
_("could not parse read iops sec %s"),
c);
return -1;
}
continue;
}
if (virXMLNodeNameEqual(node, "write_iops_sec")) {
if (virStrToLong_ui(c, NULL, 10, &dev->wiops) < 0) {
virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
_("could not parse write iops sec %s"),
c);
return -1;
}
continue;
} }
node = node->next;
} }
if (!dev->path) {
if (!path) {
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
_("missing per-device path")); _("missing per-device path"));
return -1; return -1;
} }
dev->path = g_steal_pointer(&path);
return 0; return 0;
error:
VIR_FREE(dev->path);
return -1;
} }