mrc0mmand / rpms / libguestfs

Forked from rpms/libguestfs 3 years ago
Clone

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

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