storage: Introduce virStoragePoolObjGetNames

Mostly code motion to move storageConnectList[Defined]StoragePools
and similar test driver code into virstorageobj.c and rename to
virStoragePoolObjGetNames.

Also includes a couple of variable name adjustments to keep code consistent
with other drivers.

Signed-off-by: John Ferlan <jferlan@redhat.com>
This commit is contained in:
John Ferlan 2017-03-21 09:15:18 -04:00
parent 2fae7c7fb2
commit 50e6d4e8e1
5 changed files with 63 additions and 80 deletions

View File

@ -580,6 +580,42 @@ virStoragePoolObjNumOfStoragePools(virStoragePoolObjListPtr pools,
} }
int
virStoragePoolObjGetNames(virStoragePoolObjListPtr pools,
virConnectPtr conn,
bool wantActive,
virStoragePoolObjListACLFilter aclfilter,
char **const names,
int maxnames)
{
int nnames = 0;
size_t i;
for (i = 0; i < pools->count && nnames < maxnames; i++) {
virStoragePoolObjPtr obj = pools->objs[i];
virStoragePoolObjLock(obj);
if (!aclfilter || aclfilter(conn, obj->def)) {
if (wantActive == virStoragePoolObjIsActive(obj)) {
if (VIR_STRDUP(names[nnames], obj->def->name) < 0) {
virStoragePoolObjUnlock(obj);
goto failure;
}
nnames++;
}
}
virStoragePoolObjUnlock(obj);
}
return nnames;
failure:
while (--nnames >= 0)
VIR_FREE(names[nnames]);
return -1;
}
/* /*
* virStoragePoolObjIsDuplicate: * virStoragePoolObjIsDuplicate:
* @doms : virStoragePoolObjListPtr to search * @doms : virStoragePoolObjListPtr to search

View File

@ -155,6 +155,14 @@ virStoragePoolObjNumOfStoragePools(virStoragePoolObjListPtr pools,
bool wantActive, bool wantActive,
virStoragePoolObjListACLFilter aclfilter); virStoragePoolObjListACLFilter aclfilter);
int
virStoragePoolObjGetNames(virStoragePoolObjListPtr pools,
virConnectPtr conn,
bool wantActive,
virStoragePoolObjListACLFilter aclfilter,
char **const names,
int maxnames);
void void
virStoragePoolObjFree(virStoragePoolObjPtr pool); virStoragePoolObjFree(virStoragePoolObjPtr pool);

View File

@ -999,6 +999,7 @@ virStoragePoolObjClearVols;
virStoragePoolObjDeleteDef; virStoragePoolObjDeleteDef;
virStoragePoolObjFindByName; virStoragePoolObjFindByName;
virStoragePoolObjFindByUUID; virStoragePoolObjFindByUUID;
virStoragePoolObjGetNames;
virStoragePoolObjIsDuplicate; virStoragePoolObjIsDuplicate;
virStoragePoolObjListExport; virStoragePoolObjListExport;
virStoragePoolObjListFree; virStoragePoolObjListFree;

View File

@ -490,40 +490,23 @@ storageConnectNumOfStoragePools(virConnectPtr conn)
return nactive; return nactive;
} }
static int static int
storageConnectListStoragePools(virConnectPtr conn, storageConnectListStoragePools(virConnectPtr conn,
char **const names, char **const names,
int nnames) int maxnames)
{ {
int got = 0; int got = 0;
size_t i;
if (virConnectListStoragePoolsEnsureACL(conn) < 0) if (virConnectListStoragePoolsEnsureACL(conn) < 0)
return -1; return -1;
storageDriverLock(); storageDriverLock();
for (i = 0; i < driver->pools.count && got < nnames; i++) { got = virStoragePoolObjGetNames(&driver->pools, conn, true,
virStoragePoolObjPtr obj = driver->pools.objs[i]; virConnectListStoragePoolsCheckACL,
virStoragePoolObjLock(obj); names, maxnames);
if (virConnectListStoragePoolsCheckACL(conn, obj->def) &&
virStoragePoolObjIsActive(obj)) {
if (VIR_STRDUP(names[got], obj->def->name) < 0) {
virStoragePoolObjUnlock(obj);
goto cleanup;
}
got++;
}
virStoragePoolObjUnlock(obj);
}
storageDriverUnlock(); storageDriverUnlock();
return got; return got;
cleanup:
storageDriverUnlock();
for (i = 0; i < got; i++)
VIR_FREE(names[i]);
memset(names, 0, nnames * sizeof(*names));
return -1;
} }
static int static int
@ -542,40 +525,23 @@ storageConnectNumOfDefinedStoragePools(virConnectPtr conn)
return nactive; return nactive;
} }
static int static int
storageConnectListDefinedStoragePools(virConnectPtr conn, storageConnectListDefinedStoragePools(virConnectPtr conn,
char **const names, char **const names,
int nnames) int maxnames)
{ {
int got = 0; int got = 0;
size_t i;
if (virConnectListDefinedStoragePoolsEnsureACL(conn) < 0) if (virConnectListDefinedStoragePoolsEnsureACL(conn) < 0)
return -1; return -1;
storageDriverLock(); storageDriverLock();
for (i = 0; i < driver->pools.count && got < nnames; i++) { got = virStoragePoolObjGetNames(&driver->pools, conn, false,
virStoragePoolObjPtr obj = driver->pools.objs[i]; virConnectListDefinedStoragePoolsCheckACL,
virStoragePoolObjLock(obj); names, maxnames);
if (virConnectListDefinedStoragePoolsCheckACL(conn, obj->def) &&
!virStoragePoolObjIsActive(obj)) {
if (VIR_STRDUP(names[got], obj->def->name) < 0) {
virStoragePoolObjUnlock(obj);
goto cleanup;
}
got++;
}
virStoragePoolObjUnlock(obj);
}
storageDriverUnlock(); storageDriverUnlock();
return got; return got;
cleanup:
storageDriverUnlock();
for (i = 0; i < got; i++)
VIR_FREE(names[i]);
memset(names, 0, nnames * sizeof(*names));
return -1;
} }
/* This method is required to be re-entrant / thread safe, so /* This method is required to be re-entrant / thread safe, so

View File

@ -4117,35 +4117,21 @@ testConnectNumOfStoragePools(virConnectPtr conn)
return numActive; return numActive;
} }
static int static int
testConnectListStoragePools(virConnectPtr conn, testConnectListStoragePools(virConnectPtr conn,
char **const names, char **const names,
int nnames) int maxnames)
{ {
testDriverPtr privconn = conn->privateData; testDriverPtr privconn = conn->privateData;
int n = 0; int n = 0;
size_t i;
testDriverLock(privconn); testDriverLock(privconn);
memset(names, 0, sizeof(*names)*nnames); n = virStoragePoolObjGetNames(&privconn->pools, conn, true, NULL,
for (i = 0; i < privconn->pools.count && n < nnames; i++) { names, maxnames);
virStoragePoolObjLock(privconn->pools.objs[i]);
if (virStoragePoolObjIsActive(privconn->pools.objs[i]) &&
VIR_STRDUP(names[n++], privconn->pools.objs[i]->def->name) < 0) {
virStoragePoolObjUnlock(privconn->pools.objs[i]);
goto error;
}
virStoragePoolObjUnlock(privconn->pools.objs[i]);
}
testDriverUnlock(privconn); testDriverUnlock(privconn);
return n; return n;
error:
for (n = 0; n < nnames; n++)
VIR_FREE(names[n]);
testDriverUnlock(privconn);
return -1;
} }
@ -4163,35 +4149,21 @@ testConnectNumOfDefinedStoragePools(virConnectPtr conn)
return numInactive; return numInactive;
} }
static int static int
testConnectListDefinedStoragePools(virConnectPtr conn, testConnectListDefinedStoragePools(virConnectPtr conn,
char **const names, char **const names,
int nnames) int maxnames)
{ {
testDriverPtr privconn = conn->privateData; testDriverPtr privconn = conn->privateData;
int n = 0; int n = 0;
size_t i;
testDriverLock(privconn); testDriverLock(privconn);
memset(names, 0, sizeof(*names)*nnames); n = virStoragePoolObjGetNames(&privconn->pools, conn, false, NULL,
for (i = 0; i < privconn->pools.count && n < nnames; i++) { names, maxnames);
virStoragePoolObjLock(privconn->pools.objs[i]);
if (!virStoragePoolObjIsActive(privconn->pools.objs[i]) &&
VIR_STRDUP(names[n++], privconn->pools.objs[i]->def->name) < 0) {
virStoragePoolObjUnlock(privconn->pools.objs[i]);
goto error;
}
virStoragePoolObjUnlock(privconn->pools.objs[i]);
}
testDriverUnlock(privconn); testDriverUnlock(privconn);
return n; return n;
error:
for (n = 0; n < nnames; n++)
VIR_FREE(names[n]);
testDriverUnlock(privconn);
return -1;
} }
static int static int