storage: Add ParseSourceString function for use with FindPoolSources.

This will simplify adding FindPoolSources support to more pool backends in
the future (as well as the test driver).
This commit is contained in:
Cole Robinson 2009-10-15 11:58:35 -04:00
parent 9bcb174473
commit d61fff3f4b
4 changed files with 66 additions and 25 deletions

View File

@ -474,6 +474,58 @@ cleanup:
return ret; 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 static int
virStorageDefParsePerms(virConnectPtr conn, virStorageDefParsePerms(virConnectPtr conn,
xmlXPathContextPtr ctxt, xmlXPathContextPtr ctxt,

View File

@ -376,6 +376,10 @@ void virStoragePoolObjRemove(virStoragePoolObjListPtr pools,
virStoragePoolObjPtr pool); virStoragePoolObjPtr pool);
virStoragePoolSourcePtr virStoragePoolSourcePtr
virStoragePoolDefParseSourceString(virConnectPtr conn,
const char *srcSpec,
int pool_type);
virStoragePoolSourcePtr
virStoragePoolSourceListNewSource(virConnectPtr conn, virStoragePoolSourceListNewSource(virConnectPtr conn,
virStoragePoolSourceListPtr list); virStoragePoolSourceListPtr list);
char *virStoragePoolSourceListFormat(virConnectPtr conn, char *virStoragePoolSourceListFormat(virConnectPtr conn,

View File

@ -380,6 +380,7 @@ virStoragePoolObjListFree;
virStoragePoolObjRemove; virStoragePoolObjRemove;
virStoragePoolObjSaveDef; virStoragePoolObjSaveDef;
virStoragePoolSourceFree; virStoragePoolSourceFree;
virStoragePoolDefParseSourceString;
virStoragePoolSourceListNewSource; virStoragePoolSourceListNewSource;
virStoragePoolSourceListFormat; virStoragePoolSourceListFormat;
virStorageVolDefFindByKey; virStorageVolDefFindByKey;

View File

@ -199,8 +199,6 @@ virStorageBackendFileSystemNetFindPoolSources(virConnectPtr conn,
int vars[] = { int vars[] = {
1 1
}; };
xmlDocPtr doc = NULL;
xmlXPathContextPtr xpath_ctxt = NULL;
virNetfsDiscoverState state = { virNetfsDiscoverState state = {
.host = NULL, .host = NULL,
.list = { .list = {
@ -210,31 +208,18 @@ virStorageBackendFileSystemNetFindPoolSources(virConnectPtr conn,
} }
}; };
const char *prog[] = { SHOWMOUNT, "--no-headers", "--exports", NULL, NULL }; const char *prog[] = { SHOWMOUNT, "--no-headers", "--exports", NULL, NULL };
virStoragePoolSourcePtr source = NULL;
int exitstatus; int exitstatus;
char *retval = NULL; char *retval = NULL;
unsigned int i; unsigned int i;
doc = xmlReadDoc((const xmlChar *)srcSpec, "srcSpec.xml", NULL, source = virStoragePoolDefParseSourceString(conn, srcSpec,
XML_PARSE_NOENT | XML_PARSE_NONET | VIR_STORAGE_POOL_NETFS);
XML_PARSE_NOERROR | XML_PARSE_NOWARNING); if (!source)
if (doc == NULL) {
virStorageReportError(conn, VIR_ERR_XML_ERROR, "%s", _("bad <source> spec"));
goto cleanup; goto cleanup;
}
xpath_ctxt = xmlXPathNewContext(doc); state.host = source->host.name;
if (xpath_ctxt == NULL) { prog[3] = source->host.name;
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;
if (virStorageBackendRunProgRegex(conn, NULL, prog, 1, regexes, vars, if (virStorageBackendRunProgRegex(conn, NULL, prog, 1, regexes, vars,
virStorageBackendFileSystemNetFindPoolSourcesFunc, virStorageBackendFileSystemNetFindPoolSourcesFunc,
@ -251,11 +236,10 @@ virStorageBackendFileSystemNetFindPoolSources(virConnectPtr conn,
for (i = 0; i < state.list.nsources; i++) for (i = 0; i < state.list.nsources; i++)
virStoragePoolSourceFree(&state.list.sources[i]); virStoragePoolSourceFree(&state.list.sources[i]);
VIR_FREE(state.list.sources); if (source)
VIR_FREE(state.host); virStoragePoolSourceFree(source);
xmlFreeDoc(doc); VIR_FREE(state.list.sources);
xmlXPathFreeContext(xpath_ctxt);
return retval; return retval;
} }