]> xenbits.xensource.com Git - libvirt.git/commitdiff
utils: storage: Add helper for checking if storage source is the same
authorPeter Krempa <pkrempa@redhat.com>
Fri, 13 Jul 2018 15:25:33 +0000 (17:25 +0200)
committerPeter Krempa <pkrempa@redhat.com>
Fri, 20 Jul 2018 13:41:38 +0000 (15:41 +0200)
To allow checking whether a storage source points to the same location
add a helper which checks the relevant fields. This will allow replacing
a similar check done by formatting the command line arguments for
qemu-like syntax.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
src/libvirt_private.syms
src/util/virstoragefile.c
src/util/virstoragefile.h

index 0a20eb661b9cf6b142c8469d3d5b8e2396da8904..1a4cf9842042d7ef589c15f1b4089647033f9dd6 100644 (file)
@@ -2856,6 +2856,7 @@ virStorageSourceIsBlockLocal;
 virStorageSourceIsEmpty;
 virStorageSourceIsLocalStorage;
 virStorageSourceIsRelative;
+virStorageSourceIsSameLocation;
 virStorageSourceNetworkAssignDefaultPorts;
 virStorageSourceNewFromBacking;
 virStorageSourceNewFromBackingAbsolute;
index b6a2b415e98d2b8993f32452c9ee192ae703e7cf..56082f34e9156adbb4f5f4b60dce2fd9ab179d1f 100644 (file)
@@ -2286,6 +2286,49 @@ virStorageSourceCopy(const virStorageSource *src,
 }
 
 
+/**
+ * virStorageSourceIsSameLocation:
+ *
+ * Returns true if the sources @a and @b point to the same storage location.
+ * This does not compare any other configuration option
+ */
+bool
+virStorageSourceIsSameLocation(virStorageSourcePtr a,
+                               virStorageSourcePtr b)
+{
+    size_t i;
+
+    /* there are multiple possibilities to define an empty source */
+    if (virStorageSourceIsEmpty(a) &&
+        virStorageSourceIsEmpty(b))
+        return true;
+
+    if (virStorageSourceGetActualType(a) != virStorageSourceGetActualType(b))
+        return false;
+
+    if (STRNEQ_NULLABLE(a->path, b->path) ||
+        STRNEQ_NULLABLE(a->volume, b->volume) ||
+        STRNEQ_NULLABLE(a->snapshot, b->snapshot))
+        return false;
+
+    if (a->type == VIR_STORAGE_TYPE_NETWORK) {
+        if (a->protocol != b->protocol ||
+            a->nhosts != b->nhosts)
+            return false;
+
+        for (i = 0; i < a->nhosts; i++) {
+            if (a->hosts[i].transport != b->hosts[i].transport ||
+                a->hosts[i].port != b->hosts[i].port ||
+                STRNEQ_NULLABLE(a->hosts[i].name, b->hosts[i].name) ||
+                STRNEQ_NULLABLE(a->hosts[i].socket, b->hosts[i].socket))
+                return false;
+        }
+    }
+
+    return true;
+}
+
+
 /**
  * virStorageSourceInitChainElement:
  * @newelem: New backing chain element disk source
index 991098e6c69562aebe5992d7f0990747d174801c..c2c40edf683f03b263bc61c02acd919ad2e390c3 100644 (file)
@@ -436,6 +436,9 @@ virStorageSourcePtr virStorageSourceNewFromBacking(virStorageSourcePtr parent);
 virStorageSourcePtr virStorageSourceCopy(const virStorageSource *src,
                                          bool backingChain)
     ATTRIBUTE_NONNULL(1);
+bool virStorageSourceIsSameLocation(virStorageSourcePtr a,
+                                    virStorageSourcePtr b)
+    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
 
 int virStorageSourceParseRBDColonString(const char *rbdstr,
                                         virStorageSourcePtr src)