guestfs browser 'reboot'
[guestfs-browser.git] / deviceSet.ml
diff --git a/deviceSet.ml b/deviceSet.ml
new file mode 100644 (file)
index 0000000..b51391e
--- /dev/null
@@ -0,0 +1,58 @@
+(* Guestfs Browser.
+ * Copyright (C) 2010 Red Hat Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *)
+
+let canonical dev =
+  let len = String.length dev in
+  if len >= 8 &&
+    dev.[0] = '/' &&
+    dev.[1] = 'd' && dev.[2] = 'e' && dev.[3] = 'v' &&
+    dev.[4] = '/' &&
+    (dev.[5] = 'h' || dev.[5] = 's' || dev.[5] = 'v') &&
+    dev.[6] = 'd' &&
+    dev.[7] >= 'a' && dev.[7] <= 'z' then (
+      let dev = String.copy dev in
+      dev.[5] <- 's';
+      dev
+    )
+  else
+    dev
+
+let canonical_compare dev1 dev2 =
+  let dev1 = canonical dev1 in
+  let dev2 = canonical dev2 in
+  String.compare dev1 dev2
+
+module DeviceSet = struct
+  include Set.Make (
+    struct
+      type t = String.t
+      let compare = canonical_compare
+    end
+  )
+
+  let subtract = diff
+
+  let of_list ds =
+    List.fold_left (fun set d -> add (canonical d) set) empty ds
+
+  let of_array ds =
+    of_list (Array.to_list ds)
+
+  let to_string t =
+    "{" ^ String.concat " " (elements t) ^ "}"
+end