Remove the unnecessary check for valid arguments and use virXMLPropULongLong instead of hand-written property parsers. Signed-off-by: Peter Krempa <pkrempa@xxxxxxxxxx> --- src/conf/netdev_bandwidth_conf.c | 89 +++++++++++--------------------- 1 file changed, 29 insertions(+), 60 deletions(-) diff --git a/src/conf/netdev_bandwidth_conf.c b/src/conf/netdev_bandwidth_conf.c index 2ac1983dce..f34d7499ae 100644 --- a/src/conf/netdev_bandwidth_conf.c +++ b/src/conf/netdev_bandwidth_conf.c @@ -25,61 +25,46 @@ #define VIR_FROM_THIS VIR_FROM_NONE static int -virNetDevBandwidthParseRate(xmlNodePtr node, virNetDevBandwidthRate *rate) +virNetDevBandwidthParseRate(xmlNodePtr node, + virNetDevBandwidthRate *rate, + bool allowFloor) { - g_autofree char *average = NULL; - g_autofree char *peak = NULL; - g_autofree char *burst = NULL; - g_autofree char *floor = NULL; - - if (!node || !rate) { - virReportError(VIR_ERR_INVALID_ARG, "%s", - _("invalid argument supplied")); + int rc_average; + int rc_peak; + int rc_burst; + int rc_floor; + + if ((rc_average = virXMLPropULongLong(node, "average", 10, VIR_XML_PROP_NONE, + &rate->average)) < 0) return -1; - } - average = virXMLPropString(node, "average"); - peak = virXMLPropString(node, "peak"); - burst = virXMLPropString(node, "burst"); - floor = virXMLPropString(node, "floor"); + if ((rc_peak = virXMLPropULongLong(node, "peak", 10, VIR_XML_PROP_NONE, + &rate->peak)) < 0) + return -1; - if (average) { - if (virStrToLong_ullp(average, NULL, 10, &rate->average) < 0) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, - _("could not convert bandwidth average value '%1$s'"), - average); - return -1; - } - } else if (!floor) { - virReportError(VIR_ERR_XML_DETAIL, "%s", - _("Missing mandatory average or floor attributes")); + if ((rc_burst = virXMLPropULongLong(node, "burst", 10, VIR_XML_PROP_NONE, + &rate->burst)) < 0) return -1; - } - if ((peak || burst) && !average) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", - _("'peak' and 'burst' require 'average' attribute")); + if ((rc_floor = virXMLPropULongLong(node, "floor", 10, VIR_XML_PROP_NONE, + &rate->floor)) < 0) return -1; - } - if (peak && virStrToLong_ullp(peak, NULL, 10, &rate->peak) < 0) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, - _("could not convert bandwidth peak value '%1$s'"), - peak); + if (!rc_average && !rc_floor) { + virReportError(VIR_ERR_XML_DETAIL, "%s", + _("Missing mandatory average or floor attributes")); return -1; } - if (burst && virStrToLong_ullp(burst, NULL, 10, &rate->burst) < 0) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, - _("could not convert bandwidth burst value '%1$s'"), - burst); + if ((rc_peak || rc_burst) && !rc_average) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", + _("'peak' and 'burst' require 'average' attribute")); return -1; } - if (floor && virStrToLong_ullp(floor, NULL, 10, &rate->floor) < 0) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, - _("could not convert bandwidth floor value '%1$s'"), - floor); + if (rc_floor && !allowFloor) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", + _("floor attribute is not supported for this config")); return -1; } @@ -157,32 +142,16 @@ virNetDevBandwidthParse(virNetDevBandwidth **bandwidth, if (in) { def->in = g_new0(virNetDevBandwidthRate, 1); - if (virNetDevBandwidthParseRate(in, def->in) < 0) { - /* helper reported error for us */ - return -1; - } - - if (def->in->floor && !allowFloor) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", - _("floor attribute is not supported for this config")); + if (virNetDevBandwidthParseRate(in, def->in, allowFloor) < 0) return -1; - } } if (out) { def->out = g_new0(virNetDevBandwidthRate, 1); - if (virNetDevBandwidthParseRate(out, def->out) < 0) { - /* helper reported error for us */ - return -1; - } - - if (def->out->floor) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", - _("'floor' attribute allowed " - "only in <inbound> element")); + /* floor is not allowed for <outbound> */ + if (virNetDevBandwidthParseRate(out, def->out, false) < 0) return -1; - } } if (def->in || def->out) -- 2.40.1