*)
let sanitize_data data =
- let data = Bytes.copy data in
- for i = 0 to Bytes.length data - 1
- do
- if Bytes.get data i = '\000' then
- Bytes.set data i ' '
- done;
- String.escaped (Bytes.unsafe_to_string data)
+ let data = String.init
+ (String.length data)
+ (fun i -> let c = data.[i] in if c = '\000' then ' ' else c)
+ in
+ String.escaped data
let activate_access_log = ref true
let access_log_destination = ref (File (Paths.xen_log_dir ^ "/xenstored-access.log"))
let date = string_of_date() in
let tid = string_of_tid ~con tid in
let access_type = string_of_access_type access_type in
- (* we can use unsafe_of_string here as the sanitize_data function
- immediately makes a copy of the data and operates on that. *)
- let data = sanitize_data (Bytes.unsafe_of_string data) in
+ let data = sanitize_data data in
let prefix = prefix !access_log_destination date in
let msg = Printf.sprintf "%s %s %s %s" prefix tid access_type data in
logger.write ~level msg)
let pid = Unix.getpid () in
let buf = string_of_int pid ^ "\n" in
let len = String.length buf in
- if Unix.write fd (Bytes.unsafe_of_string buf) 0 len <> len
+ if Unix.write_substring fd buf 0 len <> len
then failwith "pidfile_write failed";
)
(fun () -> Unix.close fd)
let hexify s =
let hexseq_of_char c = sprintf "%02x" (Char.code c) in
let hs = Bytes.create (String.length s * 2) in
- for i = 0 to String.length s - 1
- do
- let seq = hexseq_of_char s.[i] in
+ String.iteri (fun i c ->
+ let seq = hexseq_of_char c in
Bytes.set hs (i * 2) seq.[0];
Bytes.set hs (i * 2 + 1) seq.[1];
- done;
+ ) s;
Bytes.unsafe_to_string hs
let unhexify hs =
let read_file_single_integer filename =
let fd = Unix.openfile filename [ Unix.O_RDONLY ] 0o640 in
- let buf = Bytes.make 20 (char_of_int 0) in
+ let buf = Bytes.make 20 '\000' in
let sz = Unix.read fd buf 0 20 in
Unix.close fd;
int_of_string (Bytes.sub_string buf 0 sz)