Version 0.1.2.
[guestfs-browser.git] / utils.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 open ExtString
20
21 open Printf
22
23 let (+^) = Int64.add
24 let (-^) = Int64.sub
25 let ( *^ ) = Int64.mul
26 let (/^) = Int64.div
27
28 type ('a, 'b) either = Left of 'a | Right of 'b
29
30 let (//) = Filename.concat
31
32 let verbose = ref false
33 let set_verbose_flag () = verbose := true
34 let verbose () = !verbose
35
36 let debug fs =
37   let f str =
38     if verbose () then (
39       prerr_string Config.package;
40       prerr_string ": tid ";
41       prerr_string (string_of_int (Thread.id (Thread.self ())));
42       prerr_string ": ";
43       prerr_string str;
44       prerr_newline ()
45     )
46   in
47   ksprintf f fs
48
49 let failwith fs =
50   let f str =
51     if verbose () then (prerr_string str; prerr_newline ());
52     raise (Failure str)
53   in
54   ksprintf f fs
55
56 let trace = ref false
57 let set_trace_flag () = trace := true
58 let trace () = !trace
59
60 let connect_uri = ref None
61 let set_connect_uri conn = connect_uri := conn
62 let connect_uri () = !connect_uri
63
64 let utf8_rarrow = "\xe2\x86\x92"
65
66 let human_size i =
67   if i < 1024L then
68     sprintf "%Ld" i
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.)
75   else
76     sprintf "%.1f TB" (Int64.to_float i /. 1024. /. 1024. /. 1024. /. 1024.)
77
78 let human_size_1k i =
79   if i < 1024L then
80     sprintf "%Ld KB" i
81   else if i < 1024L *^ 1024L then
82     sprintf "%.1f MB" (Int64.to_float i /. 1024.)
83   else
84     sprintf "%.1f GB" (Int64.to_float i /. 1024. /. 1024.)
85
86 let unique = let i = ref 0 in fun () -> incr i; !i
87
88 let mklabel text =
89   (GMisc.label ~text () :> GObj.widget)
90
91 (* g_markup_escape is not bound by lablgtk2, but we want to provide
92  * extra protection for \0 characters appearing in the string
93  * anyway.
94  *)
95 let markup_escape name =
96   let f = function
97     | '&' -> "&amp;" | '<' -> "&lt;" | '>' -> "&gt;"
98     | '\000' -> "\\0"
99     | c -> String.make 1 c
100   in
101   String.replace_chars f name
102
103 let libguestfs_version_string () =
104   let g = new Guestfs.guestfs () in
105   let v = g#version () in
106   let s =
107     sprintf "%Ld.%Ld.%Ld%s"
108       v.Guestfs.major v.Guestfs.minor v.Guestfs.release v.Guestfs.extra in
109   g#close ();
110   s
111
112 let libvirt_version_string () =
113   let v = fst (Libvirt.get_version ()) in
114   sprintf "%d.%d.%d" (v / 1_000_000) ((v / 1_000) mod 1_000) (v mod 1_000)
115
116 (* File type tests.
117  *
118  * Note these have to be on Linux ABI modes.  We cannot use the
119  * OCaml (ie. host) equivalents here.
120  *)
121 let rec file_type mask mode = Int64.logand mode 0o170000L = mask
122
123 and is_socket mode =       file_type 0o140000L mode
124 and is_symlink mode =      file_type 0o120000L mode
125 and is_regular_file mode = file_type 0o100000L mode
126 and is_block mode =        file_type 0o060000L mode
127 and is_directory mode =    file_type 0o040000L mode
128 and is_char mode =         file_type 0o020000L mode
129 and is_fifo mode =         file_type 0o010000L mode
130
131 and is_suid mode =         test_bit 0o4000L mode
132 and is_sgid mode =         test_bit 0o2000L mode
133 and is_svtx mode =         test_bit 0o1000L mode
134
135 and is_ru mode =           test_bit 0o400L mode
136 and is_wu mode =           test_bit 0o200L mode
137 and is_xu mode =           test_bit 0o100L mode
138 and is_rg mode =           test_bit 0o040L mode
139 and is_wg mode =           test_bit 0o020L mode
140 and is_xg mode =           test_bit 0o010L mode
141 and is_ro mode =           test_bit 0o004L mode
142 and is_wo mode =           test_bit 0o002L mode
143 and is_xo mode =           test_bit 0o001L mode
144
145 and test_bit mask mode = Int64.logand mode mask = mask
146
147 let tmpdir () =
148   let chan = open_in "/dev/urandom" in
149   let data = String.create 16 in
150   really_input chan data 0 (String.length data);
151   close_in chan;
152   let data = Digest.to_hex (Digest.string data) in
153   (* Note this is secure, because if the name already exists, even as a
154    * symlink, mkdir(2) will fail.
155    *)
156   let tmpdir = Filename.temp_dir_name // sprintf "febootstrap%s.tmp" data in
157   Unix.mkdir tmpdir 0o700;
158   at_exit
159     (fun () ->
160        let cmd = sprintf "rm -rf %s" (Filename.quote tmpdir) in
161        ignore (Sys.command cmd));
162   tmpdir
163
164 (* This would be so much simpler with ChriS's delimited
165  * overloading macro XXX
166  *)
167 let i32_of_string_le v =
168   let b0 = int_of_char (String.unsafe_get v 0) in
169   let b1 = int_of_char (String.unsafe_get v 1) in
170   let b2 = int_of_char (String.unsafe_get v 2) in
171   let b3 = Int32.of_int (int_of_char (String.unsafe_get v 3)) in
172   Int32.logor
173     (Int32.of_int (b0 lor (b1 lsl 8) lor (b2 lsl 16)))
174     (Int32.shift_left b3 24)
175
176 let i32_of_string_be v =
177   let b0 = Int32.of_int (int_of_char (String.unsafe_get v 0)) in
178   let b1 = int_of_char (String.unsafe_get v 1) in
179   let b2 = int_of_char (String.unsafe_get v 2) in
180   let b3 = int_of_char (String.unsafe_get v 3) in
181   Int32.logor
182     (Int32.of_int (b3 lor (b2 lsl 8) lor (b1 lsl 16)))
183     (Int32.shift_left b0 24)
184
185 let i64_of_string_le v =
186   let b0 = int_of_char (String.unsafe_get v 0) in
187   let b1 = int_of_char (String.unsafe_get v 1) in
188   let b2 = int_of_char (String.unsafe_get v 2) in
189   let b3 = Int64.of_int (int_of_char (String.unsafe_get v 3)) in
190   let b4 = Int64.of_int (int_of_char (String.unsafe_get v 4)) in
191   let b5 = Int64.of_int (int_of_char (String.unsafe_get v 5)) in
192   let b6 = Int64.of_int (int_of_char (String.unsafe_get v 6)) in
193   let b7 = Int64.of_int (int_of_char (String.unsafe_get v 7)) in
194   Int64.logor
195     (Int64.logor
196        (Int64.logor
197           (Int64.logor
198              (Int64.logor
199                 (Int64.of_int (b0 lor (b1 lsl 8) lor (b2 lsl 16)))
200                 (Int64.shift_left b3 24))
201              (Int64.shift_left b4 32))
202           (Int64.shift_left b5 40))
203        (Int64.shift_left b6 48))
204     (Int64.shift_left b7 56)