Separate the algorithm for which list members to vist (which is
generic and can be shared with checkpoints, provided that checkpoints
pick the same bit values for some of its flags) from the decision on
which members to return (which is specific to snapshots). The typedef
for the callback function feels a bit heavy here, but will make it
easier to move the common portions in a later patch.
Signed-off-by: Eric Blake <eblake(a)redhat.com>
---
src/conf/virdomainsnapshotobjlist.h | 4 ++
src/conf/virdomainsnapshotobjlist.c | 60 ++++++++++++++++++-----------
2 files changed, 42 insertions(+), 22 deletions(-)
diff --git a/src/conf/virdomainsnapshotobjlist.h b/src/conf/virdomainsnapshotobjlist.h
index c13a0b4026..c7d4d265cb 100644
--- a/src/conf/virdomainsnapshotobjlist.h
+++ b/src/conf/virdomainsnapshotobjlist.h
@@ -27,6 +27,10 @@
# include "virdomainsnapshotobj.h"
# include "virbuffer.h"
+/* Filter that returns true if a given snapshot matches the filter flags */
+typedef bool (*virDomainSnapshotObjListFilter)(virDomainSnapshotObjPtr obj,
+ unsigned int flags);
+
virDomainSnapshotObjListPtr virDomainSnapshotObjListNew(void);
void virDomainSnapshotObjListFree(virDomainSnapshotObjListPtr snapshots);
diff --git a/src/conf/virdomainsnapshotobjlist.c b/src/conf/virdomainsnapshotobjlist.c
index ec670ff5c2..cd3ea569e3 100644
--- a/src/conf/virdomainsnapshotobjlist.c
+++ b/src/conf/virdomainsnapshotobjlist.c
@@ -260,6 +260,38 @@ virDomainSnapshotObjPtr
virDomainSnapshotAssignDef(virDomainSnapshotObjListPtr s
}
/* Snapshot Obj List functions */
+static bool
+virDomainSnapshotFilter(virDomainSnapshotObjPtr obj,
+ unsigned int flags)
+{
+ virDomainSnapshotDefPtr def = virDomainSnapshotObjGetDef(obj);
+
+ /* Caller has already sanitized flags and performed filtering on
+ * DESCENDANTS and LEAVES. */
+ if (flags & VIR_DOMAIN_SNAPSHOT_FILTERS_STATUS) {
+ if (!(flags & VIR_DOMAIN_SNAPSHOT_LIST_INACTIVE) &&
+ def->state == VIR_DOMAIN_SNAPSHOT_SHUTOFF)
+ return false;
+ if (!(flags & VIR_DOMAIN_SNAPSHOT_LIST_DISK_ONLY) &&
+ def->state == VIR_DOMAIN_SNAPSHOT_DISK_SNAPSHOT)
+ return false;
+ if (!(flags & VIR_DOMAIN_SNAPSHOT_LIST_ACTIVE) &&
+ def->state != VIR_DOMAIN_SNAPSHOT_SHUTOFF &&
+ def->state != VIR_DOMAIN_SNAPSHOT_DISK_SNAPSHOT)
+ return false;
+ }
+
+ if ((flags & VIR_DOMAIN_SNAPSHOT_LIST_INTERNAL) &&
+ virDomainSnapshotIsExternal(obj))
+ return false;
+ if ((flags & VIR_DOMAIN_SNAPSHOT_LIST_EXTERNAL) &&
+ !virDomainSnapshotIsExternal(obj))
+ return false;
+
+ return true;
+}
+
+
static void
virDomainSnapshotObjListDataFree(void *payload,
const void *name ATTRIBUTE_UNUSED)
@@ -292,12 +324,14 @@ virDomainSnapshotObjListFree(virDomainSnapshotObjListPtr snapshots)
VIR_FREE(snapshots);
}
+
struct virDomainSnapshotNameData {
char **const names;
int maxnames;
unsigned int flags;
int count;
bool error;
+ virDomainSnapshotObjListFilter filter;
};
static int virDomainSnapshotObjListCopyNames(void *payload,
@@ -316,26 +350,7 @@ static int virDomainSnapshotObjListCopyNames(void *payload,
if ((data->flags & VIR_DOMAIN_SNAPSHOT_LIST_NO_LEAVES) &&
!obj->nchildren)
return 0;
- if (data->flags & VIR_DOMAIN_SNAPSHOT_FILTERS_STATUS) {
- virDomainSnapshotDefPtr def = virDomainSnapshotObjGetDef(obj);
-
- if (!(data->flags & VIR_DOMAIN_SNAPSHOT_LIST_INACTIVE) &&
- def->state == VIR_DOMAIN_SNAPSHOT_SHUTOFF)
- return 0;
- if (!(data->flags & VIR_DOMAIN_SNAPSHOT_LIST_DISK_ONLY) &&
- def->state == VIR_DOMAIN_SNAPSHOT_DISK_SNAPSHOT)
- return 0;
- if (!(data->flags & VIR_DOMAIN_SNAPSHOT_LIST_ACTIVE) &&
- def->state != VIR_DOMAIN_SNAPSHOT_SHUTOFF &&
- def->state != VIR_DOMAIN_SNAPSHOT_DISK_SNAPSHOT)
- return 0;
- }
-
- if ((data->flags & VIR_DOMAIN_SNAPSHOT_LIST_INTERNAL) &&
- virDomainSnapshotIsExternal(obj))
- return 0;
- if ((data->flags & VIR_DOMAIN_SNAPSHOT_LIST_EXTERNAL) &&
- !virDomainSnapshotIsExternal(obj))
+ if (data->filter(obj, data->flags))
return 0;
if (data->names && data->count < data->maxnames &&
@@ -350,11 +365,12 @@ static int virDomainSnapshotObjListCopyNames(void *payload,
int
virDomainSnapshotObjListGetNames(virDomainSnapshotObjListPtr snapshots,
virDomainSnapshotObjPtr from,
- char **const names, int maxnames,
+ char **const names,
+ int maxnames,
unsigned int flags)
{
struct virDomainSnapshotNameData data = { names, maxnames, flags, 0,
- false };
+ false, virDomainSnapshotFilter };
size_t i;
if (!from) {
--
2.20.1