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

[libvirt] [PATCH 2/6] conf: Extract code to filter domain list into a separate function



Separate the code to simplify future refactors.
---
 src/conf/domain_conf.c | 82 +++++++++++++++++++++++++++++---------------------
 1 file changed, 47 insertions(+), 35 deletions(-)

diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index b9c4c61..057602b 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -22927,43 +22927,19 @@ virDomainDeviceDefCopy(virDomainDeviceDefPtr src,
     return ret;
 }

-struct virDomainListData {
-    virConnectPtr conn;
-    virDomainPtr *domains;
-    virDomainObjListFilter filter;
-    unsigned int flags;
-    int ndomains;
-    bool error;
-};

-#define MATCH(FLAG) (data->flags & (FLAG))
-static void
-virDomainListPopulate(void *payload,
-                      const void *name ATTRIBUTE_UNUSED,
-                      void *opaque)
+#define MATCH(FLAG) (filter & (FLAG))
+static bool
+virDomainObjMatchFilter(virDomainObjPtr vm,
+                        unsigned int filter)
 {
-    struct virDomainListData *data = opaque;
-    virDomainObjPtr vm = payload;
-    virDomainPtr dom;
-
-    if (data->error)
-        return;
-
-    virObjectLock(vm);
-    /* check if the domain matches the filter */
-
-    /* filter by the callback function (access control checks) */
-    if (data->filter != NULL &&
-        !data->filter(data->conn, vm->def))
-        goto cleanup;
-
     /* filter by active state */
     if (MATCH(VIR_CONNECT_LIST_DOMAINS_FILTERS_ACTIVE) &&
         !((MATCH(VIR_CONNECT_LIST_DOMAINS_ACTIVE) &&
            virDomainObjIsActive(vm)) ||
           (MATCH(VIR_CONNECT_LIST_DOMAINS_INACTIVE) &&
            !virDomainObjIsActive(vm))))
-        goto cleanup;
+        return false;

     /* filter by persistence */
     if (MATCH(VIR_CONNECT_LIST_DOMAINS_FILTERS_PERSISTENT) &&
@@ -22971,7 +22947,7 @@ virDomainListPopulate(void *payload,
            vm->persistent) ||
           (MATCH(VIR_CONNECT_LIST_DOMAINS_TRANSIENT) &&
            !vm->persistent)))
-        goto cleanup;
+        return false;

     /* filter by domain state */
     if (MATCH(VIR_CONNECT_LIST_DOMAINS_FILTERS_STATE)) {
@@ -22986,7 +22962,7 @@ virDomainListPopulate(void *payload,
                (st != VIR_DOMAIN_RUNNING &&
                 st != VIR_DOMAIN_PAUSED &&
                 st != VIR_DOMAIN_SHUTOFF))))
-            goto cleanup;
+            return false;
     }

     /* filter by existence of managed save state */
@@ -22995,22 +22971,59 @@ virDomainListPopulate(void *payload,
            vm->hasManagedSave) ||
           (MATCH(VIR_CONNECT_LIST_DOMAINS_NO_MANAGEDSAVE) &&
            !vm->hasManagedSave)))
-            goto cleanup;
+        return false;

     /* filter by autostart option */
     if (MATCH(VIR_CONNECT_LIST_DOMAINS_FILTERS_AUTOSTART) &&
         !((MATCH(VIR_CONNECT_LIST_DOMAINS_AUTOSTART) && vm->autostart) ||
           (MATCH(VIR_CONNECT_LIST_DOMAINS_NO_AUTOSTART) && !vm->autostart)))
-        goto cleanup;
+        return false;

     /* filter by snapshot existence */
     if (MATCH(VIR_CONNECT_LIST_DOMAINS_FILTERS_SNAPSHOT)) {
         int nsnap = virDomainSnapshotObjListNum(vm->snapshots, NULL, 0);
         if (!((MATCH(VIR_CONNECT_LIST_DOMAINS_HAS_SNAPSHOT) && nsnap > 0) ||
               (MATCH(VIR_CONNECT_LIST_DOMAINS_NO_SNAPSHOT) && nsnap <= 0)))
-            goto cleanup;
+            return false;
     }

+    return true;
+}
+#undef MATCH
+
+
+struct virDomainListData {
+    virConnectPtr conn;
+    virDomainPtr *domains;
+    virDomainObjListFilter filter;
+    unsigned int flags;
+    int ndomains;
+    bool error;
+};
+
+static void
+virDomainListPopulate(void *payload,
+                      const void *name ATTRIBUTE_UNUSED,
+                      void *opaque)
+{
+    struct virDomainListData *data = opaque;
+    virDomainObjPtr vm = payload;
+    virDomainPtr dom;
+
+    if (data->error)
+        return;
+
+    virObjectLock(vm);
+    /* check if the domain matches the filter */
+
+    /* filter by the callback function (access control checks) */
+    if (data->filter != NULL &&
+        !data->filter(data->conn, vm->def))
+        goto cleanup;
+
+    if (!virDomainObjMatchFilter(vm, data->flags))
+        goto cleanup;
+
     /* just count the machines */
     if (!data->domains) {
         data->ndomains++;
@@ -23030,7 +23043,6 @@ virDomainListPopulate(void *payload,
     virObjectUnlock(vm);
     return;
 }
-#undef MATCH


 int
-- 
2.3.5


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