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

[libvirt] [PATCHv2 1/2] nwfilter: utility function virNWFilterVarValueEqual



From: Stefan Berger <stefanb us ibm com>

To detect if an interface's nwfilter has changed, we need to also
compare the filterparams, which is a hashtable of virNWFilterVarValue.
virHashEqual can do this nicely, but requires a pointer to a function
that will compare two of the items being stored in the hashes.
---
 src/conf/nwfilter_params.c | 31 +++++++++++++++++++++++++++++++
 src/conf/nwfilter_params.h |  2 ++
 src/libvirt_private.syms   |  1 +
 3 files changed, 34 insertions(+)

diff --git a/src/conf/nwfilter_params.c b/src/conf/nwfilter_params.c
index 6dc4baa..3ac1303 100644
--- a/src/conf/nwfilter_params.c
+++ b/src/conf/nwfilter_params.c
@@ -189,6 +189,37 @@ virNWFilterVarValueGetCardinality(const virNWFilterVarValuePtr val)
     return 0;
 }
 
+bool
+virNWFilterVarValueEqual(const virNWFilterVarValuePtr a,
+                         const virNWFilterVarValuePtr b)
+{
+    unsigned int card, i, j;
+    const char *s;
+
+    if (!a || !b)
+        return false;
+
+    card = virNWFilterVarValueGetCardinality(a);
+    if (card != virNWFilterVarValueGetCardinality(b))
+        return false;
+
+    /* brute force O(n^2) comparison */
+    for (i = 0; i < card; i++) {
+        bool eq = false;
+
+        s = virNWFilterVarValueGetNthValue(a, i);
+        for (j = 0; j < card; j++) {
+            if (STREQ_NULLABLE(s, virNWFilterVarValueGetNthValue(b, j))) {
+                 eq = true;
+                 break;
+            }
+        }
+        if (!eq)
+            return false;
+    }
+    return true;
+}
+
 int
 virNWFilterVarValueAddValue(virNWFilterVarValuePtr val, char *value)
 {
diff --git a/src/conf/nwfilter_params.h b/src/conf/nwfilter_params.h
index cedf9cd..96d3033 100644
--- a/src/conf/nwfilter_params.h
+++ b/src/conf/nwfilter_params.h
@@ -57,6 +57,8 @@ const char *virNWFilterVarValueGetSimple(const virNWFilterVarValuePtr val);
 const char *virNWFilterVarValueGetNthValue(virNWFilterVarValuePtr val,
                                            unsigned int idx);
 unsigned int virNWFilterVarValueGetCardinality(const virNWFilterVarValuePtr);
+bool virNWFilterVarValueEqual(const virNWFilterVarValuePtr a,
+                              const virNWFilterVarValuePtr b);
 int virNWFilterVarValueAddValue(virNWFilterVarValuePtr val, char *value);
 int virNWFilterVarValueDelValue(virNWFilterVarValuePtr val, const char *value);
 
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 2573b8a..ada73fb 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -968,6 +968,7 @@ virNWFilterVarValueCopy;
 virNWFilterVarValueCreateSimple;
 virNWFilterVarValueCreateSimpleCopyValue;
 virNWFilterVarValueDelValue;
+virNWFilterVarValueEqual;
 virNWFilterVarValueFree;
 virNWFilterVarValueGetCardinality;
 virNWFilterVarValueGetNthValue;
-- 
1.7.11.7


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