doRemoteOpen: Refactor control flow

Use a temporary variable 'newconn' to hold the newly opened connection
until we are ready to pass it back instead of the original connection.

This way we can avoid complicated 'error'/'cleanup' sections.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Pavel Hrdina <phrdina@redhat.com>
This commit is contained in:
Peter Krempa 2022-05-18 11:18:54 +02:00
parent 7f50557c08
commit f7c422993e

View File

@ -1794,7 +1794,7 @@ remoteOpenConn(const char *uri,
virConnectPtr *conn)
{
g_autoptr(virTypedParamList) identparams = NULL;
int ret = -1;
g_autoptr(virConnect) newconn = NULL;
VIR_DEBUG("Getting secondary uri=%s readonly=%d preserveIdent=%d conn=%p",
NULLSTR(uri), readonly, preserveIdentity, conn);
@ -1814,34 +1814,30 @@ remoteOpenConn(const char *uri,
return -1;
if (!(identparams = virIdentityGetParameters(ident)))
goto error;
return -1;
}
VIR_DEBUG("Opening driver %s", uri);
if (readonly)
*conn = virConnectOpenReadOnly(uri);
newconn = virConnectOpenReadOnly(uri);
else
*conn = virConnectOpen(uri);
if (!*conn)
goto error;
VIR_DEBUG("Opened driver %p", *conn);
newconn = virConnectOpen(uri);
if (!newconn)
return -1;
VIR_DEBUG("Opened driver %p", newconn);
if (preserveIdentity) {
if (virConnectSetIdentity(*conn, identparams->par, identparams->npar, 0) < 0)
goto error;
return -1;
VIR_DEBUG("Forwarded current identity to secondary driver");
}
ret = 0;
cleanup:
return ret;
*conn = g_steal_pointer(&newconn);
error:
if (*conn) {
g_clear_pointer(conn, virConnectClose);
}
goto cleanup;
return 0;
}