Original implemetion may return pools with NULL name if
some pool disappear between two libvirt pool API call. And
caller of this function consider number = 0 as error only
but it original return negative value when error before.
This patch fix it.
V2:
Use for instead of while in clean up.
Treat zero pool returned from libvirt as normal instead of error.
Rebased.
v3:
fix wrong i++ in for while, previous patch have this bug, it is patch 6/11,
please ignore 6/11 in v2 serial.
Signed-off-by: Wenchao Xia <xiawenc(a)linux.vnet.ibm.com>
---
src/Virt_DevicePool.c | 39 +++++++++++++++++++++++++++++----------
1 files changed, 29 insertions(+), 10 deletions(-)
diff --git a/src/Virt_DevicePool.c b/src/Virt_DevicePool.c
index 79dc108..769de63 100644
--- a/src/Virt_DevicePool.c
+++ b/src/Virt_DevicePool.c
@@ -117,52 +117,71 @@ int get_disk_pool(virStoragePoolPtr poolptr, struct virt_pool
**pool)
return ret;
}
+/* This function returns the real number of pools, no negative value should be
+ returned, if error happens it returns zero, otherwise return at least 1. */
static int get_diskpool_config(virConnectPtr conn,
struct tmp_disk_pool **_pools)
{
- int count = 0;
+ int count = 0, realcount = 0;
int i;
char ** names = NULL;
struct tmp_disk_pool *pools = NULL;
+ int have_err = 0;
count = virConnectNumOfStoragePools(conn);
- if (count <= 0)
+ if (count < 0) {
+ have_err = 1;
goto out;
+ } else if (count == 0) {
+ goto out;
+ }
names = calloc(count, sizeof(char *));
if (names == NULL) {
CU_DEBUG("Failed to alloc space for %i pool names", count);
count = 0;
+ have_err = 1;
goto out;
}
- if (virConnectListStoragePools(conn, names, count) == -1) {
+ realcount = virConnectListStoragePools(conn, names, count);
+ if (realcount == -1) {
CU_DEBUG("Failed to get storage pools");
- count = 0;
+ realcount = 0;
+ have_err = 1;
+ goto out;
+ }
+ if (realcount == 0) {
+ CU_DEBUG("zero pools got, but prelist is %d.", count);
goto out;
}
- pools = calloc(count, sizeof(*pools));
+ pools = calloc(realcount, sizeof(*pools));
if (pools == NULL) {
- CU_DEBUG("Failed to alloc space for %i pool structs", count);
+ CU_DEBUG("Failed to alloc space for %i pool structs",
realcount);
+ realcount = 0;
+ have_err = 1;
goto out;
}
- for (i = 0; i < count; i++) {
+ for (i = 0; i < realcount; i++) {
pools[i].tag = strdup(names[i]);
pools[i].primordial = false;
}
out:
- for (i = 0; i < count; i++)
+ for (i = 0; i < count; i++) {
free(names[i]);
+ }
free(names);
- get_disk_parent(&pools, &count);
+ if (have_err == 0) {
+ get_disk_parent(&pools, &realcount);
+ }
*_pools = pools;
- return count;
+ return realcount;
}
static bool diskpool_set_capacity(virConnectPtr conn,
--
1.7.1