[Libguestfs] [PATCH v3 3/4] mltools: Rename run_command std*_chan -> std*_fd.

Richard W.M. Jones rjones at redhat.com
Fri Aug 17 15:16:12 UTC 2018


These are file descriptors, not the high level OCaml in_channel/
out_channel type, so we would normally not refer to them as *_chan.

Just renaming, no functional change.
---
 builder/repository_main.ml          |  2 +-
 common/mltools/tools_utils.ml       | 16 ++++++++--------
 common/mltools/tools_utils.mli      |  4 ++--
 common/mltools/tools_utils_tests.ml |  4 ++--
 v2v/python_script.ml                |  4 ++--
 v2v/python_script.mli               |  2 +-
 6 files changed, 16 insertions(+), 16 deletions(-)

diff --git a/builder/repository_main.ml b/builder/repository_main.ml
index 43f749c5a..4ec434e57 100644
--- a/builder/repository_main.ml
+++ b/builder/repository_main.ml
@@ -152,7 +152,7 @@ let compress_to file outdir =
   let cmd = [ "xz"; "-f"; "--best"; "--block-size=16777216"; "-c"; file ] in
   let file_flags = [ Unix.O_WRONLY; Unix.O_CREAT; Unix.O_TRUNC; ] in
   let outfd = Unix.openfile outimg file_flags 0o666 in
-  let res = run_command cmd ~stdout_chan:outfd in
+  let res = run_command cmd ~stdout_fd:outfd in
   if res <> 0 then
     error (f_"‘xz’ command failed");
   outimg
