[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[libvirt] [PATCH 02/10] list: Add helpers for listing storage pool objects



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

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

src/libvirt_private.syms: Export helper virStoragePoolList.
---
 src/conf/storage_conf.c  |  116 ++++++++++++++++++++++++++++++++++++++++++++++
 src/conf/storage_conf.h  |   35 ++++++++++++++
 src/libvirt_private.syms |    1 +
 3 files changed, 152 insertions(+), 0 deletions(-)

diff --git a/src/conf/storage_conf.c b/src/conf/storage_conf.c
index 3132aae..b14564f 100644
--- a/src/conf/storage_conf.c
+++ b/src/conf/storage_conf.c
@@ -1883,3 +1883,119 @@ void virStoragePoolObjUnlock(virStoragePoolObjPtr obj)
 {
     virMutexUnlock(&obj->lock);
 }
+
+#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
+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;
+            }
+            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 < npools; i++) {
+            if (tmp_pools[i])
+                virStoragePoolFree(tmp_pools[i]);
+        }
+    }
+
+    VIR_FREE(tmp_pools);
+    return ret;
+}
diff --git a/src/conf/storage_conf.h b/src/conf/storage_conf.h
index 4fb99df..bfa0819 100644
--- a/src/conf/storage_conf.h
+++ b/src/conf/storage_conf.h
@@ -518,4 +518,39 @@ enum virStoragePartedFsType {
 };
 VIR_ENUM_DECL(virStoragePartedFsType)
 
+# 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 virStoragePoolList(virConnectPtr conn,
+                       virStoragePoolObjList poolobjs,
+                       virStoragePoolPtr **pools,
+                       unsigned int flags);
+
 #endif /* __VIR_STORAGE_CONF_H__ */
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 65067d6..abfee47 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1058,6 +1058,7 @@ virStoragePoolDefParseString;
 virStoragePoolFormatDiskTypeToString;
 virStoragePoolFormatFileSystemNetTypeToString;
 virStoragePoolFormatFileSystemTypeToString;
+virStoragePoolList;
 virStoragePoolLoadAllConfigs;
 virStoragePoolObjAssignDef;
 virStoragePoolObjClearVols;
-- 
1.7.7.3


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]