diff --git a/src/conf/virstorageobj.c b/src/conf/virstorageobj.c index 5d92ab70636356f65ee620cdff5b701a50dc6009..8228a756fea77b478e7d1da6033ae3da76a6e2ba 100644 --- a/src/conf/virstorageobj.c +++ b/src/conf/virstorageobj.c @@ -215,6 +215,36 @@ virStoragePoolObjNumOfVolumes(virStorageVolDefListPtr volumes, } +int +virStoragePoolObjVolumeGetNames(virStorageVolDefListPtr volumes, + virConnectPtr conn, + virStoragePoolDefPtr pooldef, + virStoragePoolVolumeACLFilter aclfilter, + char **const names, + int maxnames) +{ + int nnames = 0; + size_t i; + + for (i = 0; i < volumes->count && nnames < maxnames; i++) { + virStorageVolDefPtr def = volumes->objs[i]; + if (aclfilter && !aclfilter(conn, pooldef, def)) + continue; + if (VIR_STRDUP(names[nnames], def->name) < 0) + goto failure; + nnames++; + } + + return nnames; + + failure: + while (--nnames >= 0) + VIR_FREE(names[nnames]); + + return -1; +} + + virStoragePoolObjPtr virStoragePoolObjAssignDef(virStoragePoolObjListPtr pools, virStoragePoolDefPtr def) diff --git a/src/conf/virstorageobj.h b/src/conf/virstorageobj.h index 3effe7a784cab590e8a789827d59beb7dfd0d38e..bf88094f1249d4c68bbdf2044a927800a7f7b51f 100644 --- a/src/conf/virstorageobj.h +++ b/src/conf/virstorageobj.h @@ -119,6 +119,14 @@ virStoragePoolObjNumOfVolumes(virStorageVolDefListPtr volumes, virStoragePoolDefPtr pooldef, virStoragePoolVolumeACLFilter aclfilter); +int +virStoragePoolObjVolumeGetNames(virStorageVolDefListPtr volumes, + virConnectPtr conn, + virStoragePoolDefPtr pooldef, + virStoragePoolVolumeACLFilter aclfilter, + char **const names, + int maxnames); + virStoragePoolObjPtr virStoragePoolObjAssignDef(virStoragePoolObjListPtr pools, virStoragePoolDefPtr def); diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index bcaa0f7a843444f6de4b6e69508071697051fc4e..d314519e530eabbed8a5849130698ed88f7e576b 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -1010,6 +1010,7 @@ virStoragePoolObjRemove; virStoragePoolObjSaveDef; virStoragePoolObjSourceFindDuplicate; virStoragePoolObjUnlock; +virStoragePoolObjVolumeGetNames; # cpu/cpu.h diff --git a/src/storage/storage_driver.c b/src/storage/storage_driver.c index 7d2f74d4ca09a073ce38089478f73717ae4b1528..b996ac4ba306a25814b8661b8a2acfe4b595a517 100644 --- a/src/storage/storage_driver.c +++ b/src/storage/storage_driver.c @@ -1411,16 +1411,14 @@ storagePoolNumOfVolumes(virStoragePoolPtr obj) return ret; } + static int storagePoolListVolumes(virStoragePoolPtr obj, char **const names, int maxnames) { virStoragePoolObjPtr pool; - size_t i; - int n = 0; - - memset(names, 0, maxnames * sizeof(*names)); + int n = -1; if (!(pool = virStoragePoolObjFromStoragePool(obj))) return -1; @@ -1434,24 +1432,12 @@ storagePoolListVolumes(virStoragePoolPtr obj, goto cleanup; } - for (i = 0; i < pool->volumes.count && n < maxnames; i++) { - if (!virStoragePoolListVolumesCheckACL(obj->conn, pool->def, - pool->volumes.objs[i])) - continue; - if (VIR_STRDUP(names[n++], pool->volumes.objs[i]->name) < 0) - goto cleanup; - } - - virStoragePoolObjUnlock(pool); - return n; - + n = virStoragePoolObjVolumeGetNames(&pool->volumes, obj->conn, pool->def, + virStoragePoolListVolumesCheckACL, + names, maxnames); cleanup: virStoragePoolObjUnlock(pool); - for (n = 0; n < maxnames; n++) - VIR_FREE(names[n]); - - memset(names, 0, maxnames * sizeof(*names)); - return -1; + return n; } static int diff --git a/src/test/test_driver.c b/src/test/test_driver.c index dac87e30ada56c0bb937aab604ad1d1d4ccca676..c3294bf9041bd0e7b2042038d70c17990601cbb9 100644 --- a/src/test/test_driver.c +++ b/src/test/test_driver.c @@ -4817,6 +4817,7 @@ testStoragePoolNumOfVolumes(virStoragePoolPtr pool) return ret; } + static int testStoragePoolListVolumes(virStoragePoolPtr pool, char **const names, @@ -4824,13 +4825,10 @@ testStoragePoolListVolumes(virStoragePoolPtr pool, { testDriverPtr privconn = pool->conn->privateData; virStoragePoolObjPtr privpool; - size_t i = 0; - int n = 0; - - memset(names, 0, maxnames * sizeof(*names)); + int n = -1; if (!(privpool = testStoragePoolObjFindByName(privconn, pool->name))) - goto cleanup; + return -1; if (!virStoragePoolObjIsActive(privpool)) { virReportError(VIR_ERR_OPERATION_INVALID, @@ -4838,24 +4836,15 @@ testStoragePoolListVolumes(virStoragePoolPtr pool, goto cleanup; } - for (i = 0; i < privpool->volumes.count && n < maxnames; i++) { - if (VIR_STRDUP(names[n++], privpool->volumes.objs[i]->name) < 0) - goto cleanup; - } + n = virStoragePoolObjVolumeGetNames(&privpool->volumes, pool->conn, + privpool->def, NULL, names, maxnames); + cleanup: virStoragePoolObjUnlock(privpool); return n; - - cleanup: - for (n = 0; n < maxnames; n++) - VIR_FREE(names[i]); - - memset(names, 0, maxnames * sizeof(*names)); - if (privpool) - virStoragePoolObjUnlock(privpool); - return -1; } + static int testStoragePoolListAllVolumes(virStoragePoolPtr obj, virStorageVolPtr **vols,