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 34c0b8cea1..da89e94083 100644
--- a/src/conf/domain_conf.h
+++ b/src/conf/domain_conf.h
@@ -3265,6 +3265,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 5e64d23a83..5d57c3cc1a 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -7460,23 +7460,21 @@ virDomainHostdevSubsysPCIDefParseXML(xmlNodePtr node,
}
-static int
+int
virDomainStorageNetworkParseHost(xmlNodePtr hostnode,
- virStorageNetHostDefPtr *hosts,
- size_t *nhosts)
+ virStorageNetHostDefPtr host)
{
int ret = -1;
- virStorageNetHostDef host;
VIR_AUTOFREE(char *) transport = NULL;
VIR_AUTOFREE(char *) port = NULL;
- 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);
@@ -7484,17 +7482,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"),
@@ -7502,26 +7500,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);
return ret;
}
@@ -7536,9 +7532,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 47acb6a009..8632e6631d 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -559,6 +559,7 @@ virDomainStateReasonFromString;
virDomainStateReasonToString;
virDomainStateTypeFromString;
virDomainStateTypeToString;
+virDomainStorageNetworkParseHost;
virDomainStorageSourceFormat;
virDomainStorageSourceParse;
virDomainTaintTypeFromString;
--
2.20.1