Blame SOURCES/0002-mllib-Enhance-and-rename-detect_compression-function.patch

0d20ef
From c07dde3ade4465dc6de99fd0761cb2707fe65ac2 Mon Sep 17 00:00:00 2001
0d20ef
From: "Richard W.M. Jones" <rjones@redhat.com>
0d20ef
Date: Sat, 18 Oct 2014 18:41:01 +0100
0d20ef
Subject: [PATCH] mllib: Enhance and rename 'detect_compression' function so it
0d20ef
 can detect a few more file types.
0d20ef
0d20ef
(cherry picked from commit d8e26d0e4dc3de649cf81584e8c060becd2e4531)
0d20ef
---
0d20ef
 builder/builder.ml     |  6 +++++-
0d20ef
 mllib/common_utils.ml  | 29 ++++++++++++++++++++---------
0d20ef
 mllib/common_utils.mli |  6 ++----
0d20ef
 3 files changed, 27 insertions(+), 14 deletions(-)
0d20ef
0d20ef
diff --git a/builder/builder.ml b/builder/builder.ml
0d20ef
index 5195cfd..121c5fb 100644
0d20ef
--- a/builder/builder.ml
0d20ef
+++ b/builder/builder.ml
0d20ef
@@ -315,8 +315,12 @@ let main () =
0d20ef
       | None -> []
0d20ef
       | Some format -> [`Format, format] in
0d20ef
     let compression_tag =
0d20ef
-      match detect_compression template with
0d20ef
+      match detect_file_type template with
0d20ef
       | `XZ -> [ `XZ, "" ]
0d20ef
+      | `GZip | `Tar | `Zip ->
0d20ef
+        eprintf (f_"%s: input file (%s) has an unsupported type\n")
0d20ef
+          prog template;
0d20ef
+        exit 1
0d20ef
       | `Unknown -> [] in
0d20ef
     [ `Template, ""; `Filename, template; `Size, Int64.to_string size ] @
0d20ef
       format_tag @ compression_tag in
0d20ef
diff --git a/mllib/common_utils.ml b/mllib/common_utils.ml
0d20ef
index adfad3f..295981c 100644
0d20ef
--- a/mllib/common_utils.ml
0d20ef
+++ b/mllib/common_utils.ml
0d20ef
@@ -547,17 +547,28 @@ let rm_rf_only_files (g : Guestfs.guestfs) dir =
0d20ef
     List.iter g#rm files
0d20ef
   )
0d20ef
 
0d20ef
-(* Detect compression of a file.
0d20ef
- *
0d20ef
- * Only detects the formats we need in virt-builder so far.
0d20ef
- *)
0d20ef
-let detect_compression filename =
0d20ef
+(* Detect type of a file. *)
0d20ef
+let detect_file_type filename =
0d20ef
   let chan = open_in filename in
0d20ef
-  let buf = String.create 6 in
0d20ef
-  really_input chan buf 0 6;
0d20ef
+  let get start size =
0d20ef
+    try
0d20ef
+      seek_in chan start;
0d20ef
+      let buf = String.create size in
0d20ef
+      really_input chan buf 0 size;
0d20ef
+      Some buf
0d20ef
+    with End_of_file | Invalid_argument _ -> None
0d20ef
+  in
0d20ef
+  let ret =
0d20ef
+    if get 0 6 = Some "\2537zXZ\000" then `XZ
0d20ef
+    else if get 0 4 = Some "PK\003\004" then `Zip
0d20ef
+    else if get 0 4 = Some "PK\005\006" then `Zip
0d20ef
+    else if get 0 4 = Some "PK\007\008" then `Zip
0d20ef
+    else if get 257 6 = Some "ustar\000" then `Tar
0d20ef
+    else if get 257 8 = Some "ustar\x20\x20\000" then `Tar
0d20ef
+    else if get 0 2 = Some "\x1f\x8b" then `GZip
0d20ef
+    else `Unknown in
0d20ef
   close_in chan;
0d20ef
-  if buf = "\2537zXZ\000" then `XZ
0d20ef
-  else `Unknown
0d20ef
+  ret
0d20ef
 
0d20ef
 let is_block_device file =
0d20ef
   try (Unix.stat file).Unix.st_kind = Unix.S_BLK
0d20ef
diff --git a/mllib/common_utils.mli b/mllib/common_utils.mli
0d20ef
index d78fd70..112648a 100644
0d20ef
--- a/mllib/common_utils.mli
0d20ef
+++ b/mllib/common_utils.mli
0d20ef
@@ -118,10 +118,8 @@ val rm_rf_only_files : Guestfs.guestfs -> string -> unit
0d20ef
 
0d20ef
     XXX Could be faster with a specific API for doing this. *)
0d20ef
 
0d20ef
-val detect_compression : string -> [`Unknown | `XZ]
0d20ef
-(** Detect compression of a file.
0d20ef
-
0d20ef
-    XXX Only detects the formats we need in virt-builder so far. *)
0d20ef
+val detect_file_type : string -> [`GZip | `Tar | `XZ | `Zip | `Unknown]
0d20ef
+(** Detect type of a file. *)
0d20ef
 
0d20ef
 val is_block_device : string -> bool
0d20ef
 val is_char_device : string -> bool
0d20ef
-- 
0d20ef
1.8.3.1
0d20ef