mirage / irmin

Irmin is a distributed database that follows the same design principles as Git
https://irmin.org
ISC License
1.84k stars 153 forks source link

irmin-pack: integrity check doesn't use ppf for results handling #2211

Closed metanivek closed 1 year ago

metanivek commented 1 year ago

We missed this when addressing #2170.

Here is a patch that NL shared that illustrates the problem. The handle_result function needs to use the supplied ppf when printing its "Ok"s. We should still use Printf when printing errors since no error is returned. Also, the patch needs to have a change to the run function to thread the ppf to handle_result.

diff --git a/src/irmin-pack/unix/checks.ml b/src/irmin-pack/unix/checks.ml
index 4e104c6891..8493f7b0fb 100644
--- a/src/irmin-pack/unix/checks.ml
+++ b/src/irmin-pack/unix/checks.ml
@@ -177,11 +177,22 @@ module Make (Store : Store) = struct
       Conf.init ~readonly:false ~fresh:false ~no_migrate:true ~indexing_strategy
         root

-    let handle_result ?name res =
+    let null =
+      match Sys.os_type with
+      | "Unix" | "Cygwin" -> "/dev/null"
+      | "Win32" -> "NUL"
+      | _ -> invalid_arg "invalid os type"
+
+    let set_ppf = function
+      | Some p -> p
+      | None -> open_out null |> Format.formatter_of_out_channel
+
+    let handle_result ?name ?ppf res =
       let name = match name with Some x -> x ^ ": " | None -> "" in
+      let ppf = set_ppf ppf in
       match res with
-      | Ok (`Fixed n) -> Printf.printf "%sOk -- fixed %d\n%!" name n
-      | Ok `No_error -> Printf.printf "%sOk\n%!" name
+      | Ok (`Fixed n) -> Fmt.pf ppf "%sOk -- fixed %d\n%!" name n
+      | Ok `No_error -> Fmt.pf ppf "%sOk\n%!" name
       | Error (`Cannot_fix x) ->
           Printf.eprintf "%sError -- cannot fix: %s\n%!" name x
       | Error (`Corrupted x) ->

   let check_minimal ?ppf ~pred ~iter ~check ~recompute_hash t =
diff --git a/src/irmin-pack/unix/checks_intf.ml b/src/irmin-pack/unix/checks_intf.ml
index 8f82c0a925..30aaa8c8eb 100644
--- a/src/irmin-pack/unix/checks_intf.ml
+++ b/src/irmin-pack/unix/checks_intf.ml
@@ -71,6 +71,7 @@ module type S = sig

     val handle_result :
       ?name:string ->
+      ?ppf:Format.formatter ->
       ( [< `Fixed of int | `No_error ],
         [< `Cannot_fix of string | `Corrupted of int ] )
       result ->