X-Git-Url: http://git.annexia.org/?a=blobdiff_plain;f=utils.ml;h=02bd7a0dd4cd28d9e4a0ea583cc63c4fb794761e;hb=refs%2Ftags%2F0.1.1;hp=8f7ce3a87bf6ddf56668a42ab4d1e4ce6333ce43;hpb=bbfe03c47f1d7f03c3e6c0cab9e4f500f588c80a;p=guestfs-browser.git diff --git a/utils.ml b/utils.ml index 8f7ce3a..02bd7a0 100644 --- a/utils.ml +++ b/utils.ml @@ -16,17 +16,126 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *) -type ('a, 'b) choice = Either of 'a | Or of 'b +open ExtString -let verbose = ref true (* XXX settable *) +open Printf + +let (+^) = Int64.add +let (-^) = Int64.sub +let ( *^ ) = Int64.mul +let (/^) = Int64.div + +type ('a, 'b) either = Left of 'a | Right of 'b + +let (//) = Filename.concat + +let verbose = ref false +let set_verbose_flag () = verbose := true +let verbose () = !verbose let debug fs = - let f str = if !verbose then (prerr_string str; prerr_newline ()) in + let f str = + if verbose () then ( + prerr_string Config.package; + prerr_string ": tid "; + prerr_string (string_of_int (Thread.id (Thread.self ()))); + prerr_string ": "; + prerr_string str; + prerr_newline () + ) + in ksprintf f fs let failwith fs = let f str = - if !verbose then (prerr_string str; prerr_newline ()); + if verbose () then (prerr_string str; prerr_newline ()); raise (Failure str) in ksprintf f fs + +let trace = ref false +let set_trace_flag () = trace := true +let trace () = !trace + +let connect_uri = ref None +let set_connect_uri conn = connect_uri := conn +let connect_uri () = !connect_uri + +let utf8_rarrow = "\xe2\x86\x92" + +let human_size i = + if i < 1024L then + sprintf "%Ld" i + else if i < 1024L *^ 1024L then + sprintf "%.1f KB" (Int64.to_float i /. 1024.) + else if i < 1024L *^ 1024L *^ 1024L then + sprintf "%.1f MB" (Int64.to_float i /. 1024. /. 1024.) + else if i < 1024L *^ 1024L *^ 1024L *^ 1024L then + sprintf "%.1f GB" (Int64.to_float i /. 1024. /. 1024. /. 1024.) + else + sprintf "%.1f TB" (Int64.to_float i /. 1024. /. 1024. /. 1024. /. 1024.) + +let human_size_1k i = + if i < 1024L then + sprintf "%Ld KB" i + else if i < 1024L *^ 1024L then + sprintf "%.1f MB" (Int64.to_float i /. 1024.) + else + sprintf "%.1f GB" (Int64.to_float i /. 1024. /. 1024.) + +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. *) +let markup_escape name = + let f = function + | '&' -> "&" | '<' -> "<" | '>' -> ">" + | c -> String.make 1 c + in + String.replace_chars f name + +let libguestfs_version_string () = + let g = new Guestfs.guestfs () in + let v = g#version () in + let s = + sprintf "%Ld.%Ld.%Ld%s" + v.Guestfs.major v.Guestfs.minor v.Guestfs.release v.Guestfs.extra in + g#close (); + s + +let libvirt_version_string () = + let v = fst (Libvirt.get_version ()) in + sprintf "%d.%d.%d" (v / 1_000_000) ((v / 1_000) mod 1_000) (v mod 1_000) + +(* File type tests. + * + * Note these have to be on Linux ABI modes. We cannot use the + * OCaml (ie. host) equivalents here. + *) +let rec file_type mask mode = Int64.logand mode 0o170000L = mask + +and is_socket mode = file_type 0o140000L mode +and is_symlink mode = file_type 0o120000L mode +and is_regular_file mode = file_type 0o100000L mode +and is_block mode = file_type 0o060000L mode +and is_directory mode = file_type 0o040000L mode +and is_char mode = file_type 0o020000L mode +and is_fifo mode = file_type 0o010000L mode + +and is_suid mode = test_bit 0o4000L mode +and is_sgid mode = test_bit 0o2000L mode +and is_svtx mode = test_bit 0o1000L mode + +and is_ru mode = test_bit 0o400L mode +and is_wu mode = test_bit 0o200L mode +and is_xu mode = test_bit 0o100L mode +and is_rg mode = test_bit 0o040L mode +and is_wg mode = test_bit 0o020L mode +and is_xg mode = test_bit 0o010L mode +and is_ro mode = test_bit 0o004L mode +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