Unify the *ListDevice API into virnodedeviceobj.c from node_device_driver
and test_driver. The only real difference between the two is that the test
driver doesn't call the aclfilter API. The name of the new API follows that
of other drivers to "GetNames".
NB: Change some variable names to match what they really are - consistency
with other drivers. Also added a clear of the input names.
This also allows virNodeDeviceObjHasCap to be static to virnodedeviceobj
Signed-off-by: John Ferlan <jferlan(a)redhat.com>
---
src/conf/virnodedeviceobj.c | 36 +++++++++++++++++++++++++++++++++++-
src/conf/virnodedeviceobj.h | 12 ++++++++----
src/libvirt_private.syms | 2 +-
src/node_device/node_device_driver.c | 34 ++++++++++------------------------
src/test/test_driver.c | 27 ++++++---------------------
5 files changed, 60 insertions(+), 51 deletions(-)
diff --git a/src/conf/virnodedeviceobj.c b/src/conf/virnodedeviceobj.c
index f0c20c2..5a8dfe7 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,40 @@ 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]);
+ memset(names, 0, maxnames * sizeof(*names));
+ 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 7d303a0..0acd8ce 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);
@@ -89,6 +85,14 @@ virNodeDeviceObjNumOfDevices(virNodeDeviceObjListPtr devs,
virNodeDeviceObjListFilter aclfilter);
int
+virNodeDeviceObjGetNames(virNodeDeviceObjListPtr devs,
+ virConnectPtr conn,
+ virNodeDeviceObjListFilter aclfilter,
+ const char *cap,
+ char **const names,
+ int maxnames);
+
+int
virNodeDeviceObjListExport(virConnectPtr conn,
virNodeDeviceObjList devobjs,
virNodeDevicePtr **devices,
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 104ed88..5df6a5d 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -946,8 +946,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 f90b168..e850a22 100644
--- a/src/node_device/node_device_driver.c
+++ b/src/node_device/node_device_driver.c
@@ -174,14 +174,17 @@ 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;
+
+ memset(names, 0, maxnames * sizeof(*names));
if (virNodeListDevicesEnsureACL(conn) < 0)
return -1;
@@ -189,29 +192,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 2f1f4fa..a9040e8 100644
--- a/src/test/test_driver.c
+++ b/src/test/test_driver.c
@@ -5395,6 +5395,7 @@ testNodeNumOfDevices(virConnectPtr conn,
return ndevs;
}
+
static int
testNodeListDevices(virConnectPtr conn,
const char *cap,
@@ -5403,35 +5404,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)
{
--
2.9.3