[libvirt PATCH 17/20] virdomainmomentobjlist: introduce virDomainMomentIsAncestor

Pavel Hrdina posted 20 patches 1 year, 4 months ago
There is a newer version of this series
[libvirt PATCH 17/20] virdomainmomentobjlist: introduce virDomainMomentIsAncestor
Posted by Pavel Hrdina 1 year, 4 months ago
This new helper will allow us to check if we are able to delete external
snapshot after user did revert to non-leaf snapshot.

Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
---
 src/conf/virdomainmomentobjlist.c | 21 +++++++++++++++++++++
 src/conf/virdomainmomentobjlist.h |  4 ++++
 src/libvirt_private.syms          |  1 +
 3 files changed, 26 insertions(+)

diff --git a/src/conf/virdomainmomentobjlist.c b/src/conf/virdomainmomentobjlist.c
index f19ec3319a..0c520fb173 100644
--- a/src/conf/virdomainmomentobjlist.c
+++ b/src/conf/virdomainmomentobjlist.c
@@ -582,3 +582,24 @@ virDomainMomentFindLeaf(virDomainMomentObjList *list)
         return moment;
     return NULL;
 }
+
+
+bool
+virDomainMomentIsAncestor(virDomainMomentObj *moment,
+                          virDomainMomentObj *ancestor)
+{
+    if (!moment)
+        return false;
+
+    if (moment == ancestor)
+        return false;
+
+    do {
+        moment = moment->parent;
+
+        if (moment == ancestor)
+            return true;
+    } while (moment);
+
+    return false;
+}
diff --git a/src/conf/virdomainmomentobjlist.h b/src/conf/virdomainmomentobjlist.h
index d2ab3b46b1..2ea6b181c0 100644
--- a/src/conf/virdomainmomentobjlist.h
+++ b/src/conf/virdomainmomentobjlist.h
@@ -157,3 +157,7 @@ virDomainMomentCheckCycles(virDomainMomentObjList *list,
 
 virDomainMomentObj *
 virDomainMomentFindLeaf(virDomainMomentObjList *list);
+
+bool
+virDomainMomentIsAncestor(virDomainMomentObj *moment,
+                          virDomainMomentObj *ancestor);
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index dd752ba55c..8a724ef241 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1222,6 +1222,7 @@ virDomainMomentDropChildren;
 virDomainMomentDropParent;
 virDomainMomentForEachChild;
 virDomainMomentForEachDescendant;
+virDomainMomentIsAncestor;
 virDomainMomentMoveChildren;
 virDomainMomentObjFree;
 virDomainMomentObjNew;
-- 
2.39.2
Re: [libvirt PATCH 17/20] virdomainmomentobjlist: introduce virDomainMomentIsAncestor
Posted by Peter Krempa 1 year, 3 months ago
On Mon, Mar 13, 2023 at 16:42:18 +0100, Pavel Hrdina wrote:
> This new helper will allow us to check if we are able to delete external
> snapshot after user did revert to non-leaf snapshot.
> 
> Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
> ---
>  src/conf/virdomainmomentobjlist.c | 21 +++++++++++++++++++++
>  src/conf/virdomainmomentobjlist.h |  4 ++++
>  src/libvirt_private.syms          |  1 +
>  3 files changed, 26 insertions(+)
> 
> diff --git a/src/conf/virdomainmomentobjlist.c b/src/conf/virdomainmomentobjlist.c
> index f19ec3319a..0c520fb173 100644
> --- a/src/conf/virdomainmomentobjlist.c
> +++ b/src/conf/virdomainmomentobjlist.c
> @@ -582,3 +582,24 @@ virDomainMomentFindLeaf(virDomainMomentObjList *list)
>          return moment;
>      return NULL;
>  }
> +
> +
> +bool
> +virDomainMomentIsAncestor(virDomainMomentObj *moment,
> +                          virDomainMomentObj *ancestor)
> +{
> +    if (!moment)
> +        return false;

for (moment = moment->parent; moment; moment = moment->parent) {
    if (moment == ancestor)
        return true;
}

return false;

This should be sufficient IIUC to replace the condition and the loop,
right?

Either way:

Reviewed-by: Peter Krempa <pkrempa@redhat.com>

Consider documenting expectations though.