]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
conf: virDomainDiskDefIotuneParse: simplify parsing
authorPeter Krempa <pkrempa@redhat.com>
Tue, 12 Apr 2016 12:49:47 +0000 (14:49 +0200)
committerPeter Krempa <pkrempa@redhat.com>
Wed, 13 Apr 2016 06:21:27 +0000 (08:21 +0200)
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

index 06159fab0074d4d91a6025945b52cc5c0d7cc4cd..5ada00f839e4b91cbeea08521f0f1979babdb4c8 100644 (file)
@@ -6680,78 +6680,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,
@@ -6838,6 +6784,7 @@ virDomainDiskDefIotuneParse(virDomainDiskDefPtr def,
 
     return 0;
 }
+#undef PARSE_IOTUNE
 
 
 #define VENDOR_LEN  8