2 * Copyright (C) 2010 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.
25 let ( *^ ) = Int64.mul
28 type ('a, 'b) either = Left of 'a | Right of 'b
30 let (//) = Filename.concat
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_rarrow = "\xe2\x86\x92"
69 else if i < 1024L *^ 1024L then
70 sprintf "%.1f KB" (Int64.to_float i /. 1024.)
71 else if i < 1024L *^ 1024L *^ 1024L then
72 sprintf "%.1f MB" (Int64.to_float i /. 1024. /. 1024.)
73 else if i < 1024L *^ 1024L *^ 1024L *^ 1024L then
74 sprintf "%.1f GB" (Int64.to_float i /. 1024. /. 1024. /. 1024.)
76 sprintf "%.1f TB" (Int64.to_float i /. 1024. /. 1024. /. 1024. /. 1024.)
81 else if i < 1024L *^ 1024L then
82 sprintf "%.1f MB" (Int64.to_float i /. 1024.)
84 sprintf "%.1f GB" (Int64.to_float i /. 1024. /. 1024.)
86 let unique = let i = ref 0 in fun () -> incr i; !i
89 (GMisc.label ~text () :> GObj.widget)
91 (* XXX No binding for g_markup_escape in lablgtk2. *)
92 let markup_escape name =
94 | '&' -> "&" | '<' -> "<" | '>' -> ">"
95 | c -> String.make 1 c
97 String.replace_chars f name
99 let libguestfs_version_string () =
100 let g = new Guestfs.guestfs () in
101 let v = g#version () in
103 sprintf "%Ld.%Ld.%Ld%s"
104 v.Guestfs.major v.Guestfs.minor v.Guestfs.release v.Guestfs.extra in
108 let libvirt_version_string () =
109 let v = fst (Libvirt.get_version ()) in
110 sprintf "%d.%d.%d" (v / 1_000_000) ((v / 1_000) mod 1_000) (v mod 1_000)
114 * Note these have to be on Linux ABI modes. We cannot use the
115 * OCaml (ie. host) equivalents here.
117 let rec file_type mask mode = Int64.logand mode 0o170000L = mask
119 and is_socket mode = file_type 0o140000L mode
120 and is_symlink mode = file_type 0o120000L mode
121 and is_regular_file mode = file_type 0o100000L mode
122 and is_block mode = file_type 0o060000L mode
123 and is_directory mode = file_type 0o040000L mode
124 and is_char mode = file_type 0o020000L mode
125 and is_fifo mode = file_type 0o010000L mode
127 and is_suid mode = test_bit 0o4000L mode
128 and is_sgid mode = test_bit 0o2000L mode
129 and is_svtx mode = test_bit 0o1000L mode
131 and is_ru mode = test_bit 0o400L mode
132 and is_wu mode = test_bit 0o200L mode
133 and is_xu mode = test_bit 0o100L mode
134 and is_rg mode = test_bit 0o040L mode
135 and is_wg mode = test_bit 0o020L mode
136 and is_xg mode = test_bit 0o010L mode
137 and is_ro mode = test_bit 0o004L mode
138 and is_wo mode = test_bit 0o002L mode
139 and is_xo mode = test_bit 0o001L mode
141 and test_bit mask mode = Int64.logand mode mask = mask