Create nodeDeviceObjFindByName which will perform the corresponding virNodeDeviceObjFindByName call for various node_device_driver callers rather than having the same repetitive code. Signed-off-by: John Ferlan <jferlan@xxxxxxxxxx> --- src/node_device/node_device_driver.c | 96 +++++++++++++----------------------- 1 file changed, 33 insertions(+), 63 deletions(-) diff --git a/src/node_device/node_device_driver.c b/src/node_device/node_device_driver.c index d4e9f6b..279c133 100644 --- a/src/node_device/node_device_driver.c +++ b/src/node_device/node_device_driver.c @@ -229,12 +229,10 @@ nodeConnectListAllNodeDevices(virConnectPtr conn, } -virNodeDevicePtr -nodeDeviceLookupByName(virConnectPtr conn, - const char *name) +static virNodeDeviceObjPtr +nodeDeviceObjFindByName(const char *name) { virNodeDeviceObjPtr obj; - virNodeDevicePtr ret = NULL; nodeDeviceLock(); obj = virNodeDeviceObjFindByName(&driver->devs, name); @@ -244,9 +242,22 @@ nodeDeviceLookupByName(virConnectPtr conn, virReportError(VIR_ERR_NO_NODE_DEVICE, _("no node device with matching name '%s'"), name); - goto cleanup; } + return obj; +} + + +virNodeDevicePtr +nodeDeviceLookupByName(virConnectPtr conn, + const char *name) +{ + virNodeDeviceObjPtr obj; + virNodeDevicePtr ret = NULL; + + if (!(obj = nodeDeviceObjFindByName(name))) + return NULL; + if (virNodeDeviceLookupByNameEnsureACL(conn, obj->def) < 0) goto cleanup; @@ -256,8 +267,7 @@ nodeDeviceLookupByName(virConnectPtr conn, } cleanup: - if (obj) - virNodeDeviceObjUnlock(obj); + virNodeDeviceObjUnlock(obj); return ret; } @@ -324,16 +334,8 @@ nodeDeviceGetXMLDesc(virNodeDevicePtr dev, virCheckFlags(0, NULL); - nodeDeviceLock(); - obj = virNodeDeviceObjFindByName(&driver->devs, dev->name); - nodeDeviceUnlock(); - - if (!obj) { - virReportError(VIR_ERR_NO_NODE_DEVICE, - _("no node device with matching name '%s'"), - dev->name); - goto cleanup; - } + if (!(obj = nodeDeviceObjFindByName(dev->name))) + return NULL; if (virNodeDeviceGetXMLDescEnsureACL(dev->conn, obj->def) < 0) goto cleanup; @@ -347,8 +349,7 @@ nodeDeviceGetXMLDesc(virNodeDevicePtr dev, ret = virNodeDeviceDefFormat(obj->def); cleanup: - if (obj) - virNodeDeviceObjUnlock(obj); + virNodeDeviceObjUnlock(obj); return ret; } @@ -359,16 +360,8 @@ nodeDeviceGetParent(virNodeDevicePtr dev) virNodeDeviceObjPtr obj; char *ret = NULL; - nodeDeviceLock(); - obj = virNodeDeviceObjFindByName(&driver->devs, dev->name); - nodeDeviceUnlock(); - - if (!obj) { - virReportError(VIR_ERR_NO_NODE_DEVICE, - _("no node device with matching name '%s'"), - dev->name); - goto cleanup; - } + if (!(obj = nodeDeviceObjFindByName(dev->name))) + return NULL; if (virNodeDeviceGetParentEnsureACL(dev->conn, obj->def) < 0) goto cleanup; @@ -382,8 +375,7 @@ nodeDeviceGetParent(virNodeDevicePtr dev) } cleanup: - if (obj) - virNodeDeviceObjUnlock(obj); + virNodeDeviceObjUnlock(obj); return ret; } @@ -396,16 +388,8 @@ nodeDeviceNumOfCaps(virNodeDevicePtr dev) int ncaps = 0; int ret = -1; - nodeDeviceLock(); - obj = virNodeDeviceObjFindByName(&driver->devs, dev->name); - nodeDeviceUnlock(); - - if (!obj) { - virReportError(VIR_ERR_NO_NODE_DEVICE, - _("no node device with matching name '%s'"), - dev->name); - goto cleanup; - } + if (!(obj = nodeDeviceObjFindByName(dev->name))) + return -1; if (virNodeDeviceNumOfCapsEnsureACL(dev->conn, obj->def) < 0) goto cleanup; @@ -427,8 +411,7 @@ nodeDeviceNumOfCaps(virNodeDevicePtr dev) ret = ncaps; cleanup: - if (obj) - virNodeDeviceObjUnlock(obj); + virNodeDeviceObjUnlock(obj); return ret; } @@ -444,16 +427,8 @@ nodeDeviceListCaps(virNodeDevicePtr dev, int ncaps = 0; int ret = -1; - nodeDeviceLock(); - obj = virNodeDeviceObjFindByName(&driver->devs, dev->name); - nodeDeviceUnlock(); - - if (!obj) { - virReportError(VIR_ERR_NO_NODE_DEVICE, - _("no node device with matching name '%s'"), - dev->name); - goto cleanup; - } + if (!(obj = nodeDeviceObjFindByName(dev->name))) + return -1; if (virNodeDeviceListCapsEnsureACL(dev->conn, obj->def) < 0) goto cleanup; @@ -483,8 +458,7 @@ nodeDeviceListCaps(virNodeDevicePtr dev, ret = ncaps; cleanup: - if (obj) - virNodeDeviceObjUnlock(obj); + virNodeDeviceObjUnlock(obj); if (ret == -1) { --ncaps; while (--ncaps >= 0) @@ -619,13 +593,10 @@ nodeDeviceDestroy(virNodeDevicePtr dev) char *wwnn = NULL, *wwpn = NULL; int parent_host = -1; + if (!(obj = nodeDeviceObjFindByName(dev->name))) + return -1; + nodeDeviceLock(); - if (!(obj = virNodeDeviceObjFindByName(&driver->devs, dev->name))) { - virReportError(VIR_ERR_NO_NODE_DEVICE, - _("no node device with matching name '%s'"), - dev->name); - goto cleanup; - } if (virNodeDeviceDestroyEnsureACL(dev->conn, obj->def) < 0) goto cleanup; @@ -651,8 +622,7 @@ nodeDeviceDestroy(virNodeDevicePtr dev) cleanup: nodeDeviceUnlock(); - if (obj) - virNodeDeviceObjUnlock(obj); + virNodeDeviceObjUnlock(obj); VIR_FREE(wwnn); VIR_FREE(wwpn); return ret; -- 2.9.3 -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list