2 * Copyright (C) 2014-2015 Red Hat Inc.
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.
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.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 (* Miscellaneous utility functions. *)
24 module D = Libvirt.Domain
26 let (//) = Filename.concat
27 let quote = Filename.quote
29 let ( +^ ) = Int64.add
30 let ( -^ ) = Int64.sub
31 let ( *^ ) = Int64.mul
32 let ( /^ ) = Int64.div
33 let ( &^ ) = Int64.logand
34 let ( ~^ ) = Int64.lognot
36 let rec filter_map f = function
40 | Some y -> y :: filter_map f xs
41 | None -> filter_map f xs
44 let sign, i = if i < 0L then "-", Int64.neg i else "", i in
47 sprintf "%s%Ld" sign i
49 let f = Int64.to_float i /. 1024. in
52 sprintf "%s%.1fK" sign f
54 let f = Int64.to_float i /. 1024. in
57 sprintf "%s%.1fM" sign f
59 let f = Int64.to_float i /. 1024. in
60 (*let i = i /^ 1024L in*)
61 sprintf "%s%.1fG" sign f
66 let bytes_of_human_size s =
67 try sscanf s "%Ld%[Gg]" (fun b _ -> b *^ 1024L *^ 1024L *^ 1024L)
68 with Scan_failure _ ->
69 try sscanf s "%Ld%[Mm]" (fun b _ -> b *^ 1024L *^ 1024L)
70 with Scan_failure _ ->
71 try sscanf s "%Ld%[Kk]" (fun b _ -> b *^ 1024L)
72 with Scan_failure _ ->
73 try sscanf s "%Ld%[Bb]" (fun b _ -> b)
74 with Scan_failure _ ->
77 let string_of_dom_state = function
78 | D.InfoNoState -> "unknown"
79 | D.InfoRunning -> "running"
80 | D.InfoBlocked -> "blocked"
81 | D.InfoPaused -> "paused"
82 | D.InfoShutdown -> "shutdown"
83 | D.InfoShutoff -> "shutoff"
84 | D.InfoCrashed -> "crashed"
86 let regexp_of_glob s =
87 let len = String.length s in
88 let buf = Buffer.create len in
89 Buffer.add_char buf '^';
91 match String.unsafe_get s i with
92 (* Wildcard characters converted to regular expressions. *)
93 | '?' -> Buffer.add_char buf '.'
94 | '*' -> Buffer.add_string buf ".*"
95 (* Must escape any character which is special for PCRE - see
96 * pcrepattern(3). However ignore [..] because they are
97 * (approximately) the same for globs and regexps.
99 | ('\\' | '^' | '$' | '.' | '|' | '(' | ')'
101 Buffer.add_char buf '\\'; Buffer.add_char buf c
102 | c -> Buffer.add_char buf c
104 Buffer.add_char buf '$';
108 let chars = "abcdefghijklmnopqrstuvwxyz0123456789" in
113 let c = Random.int 36 in
119 let name_parse name =
120 let i = try Some (String.index name ':') with Not_found -> None in
124 Some (String.sub name 0 i),
125 String.sub name (i+1) (String.length name - i - 1)