提交 b327df87 编写于 作者: P Peter Krempa

util: storagefile: Split out parsing of NBD string into a separate func

Split out the code so that the function looks homogenous after adding
more protocol specific parsers.
上级 5604c056
...@@ -2354,77 +2354,109 @@ virStorageSourceParseRBDColonString(const char *rbdstr, ...@@ -2354,77 +2354,109 @@ virStorageSourceParseRBDColonString(const char *rbdstr,
static int static int
virStorageSourceParseBackingColon(virStorageSourcePtr src, virStorageSourceParseNBDColonString(const char *nbdstr,
const char *path) virStorageSourcePtr src)
{ {
char **backing = NULL; char **backing = NULL;
int ret = -1; int ret = -1;
if (!(backing = virStringSplit(path, ":", 0))) if (!(backing = virStringSplit(nbdstr, ":", 0)))
goto cleanup; goto cleanup;
if (!backing[0] || /* we know that backing[0] now equals to "nbd" */
(src->protocol = virStorageNetProtocolTypeFromString(backing[0])) < 0) {
if (VIR_ALLOC_N(src->hosts, 1) < 0)
goto cleanup;
src->nhosts = 1;
src->hosts->transport = VIR_STORAGE_NET_HOST_TRANS_TCP;
/* format: [] denotes optional sections, uppercase are variable strings
* nbd:unix:/PATH/TO/SOCKET[:exportname=EXPORTNAME]
* nbd:HOSTNAME:PORT[:exportname=EXPORTNAME]
*/
if (!backing[1]) {
virReportError(VIR_ERR_INTERNAL_ERROR, virReportError(VIR_ERR_INTERNAL_ERROR,
_("invalid backing protocol '%s'"), _("missing remote information in '%s' for protocol nbd"),
NULLSTR(backing[0])); nbdstr);
goto cleanup; goto cleanup;
} } else if (STREQ(backing[1], "unix")) {
if (!backing[2]) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("missing unix socket path in nbd backing string %s"),
nbdstr);
goto cleanup;
}
switch ((virStorageNetProtocol) src->protocol) { if (VIR_STRDUP(src->hosts->socket, backing[2]) < 0)
case VIR_STORAGE_NET_PROTOCOL_NBD:
if (VIR_ALLOC_N(src->hosts, 1) < 0)
goto cleanup; goto cleanup;
src->nhosts = 1;
src->hosts->transport = VIR_STORAGE_NET_HOST_TRANS_TCP;
/* format: [] denotes optional sections, uppercase are variable strings } else {
* nbd:unix:/PATH/TO/SOCKET[:exportname=EXPORTNAME]
* nbd:HOSTNAME:PORT[:exportname=EXPORTNAME]
*/
if (!backing[1]) { if (!backing[1]) {
virReportError(VIR_ERR_INTERNAL_ERROR, virReportError(VIR_ERR_INTERNAL_ERROR,
_("missing remote information in '%s' for protocol nbd"), _("missing host name in nbd string '%s'"),
path); nbdstr);
goto cleanup; goto cleanup;
} else if (STREQ(backing[1], "unix")) { }
if (!backing[2]) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("missing unix socket path in nbd backing string %s"),
path);
goto cleanup;
}
if (VIR_STRDUP(src->hosts->socket, backing[2]) < 0) if (VIR_STRDUP(src->hosts->name, backing[1]) < 0)
goto cleanup; goto cleanup;
} else { if (!backing[2]) {
if (!backing[1]) { virReportError(VIR_ERR_INTERNAL_ERROR,
virReportError(VIR_ERR_INTERNAL_ERROR, _("missing port in nbd string '%s'"),
_("missing host name in nbd string '%s'"), nbdstr);
path); goto cleanup;
goto cleanup; }
}
if (VIR_STRDUP(src->hosts->name, backing[1]) < 0) if (VIR_STRDUP(src->hosts->port, backing[2]) < 0)
goto cleanup; goto cleanup;
}
if (!backing[2]) { if (backing[3] && STRPREFIX(backing[3], "exportname=")) {
virReportError(VIR_ERR_INTERNAL_ERROR, if (VIR_STRDUP(src->path, backing[3] + strlen("exportname=")) < 0)
_("missing port in nbd string '%s'"), goto cleanup;
path); }
goto cleanup;
}
if (VIR_STRDUP(src->hosts->port, backing[2]) < 0) ret = 0;
goto cleanup;
}
if (backing[3] && STRPREFIX(backing[3], "exportname=")) { cleanup:
if (VIR_STRDUP(src->path, backing[3] + strlen("exportname=")) < 0) virStringFreeList(backing);
goto cleanup;
} return ret;
break; }
static int
virStorageSourceParseBackingColon(virStorageSourcePtr src,
const char *path)
{
char *protocol = NULL;
const char *p;
int ret = -1;
if (!(p = strchr(path, ':'))) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("invalid backing protocol string '%s'"),
path);
goto cleanup;
}
if (VIR_STRNDUP(protocol, path, p - path) < 0)
goto cleanup;
if ((src->protocol = virStorageNetProtocolTypeFromString(protocol)) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("invalid backing protocol '%s'"),
protocol);
goto cleanup;
}
switch ((virStorageNetProtocol) src->protocol) {
case VIR_STORAGE_NET_PROTOCOL_NBD:
if (virStorageSourceParseNBDColonString(path, src) < 0)
goto cleanup;
break;
case VIR_STORAGE_NET_PROTOCOL_SHEEPDOG: case VIR_STORAGE_NET_PROTOCOL_SHEEPDOG:
case VIR_STORAGE_NET_PROTOCOL_RBD: case VIR_STORAGE_NET_PROTOCOL_RBD:
...@@ -2432,7 +2464,7 @@ virStorageSourceParseBackingColon(virStorageSourcePtr src, ...@@ -2432,7 +2464,7 @@ virStorageSourceParseBackingColon(virStorageSourcePtr src,
case VIR_STORAGE_NET_PROTOCOL_NONE: case VIR_STORAGE_NET_PROTOCOL_NONE:
virReportError(VIR_ERR_INTERNAL_ERROR, virReportError(VIR_ERR_INTERNAL_ERROR,
_("backing store parser is not implemented for protocol %s"), _("backing store parser is not implemented for protocol %s"),
backing[0]); protocol);
goto cleanup; goto cleanup;
case VIR_STORAGE_NET_PROTOCOL_HTTP: case VIR_STORAGE_NET_PROTOCOL_HTTP:
...@@ -2444,16 +2476,15 @@ virStorageSourceParseBackingColon(virStorageSourcePtr src, ...@@ -2444,16 +2476,15 @@ virStorageSourceParseBackingColon(virStorageSourcePtr src,
case VIR_STORAGE_NET_PROTOCOL_GLUSTER: case VIR_STORAGE_NET_PROTOCOL_GLUSTER:
virReportError(VIR_ERR_INTERNAL_ERROR, virReportError(VIR_ERR_INTERNAL_ERROR,
_("malformed backing store path for protocol %s"), _("malformed backing store path for protocol %s"),
backing[0]); protocol);
goto cleanup; goto cleanup;
} }
ret = 0; ret = 0;
cleanup: cleanup:
virStringFreeList(backing); VIR_FREE(protocol);
return ret; return ret;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册