+(* 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
+
+let file_permissions_string mode =
+ let c =
+ if is_socket mode then 's'
+ else if is_symlink mode then 'l'
+ else if is_regular_file mode then '-'
+ else if is_block mode then 'b'
+ else if is_directory mode then 'd'
+ else if is_char mode then 'c'
+ else if is_fifo mode then 'p' else '?' in
+ let ru = if is_ru mode then 'r' else '-' in
+ let wu = if is_wu mode then 'w' else '-' in
+ let xu = if is_xu mode then 'x' else '-' in
+ let rg = if is_rg mode then 'r' else '-' in
+ let wg = if is_wg mode then 'w' else '-' in
+ let xg = if is_xg mode then 'x' else '-' in
+ let ro = if is_ro mode then 'r' else '-' in
+ let wo = if is_wo mode then 'w' else '-' in
+ let xo = if is_xo mode then 'x' else '-' in
+ let str = sprintf "%c%c%c%c%c%c%c%c%c%c" c ru wu xu rg wg xg ro wo xo in
+
+ let suid = is_suid mode in
+ let sgid = is_sgid mode in
+ let svtx = is_svtx mode in
+ let str = Bytes.of_string str in
+ if suid then Bytes.set str 3 's';
+ if sgid then Bytes.set str 6 's';
+ if svtx then Bytes.set str 9 't';
+
+ Bytes.to_string str
+
+let tmpdir () =
+ let chan = open_in "/dev/urandom" in
+ let data = Bytes.create 16 in
+ really_input chan data 0 (Bytes.length data);
+ close_in chan;
+ let data = Digest.to_hex (Digest.bytes data) in
+ (* Note this is secure, because if the name already exists, even as a
+ * symlink, mkdir(2) will fail.
+ *)
+ let tmpdir =
+ Filename.get_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 -> ""