[Libguestfs] [PATCH 2/4] mllib, builder: add and use Checksums.of_string

Pino Toscano ptoscano at redhat.com
Fri Sep 30 09:31:00 UTC 2016


Add a simple way to turn a combination of checksum type and value into a
csum_t.  Use it in builder, even if still constrained.
---
 builder/simplestreams_parser.ml | 8 ++++++--
 mllib/checksums.ml              | 6 ++++++
 mllib/checksums.mli             | 6 ++++++
 3 files changed, 18 insertions(+), 2 deletions(-)

diff --git a/builder/simplestreams_parser.ml b/builder/simplestreams_parser.ml
index 13e0b5d..f7682cd 100644
--- a/builder/simplestreams_parser.ml
+++ b/builder/simplestreams_parser.ml
@@ -156,8 +156,12 @@ let get_index ~downloader ~sigchecker
               let checksums =
                 let checksums = object_find_objects (
                   function
-                  | ("sha256", Yajl_string c) -> Some (Checksums.SHA256 c)
-                  | ("sha512", Yajl_string c) -> Some (Checksums.SHA512 c)
+                  (* Since this catches all the keys, and not just
+                   * the ones related to checksums, explicitly filter
+                   * the supported checksums.
+                   *)
+                  | ("sha256"|"sha512" as t, Yajl_string c) ->
+                    Some (Checksums.of_string t c)
                   | _ -> None
                 ) disk_item in
                 match checksums with
diff --git a/mllib/checksums.ml b/mllib/checksums.ml
index 918a1c2..014e73e 100644
--- a/mllib/checksums.ml
+++ b/mllib/checksums.ml
@@ -35,6 +35,12 @@ let string_of_csum = function
   | SHA256 c -> c
   | SHA512 c -> c
 
+let of_string csum_type csum_value =
+  match String.lowercase_ascii csum_type with
+  | "sha256" -> SHA256 csum_value
+  | "sha512" -> SHA512 csum_value
+  | _ -> invalid_arg csum_type
+
 let verify_checksum csum filename =
   let prog, csum_ref =
     match csum with
diff --git a/mllib/checksums.mli b/mllib/checksums.mli
index 202bdd1..298d7df 100644
--- a/mllib/checksums.mli
+++ b/mllib/checksums.mli
@@ -22,6 +22,12 @@ type csum_t =
 
 exception Mismatched_checksum of (csum_t * string) (* expected checksum, got *)
 
+val of_string : string -> string -> csum_t
+(** [of_string type value] returns the [csum_t] for the specified
+    combination of checksum type and checksum value.
+
+    Raise [Invalid_argument] if the checksum type is not known. *)
+
 val verify_checksum : csum_t -> string -> unit
 (** Verify the checksum of the file. *)
 
-- 
2.7.4




More information about the Libguestfs mailing list