Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(pkg): type safe fetch without checksum #10492

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/dune_pkg/fetch.ml
Original file line number Diff line number Diff line change
Expand Up @@ -323,3 +323,11 @@ let fetch ~unpack ~checksum ~target ~url:(url_loc, url) =
| `http -> fetch_curl ~unpack ~checksum ~target url
| _ -> fetch_others ~unpack ~checksum ~target url)
;;

let fetch_without_checksum ~unpack ~target ~url =
fetch ~unpack ~checksum:None ~url ~target
>>| function
| Ok () -> Ok ()
| Error (Checksum_mismatch _) -> assert false
| Error (Unavailable message) -> Error message
;;
16 changes: 11 additions & 5 deletions src/dune_pkg/fetch.mli
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,24 @@ type failure =
(** [fetch ~checksum ~target url] will fetch [url] into [target]. It will verify
the downloaded file against [checksum], unless it [checksum] is [None].

@raise Checksum_mismatch
When the downloaded file doesn't match the expected [checksum], this will
pass the actually computed checksum.
@raise Unavailable
When the file can't be retrieved, e.g. not available at the location. *)
return [Error (Checksum_mismatch _)] When the downloaded file doesn't match
the expected [checksum], this will pass the actually computed checksum.

return [Error (Unavailable _))] When the file can't be retrieved, e.g. not
available at the location. *)
val fetch
: unpack:bool
-> checksum:Checksum.t option
-> target:Path.t
-> url:Loc.t * OpamUrl.t
-> (unit, failure) result Fiber.t

val fetch_without_checksum
: unpack:bool
-> target:Path.t
-> url:Loc.t * OpamUrl.t
-> (unit, User_message.t option) result Fiber.t

val fetch_git
: Rev_store.t
-> target:Path.t
Expand Down
6 changes: 2 additions & 4 deletions src/dune_pkg/source.ml
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,10 @@ let fetch_and_hash_archive_cached =
fun (url_loc, url) ->
let open Fiber.O in
Single_run_file_cache.with_ cache ~key:(OpamUrl.to_string url) ~f:(fun target ->
Fetch.fetch ~unpack:false ~checksum:None ~target ~url:(url_loc, url))
Fetch.fetch_without_checksum ~unpack:false ~target ~url:(url_loc, url))
>>| function
| Ok target -> Some (Dune_digest.file target |> Checksum.of_dune_digest)
| Error (Checksum_mismatch _) ->
Code_error.raise "Checksum mismatch when no checksum was provided" []
| Error (Unavailable message_opt) ->
| Error message_opt ->
let message =
match message_opt with
| Some message -> message
Expand Down