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