Blame SOURCES/0139-v2v-support-tar.gz-and-tar.xz-ova-files.patch

ffd6ed
From 54350043efed23af2770a9b5a4b19afb1abdf4e5 Mon Sep 17 00:00:00 2001
ffd6ed
From: Pino Toscano <ptoscano@redhat.com>
ffd6ed
Date: Wed, 15 Apr 2015 11:21:57 +0200
ffd6ed
Subject: [PATCH] v2v: support tar.gz and tar.xz ova files
ffd6ed
ffd6ed
When dealing with a ova detected as gzip of xz, uncompress few bytes of
ffd6ed
it to check whether it is a compressed tarball, and if so untar it.
ffd6ed
ffd6ed
Related to RHBZ#1186800.
ffd6ed
ffd6ed
(cherry picked from commit 3c582cfb8d62013a935953e919c79009452254f9)
ffd6ed
---
ffd6ed
 v2v/input_ova.ml | 48 ++++++++++++++++++++++++++++++++++++++++++------
ffd6ed
 1 file changed, 42 insertions(+), 6 deletions(-)
ffd6ed
ffd6ed
diff --git a/v2v/input_ova.ml b/v2v/input_ova.ml
ffd6ed
index 211db43..8079d28 100644
ffd6ed
--- a/v2v/input_ova.ml
ffd6ed
+++ b/v2v/input_ova.ml
ffd6ed
@@ -43,13 +43,32 @@ object
ffd6ed
        *)
ffd6ed
       if is_directory ova then ova
ffd6ed
       else (
ffd6ed
+        let uncompress_head zcat file =
ffd6ed
+          let cmd = sprintf "%s %s" zcat (quote file) in
ffd6ed
+          let chan_out, chan_in, chan_err = Unix.open_process_full cmd [||] in
ffd6ed
+          let buf = String.create 512 in
ffd6ed
+          let len = input chan_out buf 0 (String.length buf) in
ffd6ed
+          (* We're expecting the subprocess to fail because we close
ffd6ed
+           * the pipe early, so:
ffd6ed
+           *)
ffd6ed
+          ignore (Unix.close_process_full (chan_out, chan_in, chan_err));
ffd6ed
+
ffd6ed
+          let tmpfile, chan = Filename.open_temp_file ~temp_dir:tmpdir "ova.file." "" in
ffd6ed
+          output chan buf 0 len;
ffd6ed
+          close_out chan;
ffd6ed
+
ffd6ed
+          tmpfile in
ffd6ed
+
ffd6ed
+        let untar ?(format = "") file outdir =
ffd6ed
+          let cmd = sprintf "tar -x%sf %s -C %s" format (quote file) (quote outdir) in
ffd6ed
+          if verbose then printf "%s\n%!" cmd;
ffd6ed
+          if Sys.command cmd <> 0 then
ffd6ed
+            error (f_"error unpacking %s, see earlier error messages") ova in
ffd6ed
+
ffd6ed
         match detect_file_type ova with
ffd6ed
         | `Tar ->
ffd6ed
           (* Normal ovas are tar file (not compressed). *)
ffd6ed
-          let cmd = sprintf "tar -xf %s -C %s" (quote ova) (quote tmpdir) in
ffd6ed
-          if verbose then printf "%s\n%!" cmd;
ffd6ed
-          if Sys.command cmd <> 0 then
ffd6ed
-            error (f_"error unpacking %s, see earlier error messages") ova;
ffd6ed
+          untar ova tmpdir;
ffd6ed
           tmpdir
ffd6ed
         | `Zip ->
ffd6ed
           (* However, although not permitted by the spec, people ship
ffd6ed
@@ -62,8 +81,25 @@ object
ffd6ed
           if Sys.command cmd <> 0 then
ffd6ed
             error (f_"error unpacking %s, see earlier error messages") ova;
ffd6ed
           tmpdir
ffd6ed
-        | `GZip | `XZ | `Unknown ->
ffd6ed
-          error (f_"%s: unsupported file format\n\nFormats which we currently understand for '-i ova' are: uncompressed tar, zip") ova
ffd6ed
+        | (`GZip|`XZ) as format ->
ffd6ed
+          let zcat, tar_fmt =
ffd6ed
+            match format with
ffd6ed
+            | `GZip -> "zcat", "z"
ffd6ed
+            | `XZ -> "xzcat", "J"
ffd6ed
+            | _ -> assert false in
ffd6ed
+          let tmpfile = uncompress_head zcat ova in
ffd6ed
+          let tmpfiletype = detect_file_type tmpfile in
ffd6ed
+          (* Remove tmpfile from tmpdir, to leave it empty. *)
ffd6ed
+          Sys.remove tmpfile;
ffd6ed
+          (match tmpfiletype with
ffd6ed
+          | `Tar ->
ffd6ed
+            untar ~format:tar_fmt ova tmpdir;
ffd6ed
+            tmpdir
ffd6ed
+          | `Zip | `GZip | `XZ | `Unknown ->
ffd6ed
+            error (f_"%s: unsupported file format\n\nFormats which we currently understand for '-i ova' are: tar (uncompressed, compress with gzip or xz), zip") ova
ffd6ed
+          )
ffd6ed
+        | `Unknown ->
ffd6ed
+          error (f_"%s: unsupported file format\n\nFormats which we currently understand for '-i ova' are: tar (uncompressed, compress with gzip or xz), zip") ova
ffd6ed
       ) in
ffd6ed
 
ffd6ed
     (* Exploded path must be absolute (RHBZ#1155121). *)
ffd6ed
-- 
ffd6ed
1.8.3.1
ffd6ed