]> xenbits.xensource.com Git - xcp/xen-api-libs.git/commitdiff
CA-44731: Replace 'read_whole_file_to_string' with 'string_of_file' (which is now...
authorDavid Scott <dave.scott@eu.citrix.com>
Tue, 12 Oct 2010 11:09:10 +0000 (12:09 +0100)
committerDavid Scott <dave.scott@eu.citrix.com>
Tue, 12 Oct 2010 11:09:10 +0000 (12:09 +0100)
Signed-off-by: David Scott <dave.scott@eu.citrix.com>
netdev/netdev.ml
stdext/forkhelpers.ml
stdext/unixext.ml
stdext/unixext.mli
tapctl/tapctl.ml

index 1f09409ece96cb7c6ba0836e40a8a1f8c9d33830..26564ec6fcb5cf6aebae1b72126329678c5dbbe7 100644 (file)
@@ -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)
index 952e5f42ee048b824aedc3b94714f6c0e3cbee40..d3e6e97eaeecc13681480527130558afdd1426f9 100644 (file)
@@ -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
index 55b6a1cfeb61bb608a6a6a6fef926017bae98b1d..3ac6864c0057cef052dfc4cb304d16516a35e3ae 100644 (file)
@@ -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
index 47e68d0dcc18b8c63e786de9fc93cfb2aa53eb25..f26d5453e5c4466049293e24cf4139720281ee0d 100644 (file)
@@ -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
index 8cba5267b97568d2b9c53d1eee1a4ef0a1a7d5df..df63f3349de73c2c2778f2af31a66a09524d72ef 100644 (file)
@@ -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 =