Restructure hash calculation in delta_performer
This patch moves DeltaPerformer::CalculateAndValidateHash to
fd_utils::ReadAndHashExtents and cleans up the code. It also adds
unittests for ReadAndHashExtents.
Bug: None
Test: unittest pass
Change-Id: I297cf79ef38a7495d5bcc0e6516a1ca783e505ea
Signed-off-by: Amin Hassani <ahassani@google.com>
diff --git a/payload_consumer/delta_performer.cc b/payload_consumer/delta_performer.cc
index 6540675..a392b40 100644
--- a/payload_consumer/delta_performer.cc
+++ b/payload_consumer/delta_performer.cc
@@ -1207,28 +1207,6 @@
return true;
}
-bool DeltaPerformer::CalculateAndValidateSourceHash(
- const InstallOperation& operation, ErrorCode* error) {
- const uint64_t kMaxBlocksToRead = 256; // 1MB if block size is 4KB
- auto total_blocks = utils::BlocksInExtents(operation.src_extents());
- brillo::Blob buf(std::min(kMaxBlocksToRead, total_blocks) * block_size_);
- DirectExtentReader reader;
- TEST_AND_RETURN_FALSE(
- reader.Init(source_fd_, operation.src_extents(), block_size_));
- HashCalculator source_hasher;
- while (total_blocks > 0) {
- auto read_blocks = std::min(total_blocks, kMaxBlocksToRead);
- TEST_AND_RETURN_FALSE(reader.Read(buf.data(), read_blocks * block_size_));
- TEST_AND_RETURN_FALSE(
- source_hasher.Update(buf.data(), read_blocks * block_size_));
- total_blocks -= read_blocks;
- }
- TEST_AND_RETURN_FALSE(source_hasher.Finalize());
- TEST_AND_RETURN_FALSE(ValidateSourceHash(
- source_hasher.raw_hash(), operation, source_fd_, error));
- return true;
-}
-
namespace {
class BsdiffExtentFile : public bsdiff::FileInterface {
@@ -1309,7 +1287,11 @@
TEST_AND_RETURN_FALSE(operation.dst_length() % block_size_ == 0);
if (operation.has_src_sha256_hash()) {
- TEST_AND_RETURN_FALSE(CalculateAndValidateSourceHash(operation, error));
+ brillo::Blob source_hash;
+ TEST_AND_RETURN_FALSE(fd_utils::ReadAndHashExtents(
+ source_fd_, operation.src_extents(), block_size_, &source_hash));
+ TEST_AND_RETURN_FALSE(
+ ValidateSourceHash(source_hash, operation, source_fd_, error));
}
auto reader = std::make_unique<DirectExtentReader>();
@@ -1422,7 +1404,11 @@
TEST_AND_RETURN_FALSE(buffer_.size() >= operation.data_length());
if (operation.has_src_sha256_hash()) {
- TEST_AND_RETURN_FALSE(CalculateAndValidateSourceHash(operation, error));
+ brillo::Blob source_hash;
+ TEST_AND_RETURN_FALSE(fd_utils::ReadAndHashExtents(
+ source_fd_, operation.src_extents(), block_size_, &source_hash));
+ TEST_AND_RETURN_FALSE(
+ ValidateSourceHash(source_hash, operation, source_fd_, error));
}
auto reader = std::make_unique<DirectExtentReader>();