mrc0mmand / rpms / libguestfs

Forked from rpms/libguestfs 3 years ago
Clone

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

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