To allow testing of the algorithm, split out the extractor into a
separate helper.
---
src/storage/storage_util.c | 93 +++++++++++++++++++++++++++-------------------
src/storage/storage_util.h | 4 ++
2 files changed, 58 insertions(+), 39 deletions(-)
diff --git a/src/storage/storage_util.c b/src/storage/storage_util.c
index 7687eb89a..8459e9d5b 100644
--- a/src/storage/storage_util.c
+++ b/src/storage/storage_util.c
@@ -2836,6 +2836,59 @@ virStorageBackendDeleteLocal(virConnectPtr conn ATTRIBUTE_UNUSED,
}
+int
+virStorageUtilGlusterExtractPoolSources(const char *host,
+ const char *xml,
+ int pooltype,
+ virStoragePoolSourceListPtr list)
+{
+ xmlDocPtr doc = NULL;
+ xmlXPathContextPtr ctxt = NULL;
+ xmlNodePtr *nodes = NULL;
+ virStoragePoolSource *src = NULL;
+ size_t i;
+ int nnodes;
+ int ret = -1;
+
+ if (!(doc = virXMLParseStringCtxt(xml, _("(gluster_cli_output)"),
&ctxt)))
+ goto cleanup;
+
+ if ((nnodes = virXPathNodeSet("//volumes/volume", ctxt, &nodes)) <
0)
+ goto cleanup;
+
+ for (i = 0; i < nnodes; i++) {
+ ctxt->node = nodes[i];
+
+ if (!(src = virStoragePoolSourceListNewSource(list)))
+ goto cleanup;
+
+ if (!(src->dir = virXPathString("string(//name)", ctxt))) {
+ virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+ _("failed to extract gluster volume name"));
+ goto cleanup;
+ }
+
+ if (VIR_ALLOC_N(src->hosts, 1) < 0)
+ goto cleanup;
+ src->nhost = 1;
+
+ if (VIR_STRDUP(src->hosts[0].name, host) < 0)
+ goto cleanup;
+
+ src->format = pooltype;
+ }
+
+ ret = nnodes;
+
+ cleanup:
+ VIR_FREE(nodes);
+ xmlXPathFreeContext(ctxt);
+ xmlFreeDoc(doc);
+
+ return ret;
+}
+
+
/**
* virStorageBackendFindGlusterPoolSources:
* @host: host to detect volumes on
@@ -2856,12 +2909,6 @@ virStorageBackendFindGlusterPoolSources(const char *host,
char *glusterpath = NULL;
char *outbuf = NULL;
virCommandPtr cmd = NULL;
- xmlDocPtr doc = NULL;
- xmlXPathContextPtr ctxt = NULL;
- xmlNodePtr *nodes = NULL;
- virStoragePoolSource *src = NULL;
- size_t i;
- int nnodes;
int rc;
int ret = -1;
@@ -2892,41 +2939,9 @@ virStorageBackendFindGlusterPoolSources(const char *host,
goto cleanup;
}
- if (!(doc = virXMLParseStringCtxt(outbuf, _("(gluster_cli_output)"),
- &ctxt)))
- goto cleanup;
-
- if ((nnodes = virXPathNodeSet("//volumes/volume", ctxt, &nodes)) <
0)
- goto cleanup;
-
- for (i = 0; i < nnodes; i++) {
- ctxt->node = nodes[i];
-
- if (!(src = virStoragePoolSourceListNewSource(list)))
- goto cleanup;
-
- if (!(src->dir = virXPathString("string(//name)", ctxt))) {
- virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
- _("failed to extract gluster volume name"));
- goto cleanup;
- }
-
- if (VIR_ALLOC_N(src->hosts, 1) < 0)
- goto cleanup;
- src->nhost = 1;
-
- if (VIR_STRDUP(src->hosts[0].name, host) < 0)
- goto cleanup;
-
- src->format = pooltype;
- }
-
- ret = nnodes;
+ ret = virStorageUtilGlusterExtractPoolSources(host, outbuf, pooltype, list);
cleanup:
- VIR_FREE(nodes);
- xmlXPathFreeContext(ctxt);
- xmlFreeDoc(doc);
VIR_FREE(outbuf);
virCommandFree(cmd);
VIR_FREE(glusterpath);
diff --git a/src/storage/storage_util.h b/src/storage/storage_util.h
index fa3b6522c..bf8424808 100644
--- a/src/storage/storage_util.h
+++ b/src/storage/storage_util.h
@@ -93,6 +93,10 @@ int virStorageBackendDeleteLocal(virConnectPtr conn,
int virStorageBackendRefreshLocal(virConnectPtr conn,
virStoragePoolObjPtr pool);
+int virStorageUtilGlusterExtractPoolSources(const char *host,
+ const char *xml,
+ int pooltype,
+ virStoragePoolSourceListPtr list);
int virStorageBackendFindGlusterPoolSources(const char *host,
int pooltype,
virStoragePoolSourceListPtr list,
--
2.12.1