Use bitstring, enable display of symlinks on NTFS.
[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 CamomileLibrary
21 open Default.Camomile
22
23 open Printf
24
25 let (+^) = Int64.add
26 let (-^) = Int64.sub
27 let ( *^ ) = Int64.mul
28 let (/^) = Int64.div
29
30 type ('a, 'b) either = Left of 'a | Right of 'b
31
32 let (//) = Filename.concat
33
34 let verbose = ref false
35 let set_verbose_flag () = verbose := true
36 let verbose () = !verbose
37
38 let debug fs =
39   let f str =
40     if verbose () then (
41       prerr_string Config.package;
42       prerr_string ": tid ";
43       prerr_string (string_of_int (Thread.id (Thread.self ())));
44       prerr_string ": ";
45       prerr_string str;
46       prerr_newline ()
47     )
48   in
49   ksprintf f fs
50
51 let failwith fs =
52   let f str =
53     if verbose () then (prerr_string str; prerr_newline ());
54     raise (Failure str)
55   in
56   ksprintf f fs
57
58 let trace = ref false
59 let set_trace_flag () = trace := true
60 let trace () = !trace
61
62 let connect_uri = ref None
63 let set_connect_uri conn = connect_uri := conn
64 let connect_uri () = !connect_uri
65
66 let utf8_rarrow = "\xe2\x86\x92"
67
68 let human_size i =
69   if i < 1024L then
70     sprintf "%Ld" i
71   else if i < 1024L *^ 1024L then
72     sprintf "%.1f KB" (Int64.to_float i /. 1024.)
73   else if i < 1024L *^ 1024L *^ 1024L then
74     sprintf "%.1f MB" (Int64.to_float i /. 1024. /. 1024.)
75   else if i < 1024L *^ 1024L *^ 1024L *^ 1024L then
76     sprintf "%.1f GB" (Int64.to_float i /. 1024. /. 1024. /. 1024.)
77   else
78     sprintf "%.1f TB" (Int64.to_float i /. 1024. /. 1024. /. 1024. /. 1024.)
79
80 let human_size_1k i =
81   if i < 1024L then
82     sprintf "%Ld KB" i
83   else if i < 1024L *^ 1024L then
84     sprintf "%.1f MB" (Int64.to_float i /. 1024.)
85   else
86     sprintf "%.1f GB" (Int64.to_float i /. 1024. /. 1024.)
87
88 let unique = let i = ref 0 in fun () -> incr i; !i
89
90 let mklabel text =
91   (GMisc.label ~text () :> GObj.widget)
92
93 (* g_markup_escape is not bound by lablgtk2, but we want to provide
94  * extra protection for \0 characters appearing in the string
95  * anyway.
96  *)
97 let markup_escape name =
98   let f = function
99     | '&' -> "&amp;" | '<' -> "&lt;" | '>' -> "&gt;"
100     | '\000' -> "\\0"
101     | c -> String.make 1 c
102   in
103   String.replace_chars f name
104
105 let libguestfs_version_string () =
106   let g = new Guestfs.guestfs () in
107   let v = g#version () in
108   let s =
109     sprintf "%Ld.%Ld.%Ld%s"
110       v.Guestfs.major v.Guestfs.minor v.Guestfs.release v.Guestfs.extra in
111   g#close ();
112   s
113
114 let libvirt_version_string () =
115   let v = fst (Libvirt.get_version ()) in
116   sprintf "%d.%d.%d" (v / 1_000_000) ((v / 1_000) mod 1_000) (v mod 1_000)
117
118 (* File type tests.
119  *
120  * Note these have to be on Linux ABI modes.  We cannot use the
121  * OCaml (ie. host) equivalents here.
122  *)
123 let rec file_type mask mode = Int64.logand mode 0o170000L = mask
124
125 and is_socket mode =       file_type 0o140000L mode
126 and is_symlink mode =      file_type 0o120000L mode
127 and is_regular_file mode = file_type 0o100000L mode
128 and is_block mode =        file_type 0o060000L mode
129 and is_directory mode =    file_type 0o040000L mode
130 and is_char mode =         file_type 0o020000L mode
131 and is_fifo mode =         file_type 0o010000L mode
132
133 and is_suid mode =         test_bit 0o4000L mode
134 and is_sgid mode =         test_bit 0o2000L mode
135 and is_svtx mode =         test_bit 0o1000L mode
136
137 and is_ru mode =           test_bit 0o400L mode
138 and is_wu mode =           test_bit 0o200L mode
139 and is_xu mode =           test_bit 0o100L mode
140 and is_rg mode =           test_bit 0o040L mode
141 and is_wg mode =           test_bit 0o020L mode
142 and is_xg mode =           test_bit 0o010L mode
143 and is_ro mode =           test_bit 0o004L mode
144 and is_wo mode =           test_bit 0o002L mode
145 and is_xo mode =           test_bit 0o001L mode
146
147 and test_bit mask mode = Int64.logand mode mask = mask
148
149 let tmpdir () =
150   let chan = open_in "/dev/urandom" in
151   let data = String.create 16 in
152   really_input chan data 0 (String.length data);
153   close_in chan;
154   let data = Digest.to_hex (Digest.string data) in
155   (* Note this is secure, because if the name already exists, even as a
156    * symlink, mkdir(2) will fail.
157    *)
158   let tmpdir = Filename.temp_dir_name // sprintf "febootstrap%s.tmp" data in
159   Unix.mkdir tmpdir 0o700;
160   at_exit
161     (fun () ->
162        let cmd = sprintf "rm -rf %s" (Filename.quote tmpdir) in
163        ignore (Sys.command cmd));
164   tmpdir
165
166 let utf16le = CharEncoding.utf16le
167 let utf8 = CharEncoding.utf8
168 let recode = CharEncoding.recode_string ~in_enc:utf16le ~out_enc:utf8
169
170 let windows_string_to_utf8 str =
171   let str = recode str in
172
173   (* Windows strings include the final \0 so remove this if present. *)
174   let len = UTF8.length str in
175   if len > 0 && UChar.code (UTF8.get str (len-1)) = 0 then
176     String.sub str 0 (UTF8.last str)
177   else
178     str