]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
conf: use virXMLPropString and virXMLNodeContentString for vcpu parsing
authorPavel Hrdina <phrdina@redhat.com>
Wed, 16 Aug 2017 08:45:14 +0000 (10:45 +0200)
committerPavel Hrdina <phrdina@redhat.com>
Thu, 17 Aug 2017 13:42:48 +0000 (15:42 +0200)
XPath is good for random search of elements, not for accessing
attributes of one node.

Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
src/conf/domain_conf.c

index db042e5dc1f09812300dfbd5204548e40aef8a51..3db56ffb7aec4615051adf5847dca953aea628f8 100644 (file)
@@ -16914,66 +16914,70 @@ virDomainVcpuParse(virDomainDefPtr def,
 {
     int n;
     xmlNodePtr *nodes = NULL;
+    xmlNodePtr vcpuNode;
     size_t i;
     char *tmp = NULL;
     unsigned int maxvcpus;
     unsigned int vcpus;
     int ret = -1;
 
-    if ((n = virXPathUInt("string(./vcpu[1])", ctxt, &maxvcpus)) < 0) {
-        if (n == -2) {
-            virReportError(VIR_ERR_XML_ERROR, "%s",
-                           _("maximum vcpus count must be an integer"));
-            goto cleanup;
-        }
-
-        maxvcpus = 1;
-    }
-
-    if (virDomainDefSetVcpusMax(def, maxvcpus, xmlopt) < 0)
-        goto cleanup;
+    vcpus = maxvcpus = 1;
 
-    if ((n = virXPathUInt("string(./vcpu[1]/@current)", ctxt, &vcpus)) < 0) {
-        if (n == -2) {
-            virReportError(VIR_ERR_XML_ERROR, "%s",
-                           _("current vcpus count must be an integer"));
-            goto cleanup;
+    if ((vcpuNode = virXPathNode("./vcpu[1]", ctxt))) {
+        if ((tmp = virXMLNodeContentString(vcpuNode))) {
+            if (virStrToLong_ui(tmp, NULL, 10, &maxvcpus) < 0) {
+                virReportError(VIR_ERR_XML_ERROR, "%s",
+                               _("maximum vcpus count must be an integer"));
+                goto cleanup;
+            }
+            VIR_FREE(tmp);
         }
 
-        vcpus = maxvcpus;
-    }
-
-
-    tmp = virXPathString("string(./vcpu[1]/@placement)", ctxt);
-    if (tmp) {
-        if ((def->placement_mode =
-             virDomainCpuPlacementModeTypeFromString(tmp)) < 0) {
-             virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
-                            _("Unsupported CPU placement mode '%s'"),
-                            tmp);
-             goto cleanup;
+        if ((tmp = virXMLPropString(vcpuNode, "current"))) {
+            if (virStrToLong_ui(tmp, NULL, 10, &vcpus) < 0) {
+                virReportError(VIR_ERR_XML_ERROR, "%s",
+                               _("current vcpus count must be an integer"));
+                goto cleanup;
+            }
+            VIR_FREE(tmp);
+        } else {
+            vcpus = maxvcpus;
         }
-        VIR_FREE(tmp);
-    } else {
-        def->placement_mode = VIR_DOMAIN_CPU_PLACEMENT_MODE_STATIC;
-    }
 
-    if (def->placement_mode != VIR_DOMAIN_CPU_PLACEMENT_MODE_AUTO) {
-        tmp = virXPathString("string(./vcpu[1]/@cpuset)", ctxt);
+        tmp = virXMLPropString(vcpuNode, "placement");
         if (tmp) {
-            if (virBitmapParse(tmp, &def->cpumask, VIR_DOMAIN_CPUMASK_LEN) < 0)
-                goto cleanup;
-
-            if (virBitmapIsAllClear(def->cpumask)) {
+            if ((def->placement_mode =
+                 virDomainCpuPlacementModeTypeFromString(tmp)) < 0) {
                 virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
-                               _("Invalid value of 'cpuset': %s"), tmp);
+                               _("Unsupported CPU placement mode '%s'"),
+                               tmp);
                 goto cleanup;
             }
-
             VIR_FREE(tmp);
+        } else {
+            def->placement_mode = VIR_DOMAIN_CPU_PLACEMENT_MODE_STATIC;
+        }
+
+        if (def->placement_mode != VIR_DOMAIN_CPU_PLACEMENT_MODE_AUTO) {
+            tmp = virXMLPropString(vcpuNode, "cpuset");
+            if (tmp) {
+                if (virBitmapParse(tmp, &def->cpumask, VIR_DOMAIN_CPUMASK_LEN) < 0)
+                    goto cleanup;
+
+                if (virBitmapIsAllClear(def->cpumask)) {
+                    virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
+                                   _("Invalid value of 'cpuset': %s"), tmp);
+                    goto cleanup;
+                }
+
+                VIR_FREE(tmp);
+            }
         }
     }
 
+    if (virDomainDefSetVcpusMax(def, maxvcpus, xmlopt) < 0)
+        goto cleanup;
+
     if ((n = virXPathNodeSet("./vcpus/vcpu", ctxt, &nodes)) < 0)
         goto cleanup;