src/conf/virobjectlist.c: Add virStoragePoolMatch to filter the
pools; Add virStoragePoolList to iterate over the pool objects
with filter.

src/conf/virobjectlist.h: Declare virStoragePoolMatch,
virStoragePoolList, and the macros for filters.

src/libvirt_private.syms: Export helper virStoragePoolList.
---
 src/conf/virobjectlist.c |  118 ++++++++++++++++++++++++++++++++++++++++++++++
 src/conf/virobjectlist.h |   36 ++++++++++++++
 src/libvirt_private.syms |    1 +
 3 files changed, 155 insertions(+), 0 deletions(-)

diff --git a/src/conf/virobjectlist.c b/src/conf/virobjectlist.c
index 91eb660..fb5f974 100644
--- a/src/conf/virobjectlist.c
+++ b/src/conf/virobjectlist.c
@@ -29,6 +29,7 @@
 #include "internal.h"
 #include "virhash.h"
 #include "domain_conf.h"
+#include "storage_conf.h"
 #include "memory.h"
 #include "datatypes.h"
 #include "virterror_internal.h"
@@ -134,6 +135,64 @@ cleanup:
 }
 #undef MATCH
 
+#define MATCH(FLAG) (flags & (FLAG))
+static bool
+virStoragePoolMatch (virStoragePoolObjPtr poolobj,
+                     unsigned int flags)
+{
+    /* filter by active state */
+    if (MATCH(VIR_CONNECT_LIST_STORAGE_POOLS_FILTERS_ACTIVE) &&
+        !((MATCH(VIR_CONNECT_LIST_STORAGE_POOLS_ACTIVE) &&
+           virStoragePoolObjIsActive(poolobj)) ||
+          (MATCH(VIR_CONNECT_LIST_STORAGE_POOLS_INACTIVE) &&
+           !virStoragePoolObjIsActive(poolobj))))
+        return false;
+
+    /* filter by persistence */
+    if (MATCH(VIR_CONNECT_LIST_STORAGE_POOLS_FILTERS_PERSISTENT) &&
+        !((MATCH(VIR_CONNECT_LIST_STORAGE_POOLS_PERSISTENT) &&
+           poolobj->configFile) ||
+          (MATCH(VIR_CONNECT_LIST_STORAGE_POOLS_TRANSIENT) &&
+           !poolobj->configFile)))
+        return false;
+
+    /* filter by autostart option */
+    if (MATCH(VIR_CONNECT_LIST_STORAGE_POOLS_FILTERS_AUTOSTART) &&
+        !((MATCH(VIR_CONNECT_LIST_STORAGE_POOLS_AUTOSTART) &&
+           poolobj->autostart) ||
+          (MATCH(VIR_CONNECT_LIST_STORAGE_POOLS_NO_AUTOSTART) &&
+           !poolobj->autostart)))
+        return false;
+
+    /* filter by pool type */
+    if (MATCH(VIR_CONNECT_LIST_STORAGE_POOLS_FILTERS_POOL_TYPE)) {
+        if (!((MATCH(VIR_CONNECT_LIST_STORAGE_POOLS_DIR) &&
+               (poolobj->def->type == VIR_STORAGE_POOL_DIR))     ||
+              (MATCH(VIR_CONNECT_LIST_STORAGE_POOLS_FS) &&
+               (poolobj->def->type == VIR_STORAGE_POOL_FS))      ||
+              (MATCH(VIR_CONNECT_LIST_STORAGE_POOLS_NETFS) &&
+               (poolobj->def->type == VIR_STORAGE_POOL_NETFS))   ||
+              (MATCH(VIR_CONNECT_LIST_STORAGE_POOLS_LOGICAL) &&
+               (poolobj->def->type == VIR_STORAGE_POOL_LOGICAL)) ||
+              (MATCH(VIR_CONNECT_LIST_STORAGE_POOLS_DISK) &&
+               (poolobj->def->type == VIR_STORAGE_POOL_DISK))    ||
+              (MATCH(VIR_CONNECT_LIST_STORAGE_POOLS_ISCSI) &&
+               (poolobj->def->type == VIR_STORAGE_POOL_ISCSI))   ||
+              (MATCH(VIR_CONNECT_LIST_STORAGE_POOLS_SCSI) &&
+               (poolobj->def->type == VIR_STORAGE_POOL_SCSI))    ||
+              (MATCH(VIR_CONNECT_LIST_STORAGE_POOLS_MPATH) &&
+               (poolobj->def->type == VIR_STORAGE_POOL_MPATH))   ||
+              (MATCH(VIR_CONNECT_LIST_STORAGE_POOLS_RBD) &&
+               (poolobj->def->type == VIR_STORAGE_POOL_RBD))     ||
+              (MATCH(VIR_CONNECT_LIST_STORAGE_POOLS_SHEEPDOG) &&
+               (poolobj->def->type == VIR_STORAGE_POOL_SHEEPDOG))))
+            return false;
+    }
+
+    return true;
+}
+#undef MATCH
+
 int
 virDomainList(virConnectPtr conn,
               virHashTablePtr domobjs,
@@ -222,3 +281,62 @@ cleanup:
     }
     return ret;
 }