diff --git a/common/mltools/tools_utils.ml b/common/mltools/tools_utils.ml
index 09f1bb544..cccc424fb 100644
--- a/common/mltools/tools_utils.ml
+++ b/common/mltools/tools_utils.ml
@@ -277,8 +277,8 @@ let rec run_commands ?(echo_cmd = true) cmds =
   let res = Array.make (List.length cmds) 0 in
   let pids =
     List.mapi (
-      fun i (args, stdout_chan, stderr_chan) ->
-        let run_res = do_run args ?stdout_chan ?stderr_chan in
+      fun i (args, stdout_fd, stderr_fd) ->
+        let run_res = do_run args ?stdout_fd ?stderr_fd in
         match run_res with
         | Either (pid, app, outfd, errfd) ->
           Some (i, pid, app, outfd, errfd)
@@ -304,8 +304,8 @@ let rec run_commands ?(echo_cmd = true) cmds =
   done;
   Array.to_list res
 
-and run_command ?(echo_cmd = true) ?stdout_chan ?stderr_chan args =
-  let run_res = do_run args ~echo_cmd ?stdout_chan ?stderr_chan in
+and run_command ?(echo_cmd = true) ?stdout_fd ?stderr_fd args =
+  let run_res = do_run args ~echo_cmd ?stdout_fd ?stderr_fd in
   match run_res with
   | Either (pid, app, outfd, errfd) ->
     let _, stat = Unix.waitpid [] pid in
@@ -313,7 +313,7 @@ and run_command ?(echo_cmd = true) ?stdout_chan ?stderr_chan args =
   | Or code ->
     code
 
-and do_run ?(echo_cmd = true) ?stdout_chan ?stderr_chan args =
+and do_run ?(echo_cmd = true) ?stdout_fd ?stderr_fd args =
   let app = List.hd args in
   let get_fd default = function
     | None ->
@@ -326,13 +326,13 @@ and do_run ?(echo_cmd = true) ?stdout_chan ?stderr_chan args =
     let app =
       if Filename.is_relative app then which app
       else (Unix.access app [Unix.X_OK]; app) in
-    let outfd = get_fd Unix.stdout stdout_chan in
-    let errfd = get_fd Unix.stderr stderr_chan in
+    let outfd = get_fd Unix.stdout stdout_fd in
+    let errfd = get_fd Unix.stderr stderr_fd in
     if echo_cmd then
       debug "%s" (stringify_args args);
     let pid = Unix.create_process app (Array.of_list args) Unix.stdin
                 outfd errfd in
-    Either (pid, app, stdout_chan, stderr_chan)
+    Either (pid, app, stdout_fd, stderr_fd)
   with
   | Executable_not_found _ ->
     Or 127
diff --git a/common/mltools/tools_utils.mli b/common/mltools/tools_utils.mli
index c9350d0f9..97cc4de08 100644
--- a/common/mltools/tools_utils.mli
+++ b/common/mltools/tools_utils.mli
@@ -98,10 +98,10 @@ val run_commands : ?echo_cmd:bool -> (string list * Unix.file_descr option * Uni
     [echo_cmd] specifies whether output the full command on verbose
     mode, and it's on by default. *)
 
-val run_command : ?echo_cmd:bool -> ?stdout_chan:Unix.file_descr -> ?stderr_chan:Unix.file_descr -> string list -> int
+val run_command : ?echo_cmd:bool -> ?stdout_fd:Unix.file_descr -> ?stderr_fd:Unix.file_descr -> string list -> int
 (** Run an external command without using a shell, and return its exit code.
 
-    If [stdout_chan] or [stderr_chan] is specified, the file descriptor
+    If [stdout_fd] or [stderr_fd] is specified, the file descriptor
     is automatically closed after executing the command.
 
     [echo_cmd] specifies whether output the full command on verbose
diff --git a/common/mltools/tools_utils_tests.ml b/common/mltools/tools_utils_tests.ml
index c1d65084a..490942310 100644
--- a/common/mltools/tools_utils_tests.ml
+++ b/common/mltools/tools_utils_tests.ml
@@ -94,14 +94,14 @@ let test_run_command ctx =
   assert_equal_int 0 (run_command ["true"]);
   begin
     let tmpfile, chan = bracket_tmpfile ctx in
-    let res = run_command ["echo"; "this is a test"] ~stdout_chan:(Unix.descr_of_out_channel chan) in
+    let res = run_command ["echo"; "this is a test"] ~stdout_fd:(Unix.descr_of_out_channel chan) in
     assert_equal_int 0 res;
     let content = read_whole_file tmpfile in
     assert_equal_string "this is a test\n" content
   end;
   begin
     let tmpfile, chan = bracket_tmpfile ctx in
-    let res = run_command ["ls"; "/this-directory-is-unlikely-to-exist"] ~stderr_chan:(Unix.descr_of_out_channel chan) in
+    let res = run_command ["ls"; "/this-directory-is-unlikely-to-exist"] ~stderr_fd:(Unix.descr_of_out_channel chan) in
     assert_equal_int 2 res;
     let content = read_whole_file tmpfile in
     assert_bool "test_run_commands/not-existing/content" (String.length content > 0)
diff --git a/v2v/python_script.ml b/v2v/python_script.ml
index 1c0f9660c..3c00c28f6 100644
--- a/v2v/python_script.ml
+++ b/v2v/python_script.ml
@@ -41,13 +41,13 @@ let create ?(name = "script.py") code =
   with_open_out path (fun chan -> output_string chan code);
   { tmpdir; path }
 
-let run_command ?echo_cmd ?stdout_chan ?stderr_chan
+let run_command ?echo_cmd ?stdout_fd ?stderr_fd
                 { tmpdir; path } params args =
   let param_file = tmpdir // sprintf "params%d.json" (unique ()) in
   with_open_out
     param_file
     (fun chan -> output_string chan (JSON.string_of_doc params));
-  Tools_utils.run_command ?echo_cmd ?stdout_chan ?stderr_chan
+  Tools_utils.run_command ?echo_cmd ?stdout_fd ?stderr_fd
                           (python :: path :: param_file :: args)
 
 let path { path } = path
diff --git a/v2v/python_script.mli b/v2v/python_script.mli
index cf137b142..fd20208bf 100644
--- a/v2v/python_script.mli
+++ b/v2v/python_script.mli
@@ -29,7 +29,7 @@ val create : ?name:string -> string -> script
     [Some_source.code] where [some_source.ml] is generated from
     the Python file by [v2v/embed.sh] (see also [v2v/Makefile.am]). *)
 
-val run_command : ?echo_cmd:bool -> ?stdout_chan:Unix.file_descr -> ?stderr_chan:Unix.file_descr -> script -> JSON.doc -> string list -> int
+val run_command : ?echo_cmd:bool -> ?stdout_fd:Unix.file_descr -> ?stderr_fd:Unix.file_descr -> script -> JSON.doc -> string list -> int
 (** [run_command script params args] is a wrapper around
     {!Tools_utils.run_command} which runs the Python script with the
     supplied list of JSON parameters and the list of extra arguments.
-- 
2.18.0




More information about the Libguestfs mailing list