An upcoming patch wants to reuse XML parsing of both unix and tcp
network host descriptions in the context of setting up a backup
NBD server. Make that easier by refactoring the existing parser.
Signed-off-by: Eric Blake <eblake(a)redhat.com>
---
src/conf/domain_conf.h | 2 ++
src/conf/domain_conf.c | 43 ++++++++++++++++++++--------------------
src/libvirt_private.syms | 1 +
3 files changed, 25 insertions(+), 21 deletions(-)
diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h
index 2bc3f879f7..5db4396fd5 100644
--- a/src/conf/domain_conf.h
+++ b/src/conf/domain_conf.h
@@ -3170,6 +3170,8 @@ int virDomainDiskInsert(virDomainDefPtr def,
ATTRIBUTE_RETURN_CHECK;
void virDomainDiskInsertPreAlloced(virDomainDefPtr def,
virDomainDiskDefPtr disk);
+int virDomainStorageNetworkParseHost(xmlNodePtr hostnode,
+ virStorageNetHostDefPtr host);
int virDomainDiskDefAssignAddress(virDomainXMLOptionPtr xmlopt,
virDomainDiskDefPtr def,
const virDomainDef *vmdef);
diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index 1fc4c8a35a..0221eb0634 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -7396,23 +7396,21 @@ virDomainHostdevSubsysPCIDefParseXML(xmlNodePtr node,
}
-static int
+int
virDomainStorageNetworkParseHost(xmlNodePtr hostnode,
- virStorageNetHostDefPtr *hosts,
- size_t *nhosts)
+ virStorageNetHostDefPtr host)
{
int ret = -1;
char *transport = NULL;
char *port = NULL;
- virStorageNetHostDef host;
- memset(&host, 0, sizeof(host));
- host.transport = VIR_STORAGE_NET_HOST_TRANS_TCP;
+ memset(host, 0, sizeof(*host));
+ host->transport = VIR_STORAGE_NET_HOST_TRANS_TCP;
/* transport can be tcp (default), unix or rdma. */
if ((transport = virXMLPropString(hostnode, "transport"))) {
- host.transport = virStorageNetHostTransportTypeFromString(transport);
- if (host.transport < 0) {
+ host->transport = virStorageNetHostTransportTypeFromString(transport);
+ if (host->transport < 0) {
virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
_("unknown protocol transport type '%s'"),
transport);
@@ -7420,17 +7418,17 @@ virDomainStorageNetworkParseHost(xmlNodePtr hostnode,
}
}
- host.socket = virXMLPropString(hostnode, "socket");
+ host->socket = virXMLPropString(hostnode, "socket");
- if (host.transport == VIR_STORAGE_NET_HOST_TRANS_UNIX &&
- host.socket == NULL) {
+ if (host->transport == VIR_STORAGE_NET_HOST_TRANS_UNIX &&
+ host->socket == NULL) {
virReportError(VIR_ERR_XML_ERROR, "%s",
_("missing socket for unix transport"));
goto cleanup;
}
- if (host.transport != VIR_STORAGE_NET_HOST_TRANS_UNIX &&
- host.socket != NULL) {
+ if (host->transport != VIR_STORAGE_NET_HOST_TRANS_UNIX &&
+ host->socket != NULL) {
virReportError(VIR_ERR_XML_ERROR,
_("transport '%s' does not support "
"socket attribute"),
@@ -7438,26 +7436,24 @@ virDomainStorageNetworkParseHost(xmlNodePtr hostnode,
goto cleanup;
}
- if (host.transport != VIR_STORAGE_NET_HOST_TRANS_UNIX) {
- if (!(host.name = virXMLPropString(hostnode, "name"))) {
+ if (host->transport != VIR_STORAGE_NET_HOST_TRANS_UNIX) {
+ if (!(host->name = virXMLPropString(hostnode, "name"))) {
virReportError(VIR_ERR_XML_ERROR, "%s",
_("missing name for host"));
goto cleanup;
}
if ((port = virXMLPropString(hostnode, "port"))) {
- if (virStringParsePort(port, &host.port) < 0)
+ if (virStringParsePort(port, &host->port) < 0)
goto cleanup;
}
}
- if (VIR_APPEND_ELEMENT(*hosts, *nhosts, host) < 0)
- goto cleanup;
-
ret = 0;
cleanup:
- virStorageNetHostDefClear(&host);
+ if (ret < 0)
+ virStorageNetHostDefClear(host);
VIR_FREE(transport);
VIR_FREE(port);
return ret;
@@ -7474,9 +7470,14 @@ virDomainStorageNetworkParseHosts(xmlNodePtr node,
for (child = node->children; child; child = child->next) {
if (child->type == XML_ELEMENT_NODE &&
virXMLNodeNameEqual(child, "host")) {
+ virStorageNetHostDef host;
- if (virDomainStorageNetworkParseHost(child, hosts, nhosts) < 0)
+ if (virDomainStorageNetworkParseHost(child, &host) < 0)
return -1;
+ if (VIR_APPEND_ELEMENT(*hosts, *nhosts, host) < 0) {
+ virStorageNetHostDefClear(&host);
+ return -1;
+ }
}
}
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 6b401aa5e0..28a2ef707e 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -555,6 +555,7 @@ virDomainStateReasonFromString;
virDomainStateReasonToString;
virDomainStateTypeFromString;
virDomainStateTypeToString;
+virDomainStorageNetworkParseHost;
virDomainStorageSourceFormat;
virDomainStorageSourceParse;
virDomainTaintTypeFromString;
--
2.20.1