Since the structure was pre-initialized to 0 we don't need to set every single member to 0 if it's not present in the XML. Additionally if we put the name of the field into the error message the code can be simplified using a macro to parse the members. --- src/conf/domain_conf.c | 81 +++++++++----------------------------------------- 1 file changed, 14 insertions(+), 67 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index ac464d3..3c066b4 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -6675,78 +6675,24 @@ virDomainDiskBackingStoreParse(xmlXPathContextPtr ctxt, return ret; } +#define PARSE_IOTUNE(val) \ + if (virXPathULongLong("string(./iotune/" #val ")", \ + ctxt, &def->blkdeviotune.val) == -2) { \ + virReportError(VIR_ERR_XML_ERROR, \ + _("disk iotune field '%s' must be an integer"), #val); \ + return -1; \ + } static int virDomainDiskDefIotuneParse(virDomainDiskDefPtr def, xmlXPathContextPtr ctxt) { - int ret; - - ret = virXPathULongLong("string(./iotune/total_bytes_sec)", - ctxt, - &def->blkdeviotune.total_bytes_sec); - if (ret == -2) { - virReportError(VIR_ERR_XML_ERROR, "%s", - _("total throughput limit must be an integer")); - return -1; - } else if (ret < 0) { - def->blkdeviotune.total_bytes_sec = 0; - } - - ret = virXPathULongLong("string(./iotune/read_bytes_sec)", - ctxt, - &def->blkdeviotune.read_bytes_sec); - if (ret == -2) { - virReportError(VIR_ERR_XML_ERROR, "%s", - _("read throughput limit must be an integer")); - return -1; - } else if (ret < 0) { - def->blkdeviotune.read_bytes_sec = 0; - } - - ret = virXPathULongLong("string(./iotune/write_bytes_sec)", - ctxt, - &def->blkdeviotune.write_bytes_sec); - if (ret == -2) { - virReportError(VIR_ERR_XML_ERROR, "%s", - _("write throughput limit must be an integer")); - return -1; - } else if (ret < 0) { - def->blkdeviotune.write_bytes_sec = 0; - } - - ret = virXPathULongLong("string(./iotune/total_iops_sec)", - ctxt, - &def->blkdeviotune.total_iops_sec); - if (ret == -2) { - virReportError(VIR_ERR_XML_ERROR, "%s", - _("total I/O operations limit must be an integer")); - return -1; - } else if (ret < 0) { - def->blkdeviotune.total_iops_sec = 0; - } - - ret = virXPathULongLong("string(./iotune/read_iops_sec)", - ctxt, - &def->blkdeviotune.read_iops_sec); - if (ret == -2) { - virReportError(VIR_ERR_XML_ERROR, "%s", - _("read I/O operations limit must be an integer")); - return -1; - } else if (ret < 0) { - def->blkdeviotune.read_iops_sec = 0; - } - - ret = virXPathULongLong("string(./iotune/write_iops_sec)", - ctxt, - &def->blkdeviotune.write_iops_sec); - if (ret == -2) { - virReportError(VIR_ERR_XML_ERROR, "%s", - _("write I/O operations limit must be an integer")); - return -1; - } else if (ret < 0) { - def->blkdeviotune.write_iops_sec = 0; - } + PARSE_IOTUNE(total_bytes_sec); + PARSE_IOTUNE(read_bytes_sec); + PARSE_IOTUNE(write_bytes_sec); + PARSE_IOTUNE(total_iops_sec); + PARSE_IOTUNE(read_iops_sec); + PARSE_IOTUNE(write_iops_sec); if (virXPathULongLong("string(./iotune/total_bytes_sec_max)", ctxt, @@ -6833,6 +6779,7 @@ virDomainDiskDefIotuneParse(virDomainDiskDefPtr def, return 0; } +#undef PARSE_IOTUNE #define VENDOR_LEN 8 -- 2.8.0 -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list