Blame SOURCES/0014-common-mltools-allow-fd-for-machine-readable-output.patch

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