diff options
author | Alex Deymo <deymo@google.com> | 2017-10-26 16:19:33 +0200 |
---|---|---|
committer | Alex Deymo <deymo@google.com> | 2017-10-27 13:52:46 +0200 |
commit | 4dadd8b4439358585be374226357b35aece52f17 (patch) | |
tree | cd92dfcebf2e1598284bf47a2506afdaae8ddc6f /split_patch_writer.cc | |
parent | e4458302223554ed97c799d95d4197e042f6dfbb (diff) | |
download | bsdiff-4dadd8b4439358585be374226357b35aece52f17.tar.gz |
Pass the size of the new file to the PatchWriterInterface::Init()
Most patch formats include the size of the new file in the header.
To help streaming the patch to disk while generating it, this CL
passes the size of the new file to the patch writer on initialization.
To do this, we also move the Init() call to the patch writer to the
DiffEncoder, which makes more sense since the Close() call is also made
from the DiffEnconder.
Bug: None
Test: Updated tests to check for this value.
Change-Id: Idfaedbd492d68ab6e6cb2c1cb3883947f068c3aa
Diffstat (limited to 'split_patch_writer.cc')
-rw-r--r-- | split_patch_writer.cc | 19 |
1 files changed, 16 insertions, 3 deletions
diff --git a/split_patch_writer.cc b/split_patch_writer.cc index 4123440..3e0d6e7 100644 --- a/split_patch_writer.cc +++ b/split_patch_writer.cc @@ -12,12 +12,24 @@ using std::endl; namespace bsdiff { -bool SplitPatchWriter::Init() { +bool SplitPatchWriter::Init(size_t new_size) { + new_size_ = new_size; // Fail gracefully if re-initialized. if (current_patch_ || patches_.empty()) return false; - return patches_[0]->Init(); + size_t expected_patches = (new_size_ + new_chunk_size_ - 1) / new_chunk_size_; + if (expected_patches == 0) + expected_patches = 1; + if (expected_patches != patches_.size()) { + LOG(ERROR) << "Expected " << expected_patches << " for a new file of size " + << new_size_ << " split in chunks of " << new_chunk_size_ + << " but got " << patches_.size() << " instead." << endl; + return false; + } + + return patches_[0]->Init( + std::min(static_cast<uint64_t>(new_size_), new_chunk_size_)); } bool SplitPatchWriter::WriteDiffStream(const uint8_t* data, size_t size) { @@ -61,7 +73,8 @@ bool SplitPatchWriter::AddControlEntry(const ControlEntry& entry) { LOG(ERROR) << "Writing past the last patch" << endl; return false; } - if (!patches_[current_patch_]->Init()) { + if (!patches_[current_patch_]->Init(std::min( + new_size_ - current_patch_ * new_chunk_size_, new_chunk_size_))) { LOG(ERROR) << "Failed to initialize patch " << current_patch_ << endl; return false; } |