From 1e522e2b392e8170c6ec00b72991302147205b7b Mon Sep 17 00:00:00 2001 From: Robur Date: Tue, 9 Jan 2024 14:48:53 +0000 Subject: [PATCH] builder_db_app: verify_data_dir: compute size and sha256 only once per artifact --- bin/builder_db_app.ml | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/bin/builder_db_app.ml b/bin/builder_db_app.ml index 53eeae5..99d9923 100644 --- a/bin/builder_db_app.ml +++ b/bin/builder_db_app.ml @@ -296,20 +296,23 @@ let verify_data_dir () datadir = let* () = Db.iter_s build_artifacts (fun (_job, _uuid, (_fpath, sha256, size)) -> progress (); - let abs_path = Fpath.(v datadir // artifact_path sha256) in - (match Bos.OS.File.read abs_path with - | Error (`Msg msg) -> Logs.err (fun m -> m "file %a not present: %s" Fpath.pp abs_path msg) - | Ok data -> - files_tracked := FpathSet.add (artifact_path sha256) !files_tracked; - let s = Int64.of_int (String.length data) in - if s <> size then Logs.err (fun m -> m "File %a has different size (in DB %Lu on disk %Lu)" Fpath.pp abs_path size s); - let sha256' = Mirage_crypto.Hash.SHA256.digest (Cstruct.of_string data) in - if not (Cstruct.equal sha256 sha256') then - Logs.err (fun m -> m "File %a has different hash (in DB %a on disk %a" - Fpath.pp abs_path - Hex.pp (Hex.of_cstruct sha256) - Hex.pp (Hex.of_cstruct sha256'))) ; - Ok () + if not (FpathSet.mem (artifact_path sha256) !files_tracked) then + let abs_path = Fpath.(v datadir // artifact_path sha256) in + (match Bos.OS.File.read abs_path with + | Error (`Msg msg) -> Logs.err (fun m -> m "file %a not present: %s" Fpath.pp abs_path msg) + | Ok data -> + files_tracked := FpathSet.add (artifact_path sha256) !files_tracked; + let s = Int64.of_int (String.length data) in + if s <> size then Logs.err (fun m -> m "File %a has different size (in DB %Lu on disk %Lu)" Fpath.pp abs_path size s); + let sha256' = Mirage_crypto.Hash.SHA256.digest (Cstruct.of_string data) in + if not (Cstruct.equal sha256 sha256') then + Logs.err (fun m -> m "File %a has different hash (in DB %a on disk %a)" + Fpath.pp abs_path + Hex.pp (Hex.of_cstruct sha256) + Hex.pp (Hex.of_cstruct sha256'))) ; + Ok () + else + Ok () ) () in Db.iter_s script_and_console (fun (job, uuid, console, script) ->