From a37380a1049141b0b0545c48e15afffd91640f79 Mon Sep 17 00:00:00 2001 From: David Scott Date: Tue, 12 Oct 2010 12:09:10 +0100 Subject: [PATCH] CA-44731: Replace 'read_whole_file_to_string' with 'string_of_file' (which is now part of a related family: {string,buffer,bigbuffer}_of_file) Signed-off-by: David Scott --- netdev/netdev.ml | 2 +- stdext/forkhelpers.ml | 7 +++-- stdext/unixext.ml | 62 +++++++++++++++++-------------------------- stdext/unixext.mli | 21 +++++++++++---- tapctl/tapctl.ml | 2 +- 5 files changed, 45 insertions(+), 49 deletions(-) diff --git a/netdev/netdev.ml b/netdev/netdev.ml index 1f09409..26564ec 100644 --- a/netdev/netdev.ml +++ b/netdev/netdev.ml @@ -409,7 +409,7 @@ let get_bios_name name = let network_config_file = "/etc/xensource/network.conf" let network_backend = try - kind_of_string (String.strip String.isspace (Unixext.read_whole_file_to_string network_config_file)) + kind_of_string (String.strip String.isspace (Unixext.string_of_file network_config_file)) with | Unix.Unix_error(Unix.ENOENT, "open", _) -> Bridge | Unix.Unix_error(err, op, path) -> failwith (Printf.sprintf "Unix error: %s (%s,%s)\n" (Unix.error_message err) op path) diff --git a/stdext/forkhelpers.ml b/stdext/forkhelpers.ml index 952e5f4..d3e6e97 100644 --- a/stdext/forkhelpers.ml +++ b/stdext/forkhelpers.ml @@ -68,10 +68,9 @@ type 'a result = Success of string * 'a | Failure of string * exn let with_logfile_fd ?(delete = true) prefix f = let logfile = Filename.temp_file prefix ".log" in let read_logfile () = - let log_fd = Unix.openfile logfile [ Unix.O_RDONLY ] 0o0 in - finally - (fun () -> Unixext.read_whole_file 1024 1024 log_fd) - (fun () -> Unix.close log_fd; Unix.unlink logfile) in + let contents = Unixext.string_of_file logfile in + Unix.unlink logfile; + contents in let log_fd = Unix.openfile logfile [ Unix.O_WRONLY; Unix.O_CREAT ] 0o0 in try diff --git a/stdext/unixext.ml b/stdext/unixext.ml index 55b6a1c..3ac6864 100644 --- a/stdext/unixext.ml +++ b/stdext/unixext.ml @@ -112,19 +112,16 @@ let with_file file mode perms f = Unix.close fd; r -(** [file_blocks_fold block_size f start file_path] folds [f] over blocks (strings) - from the file [file_path] with initial value [start] *) -let file_blocks_fold block_size f start file_path = - with_file file_path [ Unix.O_RDONLY ] 0 - (fun fd -> - let block = String.create block_size in - let rec fold acc = - let n = Unix.read fd block 0 block_size in - (* Consider making the interface explicitly use Substrings *) - let s = if n = block_size then block else String.sub block 0 n in - if n = 0 then acc else fold (f acc s) in - fold start - ) +(** [fd_blocks_fold block_size f start fd] folds [f] over blocks (strings) + from the fd [fd] with initial value [start] *) +let fd_blocks_fold block_size f start fd = + let block = String.create block_size in + let rec fold acc = + let n = Unix.read fd block 0 block_size in + (* Consider making the interface explicitly use Substrings *) + let s = if n = block_size then block else String.sub block 0 n in + if n = 0 then acc else fold (f acc s) in + fold start let with_directory dir f = let dh = Unix.opendir dir in @@ -135,30 +132,19 @@ let with_directory dir f = Unix.closedir dh; r -let buffer_of_file file_path = - file_blocks_fold 1024 (fun b s -> Buffer.add_string b s; b) (Buffer.create 1024) file_path - -let bigbuffer_of_file file_path = - file_blocks_fold 1024 (fun b s -> Bigbuffer.append_string b s; b) (Bigbuffer.make ()) file_path - -(** Read whole file from specified fd *) -let read_whole_file size_hint block_size fd = - let filebuf = Buffer.create size_hint in - let blockbuf = String.create block_size in - let rec do_read() = - let nread = Unix.read fd blockbuf 0 block_size in - if nread=0 then - Buffer.contents filebuf - else - begin - Buffer.add_substring filebuf blockbuf 0 nread; - do_read() - end in - do_read() - -(** Read whole file into string *) -let read_whole_file_to_string fname = - with_file fname [ Unix.O_RDONLY ] 0o0 (read_whole_file 1024 1024) +let buffer_of_fd fd = + fd_blocks_fold 1024 (fun b s -> Buffer.add_string b s; b) (Buffer.create 1024) fd + +let bigbuffer_of_fd fd = + fd_blocks_fold 1024 (fun b s -> Bigbuffer.append_string b s; b) (Bigbuffer.make ()) fd + +let string_of_fd fd = Buffer.contents (buffer_of_fd fd) + +let buffer_of_file file_path = with_file file_path [ Unix.O_RDONLY ] 0 buffer_of_fd + +let bigbuffer_of_file file_path = with_file file_path [ Unix.O_RDONLY ] 0 bigbuffer_of_fd + +let string_of_file file_path = Buffer.contents (buffer_of_file file_path) (** Opens a temp file, applies the fd to the function, when the function completes, renames the file as required. *) @@ -286,7 +272,7 @@ let kill_and_wait ?(signal = Sys.sigterm) ?(timeout=10.) pid = let loop_time_waiting = 0.03 in let left = ref timeout in let readcmdline pid = - try read_whole_file_to_string (Printf.sprintf "/proc/%d/cmdline" pid) + try string_of_file (Printf.sprintf "/proc/%d/cmdline" pid) with _ -> "" in let reference = readcmdline pid and quit = ref false in diff --git a/stdext/unixext.mli b/stdext/unixext.mli index 47e68d0..f26d545 100644 --- a/stdext/unixext.mli +++ b/stdext/unixext.mli @@ -30,21 +30,32 @@ val file_lines_fold : ('a -> string -> 'a) -> 'a -> string -> 'a (** Applies function [f] to every line in the file at [file_path]. *) val file_lines_iter : (string -> unit) -> string -> unit -(** [file_blocks_fold block_size f start file_path] folds [f] over blocks (strings) - from the file [file_path] with initial value [start] *) -val file_blocks_fold: int -> ('a -> string -> 'a) -> 'a -> string -> 'a +(** [fd_blocks_fold block_size f start fd] folds [f] over blocks (strings) + from the fd [fd] with initial value [start] *) +val fd_blocks_fold: int -> ('a -> string -> 'a) -> 'a -> Unix.file_descr -> 'a (** Alias for function [file_lines_iter]. *) val readfile_line : (string -> 'a) -> string -> unit +(** [buffer_of_fd fd] returns a Buffer.t containing all data read from [fd] up to EOF *) +val buffer_of_fd : Unix.file_descr -> Buffer.t + +(** [bigbuffer_of_fd fd] returns a Bigbuffer.t containing all data read from [fd] up +to EOF *) +val bigbuffer_of_fd : Unix.file_descr -> Bigbuffer.t + +(** [string_of_fd fd] returns a string containing all data read from [fd] up to EOF *) +val string_of_fd : Unix.file_descr -> string + (** [buffer_of_file file] returns a Buffer.t containing the contents of [file] *) val buffer_of_file : string -> Buffer.t (** [bigbuffer_of_file file] returns a Bigbuffer.t containing the contents of [file] *) val bigbuffer_of_file : string -> Bigbuffer.t -val read_whole_file : int -> int -> Unix.file_descr -> string -val read_whole_file_to_string : string -> string +(** [string_of_file file] returns a string containing the contents of [file] *) +val string_of_file : string -> string + val atomic_write_to_file : string -> Unix.file_perm -> (Unix.file_descr -> 'a) -> 'a val write_string_to_file : string -> string -> unit val execv_get_output : string -> string array -> int * Unix.file_descr diff --git a/tapctl/tapctl.ml b/tapctl/tapctl.ml index 8cba526..df63f33 100644 --- a/tapctl/tapctl.ml +++ b/tapctl/tapctl.ml @@ -56,7 +56,7 @@ module Dummy = struct let get_dummy_tapdisk_list ctx = let filename = get_dummy_tapdisk_list_filename ctx in try - dummy_tap_list_of_rpc (Jsonrpc.of_string (Unixext.read_whole_file_to_string filename)) + dummy_tap_list_of_rpc (Jsonrpc.of_string (Unixext.string_of_file filename)) with _ -> [] let write_dummy_tapdisk_list ctx list = -- 2.39.5