mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2024-12-22 13:45:38 +00:00
snapshot: Refactor list filtering
Separate the algorithm for which list members to vist (which is
generic and can be shared with checkpoints, provided that common
filtering bits are either declared with the same value or have a
mapping from public API to common value) 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.
As part of the refactoring, note that the macros for selecting filter
bits are specific to listing functionality, so they belong better in
virdomainsnapshotobjlist.h (missed in commit 9b75154c
).
Signed-off-by: Eric Blake <eblake@redhat.com>
Reviewed-by: John Ferlan <jferlan@redhat.com>
This commit is contained in:
parent
55c2ab3e2b
commit
de80cdbcc9
@ -133,29 +133,6 @@ int virDomainSnapshotAlignDisks(virDomainSnapshotDefPtr snapshot,
|
||||
int default_snapshot,
|
||||
bool require_match);
|
||||
|
||||
# define VIR_DOMAIN_SNAPSHOT_FILTERS_METADATA \
|
||||
(VIR_DOMAIN_SNAPSHOT_LIST_METADATA | \
|
||||
VIR_DOMAIN_SNAPSHOT_LIST_NO_METADATA)
|
||||
|
||||
# define VIR_DOMAIN_SNAPSHOT_FILTERS_LEAVES \
|
||||
(VIR_DOMAIN_SNAPSHOT_LIST_LEAVES | \
|
||||
VIR_DOMAIN_SNAPSHOT_LIST_NO_LEAVES)
|
||||
|
||||
# define VIR_DOMAIN_SNAPSHOT_FILTERS_STATUS \
|
||||
(VIR_DOMAIN_SNAPSHOT_LIST_INACTIVE | \
|
||||
VIR_DOMAIN_SNAPSHOT_LIST_ACTIVE | \
|
||||
VIR_DOMAIN_SNAPSHOT_LIST_DISK_ONLY)
|
||||
|
||||
# define VIR_DOMAIN_SNAPSHOT_FILTERS_LOCATION \
|
||||
(VIR_DOMAIN_SNAPSHOT_LIST_INTERNAL | \
|
||||
VIR_DOMAIN_SNAPSHOT_LIST_EXTERNAL)
|
||||
|
||||
# define VIR_DOMAIN_SNAPSHOT_FILTERS_ALL \
|
||||
(VIR_DOMAIN_SNAPSHOT_FILTERS_METADATA | \
|
||||
VIR_DOMAIN_SNAPSHOT_FILTERS_LEAVES | \
|
||||
VIR_DOMAIN_SNAPSHOT_FILTERS_STATUS | \
|
||||
VIR_DOMAIN_SNAPSHOT_FILTERS_LOCATION)
|
||||
|
||||
bool virDomainSnapshotDefIsExternal(virDomainSnapshotDefPtr def);
|
||||
bool virDomainSnapshotIsExternal(virDomainSnapshotObjPtr snap);
|
||||
|
||||
|
@ -259,6 +259,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)
|
||||
@ -291,12 +323,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,
|
||||
@ -315,26 +349,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 &&
|
||||
@ -349,11 +364,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) {
|
||||
|
@ -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);
|
||||
|
||||
@ -70,6 +74,29 @@ int virDomainSnapshotForEach(virDomainSnapshotObjListPtr snapshots,
|
||||
void *data);
|
||||
int virDomainSnapshotUpdateRelations(virDomainSnapshotObjListPtr snapshots);
|
||||
|
||||
# define VIR_DOMAIN_SNAPSHOT_FILTERS_METADATA \
|
||||
(VIR_DOMAIN_SNAPSHOT_LIST_METADATA | \
|
||||
VIR_DOMAIN_SNAPSHOT_LIST_NO_METADATA)
|
||||
|
||||
# define VIR_DOMAIN_SNAPSHOT_FILTERS_LEAVES \
|
||||
(VIR_DOMAIN_SNAPSHOT_LIST_LEAVES | \
|
||||
VIR_DOMAIN_SNAPSHOT_LIST_NO_LEAVES)
|
||||
|
||||
# define VIR_DOMAIN_SNAPSHOT_FILTERS_STATUS \
|
||||
(VIR_DOMAIN_SNAPSHOT_LIST_INACTIVE | \
|
||||
VIR_DOMAIN_SNAPSHOT_LIST_ACTIVE | \
|
||||
VIR_DOMAIN_SNAPSHOT_LIST_DISK_ONLY)
|
||||
|
||||
# define VIR_DOMAIN_SNAPSHOT_FILTERS_LOCATION \
|
||||
(VIR_DOMAIN_SNAPSHOT_LIST_INTERNAL | \
|
||||
VIR_DOMAIN_SNAPSHOT_LIST_EXTERNAL)
|
||||
|
||||
# define VIR_DOMAIN_SNAPSHOT_FILTERS_ALL \
|
||||
(VIR_DOMAIN_SNAPSHOT_FILTERS_METADATA | \
|
||||
VIR_DOMAIN_SNAPSHOT_FILTERS_LEAVES | \
|
||||
VIR_DOMAIN_SNAPSHOT_FILTERS_STATUS | \
|
||||
VIR_DOMAIN_SNAPSHOT_FILTERS_LOCATION)
|
||||
|
||||
int virDomainListSnapshots(virDomainSnapshotObjListPtr snapshots,
|
||||
virDomainSnapshotObjPtr from,
|
||||
virDomainPtr dom,
|
||||
|
@ -35,7 +35,7 @@
|
||||
#include "virsh-util.h"
|
||||
#include "virstring.h"
|
||||
#include "virxml.h"
|
||||
#include "conf/snapshot_conf.h"
|
||||
#include "conf/virdomainsnapshotobjlist.h"
|
||||
#include "vsh-table.h"
|
||||
|
||||
/* Helper for snapshot-create and snapshot-create-as */
|
||||
|
Loading…
Reference in New Issue
Block a user