]> xenbits.xensource.com Git - libvirt.git/commitdiff
conf: Split out parsing of network disk source XML elements
authorPeter Krempa <pkrempa@redhat.com>
Tue, 9 May 2017 12:25:02 +0000 (14:25 +0200)
committerPeter Krempa <pkrempa@redhat.com>
Wed, 4 Oct 2017 08:38:49 +0000 (10:38 +0200)
virDomainDiskSourceParse got to the point of being an ugly spaghetti
mess by adding more and more stuff into it. Split out parsing of network
disk information into a separate function so that it stays contained.

src/conf/domain_conf.c

index 87192eb2dcc3ba45c305678a0f00d998e91aa492..98f5666ef42e4384c8375611c33ea39340edc005 100644 (file)
@@ -8106,107 +8106,125 @@ virDomainDiskSourcePoolDefParse(xmlNodePtr node,
 }
 
 
-int
-virDomainDiskSourceParse(xmlNodePtr node,
-                         xmlXPathContextPtr ctxt,
-                         virStorageSourcePtr src,
-                         unsigned int flags)
+static int
+virDomainDiskSourceNetworkParse(xmlNodePtr node,
+                                xmlXPathContextPtr ctxt,
+                                virStorageSourcePtr src,
+                                unsigned int flags)
 {
-    int ret = -1;
     char *protocol = NULL;
-    xmlNodePtr saveNode = ctxt->node;
     char *haveTLS = NULL;
     char *tlsCfg = NULL;
     int tlsCfgVal;
+    int ret = -1;
 
-    ctxt->node = node;
+    if (!(protocol = virXMLPropString(node, "protocol"))) {
+        virReportError(VIR_ERR_XML_ERROR, "%s",
+                       _("missing network source protocol type"));
+        goto cleanup;
+    }
 
-    switch ((virStorageType)src->type) {
-    case VIR_STORAGE_TYPE_FILE:
-        src->path = virXMLPropString(node, "file");
-        break;
-    case VIR_STORAGE_TYPE_BLOCK:
-        src->path = virXMLPropString(node, "dev");
-        break;
-    case VIR_STORAGE_TYPE_DIR:
-        src->path = virXMLPropString(node, "dir");
-        break;
-    case VIR_STORAGE_TYPE_NETWORK:
-        if (!(protocol = virXMLPropString(node, "protocol"))) {
-            virReportError(VIR_ERR_XML_ERROR, "%s",
-                           _("missing network source protocol type"));
+    if ((src->protocol = virStorageNetProtocolTypeFromString(protocol)) <= 0) {
+        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
+                       _("unknown protocol type '%s'"), protocol);
+        goto cleanup;
+    }
+
+    if (!(src->path = virXMLPropString(node, "name")) &&
+        src->protocol != VIR_STORAGE_NET_PROTOCOL_NBD) {
+        virReportError(VIR_ERR_XML_ERROR, "%s",
+                       _("missing name for disk source"));
+        goto cleanup;
+    }
+
+    /* Check tls=yes|no domain setting for the block device
+     * At present only VxHS. Other block devices may be added later */
+    if (src->protocol == VIR_STORAGE_NET_PROTOCOL_VXHS &&
+        (haveTLS = virXMLPropString(node, "tls"))) {
+        if ((src->haveTLS =
+            virTristateBoolTypeFromString(haveTLS)) <= 0) {
+            virReportError(VIR_ERR_XML_ERROR,
+                       _("unknown disk source 'tls' setting '%s'"),
+                       haveTLS);
             goto cleanup;
         }
+    }
 
-        if ((src->protocol = virStorageNetProtocolTypeFromString(protocol)) <= 0) {
-            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
-                           _("unknown protocol type '%s'"), protocol);
+    if ((flags & VIR_DOMAIN_DEF_PARSE_STATUS) &&
+        (tlsCfg = virXMLPropString(node, "tlsFromConfig"))) {
+        if (virStrToLong_i(tlsCfg, NULL, 10, &tlsCfgVal) < 0) {
+            virReportError(VIR_ERR_XML_ERROR,
+                           _("Invalid tlsFromConfig value: %s"),
+                           tlsCfg);
             goto cleanup;
         }
+        src->tlsFromConfig = !!tlsCfgVal;
+    }
 
-        if (!(src->path = virXMLPropString(node, "name")) &&
-            src->protocol != VIR_STORAGE_NET_PROTOCOL_NBD) {
-            virReportError(VIR_ERR_XML_ERROR, "%s",
-                           _("missing name for disk source"));
+    /* for historical reasons the volume name for gluster volume is stored
+     * as a part of the path. This is hard to work with when dealing with
+     * relative names. Split out the volume into a separate variable */
+    if (src->path && src->protocol == VIR_STORAGE_NET_PROTOCOL_GLUSTER) {
+        char *tmp;
+        if (!(tmp = strchr(src->path, '/')) ||
+            tmp == src->path) {
+            virReportError(VIR_ERR_XML_ERROR,
+                           _("missing volume name or file name in "
+                             "gluster source path '%s'"), src->path);
             goto cleanup;
         }
 
-        /* Check tls=yes|no domain setting for the block device
-         * At present only VxHS. Other block devices may be added later */
-        if (src->protocol == VIR_STORAGE_NET_PROTOCOL_VXHS &&
-            (haveTLS = virXMLPropString(node, "tls"))) {
-            if ((src->haveTLS =
-                virTristateBoolTypeFromString(haveTLS)) <= 0) {
-                virReportError(VIR_ERR_XML_ERROR,
-                           _("unknown disk source 'tls' setting '%s'"),
-                           haveTLS);
-                goto cleanup;
-            }
-        }
+        src->volume = src->path;
 
-        if ((flags & VIR_DOMAIN_DEF_PARSE_STATUS) &&
-            (tlsCfg = virXMLPropString(node, "tlsFromConfig"))) {
-            if (virStrToLong_i(tlsCfg, NULL, 10, &tlsCfgVal) < 0) {
-                virReportError(VIR_ERR_XML_ERROR,
-                               _("Invalid tlsFromConfig value: %s"),
-                               tlsCfg);
-                goto cleanup;
-            }
-            src->tlsFromConfig = !!tlsCfgVal;
-        }
+        if (VIR_STRDUP(src->path, tmp) < 0)
+            goto cleanup;
 
-        /* for historical reasons the volume name for gluster volume is stored
-         * as a part of the path. This is hard to work with when dealing with
-         * relative names. Split out the volume into a separate variable */
-        if (src->path && src->protocol == VIR_STORAGE_NET_PROTOCOL_GLUSTER) {
-            char *tmp;
-            if (!(tmp = strchr(src->path, '/')) ||
-                tmp == src->path) {
-                virReportError(VIR_ERR_XML_ERROR,
-                               _("missing volume name or file name in "
-                                 "gluster source path '%s'"), src->path);
-                goto cleanup;
-            }
+        tmp[0] = '\0';
+    }
 
-            src->volume = src->path;
+    /* snapshot currently works only for remote disks */
+    src->snapshot = virXPathString("string(./snapshot/@name)", ctxt);
 
-            if (VIR_STRDUP(src->path, tmp) < 0)
-                goto cleanup;
+    /* config file currently only works with remote disks */
+    src->configFile = virXPathString("string(./config/@file)", ctxt);
 
-            tmp[0] = '\0';
-        }
+    if (virDomainStorageNetworkParseHosts(node, &src->hosts, &src->nhosts) < 0)
+        goto cleanup;
 
-        /* snapshot currently works only for remote disks */
-        src->snapshot = virXPathString("string(./snapshot/@name)", ctxt);
+    virStorageSourceNetworkAssignDefaultPorts(src);
 
-        /* config file currently only works with remote disks */
-        src->configFile = virXPathString("string(./config/@file)", ctxt);
+    ret = 0;
 
-        if (virDomainStorageNetworkParseHosts(node, &src->hosts, &src->nhosts) < 0)
-            goto cleanup;
+ cleanup:
+    VIR_FREE(protocol);
+    return ret;
+}
 
-        virStorageSourceNetworkAssignDefaultPorts(src);
 
+int
+virDomainDiskSourceParse(xmlNodePtr node,
+                         xmlXPathContextPtr ctxt,
+                         virStorageSourcePtr src,
+                         unsigned int flags)
+{
+    int ret = -1;
+    xmlNodePtr saveNode = ctxt->node;
+
+    ctxt->node = node;
+
+    switch ((virStorageType)src->type) {
+    case VIR_STORAGE_TYPE_FILE:
+        src->path = virXMLPropString(node, "file");
+        break;
+    case VIR_STORAGE_TYPE_BLOCK:
+        src->path = virXMLPropString(node, "dev");
+        break;
+    case VIR_STORAGE_TYPE_DIR:
+        src->path = virXMLPropString(node, "dir");
+        break;
+    case VIR_STORAGE_TYPE_NETWORK:
+        if (virDomainDiskSourceNetworkParse(node, ctxt, src, flags) < 0)
+            goto cleanup;
         break;
     case VIR_STORAGE_TYPE_VOLUME:
         if (virDomainDiskSourcePoolDefParse(node, &src->srcpool) < 0)
@@ -8229,9 +8247,6 @@ virDomainDiskSourceParse(xmlNodePtr node,
     ret = 0;
 
  cleanup:
-    VIR_FREE(protocol);
-    VIR_FREE(haveTLS);
-    VIR_FREE(tlsCfg);
     ctxt->node = saveNode;
     return ret;
 }