aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'applypatch/imgdiff.cpp')
-rw-r--r--applypatch/imgdiff.cpp45
1 files changed, 27 insertions, 18 deletions
diff --git a/applypatch/imgdiff.cpp b/applypatch/imgdiff.cpp
index 3dae63d4..674cc2b1 100644
--- a/applypatch/imgdiff.cpp
+++ b/applypatch/imgdiff.cpp
@@ -955,14 +955,17 @@ bool ZipModeImage::SplitZipModeImageWithLimit(const ZipModeImage& tgt_image,
955 tgt->GetRawDataLength()); 955 tgt->GetRawDataLength());
956 } 956 }
957 } else { 957 } else {
958 ZipModeImage::AddSplitImageFromChunkList(tgt_image, src_image, src_ranges, split_tgt_chunks, 958 bool added_image = ZipModeImage::AddSplitImageFromChunkList(
959 split_src_chunks, split_tgt_images, 959 tgt_image, src_image, src_ranges, split_tgt_chunks, split_src_chunks, split_tgt_images,
960 split_src_images); 960 split_src_images);
961 961
962 split_tgt_chunks.clear(); 962 split_tgt_chunks.clear();
963 split_src_chunks.clear(); 963 split_src_chunks.clear();
964 used_src_ranges.Insert(src_ranges); 964 // No need to update the split_src_ranges if we don't update the split source images.
965 split_src_ranges->push_back(std::move(src_ranges)); 965 if (added_image) {
966 used_src_ranges.Insert(src_ranges);
967 split_src_ranges->push_back(std::move(src_ranges));
968 }
966 src_ranges.Clear(); 969 src_ranges.Clear();
967 970
968 // We don't have enough space for the current chunk; start a new split image and handle 971 // We don't have enough space for the current chunk; start a new split image and handle
@@ -973,9 +976,12 @@ bool ZipModeImage::SplitZipModeImageWithLimit(const ZipModeImage& tgt_image,
973 976
974 // TODO Trim it in case the CD exceeds limit too much. 977 // TODO Trim it in case the CD exceeds limit too much.
975 src_ranges.Insert(central_directory->GetStartOffset(), central_directory->DataLengthForPatch()); 978 src_ranges.Insert(central_directory->GetStartOffset(), central_directory->DataLengthForPatch());
976 ZipModeImage::AddSplitImageFromChunkList(tgt_image, src_image, src_ranges, split_tgt_chunks, 979 bool added_image = ZipModeImage::AddSplitImageFromChunkList(tgt_image, src_image, src_ranges,
977 split_src_chunks, split_tgt_images, split_src_images); 980 split_tgt_chunks, split_src_chunks,
978 split_src_ranges->push_back(std::move(src_ranges)); 981 split_tgt_images, split_src_images);
982 if (added_image) {
983 split_src_ranges->push_back(std::move(src_ranges));
984 }
979 985
980 ValidateSplitImages(*split_tgt_images, *split_src_images, *split_src_ranges, 986 ValidateSplitImages(*split_tgt_images, *split_src_images, *split_src_ranges,
981 tgt_image.file_content_.size()); 987 tgt_image.file_content_.size());
@@ -983,7 +989,7 @@ bool ZipModeImage::SplitZipModeImageWithLimit(const ZipModeImage& tgt_image,
983 return true; 989 return true;
984} 990}
985 991
986void ZipModeImage::AddSplitImageFromChunkList(const ZipModeImage& tgt_image, 992bool ZipModeImage::AddSplitImageFromChunkList(const ZipModeImage& tgt_image,
987 const ZipModeImage& src_image, 993 const ZipModeImage& src_image,
988 const SortedRangeSet& split_src_ranges, 994 const SortedRangeSet& split_src_ranges,
989 const std::vector<ImageChunk>& split_tgt_chunks, 995 const std::vector<ImageChunk>& split_tgt_chunks,
@@ -991,12 +997,6 @@ void ZipModeImage::AddSplitImageFromChunkList(const ZipModeImage& tgt_image,
991 std::vector<ZipModeImage>* split_tgt_images, 997 std::vector<ZipModeImage>* split_tgt_images,
992 std::vector<ZipModeImage>* split_src_images) { 998 std::vector<ZipModeImage>* split_src_images) {
993 CHECK(!split_tgt_chunks.empty()); 999 CHECK(!split_tgt_chunks.empty());
994 // Target chunks should occupy at least one block.
995 // TODO put a warning and change the type to raw if it happens in extremely rare cases.
996 size_t tgt_size = split_tgt_chunks.back().GetStartOffset() +
997 split_tgt_chunks.back().DataLengthForPatch() -
998 split_tgt_chunks.front().GetStartOffset();
999 CHECK_GE(tgt_size, BLOCK_SIZE);
1000 1000
1001 std::vector<ImageChunk> aligned_tgt_chunks; 1001 std::vector<ImageChunk> aligned_tgt_chunks;
1002 1002
@@ -1015,7 +1015,12 @@ void ZipModeImage::AddSplitImageFromChunkList(const ZipModeImage& tgt_image,
1015 1015
1016 i++; 1016 i++;
1017 } 1017 }
1018 CHECK_LT(i, split_tgt_chunks.size()); 1018
1019 // Nothing left after alignment in the current split tgt chunks; skip adding the split_tgt_image.
1020 if (i == split_tgt_chunks.size()) {
1021 return false;
1022 }
1023
1019 aligned_tgt_chunks.insert(aligned_tgt_chunks.end(), split_tgt_chunks.begin() + i + 1, 1024 aligned_tgt_chunks.insert(aligned_tgt_chunks.end(), split_tgt_chunks.begin() + i + 1,
1020 split_tgt_chunks.end()); 1025 split_tgt_chunks.end());
1021 CHECK(!aligned_tgt_chunks.empty()); 1026 CHECK(!aligned_tgt_chunks.empty());
@@ -1024,8 +1029,10 @@ void ZipModeImage::AddSplitImageFromChunkList(const ZipModeImage& tgt_image,
1024 size_t end_offset = 1029 size_t end_offset =
1025 aligned_tgt_chunks.back().GetStartOffset() + aligned_tgt_chunks.back().GetRawDataLength(); 1030 aligned_tgt_chunks.back().GetStartOffset() + aligned_tgt_chunks.back().GetRawDataLength();
1026 if (end_offset % BLOCK_SIZE != 0 && end_offset < tgt_image.file_content_.size()) { 1031 if (end_offset % BLOCK_SIZE != 0 && end_offset < tgt_image.file_content_.size()) {
1032 size_t tail_block_length = std::min<size_t>(tgt_image.file_content_.size() - end_offset,
1033 BLOCK_SIZE - (end_offset % BLOCK_SIZE));
1027 aligned_tgt_chunks.emplace_back(CHUNK_NORMAL, end_offset, &tgt_image.file_content_, 1034 aligned_tgt_chunks.emplace_back(CHUNK_NORMAL, end_offset, &tgt_image.file_content_,
1028 BLOCK_SIZE - (end_offset % BLOCK_SIZE)); 1035 tail_block_length);
1029 } 1036 }
1030 1037
1031 ZipModeImage split_tgt_image(false); 1038 ZipModeImage split_tgt_image(false);
@@ -1049,6 +1056,8 @@ void ZipModeImage::AddSplitImageFromChunkList(const ZipModeImage& tgt_image,
1049 1056
1050 split_tgt_images->push_back(std::move(split_tgt_image)); 1057 split_tgt_images->push_back(std::move(split_tgt_image));
1051 split_src_images->push_back(std::move(split_src_image)); 1058 split_src_images->push_back(std::move(split_src_image));
1059
1060 return true;
1052} 1061}
1053 1062
1054void ZipModeImage::ValidateSplitImages(const std::vector<ZipModeImage>& split_tgt_images, 1063void ZipModeImage::ValidateSplitImages(const std::vector<ZipModeImage>& split_tgt_images,
@@ -1536,7 +1545,7 @@ int imgdiff(int argc, const char** argv) {
1536 " patches together and output them into <patch-file>.\n" 1545 " patches together and output them into <patch-file>.\n"
1537 " --split-info, Output the split information (patch_size, tgt_size, src_ranges);\n" 1546 " --split-info, Output the split information (patch_size, tgt_size, src_ranges);\n"
1538 " zip mode with block-limit only.\n" 1547 " zip mode with block-limit only.\n"
1539 " --debug_dir, Debug directory to put the split srcs and patches, zip mode only.\n" 1548 " --debug-dir, Debug directory to put the split srcs and patches, zip mode only.\n"
1540 " -v, --verbose, Enable verbose logging."; 1549 " -v, --verbose, Enable verbose logging.";
1541 return 2; 1550 return 2;
1542 } 1551 }