X-Git-Url: http://git.annexia.org/?a=blobdiff_plain;f=utils.ml;h=3afcf99be5f1b5cb04c8d56cd3ea71af4424fa4f;hb=f53eb0cec9cd5272de5b0e9009b27231ed252487;hp=02bd7a0dd4cd28d9e4a0ea583cc63c4fb794761e;hpb=02614e74adee2a5d499bd557a6adde99a3c56e73;p=guestfs-browser.git diff --git a/utils.ml b/utils.ml index 02bd7a0..3afcf99 100644 --- a/utils.ml +++ b/utils.ml @@ -17,6 +17,7 @@ *) open ExtString +open ExtList open Printf @@ -63,6 +64,38 @@ let connect_uri () = !connect_uri let utf8_rarrow = "\xe2\x86\x92" +let pretty_string_of_exn = + function + | Guestfs.Error str -> + "Libguestfs error", + sprintf "libguestfs reported an error: + +%s + +To get more information about libguestfs errors, run guestfs-browser +with the -x flag on the command line." + str + + | Libvirt.Virterror err -> + "Libvirt error", + sprintf "libvirt reported an error: + +%s + +To get more information about libvirt errors, run guestfs-browser +from the command line like this: + +LIBVIRT_DEBUG=1 guestfs-browser" + (Libvirt.Virterror.to_string err) + + (* Add more exception types here as we come across them. Last + * case below is the catch-all. + *) + | exn -> + let str = Printexc.to_string exn in + debug "pretty_string_of_exn: unhandled exception %s" str; + "Error", str + let human_size i = if i < 1024L then sprintf "%Ld" i @@ -88,10 +121,14 @@ let unique = let i = ref 0 in fun () -> incr i; !i let mklabel text = (GMisc.label ~text () :> GObj.widget) -(* XXX No binding for g_markup_escape in lablgtk2. *) +(* g_markup_escape is not bound by lablgtk2, but we want to provide + * extra protection for \0 characters appearing in the string + * anyway. + *) let markup_escape name = let f = function | '&' -> "&" | '<' -> "<" | '>' -> ">" + | '\000' -> "\\0" | c -> String.make 1 c in String.replace_chars f name @@ -139,3 +176,100 @@ and is_wo mode = test_bit 0o002L mode and is_xo mode = test_bit 0o001L mode and test_bit mask mode = Int64.logand mode mask = mask + +let tmpdir () = + let chan = open_in "/dev/urandom" in + let data = String.create 16 in + really_input chan data 0 (String.length data); + close_in chan; + let data = Digest.to_hex (Digest.string data) in + (* Note this is secure, because if the name already exists, even as a + * symlink, mkdir(2) will fail. + *) + let tmpdir = Filename.temp_dir_name // sprintf "guestfsbrowser%s.tmp" data in + Unix.mkdir tmpdir 0o700; + at_exit + (fun () -> + let cmd = sprintf "rm -rf %s" (Filename.quote tmpdir) in + ignore (Sys.command cmd)); + tmpdir + +module CE = CamomileLibraryDefault.Camomile.CharEncoding +module UTF8 = CamomileLibraryDefault.Camomile.UTF8 +module UChar = CamomileLibraryDefault.Camomile.UChar +let utf16le = CE.utf16le +let utf8 = CE.utf8 +let recode = CE.recode_string ~in_enc:utf16le ~out_enc:utf8 + +let windows_string_to_utf8 str = + let str = recode str in + + (* Windows strings include the final \0 so remove this if present. *) + let len = UTF8.length str in + if len > 0 && UChar.code (UTF8.get str (len-1)) = 0 then + String.sub str 0 (UTF8.last str) + else + str + +(* Best effort convert hive value to printable string. *) +let rec printable_hivex_value ?split_long_lines t v = + let hex = reg_hex_of_string ?split_long_lines in + match t with + | Hivex.REG_NONE -> if v = "" then "" else hex v + | Hivex.REG_SZ -> + (try windows_string_to_utf8 v with _ -> hex v) + | Hivex.REG_EXPAND_SZ -> + (try windows_string_to_utf8 v with _ -> hex v) + | Hivex.REG_BINARY -> hex v + | Hivex.REG_DWORD -> + (bitmatch Bitstring.bitstring_of_string v with + | { i : 32 : littleendian } -> sprintf "%08lx" i + | { _ } -> hex v) + | Hivex.REG_DWORD_BIG_ENDIAN -> + (bitmatch Bitstring.bitstring_of_string v with + | { i : 32 : bigendian } -> sprintf "%08lx" i + | { _ } -> hex v) + | Hivex.REG_LINK -> hex v + | Hivex.REG_MULTI_SZ -> (* XXX should be better for this one *) + hex v + | Hivex.REG_RESOURCE_LIST -> hex v + | Hivex.REG_FULL_RESOURCE_DESCRIPTOR -> hex v + | Hivex.REG_RESOURCE_REQUIREMENTS_LIST -> hex v + | Hivex.REG_QWORD -> + (bitmatch Bitstring.bitstring_of_string v with + | { i : 64 : littleendian } -> sprintf "%016Lx" i + | { _ } -> hex v) + | Hivex.REG_UNKNOWN i32 -> hex v + +(* Convert binary data to a hex string. This includes line breaks. *) +and reg_hex_of_string ?(split_long_lines=false) v = + let vs = String.explode v in + let vs = List.mapi ( + fun i c -> + sprintf "%s%02x" + (if split_long_lines && i mod 16 = 0 then "\n" else "") + (int_of_char c) + ) vs in + String.concat "," vs + +let local_file_exists filename = + try Unix.access filename [Unix.F_OK]; true + with Unix.Unix_error _ -> false + +let basename pathname = + let len = String.length pathname in + try + let i = String.rindex pathname '/' in + let r = String.sub pathname (i+1) (len-i-1) in + if r = "" then "root" else r + with + Not_found -> pathname + +let extension pathname = + let len = String.length pathname in + try + let i = String.rindex pathname '.' in + let r = String.sub pathname i (len-i) in + r + with + Not_found -> ""