[Libguestfs] [PATCH 2/3] mllib: Add function for comparing LVM2 UUIDs, ignoring '-' characters.

Richard W.M. Jones rjones at redhat.com
Wed Jan 28 14:25:37 UTC 2015


---
 mllib/common_utils.ml  | 21 +++++++++++++++++++++
 mllib/common_utils.mli |  3 +++
 2 files changed, 24 insertions(+)

diff --git a/mllib/common_utils.ml b/mllib/common_utils.ml
index d4994cf..898be17 100644
--- a/mllib/common_utils.ml
+++ b/mllib/common_utils.ml
@@ -506,6 +506,27 @@ let compare_version v1 v2 =
   in
   compare (split_version v1) (split_version v2)
 
+(* Annoying LVM2 returns a differing UUID strings for different
+ * function calls (sometimes containing or not containing '-'
+ * characters), so we have to normalize each string before
+ * comparison.  c.f. 'compare_pvuuids' in virt-filesystem.
+ *)
+let compare_lvm2_uuids uuid1 uuid2 =
+  let n1 = String.length uuid1 and n2 = String.length uuid2 in
+  let rec loop i1 i2 =
+    if i1 = n1 && i2 = n2 then 0            (* matching *)
+    else if i1 >= n1 then 1                 (* different lengths *)
+    else if i2 >= n2 then -1
+    else if uuid1.[i1] = '-' then loop (i1+1) i2 (* ignore '-' characters *)
+    else if uuid2.[i2] = '-' then loop i1 (i2+1)
+    else (
+      let c = compare uuid1.[i1] uuid2.[i2] in
+      if c <> 0 then c                          (* not matching *)
+      else loop (i1+1) (i2+1)
+    )
+  in
+  loop 0 0
+
 (* Run an external command, slurp up the output as a list of lines. *)
 let external_command ~prog cmd =
   let chan = Unix.open_process_in cmd in
diff --git a/mllib/common_utils.mli b/mllib/common_utils.mli
index ccb2e5f..5d3149a 100644
--- a/mllib/common_utils.mli
+++ b/mllib/common_utils.mli
@@ -106,6 +106,9 @@ val display_long_options : unit -> 'a
 val compare_version : string -> string -> int
 (** Compare two version strings. *)
 
+val compare_lvm2_uuids : string -> string -> int
+(** Compare two LVM2 UUIDs, ignoring '-' characters. *)
+
 val external_command : prog:string -> string -> string list
 (** Run an external command, slurp up the output as a list of lines. *)
 
-- 
2.1.0




More information about the Libguestfs mailing list