slave: Use slightly modified event_callback.
[guestfs-browser.git] / deviceSet.ml
1 (* Guestfs Browser.
2  * Copyright (C) 2010 Red Hat Inc.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  *)
18
19 let canonical dev =
20   let len = String.length dev in
21   if len >= 8 &&
22     dev.[0] = '/' &&
23     dev.[1] = 'd' && dev.[2] = 'e' && dev.[3] = 'v' &&
24     dev.[4] = '/' &&
25     (dev.[5] = 'h' || dev.[5] = 's' || dev.[5] = 'v') &&
26     dev.[6] = 'd' &&
27     dev.[7] >= 'a' && dev.[7] <= 'z' then (
28       let dev = String.copy dev in
29       dev.[5] <- 's';
30       dev
31     )
32   else
33     dev
34
35 let canonical_compare dev1 dev2 =
36   let dev1 = canonical dev1 in
37   let dev2 = canonical dev2 in
38   String.compare dev1 dev2
39
40 module DeviceSet = struct
41   include Set.Make (
42     struct
43       type t = String.t
44       let compare = canonical_compare
45     end
46   )
47
48   let subtract = diff
49
50   let of_list ds =
51     List.fold_left (fun set d -> add (canonical d) set) empty ds
52
53   let of_array ds =
54     of_list (Array.to_list ds)
55
56   let to_string t =
57     "{" ^ String.concat " " (elements t) ^ "}"
58 end