diff --git a/src/conf/virnodedeviceobj.c b/src/conf/virnodedeviceobj.c index f0c20c28b1..5b15363d8d 100644 --- a/src/conf/virnodedeviceobj.c +++ b/src/conf/virnodedeviceobj.c @@ -33,7 +33,7 @@ VIR_LOG_INIT("conf.virnodedeviceobj"); -int +static int virNodeDeviceObjHasCap(const virNodeDeviceObj *dev, const char *cap) { @@ -496,6 +496,39 @@ virNodeDeviceObjNumOfDevices(virNodeDeviceObjListPtr devs, } +int +virNodeDeviceObjGetNames(virNodeDeviceObjListPtr devs, + virConnectPtr conn, + virNodeDeviceObjListFilter aclfilter, + const char *cap, + char **const names, + int maxnames) +{ + int nnames = 0; + size_t i; + + for (i = 0; i < devs->count && nnames < maxnames; i++) { + virNodeDeviceObjPtr obj = devs->objs[i]; + virNodeDeviceObjLock(obj); + if (aclfilter && aclfilter(conn, obj->def) && + (!cap || virNodeDeviceObjHasCap(obj, cap))) { + if (VIR_STRDUP(names[nnames++], obj->def->name) < 0) { + virNodeDeviceObjUnlock(obj); + goto failure; + } + } + virNodeDeviceObjUnlock(obj); + } + + return nnames; + + failure: + while (--nnames >= 0) + VIR_FREE(names[nnames]); + return -1; +} + + #define MATCH(FLAG) ((flags & (VIR_CONNECT_LIST_NODE_DEVICES_CAP_ ## FLAG)) && \ virNodeDeviceCapMatch(devobj, VIR_NODE_DEV_CAP_ ## FLAG)) static bool diff --git a/src/conf/virnodedeviceobj.h b/src/conf/virnodedeviceobj.h index 7d303a060a..0acd8cedad 100644 --- a/src/conf/virnodedeviceobj.h +++ b/src/conf/virnodedeviceobj.h @@ -40,10 +40,6 @@ struct _virNodeDeviceDriverState { }; -int -virNodeDeviceObjHasCap(const virNodeDeviceObj *dev, - const char *cap); - virNodeDeviceObjPtr virNodeDeviceObjFindByName(virNodeDeviceObjListPtr devs, const char *name); @@ -88,6 +84,14 @@ virNodeDeviceObjNumOfDevices(virNodeDeviceObjListPtr devs, const char *cap, virNodeDeviceObjListFilter aclfilter); +int +virNodeDeviceObjGetNames(virNodeDeviceObjListPtr devs, + virConnectPtr conn, + virNodeDeviceObjListFilter aclfilter, + const char *cap, + char **const names, + int maxnames); + int virNodeDeviceObjListExport(virConnectPtr conn, virNodeDeviceObjList devobjs, diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index bd5a0c3b78..62bc36ac58 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -948,8 +948,8 @@ virInterfaceObjUnlock; virNodeDeviceObjAssignDef; virNodeDeviceObjFindByName; virNodeDeviceObjFindBySysfsPath; +virNodeDeviceObjGetNames; virNodeDeviceObjGetParentHost; -virNodeDeviceObjHasCap; virNodeDeviceObjListExport; virNodeDeviceObjListFree; virNodeDeviceObjLock; diff --git a/src/node_device/node_device_driver.c b/src/node_device/node_device_driver.c index f90b168749..7e705e2b18 100644 --- a/src/node_device/node_device_driver.c +++ b/src/node_device/node_device_driver.c @@ -174,14 +174,15 @@ nodeNumOfDevices(virConnectPtr conn, return ndevs; } + int nodeListDevices(virConnectPtr conn, const char *cap, - char **const names, int maxnames, + char **const names, + int maxnames, unsigned int flags) { - int ndevs = 0; - size_t i; + int nnames; if (virNodeListDevicesEnsureACL(conn) < 0) return -1; @@ -189,29 +190,12 @@ nodeListDevices(virConnectPtr conn, virCheckFlags(0, -1); nodeDeviceLock(); - for (i = 0; i < driver->devs.count && ndevs < maxnames; i++) { - virNodeDeviceObjPtr obj = driver->devs.objs[i]; - virNodeDeviceObjLock(obj); - if (virNodeListDevicesCheckACL(conn, obj->def) && - (cap == NULL || - virNodeDeviceObjHasCap(obj, cap))) { - if (VIR_STRDUP(names[ndevs++], obj->def->name) < 0) { - virNodeDeviceObjUnlock(obj); - goto failure; - } - } - virNodeDeviceObjUnlock(obj); - } + nnames = virNodeDeviceObjGetNames(&driver->devs, conn, + virNodeListDevicesCheckACL, + cap, names, maxnames); nodeDeviceUnlock(); - return ndevs; - - failure: - nodeDeviceUnlock(); - --ndevs; - while (--ndevs >= 0) - VIR_FREE(names[ndevs]); - return -1; + return nnames; } int diff --git a/src/test/test_driver.c b/src/test/test_driver.c index cf6ccd98a4..f9d9512032 100644 --- a/src/test/test_driver.c +++ b/src/test/test_driver.c @@ -5382,6 +5382,7 @@ testNodeNumOfDevices(virConnectPtr conn, return ndevs; } + static int testNodeListDevices(virConnectPtr conn, const char *cap, @@ -5390,35 +5391,19 @@ testNodeListDevices(virConnectPtr conn, unsigned int flags) { testDriverPtr driver = conn->privateData; - int ndevs = 0; - size_t i; + int nnames = 0; virCheckFlags(0, -1); testDriverLock(driver); - for (i = 0; i < driver->devs.count && ndevs < maxnames; i++) { - virNodeDeviceObjLock(driver->devs.objs[i]); - if (cap == NULL || - virNodeDeviceObjHasCap(driver->devs.objs[i], cap)) { - if (VIR_STRDUP(names[ndevs++], driver->devs.objs[i]->def->name) < 0) { - virNodeDeviceObjUnlock(driver->devs.objs[i]); - goto failure; - } - } - virNodeDeviceObjUnlock(driver->devs.objs[i]); - } + nnames = virNodeDeviceObjGetNames(&driver->devs, conn, NULL, + cap, names, maxnames); testDriverUnlock(driver); - return ndevs; - - failure: - testDriverUnlock(driver); - --ndevs; - while (--ndevs >= 0) - VIR_FREE(names[ndevs]); - return -1; + return nnames; } + static virNodeDevicePtr testNodeDeviceLookupByName(virConnectPtr conn, const char *name) {