[libvirt] [PATCH 3/4] storage: Add ParseSourceString function for use with FindPoolSources.

Cole Robinson crobinso at redhat.com
Fri Oct 16 16:35:13 UTC 2009


This will simplify adding FindPoolSources support to more pool backends in
the future (as well as the test driver).

Signed-off-by: Cole Robinson <crobinso at redhat.com>
---
 src/conf/storage_conf.c          |   52 ++++++++++++++++++++++++++++++++++++++
 src/conf/storage_conf.h          |    4 +++
 src/libvirt_private.syms         |    1 +
 src/storage/storage_backend_fs.c |   34 ++++++------------------
 4 files changed, 66 insertions(+), 25 deletions(-)

diff --git a/src/conf/storage_conf.c b/src/conf/storage_conf.c
index 13056e8..2924a0d 100644
--- a/src/conf/storage_conf.c
+++ b/src/conf/storage_conf.c
@@ -474,6 +474,58 @@ cleanup:
     return ret;
 }
 
+virStoragePoolSourcePtr
+virStoragePoolDefParseSourceString(virConnectPtr conn,
+                                   const char *srcSpec,
+                                   int pool_type)
+{
+    xmlDocPtr doc = NULL;
+    xmlNodePtr node = NULL;
+    xmlXPathContextPtr xpath_ctxt = NULL;
+    virStoragePoolSourcePtr def = NULL, ret = NULL;
+
+    doc = xmlReadDoc((const xmlChar *)srcSpec, "srcSpec.xml", NULL,
+                     XML_PARSE_NOENT | XML_PARSE_NONET |
+                     XML_PARSE_NOERROR | XML_PARSE_NOWARNING);
+
+    if (doc == NULL) {
+        virStorageReportError(conn, VIR_ERR_XML_ERROR,
+                              "%s", _("bad <source> spec"));
+        goto cleanup;
+    }
+
+    xpath_ctxt = xmlXPathNewContext(doc);
+    if (xpath_ctxt == NULL) {
+        virReportOOMError(conn);
+        goto cleanup;
+    }
+
+    if (VIR_ALLOC(def) < 0) {
+        virReportOOMError(conn);
+        goto cleanup;
+    }
+
+    node = virXPathNode(conn, "/source", xpath_ctxt);
+    if (!node) {
+        virStorageReportError(conn, VIR_ERR_XML_ERROR,
+                              "%s", _("root element was not source"));
+        goto cleanup;
+    }
+
+    if (virStoragePoolDefParseSource(conn, xpath_ctxt, def, pool_type,
+                                     node) < 0)
+        goto cleanup;
+
+    ret = def;
+    def = NULL;
+cleanup:
+    if (def)
+        virStoragePoolSourceFree(def);
+    xmlFreeDoc(doc);
+    xmlXPathFreeContext(xpath_ctxt);
+
+    return ret;
+}
 static int
 virStorageDefParsePerms(virConnectPtr conn,
                         xmlXPathContextPtr ctxt,
diff --git a/src/conf/storage_conf.h b/src/conf/storage_conf.h
index 652448a..a795981 100644
--- a/src/conf/storage_conf.h
+++ b/src/conf/storage_conf.h
@@ -376,6 +376,10 @@ void virStoragePoolObjRemove(virStoragePoolObjListPtr pools,
                              virStoragePoolObjPtr pool);
 
 virStoragePoolSourcePtr
+virStoragePoolDefParseSourceString(virConnectPtr conn,
+                                   const char *srcSpec,
+                                   int pool_type);
+virStoragePoolSourcePtr
 virStoragePoolSourceListNewSource(virConnectPtr conn,
                                   virStoragePoolSourceListPtr list);
 char *virStoragePoolSourceListFormat(virConnectPtr conn,
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index ec3c7d9..be1c870 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -376,6 +376,7 @@ virStoragePoolObjListFree;
 virStoragePoolObjRemove;
 virStoragePoolObjSaveDef;
 virStoragePoolSourceFree;
+virStoragePoolDefParseSourceString;
 virStoragePoolSourceListNewSource;
 virStoragePoolSourceListFormat;
 virStorageVolDefFindByKey;
diff --git a/src/storage/storage_backend_fs.c b/src/storage/storage_backend_fs.c
index 2b7f083..16e4bd9 100644
--- a/src/storage/storage_backend_fs.c
+++ b/src/storage/storage_backend_fs.c
@@ -199,8 +199,6 @@ virStorageBackendFileSystemNetFindPoolSources(virConnectPtr conn,
     int vars[] = {
         1
     };
-    xmlDocPtr doc = NULL;
-    xmlXPathContextPtr xpath_ctxt = NULL;
     virNetfsDiscoverState state = {
         .host = NULL,
         .list = {
@@ -210,31 +208,18 @@ virStorageBackendFileSystemNetFindPoolSources(virConnectPtr conn,
         }
     };
     const char *prog[] = { SHOWMOUNT, "--no-headers", "--exports", NULL, NULL };
+    virStoragePoolSourcePtr source = NULL;
     int exitstatus;
     char *retval = NULL;
     unsigned int i;
 
-    doc = xmlReadDoc((const xmlChar *)srcSpec, "srcSpec.xml", NULL,
-                     XML_PARSE_NOENT | XML_PARSE_NONET |
-                     XML_PARSE_NOERROR | XML_PARSE_NOWARNING);
-    if (doc == NULL) {
-        virStorageReportError(conn, VIR_ERR_XML_ERROR, "%s", _("bad <source> spec"));
+    source = virStoragePoolDefParseSourceString(conn, srcSpec,
+                                                VIR_STORAGE_POOL_NETFS);
+    if (!source)
         goto cleanup;
-    }
 
-    xpath_ctxt = xmlXPathNewContext(doc);
-    if (xpath_ctxt == NULL) {
-        virReportOOMError(conn);
-        goto cleanup;
-    }
-
-    state.host = virXPathString(conn, "string(/source/host/@name)", xpath_ctxt);
-    if (!state.host || !state.host[0]) {
-        virStorageReportError(conn, VIR_ERR_XML_ERROR, "%s",
-                              _("missing <host> in <source> spec"));
-        goto cleanup;
-    }
-    prog[3] = state.host;
+    state.host = source->host.name;
+    prog[3] = source->host.name;
 
     if (virStorageBackendRunProgRegex(conn, NULL, prog, 1, regexes, vars,
                                       virStorageBackendFileSystemNetFindPoolSourcesFunc,
@@ -251,11 +236,10 @@ virStorageBackendFileSystemNetFindPoolSources(virConnectPtr conn,
     for (i = 0; i < state.list.nsources; i++)
         virStoragePoolSourceFree(&state.list.sources[i]);
 
-    VIR_FREE(state.list.sources);
-    VIR_FREE(state.host);
+    if (source)
+        virStoragePoolSourceFree(source);
 
-    xmlFreeDoc(doc);
-    xmlXPathFreeContext(xpath_ctxt);
+    VIR_FREE(state.list.sources);
 
     return retval;
 }
-- 
1.6.0.6




More information about the libvir-list mailing list