Blame SOURCES/0140-v2v-use-.ovf-and-.mf-files-anywhere-within-ova-files.patch

ffd6ed
From 39536e75adc3444fe77952be8fdc978271dbb2eb Mon Sep 17 00:00:00 2001
ffd6ed
From: Pino Toscano <ptoscano@redhat.com>
ffd6ed
Date: Wed, 15 Apr 2015 13:22:13 +0200
ffd6ed
Subject: [PATCH] v2v: use .ovf and .mf files anywhere within ova files
ffd6ed
ffd6ed
Do not rely on .ovf and .mf files being in the top-level of the ova
ffd6ed
archive, but search them anywhere within the content of the ova.
ffd6ed
ffd6ed
This also changes the result of the search of the .ovf file: previously,
ffd6ed
one (random) file was picked in case there were more than one, while now
ffd6ed
this situation triggers an error.
ffd6ed
ffd6ed
Related to RHBZ#1186800.
ffd6ed
ffd6ed
(cherry picked from commit 8049474636d1a56cb259af6e527fb6b0a42e43e1)
ffd6ed
---
ffd6ed
 v2v/input_ova.ml | 91 +++++++++++++++++++++++++++++++++-----------------------
ffd6ed
 1 file changed, 53 insertions(+), 38 deletions(-)
ffd6ed
ffd6ed
diff --git a/v2v/input_ova.ml b/v2v/input_ova.ml
ffd6ed
index 8079d28..f530b92 100644
ffd6ed
--- a/v2v/input_ova.ml
ffd6ed
+++ b/v2v/input_ova.ml
ffd6ed
@@ -107,51 +107,66 @@ object
ffd6ed
       if not (Filename.is_relative exploded) then exploded
ffd6ed
       else Sys.getcwd () // exploded in
ffd6ed
 
ffd6ed
-    let files = Sys.readdir exploded in
ffd6ed
-    let ovf = ref "" in
ffd6ed
+    (* Find files in [dir] ending with [ext]. *)
ffd6ed
+    let find_files dir ext =
ffd6ed
+      let rec loop = function
ffd6ed
+        | [] -> []
ffd6ed
+        | dir :: rest ->
ffd6ed
+          let files = Array.to_list (Sys.readdir dir) in
ffd6ed
+          let files = List.map (Filename.concat dir) files in
ffd6ed
+          let dirs, files = List.partition Sys.is_directory files in
ffd6ed
+          let files = List.filter (
ffd6ed
+            fun x ->
ffd6ed
+              Filename.check_suffix x ext
ffd6ed
+          ) files in
ffd6ed
+          files @ loop (rest @ dirs)
ffd6ed
+      in
ffd6ed
+      loop [dir]
ffd6ed
+    in
ffd6ed
+
ffd6ed
     (* Search for the ovf file. *)
ffd6ed
-    Array.iter (
ffd6ed
-      fun file ->
ffd6ed
-        if Filename.check_suffix file ".ovf" then ovf := file
ffd6ed
-    ) files;
ffd6ed
-    let ovf = !ovf in
ffd6ed
-    if ovf = "" then
ffd6ed
-      error (f_"no .ovf file was found in %s") ova;
ffd6ed
+    let ovf = find_files exploded ".ovf" in
ffd6ed
+    let ovf =
ffd6ed
+      match ovf with
ffd6ed
+      | [] ->
ffd6ed
+        error (f_"no .ovf file was found in %s") ova
ffd6ed
+      | [x] -> x
ffd6ed
+      | _ :: _ ->
ffd6ed
+        error (f_"more than one .ovf file was found in %s") ova in
ffd6ed
 
ffd6ed
     (* Read any .mf (manifest) files and verify sha1. *)
ffd6ed
+    let mf = find_files exploded ".mf" in
ffd6ed
     let rex = Str.regexp "SHA1(\\(.*\\))=\\([0-9a-fA-F]+\\)\r?" in
ffd6ed
-    Array.iter (
ffd6ed
+    List.iter (
ffd6ed
       fun mf ->
ffd6ed
-        if Filename.check_suffix mf ".mf" then (
ffd6ed
-          let chan = open_in (exploded // mf) in
ffd6ed
-          let rec loop () =
ffd6ed
-            let line = input_line chan in
ffd6ed
-            if Str.string_match rex line 0 then (
ffd6ed
-              let disk = Str.matched_group 1 line in
ffd6ed
-              let expected = Str.matched_group 2 line in
ffd6ed
-              let cmd = sprintf "sha1sum %s" (quote (exploded // disk)) in
ffd6ed
-              let out = external_command ~prog cmd in
ffd6ed
-              match out with
ffd6ed
-              | [] ->
ffd6ed
-                error (f_"no output from sha1sum command, see previous errors")
ffd6ed
-              | [line] ->
ffd6ed
-                let actual, _ = string_split " " line in
ffd6ed
-                if actual <> expected then
ffd6ed
-                  error (f_"checksum of disk %s does not match manifest %s (actual sha1(%s) = %s, expected sha1 (%s) = %s)")
ffd6ed
-                    disk mf disk actual disk expected;
ffd6ed
-                if verbose then
ffd6ed
-                  printf "sha1 of %s matches expected checksum %s\n%!"
ffd6ed
-                    disk expected
ffd6ed
-              | _::_ -> error (f_"cannot parse output of sha1sum command")
ffd6ed
-            )
ffd6ed
-          in
ffd6ed
-          (try loop () with End_of_file -> ());
ffd6ed
-          close_in chan
ffd6ed
-        )
ffd6ed
-    ) files;
ffd6ed
+        let chan = open_in mf in
ffd6ed
+        let rec loop () =
ffd6ed
+          let line = input_line chan in
ffd6ed
+          if Str.string_match rex line 0 then (
ffd6ed
+            let disk = Str.matched_group 1 line in
ffd6ed
+            let expected = Str.matched_group 2 line in
ffd6ed
+            let cmd = sprintf "sha1sum %s" (quote (exploded // disk)) in
ffd6ed
+            let out = external_command ~prog cmd in
ffd6ed
+            match out with
ffd6ed
+            | [] ->
ffd6ed
+              error (f_"no output from sha1sum command, see previous errors")
ffd6ed
+            | [line] ->
ffd6ed
+              let actual, _ = string_split " " line in
ffd6ed
+              if actual <> expected then
ffd6ed
+                error (f_"checksum of disk %s does not match manifest %s (actual sha1(%s) = %s, expected sha1 (%s) = %s)")
ffd6ed
+                  disk mf disk actual disk expected;
ffd6ed
+              if verbose then
ffd6ed
+                printf "sha1 of %s matches expected checksum %s\n%!"
ffd6ed
+                  disk expected
ffd6ed
+            | _::_ -> error (f_"cannot parse output of sha1sum command")
ffd6ed
+          )
ffd6ed
+        in
ffd6ed
+        (try loop () with End_of_file -> ());
ffd6ed
+        close_in chan
ffd6ed
+    ) mf;
ffd6ed
 
ffd6ed
     (* Parse the ovf file. *)
ffd6ed
-    let xml = read_whole_file (exploded // ovf) in
ffd6ed
+    let xml = read_whole_file ovf in
ffd6ed
     let doc = Xml.parse_memory xml in
ffd6ed
 
ffd6ed
     (* Handle namespaces. *)
ffd6ed
-- 
ffd6ed
1.8.3.1
ffd6ed