nodedev: Introduce virNodeDeviceObjGetNames

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@redhat.com>
This commit is contained in:
John Ferlan 2017-03-19 15:51:03 -04:00
parent be3c2dfd1a
commit d06d1a329d
5 changed files with 57 additions and 51 deletions

View File

@ -33,7 +33,7 @@
VIR_LOG_INIT("conf.virnodedeviceobj"); VIR_LOG_INIT("conf.virnodedeviceobj");
int static int
virNodeDeviceObjHasCap(const virNodeDeviceObj *dev, virNodeDeviceObjHasCap(const virNodeDeviceObj *dev,
const char *cap) 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)) && \ #define MATCH(FLAG) ((flags & (VIR_CONNECT_LIST_NODE_DEVICES_CAP_ ## FLAG)) && \
virNodeDeviceCapMatch(devobj, VIR_NODE_DEV_CAP_ ## FLAG)) virNodeDeviceCapMatch(devobj, VIR_NODE_DEV_CAP_ ## FLAG))
static bool static bool

View File

@ -40,10 +40,6 @@ struct _virNodeDeviceDriverState {
}; };
int
virNodeDeviceObjHasCap(const virNodeDeviceObj *dev,
const char *cap);
virNodeDeviceObjPtr virNodeDeviceObjPtr
virNodeDeviceObjFindByName(virNodeDeviceObjListPtr devs, virNodeDeviceObjFindByName(virNodeDeviceObjListPtr devs,
const char *name); const char *name);
@ -88,6 +84,14 @@ virNodeDeviceObjNumOfDevices(virNodeDeviceObjListPtr devs,
const char *cap, const char *cap,
virNodeDeviceObjListFilter aclfilter); virNodeDeviceObjListFilter aclfilter);
int
virNodeDeviceObjGetNames(virNodeDeviceObjListPtr devs,
virConnectPtr conn,
virNodeDeviceObjListFilter aclfilter,
const char *cap,
char **const names,
int maxnames);
int int
virNodeDeviceObjListExport(virConnectPtr conn, virNodeDeviceObjListExport(virConnectPtr conn,
virNodeDeviceObjList devobjs, virNodeDeviceObjList devobjs,

View File

@ -948,8 +948,8 @@ virInterfaceObjUnlock;
virNodeDeviceObjAssignDef; virNodeDeviceObjAssignDef;
virNodeDeviceObjFindByName; virNodeDeviceObjFindByName;
virNodeDeviceObjFindBySysfsPath; virNodeDeviceObjFindBySysfsPath;
virNodeDeviceObjGetNames;
virNodeDeviceObjGetParentHost; virNodeDeviceObjGetParentHost;
virNodeDeviceObjHasCap;
virNodeDeviceObjListExport; virNodeDeviceObjListExport;
virNodeDeviceObjListFree; virNodeDeviceObjListFree;
virNodeDeviceObjLock; virNodeDeviceObjLock;

View File

@ -174,14 +174,15 @@ nodeNumOfDevices(virConnectPtr conn,
return ndevs; return ndevs;
} }
int int
nodeListDevices(virConnectPtr conn, nodeListDevices(virConnectPtr conn,
const char *cap, const char *cap,
char **const names, int maxnames, char **const names,
int maxnames,
unsigned int flags) unsigned int flags)
{ {
int ndevs = 0; int nnames;
size_t i;
if (virNodeListDevicesEnsureACL(conn) < 0) if (virNodeListDevicesEnsureACL(conn) < 0)
return -1; return -1;
@ -189,29 +190,12 @@ nodeListDevices(virConnectPtr conn,
virCheckFlags(0, -1); virCheckFlags(0, -1);
nodeDeviceLock(); nodeDeviceLock();
for (i = 0; i < driver->devs.count && ndevs < maxnames; i++) { nnames = virNodeDeviceObjGetNames(&driver->devs, conn,
virNodeDeviceObjPtr obj = driver->devs.objs[i]; virNodeListDevicesCheckACL,
virNodeDeviceObjLock(obj); cap, names, maxnames);
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);
}
nodeDeviceUnlock(); nodeDeviceUnlock();
return ndevs; return nnames;
failure:
nodeDeviceUnlock();
--ndevs;
while (--ndevs >= 0)
VIR_FREE(names[ndevs]);
return -1;
} }
int int

View File

@ -5382,6 +5382,7 @@ testNodeNumOfDevices(virConnectPtr conn,
return ndevs; return ndevs;
} }
static int static int
testNodeListDevices(virConnectPtr conn, testNodeListDevices(virConnectPtr conn,
const char *cap, const char *cap,
@ -5390,35 +5391,19 @@ testNodeListDevices(virConnectPtr conn,
unsigned int flags) unsigned int flags)
{ {
testDriverPtr driver = conn->privateData; testDriverPtr driver = conn->privateData;
int ndevs = 0; int nnames = 0;
size_t i;
virCheckFlags(0, -1); virCheckFlags(0, -1);
testDriverLock(driver); testDriverLock(driver);
for (i = 0; i < driver->devs.count && ndevs < maxnames; i++) { nnames = virNodeDeviceObjGetNames(&driver->devs, conn, NULL,
virNodeDeviceObjLock(driver->devs.objs[i]); cap, names, maxnames);
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]);
}
testDriverUnlock(driver); testDriverUnlock(driver);
return ndevs; return nnames;
failure:
testDriverUnlock(driver);
--ndevs;
while (--ndevs >= 0)
VIR_FREE(names[ndevs]);
return -1;
} }
static virNodeDevicePtr static virNodeDevicePtr
testNodeDeviceLookupByName(virConnectPtr conn, const char *name) testNodeDeviceLookupByName(virConnectPtr conn, const char *name)
{ {