summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobert Newson <rnewson@apache.org>2023-05-06 14:28:26 +0100
committerRobert Newson <rnewson@apache.org>2023-05-06 14:36:57 +0100
commit2663bdc6bf3d7c512d6296026c11ef9d9f3c9465 (patch)
treeb4a31ccc42fa2a4b1808be0f83717951fcd26918
parent3cd5a17a603ee9ecdbaf20c055f6d4adccd92b47 (diff)
downloadcouchdb-2663bdc6bf3d7c512d6296026c11ef9d9f3c9465.tar.gz
consolidate checksum verification
-rw-r--r--src/couch/src/couch_file.erl33
1 files changed, 18 insertions, 15 deletions
diff --git a/src/couch/src/couch_file.erl b/src/couch/src/couch_file.erl
index f52417666..315f3a795 100644
--- a/src/couch/src/couch_file.erl
+++ b/src/couch/src/couch_file.erl
@@ -676,13 +676,7 @@ load_header(Fd, Pos, HeaderLen, RestBlock) ->
end,
<<Checksum:16/binary, HeaderBin/binary>> =
iolist_to_binary(remove_block_prefixes(?PREFIX_SIZE, RawBin)),
- case exxhash:xxhash128(HeaderBin) of
- Checksum ->
- ok;
- <<_/binary>> ->
- couch_stats:increment_counter([couch_file, old_checksums]),
- Checksum = couch_hash:md5_hash(HeaderBin)
- end,
+ true = verify_checksum(HeaderBin, Checksum),
{ok, HeaderBin}.
%% Read multiple block locations using a single file:pread/2.
@@ -861,16 +855,25 @@ monitored_by_pids() ->
verify_checksum(_Fd, _Pos, IoList, <<>>) ->
IoList;
verify_checksum(Fd, Pos, IoList, Checksum) ->
- case exxhash:xxhash128(iolist_to_binary(IoList)) of
- Checksum ->
+ case verify_checksum(IoList, Checksum) of
+ true ->
IoList;
- <<_/binary>> ->
- case couch_hash:md5_hash(IoList) of
- Checksum ->
+ false ->
+ report_checksum_error(Fd, Pos)
+ end.
+
+verify_checksum(Data, ExpectedChecksum) ->
+ Bin = iolist_to_binary(Data),
+ case ExpectedChecksum == exxhash:xxhash128(Bin) of
+ true ->
+ true;
+ false ->
+ case ExpectedChecksum == couch_hash:md5_hash(Data) of
+ true ->
couch_stats:increment_counter([couch_file, old_checksums]),
- IoList;
- _ ->
- report_checksum_error(Fd, Pos)
+ true;
+ false ->
+ false
end
end.