mllib: factorize code to add Checksum.get_checksum function

Getting checksum involves the same code than verifying them. Create
a get_checksum function and use it in verify_checksum.
This commit is contained in:
Cédric Bosdonnat
2017-02-10 16:05:56 +01:00
committed by Richard W.M. Jones
parent 1230543f71
commit c45ce1a7bb
2 changed files with 24 additions and 10 deletions

View File

@@ -45,14 +45,14 @@ let of_string csum_type csum_value =
| "sha512" -> SHA512 csum_value
| _ -> invalid_arg csum_type
let verify_checksum csum ?tar filename =
let prog, csum_ref =
match csum with
| SHA1 c -> "sha1sum", c
| SHA256 c -> "sha256sum", c
| SHA512 c -> "sha512sum", c
let compute_checksum csum_type ?tar filename =
let prog =
match csum_type with
| "sha1" -> "sha1sum"
| "sha256" -> "sha256sum"
| "sha512" -> "sha512sum"
| _ -> error (f_"unhandled checksum type '%s'") csum_type
in
let cmd =
match tar with
| None ->
@@ -66,9 +66,14 @@ let verify_checksum csum ?tar filename =
| [] ->
error (f_"%s did not return any output") prog
| line :: _ ->
let csum_actual = fst (String.split " " line) in
if csum_ref <> csum_actual then
raise (Mismatched_checksum (csum, csum_actual))
let csum_str = fst (String.split " " line) in
of_string csum_type csum_str
let verify_checksum csum ?tar filename =
let csum_type = string_of_csum_t csum in
let csum_actual = compute_checksum csum_type ?tar filename in
if csum <> csum_actual then
raise (Mismatched_checksum (csum, (string_of_csum csum_actual)))
let verify_checksums checksums filename =
List.iter (fun c -> verify_checksum c filename) checksums

View File

@@ -43,3 +43,12 @@ val string_of_csum_t : csum_t -> string
val string_of_csum : csum_t -> string
(** Return a string representation of the checksum value. *)
val compute_checksum : string -> ?tar:string -> string -> csum_t
(** [compute_checksum type filename] Computes the checksum of the file.
The [type] is one the possible results of the [string_of_csum_t]
function.
When optional [tar] is used it is path to uncompressed tar archive
and the [filename] is a path in the tar archive. *)