mllib, builder: add and use Checksums.of_string

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.
This commit is contained in:
Pino Toscano
2016-09-30 11:04:27 +02:00
parent 7a09db5e48
commit a58dff49c5
3 changed files with 18 additions and 2 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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. *)