2 * Copyright (C) 2011 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 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.
26 let ( *^ ) = Int64.mul
28 let (&^) = Int64.logand
30 type ('a, 'b) either = Left of 'a | Right of 'b
32 let verbose = ref false
33 let set_verbose_flag () = verbose := true
34 let verbose () = !verbose
39 prerr_string Config.package;
40 prerr_string ": tid ";
41 prerr_string (string_of_int (Thread.id (Thread.self ())));
51 if verbose () then (prerr_string str; prerr_newline ());
57 let set_trace_flag () = trace := true
60 let connect_uri = ref None
61 let set_connect_uri conn = connect_uri := conn
62 let connect_uri () = !connect_uri
64 let utf8_copyright = "\194\169"
65 let utf8_rarrow = "\xe2\x86\x92"
70 else if i < 1024L *^ 1024L then
71 sprintf "%.1f KB" (Int64.to_float i /. 1024.)
72 else if i < 1024L *^ 1024L *^ 1024L then
73 sprintf "%.1f MB" (Int64.to_float i /. 1024. /. 1024.)
74 else if i < 1024L *^ 1024L *^ 1024L *^ 1024L then
75 sprintf "%.1f GB" (Int64.to_float i /. 1024. /. 1024. /. 1024.)
77 sprintf "%.1f TB" (Int64.to_float i /. 1024. /. 1024. /. 1024. /. 1024.)
82 else if i < 1024L *^ 1024L then
83 sprintf "%.1f MB" (Int64.to_float i /. 1024.)
85 sprintf "%.1f GB" (Int64.to_float i /. 1024. /. 1024.)
87 let unique = let i = ref 0 in fun () -> incr i; !i
89 let rec find_map f = function
90 | [] -> raise Not_found
94 | None -> find_map f xs
96 (* g_markup_escape is not bound by lablgtk2, but we want to provide
97 * extra protection for \0 characters appearing in the string
100 let markup_escape name =
102 | '&' -> "&" | '<' -> "<" | '>' -> ">"
104 | c -> String.make 1 c
106 String.replace_chars f name
108 let libguestfs_version_string () =
109 let g = new Guestfs.guestfs () in
110 let v = g#version () in
112 sprintf "%Ld.%Ld.%Ld%s"
113 v.Guestfs.major v.Guestfs.minor v.Guestfs.release v.Guestfs.extra in
117 let libvirt_version_string () =
118 let v = fst (Libvirt.get_version ()) in
119 sprintf "%d.%d.%d" (v / 1_000_000) ((v / 1_000) mod 1_000) (v mod 1_000)