Rather than have lots of ugly inline code create helpers to try and
make things more readable. While creating the helpers realign the code
as necessary.

Signed-off-by: John Ferlan <jfer...@redhat.com>
---
 src/conf/storage_adapter_conf.c | 194 +++++++++++++++++++++++-----------------
 1 file changed, 114 insertions(+), 80 deletions(-)

diff --git a/src/conf/storage_adapter_conf.c b/src/conf/storage_adapter_conf.c
index 4f5b665..a2d4a3a 100644
--- a/src/conf/storage_adapter_conf.c
+++ b/src/conf/storage_adapter_conf.c
@@ -37,16 +37,23 @@ VIR_ENUM_IMPL(virStoragePoolSourceAdapter,
               "default", "scsi_host", "fc_host")
 
 
+static void
+virStorageAdapterFCHostClear(virStoragePoolSourceAdapterPtr adapter)
+{
+    VIR_FREE(adapter->data.fchost.wwnn);
+    VIR_FREE(adapter->data.fchost.wwpn);
+    VIR_FREE(adapter->data.fchost.parent);
+    VIR_FREE(adapter->data.fchost.parent_wwnn);
+    VIR_FREE(adapter->data.fchost.parent_wwpn);
+    VIR_FREE(adapter->data.fchost.parent_fabric_wwn);
+}
+
+
 void
 virStorageAdapterClear(virStoragePoolSourceAdapterPtr adapter)
 {
     if (adapter->type == VIR_STORAGE_POOL_SOURCE_ADAPTER_TYPE_FC_HOST) {
-        VIR_FREE(adapter->data.fchost.wwnn);
-        VIR_FREE(adapter->data.fchost.wwpn);
-        VIR_FREE(adapter->data.fchost.parent);
-        VIR_FREE(adapter->data.fchost.parent_wwnn);
-        VIR_FREE(adapter->data.fchost.parent_wwpn);
-        VIR_FREE(adapter->data.fchost.parent_fabric_wwn);
+        virStorageAdapterFCHostClear(adapter);
     } else if (adapter->type ==
                VIR_STORAGE_POOL_SOURCE_ADAPTER_TYPE_SCSI_HOST) {
         VIR_FREE(adapter->data.scsi_host.name);
@@ -54,6 +61,40 @@ virStorageAdapterClear(virStoragePoolSourceAdapterPtr 
adapter)
 }
 
 
+static int
+virStorageAdapterFCHostParseXML(xmlNodePtr node,
+                                virStoragePoolSourcePtr source)
+{
+    char *managed = NULL;
+
+    source->adapter.data.fchost.parent = virXMLPropString(node, "parent");
+    if ((managed = virXMLPropString(node, "managed"))) {
+        source->adapter.data.fchost.managed =
+            virTristateBoolTypeFromString(managed);
+        if (source->adapter.data.fchost.managed < 0) {
+            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
+                           _("unknown fc_host managed setting '%s'"),
+                           managed);
+            VIR_FREE(managed);
+            return -1;
+        }
+    }
+
+    source->adapter.data.fchost.parent_wwnn =
+        virXMLPropString(node, "parent_wwnn");
+    source->adapter.data.fchost.parent_wwpn =
+        virXMLPropString(node, "parent_wwpn");
+    source->adapter.data.fchost.parent_fabric_wwn =
+        virXMLPropString(node, "parent_fabric_wwn");
+
+    source->adapter.data.fchost.wwpn = virXMLPropString(node, "wwpn");
+    source->adapter.data.fchost.wwnn = virXMLPropString(node, "wwnn");
+
+    VIR_FREE(managed);
+    return 0;
+}
+
+
 int
 virStorageAdapterParseXML(virStoragePoolSourcePtr source,
                           xmlNodePtr node,
@@ -62,7 +103,6 @@ virStorageAdapterParseXML(virStoragePoolSourcePtr source,
     int ret = -1;
     xmlNodePtr relnode = ctxt->node;
     char *adapter_type = NULL;
-    char *managed = NULL;
 
     ctxt->node = node;
 
@@ -77,29 +117,8 @@ virStorageAdapterParseXML(virStoragePoolSourcePtr source,
 
         if (source->adapter.type ==
             VIR_STORAGE_POOL_SOURCE_ADAPTER_TYPE_FC_HOST) {
-            source->adapter.data.fchost.parent =
-                virXMLPropString(node, "parent");
-            managed = virXMLPropString(node, "managed");
-            if (managed) {
-                source->adapter.data.fchost.managed =
-                    virTristateBoolTypeFromString(managed);
-                if (source->adapter.data.fchost.managed < 0) {
-                    virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
-                                   _("unknown fc_host managed setting '%s'"),
-                                   managed);
-                    goto cleanup;
-                }
-            }
-
-            source->adapter.data.fchost.parent_wwnn =
-                virXMLPropString(node, "parent_wwnn");
-            source->adapter.data.fchost.parent_wwpn =
-                virXMLPropString(node, "parent_wwpn");
-            source->adapter.data.fchost.parent_fabric_wwn =
-                virXMLPropString(node, "parent_fabric_wwn");
-
-            source->adapter.data.fchost.wwpn = virXMLPropString(node, "wwpn");
-            source->adapter.data.fchost.wwnn = virXMLPropString(node, "wwnn");
+            if (virStorageAdapterFCHostParseXML(node, source) < 0)
+                goto cleanup;
         } else if (source->adapter.type ==
                    VIR_STORAGE_POOL_SOURCE_ADAPTER_TYPE_SCSI_HOST) {
 
@@ -171,56 +190,63 @@ virStorageAdapterParseXML(virStoragePoolSourcePtr source,
  cleanup:
     ctxt->node = relnode;
     VIR_FREE(adapter_type);
-    VIR_FREE(managed);
     return ret;
 }
 
 
-int
-virStorageAdapterParseValidate(virStoragePoolDefPtr ret)
+static int
+virStorageAdapterFCHostParseValidate(virStoragePoolDefPtr ret)
 {
-    if (!ret->source.adapter.type) {
+    if (!ret->source.adapter.data.fchost.wwnn ||
+        !ret->source.adapter.data.fchost.wwpn) {
         virReportError(VIR_ERR_XML_ERROR, "%s",
-                       _("missing storage pool source adapter"));
+                       _("'wwnn' and 'wwpn' must be specified for adapter "
+                         "type 'fchost'"));
         return -1;
     }
 
-    if (ret->source.adapter.type ==
-        VIR_STORAGE_POOL_SOURCE_ADAPTER_TYPE_FC_HOST) {
-        if (!ret->source.adapter.data.fchost.wwnn ||
-            !ret->source.adapter.data.fchost.wwpn) {
-            virReportError(VIR_ERR_XML_ERROR, "%s",
-                           _("'wwnn' and 'wwpn' must be specified for adapter "
-                             "type 'fchost'"));
-            return -1;
-        }
+    if (!virValidateWWN(ret->source.adapter.data.fchost.wwnn) ||
+        !virValidateWWN(ret->source.adapter.data.fchost.wwpn))
+        return -1;
 
-        if (!virValidateWWN(ret->source.adapter.data.fchost.wwnn) ||
-            !virValidateWWN(ret->source.adapter.data.fchost.wwpn))
-            return -1;
+    if ((ret->source.adapter.data.fchost.parent_wwnn &&
+         !ret->source.adapter.data.fchost.parent_wwpn) ||
+        (!ret->source.adapter.data.fchost.parent_wwnn &&
+         ret->source.adapter.data.fchost.parent_wwpn)) {
+        virReportError(VIR_ERR_XML_ERROR, "%s",
+                       _("must supply both parent_wwnn and "
+                         "parent_wwpn not just one or the other"));
+        return -1;
+    }
 
-        if ((ret->source.adapter.data.fchost.parent_wwnn &&
-             !ret->source.adapter.data.fchost.parent_wwpn) ||
-            (!ret->source.adapter.data.fchost.parent_wwnn &&
-             ret->source.adapter.data.fchost.parent_wwpn)) {
-            virReportError(VIR_ERR_XML_ERROR, "%s",
-                           _("must supply both parent_wwnn and "
-                             "parent_wwpn not just one or the other"));
-                return -1;
-        }
+    if (ret->source.adapter.data.fchost.parent_wwnn &&
+        !virValidateWWN(ret->source.adapter.data.fchost.parent_wwnn))
+        return -1;
 
-        if (ret->source.adapter.data.fchost.parent_wwnn &&
-            !virValidateWWN(ret->source.adapter.data.fchost.parent_wwnn))
-            return -1;
+    if (ret->source.adapter.data.fchost.parent_wwpn &&
+        !virValidateWWN(ret->source.adapter.data.fchost.parent_wwpn))
+        return -1;
 
-        if (ret->source.adapter.data.fchost.parent_wwpn &&
-            !virValidateWWN(ret->source.adapter.data.fchost.parent_wwpn))
-            return -1;
+    if (ret->source.adapter.data.fchost.parent_fabric_wwn &&
+        !virValidateWWN(ret->source.adapter.data.fchost.parent_fabric_wwn))
+        return -1;
 
-        if (ret->source.adapter.data.fchost.parent_fabric_wwn &&
-            !virValidateWWN(ret->source.adapter.data.fchost.parent_fabric_wwn))
-            return -1;
+    return 0;
+}
 
+
+int
+virStorageAdapterParseValidate(virStoragePoolDefPtr ret)
+{
+    if (!ret->source.adapter.type) {
+        virReportError(VIR_ERR_XML_ERROR, "%s",
+                       _("missing storage pool source adapter"));
+        return -1;
+    }
+
+    if (ret->source.adapter.type ==
+        VIR_STORAGE_POOL_SOURCE_ADAPTER_TYPE_FC_HOST) {
+        return virStorageAdapterFCHostParseValidate(ret);
     } else if (ret->source.adapter.type ==
                VIR_STORAGE_POOL_SOURCE_ADAPTER_TYPE_SCSI_HOST) {
         if (!ret->source.adapter.data.scsi_host.name &&
@@ -244,6 +270,28 @@ virStorageAdapterParseValidate(virStoragePoolDefPtr ret)
 }
 
 
+static void
+virStorageAdapterFCHostFormat(virBufferPtr buf,
+                              virStoragePoolSourcePtr src)
+{
+    virBufferEscapeString(buf, " parent='%s'",
+                          src->adapter.data.fchost.parent);
+    if (src->adapter.data.fchost.managed)
+        virBufferAsprintf(buf, " managed='%s'",
+                          
virTristateBoolTypeToString(src->adapter.data.fchost.managed));
+    virBufferEscapeString(buf, " parent_wwnn='%s'",
+                          src->adapter.data.fchost.parent_wwnn);
+    virBufferEscapeString(buf, " parent_wwpn='%s'",
+                          src->adapter.data.fchost.parent_wwpn);
+    virBufferEscapeString(buf, " parent_fabric_wwn='%s'",
+                          src->adapter.data.fchost.parent_fabric_wwn);
+
+    virBufferAsprintf(buf, " wwnn='%s' wwpn='%s'/>\n",
+                      src->adapter.data.fchost.wwnn,
+                      src->adapter.data.fchost.wwpn);
+}
+
+
 void
 virStorageAdapterFormat(virBufferPtr buf,
                         virStoragePoolSourcePtr src)
@@ -252,21 +300,7 @@ virStorageAdapterFormat(virBufferPtr buf,
                       
virStoragePoolSourceAdapterTypeToString(src->adapter.type));
 
     if (src->adapter.type == VIR_STORAGE_POOL_SOURCE_ADAPTER_TYPE_FC_HOST) {
-        virBufferEscapeString(buf, " parent='%s'",
-                              src->adapter.data.fchost.parent);
-        if (src->adapter.data.fchost.managed)
-            virBufferAsprintf(buf, " managed='%s'",
-                              
virTristateBoolTypeToString(src->adapter.data.fchost.managed));
-        virBufferEscapeString(buf, " parent_wwnn='%s'",
-                              src->adapter.data.fchost.parent_wwnn);
-        virBufferEscapeString(buf, " parent_wwpn='%s'",
-                              src->adapter.data.fchost.parent_wwpn);
-        virBufferEscapeString(buf, " parent_fabric_wwn='%s'",
-                              src->adapter.data.fchost.parent_fabric_wwn);
-
-        virBufferAsprintf(buf, " wwnn='%s' wwpn='%s'/>\n",
-                          src->adapter.data.fchost.wwnn,
-                          src->adapter.data.fchost.wwpn);
+        virStorageAdapterFCHostFormat(buf, src);
     } else if (src->adapter.type ==
                VIR_STORAGE_POOL_SOURCE_ADAPTER_TYPE_SCSI_HOST) {
         if (src->adapter.data.scsi_host.name) {
-- 
2.9.3

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list

Reply via email to