render / rpms / libvirt

Forked from rpms/libvirt 9 months ago
Clone
c401cc
From 7650460024a828774440d45e8497b21350cb206d Mon Sep 17 00:00:00 2001
c401cc
Message-Id: <7650460024a828774440d45e8497b21350cb206d@dist-git>
c401cc
From: Peter Krempa <pkrempa@redhat.com>
c401cc
Date: Wed, 26 Feb 2014 14:54:13 +0100
c401cc
Subject: [PATCH] conf: Refactor virDomainDiskSourceDefParse
c401cc
c401cc
https://bugzilla.redhat.com/show_bug.cgi?id=1032370
c401cc
c401cc
Now that the function is separate clean out a few ugly places and fix up
c401cc
error messages.
c401cc
c401cc
(cherry picked from commit c2986ff0d3da47f26e356fd12310fc032be1c7a5)
c401cc
c401cc
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
c401cc
---
c401cc
 src/conf/domain_conf.c | 119 +++++++++++++++++++++++--------------------------
c401cc
 1 file changed, 56 insertions(+), 63 deletions(-)
c401cc
c401cc
diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
c401cc
index eef2176..409fcda 100644
c401cc
--- a/src/conf/domain_conf.c
c401cc
+++ b/src/conf/domain_conf.c
c401cc
@@ -4837,17 +4837,18 @@ virDomainDiskSourceDefParse(xmlNodePtr node,
c401cc
                             int type,
c401cc
                             char **source,
c401cc
                             int *proto,
c401cc
-                            size_t *ndefhosts,
c401cc
-                            virDomainDiskHostDefPtr *defhosts,
c401cc
+                            size_t *nhosts,
c401cc
+                            virDomainDiskHostDefPtr *hosts,
c401cc
                             virDomainDiskSourcePoolDefPtr *srcpool)
