Enable throbber animation.
[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 open ExtList
21
22 open Printf
23
24 let (+^) = Int64.add
25 let (-^) = Int64.sub
26 let ( *^ ) = Int64.mul
27 let (/^) = Int64.div
28
29 type ('a, 'b) either = Left of 'a | Right of 'b
30
31 let (//) = Filename.concat
32
33 let verbose = ref false
34 let set_verbose_flag () = verbose := true
35 let verbose () = !verbose
36
37 let debug fs =
38   let f str =
39     if verbose () then (
40       prerr_string Config.package;
41       prerr_string ": tid ";
42       prerr_string (string_of_int (Thread.id (Thread.self ())));
43       prerr_string ": ";
44       prerr_string str;
45       prerr_newline ()
46     )
47   in
48   ksprintf f fs
49
50 let failwith fs =
51   let f str =
52     if verbose () then (prerr_string str; prerr_newline ());
53     raise (Failure str)
54   in
55   ksprintf f fs
56
57 let trace = ref false
58 let set_trace_flag () = trace := true
59 let trace () = !trace
60
61 let connect_uri = ref None
62 let set_connect_uri conn = connect_uri := conn
63 let connect_uri () = !connect_uri
64
65 let utf8_rarrow = "\xe2\x86\x92"
66
67 let pretty_string_of_exn =
68   function
69   | Guestfs.Error str ->
70       "Libguestfs error",
71       sprintf "libguestfs reported an error:
72
73 %s
74
75 To get more information about libguestfs errors, run guestfs-browser
76 with the -x flag on the command line."
77         str
78
79   | Libvirt.Virterror err ->
80       "Libvirt error",
81       sprintf "libvirt reported an error:
82
83 %s
84
85 To get more information about libvirt errors, run guestfs-browser
86 from the command line like this:
87
88 LIBVIRT_DEBUG=1 guestfs-browser"
89         (Libvirt.Virterror.to_string err)
90
91   (* Add more exception types here as we come across them.  Last
92    * case below is the catch-all.
93    *)
94   | exn ->
95       let str = Printexc.to_string exn in
96       debug "pretty_string_of_exn: unhandled exception %s" str;
97       "Error", str
98
99 let human_size i =
100   if i < 1024L then
101     sprintf "%Ld" i
102   else if i < 1024L *^ 1024L then
103     sprintf "%.1f KB" (Int64.to_float i /. 1024.)
104   else if i < 1024L *^ 1024L *^ 1024L then
105     sprintf "%.1f MB" (Int64.to_float i /. 1024. /. 1024.)
106   else if i < 1024L *^ 1024L *^ 1024L *^ 1024L then
107     sprintf "%.1f GB" (Int64.to_float i /. 1024. /. 1024. /. 1024.)
108   else
109     sprintf "%.1f TB" (Int64.to_float i /. 1024. /. 1024. /. 1024. /. 1024.)
110
111 let human_size_1k i =
112   if i < 1024L then
113     sprintf "%Ld KB" i
114   else if i < 1024L *^ 1024L then
115     sprintf "%.1f MB" (Int64.to_float i /. 1024.)
116   else
117     sprintf "%.1f GB" (Int64.to_float i /. 1024. /. 1024.)
118
119 let unique = let i = ref 0 in fun () -> incr i; !i
120
121 let mklabel text =
122   (GMisc.label ~text () :> GObj.widget)
123
124 (* g_markup_escape is not bound by lablgtk2, but we want to provide
125  * extra protection for \0 characters appearing in the string
126  * anyway.
127  *)
128 let markup_escape name =
129   let f = function
130     | '&' -> "&amp;" | '<' -> "&lt;" | '>' -> "&gt;"
131     | '\000' -> "\\0"
132     | c -> String.make 1 c
133   in
134   String.replace_chars f name
135
136 let libguestfs_version_string () =
137   let g = new Guestfs.guestfs () in
138   let v = g#version () in
139   let s =
140     sprintf "%Ld.%Ld.%Ld%s"
141       v.Guestfs.major v.Guestfs.minor v.Guestfs.release v.Guestfs.extra in
142   g#close ();
143   s
144
145 let libvirt_version_string () =
146   let v = fst (Libvirt.get_version ()) in
147   sprintf "%d.%d.%d" (v / 1_000_000) ((v / 1_000) mod 1_000) (v mod 1_000)
148
149 (* File type tests.
150  *
151  * Note these have to be on Linux ABI modes.  We cannot use the
152  * OCaml (ie. host) equivalents here.
153  *)
154 let rec file_type mask mode = Int64.logand mode 0o170000L = mask
155
156 and is_socket mode =       file_type 0o140000L mode
157 and is_symlink mode =      file_type 0o120000L mode
158 and is_regular_file mode = file_type 0o100000L mode
159 and is_block mode =        file_type 0o060000L mode
160 and is_directory mode =    file_type 0o040000L mode
161 and is_char mode =         file_type 0o020000L mode
162 and is_fifo mode =         file_type 0o010000L mode
163
164 and is_suid mode =         test_bit 0o4000L mode
165 and is_sgid mode =         test_bit 0o2000L mode
166 and is_svtx mode =         test_bit 0o1000L mode
167
168 and is_ru mode =           test_bit 0o400L mode
169 and is_wu mode =           test_bit 0o200L mode
170 and is_xu mode =           test_bit 0o100L mode
171 and is_rg mode =           test_bit 0o040L mode
172 and is_wg mode =           test_bit 0o020L mode
173 and is_xg mode =           test_bit 0o010L mode
174 and is_ro mode =           test_bit 0o004L mode
175 and is_wo mode =           test_bit 0o002L mode
176 and is_xo mode =           test_bit 0o001L mode
177
178 and test_bit mask mode = Int64.logand mode mask = mask
179
180 let tmpdir () =
181   let chan = open_in "/dev/urandom" in
182   let data = String.create 16 in
183   really_input chan data 0 (String.length data);
184   close_in chan;
185   let data = Digest.to_hex (Digest.string data) in
186   (* Note this is secure, because if the name already exists, even as a
187    * symlink, mkdir(2) will fail.
188    *)
189   let tmpdir = Filename.temp_dir_name // sprintf "guestfsbrowser%s.tmp" data in
190   Unix.mkdir tmpdir 0o700;
191   at_exit
192     (fun () ->
193        let cmd = sprintf "rm -rf %s" (Filename.quote tmpdir) in
194        ignore (Sys.command cmd));
195   tmpdir
196
197 module CE = CamomileLibraryDefault.Camomile.CharEncoding
198 module UTF8 = CamomileLibraryDefault.Camomile.UTF8
199 module UChar = CamomileLibraryDefault.Camomile.UChar
200 let utf16le = CE.utf16le
201 let utf8 = CE.utf8
202 let recode = CE.recode_string ~in_enc:utf16le ~out_enc:utf8
203
204 let windows_string_to_utf8 str =
205   let str = recode str in
206
207   (* Windows strings include the final \0 so remove this if present. *)
208   let len = UTF8.length str in
209   if len > 0 && UChar.code (UTF8.get str (len-1)) = 0 then
210     String.sub str 0 (UTF8.last str)
211   else
212     str
213
214 (* Best effort convert hive value to printable string. *)
215 let rec printable_hivex_value ?split_long_lines t v =
216   let hex = reg_hex_of_string ?split_long_lines in
217   match t with
218   | Hivex.REG_NONE -> if v = "" then "" else hex v
219   | Hivex.REG_SZ ->
220       (try windows_string_to_utf8 v with _ -> hex v)
221   | Hivex.REG_EXPAND_SZ ->
222       (try windows_string_to_utf8 v with _ -> hex v)
223   | Hivex.REG_BINARY -> hex v
224   | Hivex.REG_DWORD ->
225       (bitmatch Bitstring.bitstring_of_string v with
226        | { i : 32 : littleendian } -> sprintf "%08lx" i
227        | { _ } -> hex v)
228   | Hivex.REG_DWORD_BIG_ENDIAN ->
229       (bitmatch Bitstring.bitstring_of_string v with
230        | { i : 32 : bigendian } -> sprintf "%08lx" i
231        | { _ } -> hex v)
232   | Hivex.REG_LINK -> hex v
233   | Hivex.REG_MULTI_SZ -> (* XXX should be better for this one *)
234       hex v
235   | Hivex.REG_RESOURCE_LIST -> hex v
236   | Hivex.REG_FULL_RESOURCE_DESCRIPTOR -> hex v
237   | Hivex.REG_RESOURCE_REQUIREMENTS_LIST -> hex v
238   | Hivex.REG_QWORD ->
239       (bitmatch Bitstring.bitstring_of_string v with
240        | { i : 64 : littleendian } -> sprintf "%016Lx" i
241        | { _ } -> hex v)
242   | Hivex.REG_UNKNOWN i32 -> hex v
243
244 (* Convert binary data to a hex string.  This includes line breaks. *)
245 and reg_hex_of_string ?(split_long_lines=false) v =
246   let vs = String.explode v in
247   let vs = List.mapi (
248     fun i c ->
249       sprintf "%s%02x"
250         (if split_long_lines && i mod 16 = 0 then "\n" else "")
251         (int_of_char c)
252   ) vs in
253   String.concat "," vs
254
255 let local_file_exists filename =
256   try Unix.access filename [Unix.F_OK]; true
257   with Unix.Unix_error _ -> false
258
259 let basename pathname =
260   let len = String.length pathname in
261   try
262     let i = String.rindex pathname '/' in
263     let r = String.sub pathname (i+1) (len-i-1) in
264     if r = "" then "root" else r
265   with
266     Not_found -> pathname
267
268 let extension pathname =
269   let len = String.length pathname in
270   try
271     let i = String.rindex pathname '.' in
272     let r = String.sub pathname i (len-i) in
273     r
274   with
275     Not_found -> ""