From f7acb14822f1a962a211c0d11488ffceadde2b68 Mon Sep 17 00:00:00 2001 From: Pino Toscano Date: Fri, 22 Mar 2019 12:59:11 +0100 Subject: [PATCH] common/mltools: allow fd for machine readable output Allow to specify a file descriptor for the machine readable output. Use the same assumption as done in v2v, i.e. that Unix.file_descr is simply the int file descriptor. (cherry picked from commit 70514dfaf1e45b5ad34f20f3297af9782099cf80) --- common/mltools/test-machine-readable.sh | 7 +++++++ common/mltools/tools_utils.ml | 11 ++++++++++- lib/guestfs.pod | 5 +++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/common/mltools/test-machine-readable.sh b/common/mltools/test-machine-readable.sh index 1162c58e9..824460e6d 100755 --- a/common/mltools/test-machine-readable.sh +++ b/common/mltools/test-machine-readable.sh @@ -65,3 +65,10 @@ test $($t --machine-readable=stream:stdout |& wc -l) -eq 3 # Output "stream:stderr". $t --machine-readable=stream:stderr 2>&1 >/dev/null | grep 'machine-readable' test $($t --machine-readable=stream:stderr 2>&1 >/dev/null | wc -l) -eq 2 + +# Output "fd:". +fn="$tmpdir/fdfile" +exec 4>"$fn" +$t --machine-readable=fd:4 +exec 4>&- +test $(cat "$fn" | wc -l) -eq 1 diff --git a/common/mltools/tools_utils.ml b/common/mltools/tools_utils.ml index ade4cb37f..35478f39e 100644 --- a/common/mltools/tools_utils.ml +++ b/common/mltools/tools_utils.ml @@ -41,6 +41,7 @@ type machine_readable_output_type = | NoOutput | Channel of out_channel | File of string + | Fd of int let machine_readable_output = ref NoOutput let machine_readable_channel = ref None let machine_readable () = @@ -50,7 +51,10 @@ let machine_readable () = match !machine_readable_output with | NoOutput -> None | Channel chan -> Some chan - | File f -> Some (open_out f) in + | File f -> Some (open_out f) + | Fd fd -> + (* Note that Unix.file_descr is really just an int. *) + Some (Unix.out_channel_of_descr (Obj.magic fd)) in machine_readable_channel := chan ); !machine_readable_channel @@ -296,6 +300,11 @@ let create_standard_options argspec ?anon_fun ?(key_opts = false) ?(machine_read | n -> error (f_"invalid output stream for --machine-readable: %s") fmt in machine_readable_output := Channel chan + | "fd" -> + (try + machine_readable_output := Fd (int_of_string outname) + with Failure _ -> + error (f_"invalid output fd for --machine-readable: %s") fmt) | n -> error (f_"invalid output for --machine-readable: %s") fmt ) diff --git a/lib/guestfs.pod b/lib/guestfs.pod index 53cece2da..f11028466 100644 --- a/lib/guestfs.pod +++ b/lib/guestfs.pod @@ -3287,6 +3287,11 @@ The possible values are: =over 4 +=item BI + +The output goes to the specified I, which is a file descriptor +already opened for writing. + =item BF The output goes to the specified F. -- 2.18.4