Similarly to qemu_driver.c, we can join often repeating code of looking up network into one function: networkObjFromNetwork. Signed-off-by: Michal Privoznik <mprivozn@xxxxxxxxxx> --- src/network/bridge_driver.c | 76 ++++++++++++++++++--------------------------- 1 file changed, 31 insertions(+), 45 deletions(-) diff --git a/src/network/bridge_driver.c b/src/network/bridge_driver.c index 62ae0b7..3a8be90 100644 --- a/src/network/bridge_driver.c +++ b/src/network/bridge_driver.c @@ -112,6 +112,27 @@ static int networkUnplugBandwidth(virNetworkObjPtr net, static virNetworkDriverStatePtr driverState = NULL; +static virNetworkObjPtr +networkObjFromNetwork(virNetworkPtr net) +{ + virNetworkDriverStatePtr driver = net->conn->networkPrivateData; + virNetworkObjPtr network; + char uuidstr[VIR_UUID_STRING_BUFLEN]; + + networkDriverLock(driver); + network = virNetworkFindByUUID(&driver->networks, net->uuid); + networkDriverUnlock(driver); + + if (!network) { + virUUIDFormat(net->uuid, uuidstr); + virReportError(VIR_ERR_NO_NETWORK, + _("no network with matching uuid '%s' (%s)"), + uuidstr, net->name); + } + + return network; +} + static char * networkDnsmasqLeaseFileNameDefault(const char *netname) { @@ -2260,20 +2281,14 @@ cleanup: static int networkIsActive(virNetworkPtr net) { - virNetworkDriverStatePtr driver = net->conn->networkPrivateData; virNetworkObjPtr obj; int ret = -1; - networkDriverLock(driver); - obj = virNetworkFindByUUID(&driver->networks, net->uuid); - networkDriverUnlock(driver); - if (!obj) { - virReportError(VIR_ERR_NO_NETWORK, NULL); - goto cleanup; - } + if (!(obj = networkObjFromNetwork(net))) + return ret; if (virNetworkIsActiveEnsureACL(net->conn, obj->def) < 0) goto cleanup; ret = virNetworkObjIsActive(obj); @@ -2285,20 +2300,14 @@ cleanup: static int networkIsPersistent(virNetworkPtr net) { - virNetworkDriverStatePtr driver = net->conn->networkPrivateData; virNetworkObjPtr obj; int ret = -1; - networkDriverLock(driver); - obj = virNetworkFindByUUID(&driver->networks, net->uuid); - networkDriverUnlock(driver); - if (!obj) { - virReportError(VIR_ERR_NO_NETWORK, NULL); - goto cleanup; - } + if (!(obj = networkObjFromNetwork(net))) + return ret; if (virNetworkIsPersistentEnsureACL(net->conn, obj->def) < 0) goto cleanup; ret = obj->persistent; @@ -2832,30 +2841,22 @@ cleanup: static char *networkGetXMLDesc(virNetworkPtr net, unsigned int flags) { - virNetworkDriverStatePtr driver = net->conn->networkPrivateData; virNetworkObjPtr network; virNetworkDefPtr def; char *ret = NULL; virCheckFlags(VIR_NETWORK_XML_INACTIVE, NULL); - networkDriverLock(driver); - network = virNetworkFindByUUID(&driver->networks, net->uuid); - networkDriverUnlock(driver); - - if (!network) { - virReportError(VIR_ERR_NO_NETWORK, - "%s", _("no network with matching uuid")); - goto cleanup; - } + if (!(network = networkObjFromNetwork(net))) + return ret; if (virNetworkGetXMLDescEnsureACL(net->conn, network->def) < 0) goto cleanup; if ((flags & VIR_NETWORK_XML_INACTIVE) && network->newDef) def = network->newDef; else def = network->def; ret = virNetworkDefFormat(def, flags); @@ -2866,29 +2867,21 @@ cleanup: } static char *networkGetBridgeName(virNetworkPtr net) { - virNetworkDriverStatePtr driver = net->conn->networkPrivateData; virNetworkObjPtr network; char *bridge = NULL; - networkDriverLock(driver); - network = virNetworkFindByUUID(&driver->networks, net->uuid); - networkDriverUnlock(driver); - - if (!network) { - virReportError(VIR_ERR_NO_NETWORK, - "%s", _("no network with matching id")); - goto cleanup; - } + if (!(network = networkObjFromNetwork(net))) + return bridge; if (virNetworkGetBridgeNameEnsureACL(net->conn, network->def) < 0) goto cleanup; if (!(network->def->bridge)) { virReportError(VIR_ERR_INTERNAL_ERROR, _("network '%s' does not have a bridge name."), network->def->name); goto cleanup; } ignore_value(VIR_STRDUP(bridge, network->def->bridge)); @@ -2900,22 +2893,15 @@ cleanup: static int networkGetAutostart(virNetworkPtr net, int *autostart) { - virNetworkDriverStatePtr driver = net->conn->networkPrivateData; virNetworkObjPtr network; int ret = -1; - networkDriverLock(driver); - network = virNetworkFindByUUID(&driver->networks, net->uuid); - networkDriverUnlock(driver); - if (!network) { - virReportError(VIR_ERR_NO_NETWORK, - "%s", _("no network with matching uuid")); - goto cleanup; - } + if (!(network = networkObjFromNetwork(net))) + return ret; if (virNetworkGetAutostartEnsureACL(net->conn, network->def) < 0) goto cleanup; *autostart = network->autostart; ret = 0; -- 1.8.1.5 -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list