c401cc
 {
c401cc
     char *protocol = NULL;
c401cc
     char *transport = NULL;
c401cc
-    virDomainDiskHostDefPtr hosts = NULL;
c401cc
-    int nhosts = 0;
c401cc
+    virDomainDiskHostDef host;
c401cc
     xmlNodePtr child;
c401cc
     int ret = -1;
c401cc
 
c401cc
+    memset(&host, 0, sizeof(host));
c401cc
+
c401cc
     switch (type) {
c401cc
     case VIR_DOMAIN_DISK_TYPE_FILE:
c401cc
         *source = virXMLPropString(node, "file");
c401cc
@@ -4859,110 +4860,102 @@ virDomainDiskSourceDefParse(xmlNodePtr node,
c401cc
         *source = virXMLPropString(node, "dir");
c401cc
         break;
c401cc
     case VIR_DOMAIN_DISK_TYPE_NETWORK:
c401cc
-        protocol = virXMLPropString(node, "protocol");
c401cc
-        if (protocol == NULL) {
c401cc
-            virReportError(VIR_ERR_INTERNAL_ERROR,
c401cc
-                           "%s", _("missing protocol type"));
c401cc
-            goto error;
c401cc
+        if (!(protocol = virXMLPropString(node, "protocol"))) {
c401cc
+            virReportError(VIR_ERR_XML_ERROR, "%s",
c401cc
+                           _("missing network source protocol type"));
c401cc
+            goto cleanup;
c401cc
         }
c401cc
-        *proto = virDomainDiskProtocolTypeFromString(protocol);
c401cc
-        if (*proto < 0) {
c401cc
-            virReportError(VIR_ERR_INTERNAL_ERROR,
c401cc
-                           _("unknown protocol type '%s'"),
c401cc
-                           protocol);
c401cc
-            goto error;
c401cc
+
c401cc
+        if ((*proto = virDomainDiskProtocolTypeFromString(protocol)) < 0){
c401cc
+            virReportError(VIR_ERR_XML_ERROR,
c401cc
+                           _("unknown protocol type '%s'"), protocol);
c401cc
+            goto cleanup;
c401cc
         }
c401cc
 
c401cc
         if (!(*source = virXMLPropString(node, "name")) &&
c401cc
             *proto != VIR_DOMAIN_DISK_PROTOCOL_NBD) {
c401cc
-            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
c401cc
+            virReportError(VIR_ERR_XML_ERROR, "%s",
c401cc
                            _("missing name for disk source"));
c401cc
-            goto error;
c401cc
+            goto cleanup;
c401cc
         }
c401cc
+
c401cc
         child = node->children;
c401cc
         while (child != NULL) {
c401cc
             if (child->type == XML_ELEMENT_NODE &&
c401cc
                 xmlStrEqual(child->name, BAD_CAST "host")) {
c401cc
-                if (VIR_REALLOC_N(hosts, nhosts + 1) < 0)
c401cc
-                    goto error;
c401cc
-                hosts[nhosts].name = NULL;
c401cc
-                hosts[nhosts].port = NULL;
c401cc
-                hosts[nhosts].transport = VIR_DOMAIN_DISK_PROTO_TRANS_TCP;
c401cc
-                hosts[nhosts].socket = NULL;
c401cc
-                nhosts++;
c401cc
+
c401cc
+                host.transport = VIR_DOMAIN_DISK_PROTO_TRANS_TCP;
c401cc
 
c401cc
                 /* transport can be tcp (default), unix or rdma.  */
c401cc
-                transport = virXMLPropString(child, "transport");
c401cc
-                if (transport != NULL) {
c401cc
-                    hosts[nhosts - 1].transport = virDomainDiskProtocolTransportTypeFromString(transport);
c401cc
-                    if (hosts[nhosts - 1].transport < 0) {
c401cc
+                if ((transport = virXMLPropString(child, "transport"))) {
c401cc
+                    host.transport = virDomainDiskProtocolTransportTypeFromString(transport);
c401cc
+                    if (host.transport < 0) {
c401cc
                         virReportError(VIR_ERR_XML_ERROR,
c401cc
                                        _("unknown protocol transport type '%s'"),
c401cc
                                        transport);
c401cc
-                        goto error;
c401cc
+                        goto cleanup;
c401cc
                     }
c401cc
                 }
c401cc
-                hosts[nhosts - 1].socket = virXMLPropString(child, "socket");
c401cc
-                if (hosts[nhosts - 1].transport == VIR_DOMAIN_DISK_PROTO_TRANS_UNIX &&
c401cc
-                    hosts[nhosts - 1].socket == NULL) {
c401cc
-                    virReportError(VIR_ERR_XML_ERROR,
c401cc
-                                   "%s", _("missing socket for unix transport"));
c401cc
-                    goto error;
c401cc
+
c401cc
+                host.socket = virXMLPropString(child, "socket");
c401cc
+
c401cc
+                if (host.transport == VIR_DOMAIN_DISK_PROTO_TRANS_UNIX &&
c401cc
+                    host.socket == NULL) {
c401cc
+                    virReportError(VIR_ERR_XML_ERROR, "%s",
c401cc
+                                   _("missing socket for unix transport"));
c401cc
+                    goto cleanup;
c401cc
                 }
c401cc
-                if (hosts[nhosts - 1].transport != VIR_DOMAIN_DISK_PROTO_TRANS_UNIX &&
c401cc
-                    hosts[nhosts - 1].socket != NULL) {
c401cc
+
c401cc
+                if (host.transport != VIR_DOMAIN_DISK_PROTO_TRANS_UNIX &&
c401cc
+                    host.socket != NULL) {
c401cc
                     virReportError(VIR_ERR_XML_ERROR,
c401cc
-                                   _("transport %s does not support socket attribute"),
c401cc
+                                   _("transport '%s' does not support "
c401cc
+                                     "socket attribute"),
c401cc
                                    transport);
c401cc
-                    goto error;
c401cc
+                    goto cleanup;
c401cc
                 }
c401cc
+
c401cc
                 VIR_FREE(transport);
c401cc
-                if (hosts[nhosts - 1].transport != VIR_DOMAIN_DISK_PROTO_TRANS_UNIX) {
c401cc
-                    hosts[nhosts - 1].name = virXMLPropString(child, "name");
c401cc
-                    if (!hosts[nhosts - 1].name) {
c401cc
-                        virReportError(VIR_ERR_XML_ERROR,
c401cc
-                                       "%s", _("missing name for host"));
c401cc
-                        goto error;
c401cc
+
c401cc
+                if (host.transport != VIR_DOMAIN_DISK_PROTO_TRANS_UNIX) {
c401cc
+                    if (!(host.name = virXMLPropString(child, "name"))) {
c401cc
+                        virReportError(VIR_ERR_XML_ERROR, "%s",
c401cc
+                                       _("missing name for host"));
c401cc
+                        goto cleanup;
c401cc
                     }
c401cc
-                    hosts[nhosts - 1].port = virXMLPropString(child, "port");
c401cc
+
c401cc
+                    host.port = virXMLPropString(child, "port");
c401cc
                 }
c401cc
+
c401cc
+                if (VIR_APPEND_ELEMENT(*hosts, *nhosts, host) < 0)
c401cc
+                    goto cleanup;
c401cc
             }
c401cc
             child = child->next;
c401cc
         }
c401cc
         break;
c401cc
     case VIR_DOMAIN_DISK_TYPE_VOLUME:
c401cc
         if (virDomainDiskSourcePoolDefParse(node, srcpool) < 0)
c401cc
-            goto error;
c401cc
+            goto cleanup;
c401cc
         break;
c401cc
     default:
c401cc
         virReportError(VIR_ERR_INTERNAL_ERROR,
c401cc
                        _("unexpected disk type %s"),
c401cc
                        virDomainDiskTypeToString(type));
c401cc
-        goto error;
c401cc
+        goto cleanup;
c401cc
     }
c401cc
 
c401cc
-    /* People sometimes pass a bogus '' source path
c401cc
-       when they mean to omit the source element
c401cc
-       completely (e.g. CDROM without media). This is
c401cc
-       just a little compatibility check to help
c401cc
-       those broken apps */
c401cc
+    /* People sometimes pass a bogus '' source path when they mean to omit the
c401cc
+     * source element completely (e.g. CDROM without media). This is just a
c401cc
+     * little compatibility check to help those broken apps */
c401cc
     if (*source && STREQ(*source, ""))
c401cc
         VIR_FREE(*source);
c401cc
 
c401cc
-    *ndefhosts = nhosts;
c401cc
-    *defhosts = hosts;
c401cc
-    nhosts = 0;
c401cc
-
c401cc
     ret = 0;
c401cc
 
c401cc
-error:
c401cc
+cleanup:
c401cc
+    virDomainDiskHostDefClear(&host);
c401cc
     VIR_FREE(protocol);
c401cc
     VIR_FREE(transport);
c401cc
-    while (nhosts > 0) {
c401cc
-        virDomainDiskHostDefClear(&hosts[nhosts - 1]);
c401cc
-        nhosts--;
c401cc
-    }
c401cc
-
c401cc
     return ret;
c401cc
 }
c401cc
 
c401cc
-- 
c401cc
1.9.0
c401cc