+
+int
+virStoragePoolList(virConnectPtr conn,
+                   virStoragePoolObjList poolobjs,
+                   virStoragePoolPtr **pools,
+                   unsigned int flags)
+{
+    virStoragePoolPtr *tmp_pools = NULL;
+    virStoragePoolPtr pool = NULL;
+    int npools = 0;
+    int ret = -1;
+    int i;
+
+    if (pools) {
+        if (VIR_ALLOC_N(tmp_pools, poolobjs.count + 1) < 0) {
+            virReportOOMError();
+            goto cleanup;
+        }
+    }
+
+    for (i = 0; i < poolobjs.count; i++) {
+        virStoragePoolObjPtr poolobj = poolobjs.objs[i];
+        virStoragePoolObjLock(poolobj);
+        if (virStoragePoolMatch(poolobj, flags)) {
+            if (pools) {
+                if (!(pool = virGetStoragePool(conn,
+                                               poolobj->def->name,
+                                               poolobj->def->uuid))) {
+                    virStoragePoolObjUnlock(poolobj);
+                    goto cleanup;
+                }
+                tmp_pools[npools++] = pool;
+            } else {
+                npools++;
+            }
+        }
+        virStoragePoolObjUnlock(poolobj);
+    }
+
+    if (tmp_pools) {
+        /* trim the array to the final size */
+        ignore_value(VIR_REALLOC_N(tmp_pools, npools + 1));
+        *pools = tmp_pools;
+        tmp_pools = NULL;
+    }
+
+    ret = npools;
+
+cleanup:
+    if (tmp_pools) {
+        for (i = 0; i < poolobjs.count; i++) {
+            if (tmp_pools[i])
+                virStoragePoolFree(tmp_pools[i]);
+        }
+    }
+
+    VIR_FREE(tmp_pools);
+    return ret;
+}
diff --git a/src/conf/virobjectlist.h b/src/conf/virobjectlist.h
index 510a900..b93cd19 100644
--- a/src/conf/virobjectlist.h
+++ b/src/conf/virobjectlist.h
@@ -27,6 +27,7 @@
 # include "internal.h"
 # include "virhash.h"
 # include "domain_conf.h"
+# include "storage_conf.h"
 
 # define VIR_CONNECT_LIST_DOMAINS_FILTERS_ACTIVE   \
                 (VIR_CONNECT_LIST_DOMAINS_ACTIVE | \
@@ -74,6 +75,36 @@
                (VIR_DOMAIN_SNAPSHOT_FILTERS_METADATA  | \
                 VIR_DOMAIN_SNAPSHOT_FILTERS_LEAVES)
 
+# define VIR_CONNECT_LIST_STORAGE_POOLS_FILTERS_ACTIVE   \
+                (VIR_CONNECT_LIST_STORAGE_POOLS_ACTIVE | \
+                 VIR_CONNECT_LIST_STORAGE_POOLS_INACTIVE)
+
+# define VIR_CONNECT_LIST_STORAGE_POOLS_FILTERS_PERSISTENT   \
+                (VIR_CONNECT_LIST_STORAGE_POOLS_PERSISTENT | \
+                 VIR_CONNECT_LIST_STORAGE_POOLS_TRANSIENT)
+
+# define VIR_CONNECT_LIST_STORAGE_POOLS_FILTERS_AUTOSTART    \
+                (VIR_CONNECT_LIST_STORAGE_POOLS_AUTOSTART |  \
+                 VIR_CONNECT_LIST_STORAGE_POOLS_NO_AUTOSTART)
+
+# define VIR_CONNECT_LIST_STORAGE_POOLS_FILTERS_POOL_TYPE  \
+                (VIR_CONNECT_LIST_STORAGE_POOLS_DIR      | \
+                 VIR_CONNECT_LIST_STORAGE_POOLS_FS       | \
+                 VIR_CONNECT_LIST_STORAGE_POOLS_NETFS    | \
+                 VIR_CONNECT_LIST_STORAGE_POOLS_LOGICAL  | \
+                 VIR_CONNECT_LIST_STORAGE_POOLS_DISK     | \
+                 VIR_CONNECT_LIST_STORAGE_POOLS_ISCSI    | \
+                 VIR_CONNECT_LIST_STORAGE_POOLS_SCSI     | \
+                 VIR_CONNECT_LIST_STORAGE_POOLS_MPATH    | \
+                 VIR_CONNECT_LIST_STORAGE_POOLS_RBD      | \
+                 VIR_CONNECT_LIST_STORAGE_POOLS_SHEEPDOG)
+
+# define VIR_CONNECT_LIST_STORAGE_POOLS_FILTERS_ALL                  \
+                (VIR_CONNECT_LIST_STORAGE_POOLS_FILTERS_ACTIVE     | \
+                 VIR_CONNECT_LIST_STORAGE_POOLS_FILTERS_PERSISTENT | \
+                 VIR_CONNECT_LIST_STORAGE_POOLS_FILTERS_AUTOSTART  | \
+                 VIR_CONNECT_LIST_STORAGE_POOLS_FILTERS_POOL_TYPE)
+
 int virDomainList(virConnectPtr conn, virHashTablePtr domobjs,
                   virDomainPtr **domains, unsigned int flags);
 
@@ -83,4 +114,9 @@ int virDomainListSnapshots(virDomainSnapshotObjListPtr 
snapshots,
                            virDomainSnapshotPtr **snaps,
                            unsigned int flags);
 
+int virStoragePoolList(virConnectPtr conn,
+                       virStoragePoolObjList poolobjs,
+                       virStoragePoolPtr **pools,
+                       unsigned int flags);
+
 #endif /* __VIR_OBJECT_LIST_H__ */
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index a15abe4..8ae495d 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1243,6 +1243,7 @@ virDBusGetSystemBus;
 # virobjectlist.h
 virDomainList;
 virDomainListSnapshots;
+virStoragePoolList;
 
 
 # virfile.h
-- 
1.7.7.3

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list

Reply via email to