delta_diff_utils.h revision aea4c1cea20dda7ae7e85fc8924a2d784f70d806
1// 2// Copyright (C) 2015 The Android Open Source Project 3// 4// Licensed under the Apache License, Version 2.0 (the "License"); 5// you may not use this file except in compliance with the License. 6// You may obtain a copy of the License at 7// 8// http://www.apache.org/licenses/LICENSE-2.0 9// 10// Unless required by applicable law or agreed to in writing, software 11// distributed under the License is distributed on an "AS IS" BASIS, 12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13// See the License for the specific language governing permissions and 14// limitations under the License. 15// 16 17#ifndef UPDATE_ENGINE_PAYLOAD_GENERATOR_DELTA_DIFF_UTILS_H_ 18#define UPDATE_ENGINE_PAYLOAD_GENERATOR_DELTA_DIFF_UTILS_H_ 19 20#include <string> 21#include <vector> 22 23#include <chromeos/secure_blob.h> 24 25#include "update_engine/payload_generator/annotated_operation.h" 26#include "update_engine/payload_generator/extent_ranges.h" 27#include "update_engine/payload_generator/payload_generation_config.h" 28#include "update_engine/update_metadata.pb.h" 29 30namespace chromeos_update_engine { 31 32namespace diff_utils { 33 34// Create operations in |aops| to produce all the blocks in the |new_part| 35// partition using the filesystem opened in that PartitionConfig. 36// It uses the files reported by the filesystem in |old_part| and the data 37// blocks in that partition (if available) to determine the best way to compress 38// the new files (REPLACE, REPLACE_BZ, COPY, BSDIFF) and writes any necessary 39// data to the end of |data_fd| updating |data_file_size| accordingly. 40// |hard_chunk_blocks| and |soft_chunk_blocks| are the hard and soft chunk 41// limits in number of blocks respectively. The soft chunk limit is used to 42// split MOVE and SOURCE_COPY operations and REPLACE_BZ of zeroed blocks, while 43// the hard limit is used to split a file when generating other operations. A 44// value of -1 in |hard_chunk_blocks| means whole files. 45bool DeltaReadPartition(std::vector<AnnotatedOperation>* aops, 46 const PartitionConfig& old_part, 47 const PartitionConfig& new_part, 48 ssize_t hard_chunk_blocks, 49 size_t soft_chunk_blocks, 50 BlobFileWriter* blob_file, 51 bool src_ops_allowed); 52 53// Create operations in |aops| for identical blocks that moved around in the old 54// and new partition and also handle zeroed blocks. The old and new partition 55// are stored in the |old_part| and |new_part| files and have |old_num_blocks| 56// and |new_num_blocks| respectively. The maximum operation size is 57// |chunk_blocks| blocks, or unlimited if |chunk_blocks| is -1. The blobs of the 58// produced operations are stored in the |data_fd| file whose size is updated 59// in the value pointed by |data_file_size|. 60// The collections |old_visited_blocks| and |new_visited_blocks| state what 61// blocks already have operations reading or writing them and only operations 62// for unvisited blocks are produced by this function updating both collections 63// with the used blocks. 64bool DeltaMovedAndZeroBlocks(std::vector<AnnotatedOperation>* aops, 65 const std::string& old_part, 66 const std::string& new_part, 67 size_t old_num_blocks, 68 size_t new_num_blocks, 69 ssize_t chunk_blocks, 70 bool src_ops_allowed, 71 BlobFileWriter* blob_file, 72 ExtentRanges* old_visited_blocks, 73 ExtentRanges* new_visited_blocks); 74 75// For a given file |name| append operations to |aops| to produce it in the 76// |new_part|. The file will be split in chunks of |chunk_blocks| blocks each 77// or treated as a single chunk if |chunk_blocks| is -1. The file data is 78// stored in |new_part| in the blocks described by |new_extents| and, if it 79// exists, the old version exists in |old_part| in the blocks described by 80// |old_extents|. The operations added to |aops| reference the data blob 81// in the file |data_fd|, which has length *data_file_size. *data_file_size is 82// updated appropriately. Returns true on success. 83bool DeltaReadFile(std::vector<AnnotatedOperation>* aops, 84 const std::string& old_part, 85 const std::string& new_part, 86 const std::vector<Extent>& old_extents, 87 const std::vector<Extent>& new_extents, 88 const std::string& name, 89 ssize_t chunk_blocks, 90 BlobFileWriter* blob_file, 91 bool src_ops_allowed); 92 93// Reads the blocks |old_extents| from |old_part| (if it exists) and the 94// |new_extents| from |new_part| and determines the smallest way to encode 95// this |new_extents| for the diff. It stores necessary data in |out_data| and 96// fills in |out_op|. If there's no change in old and new files, it creates a 97// MOVE operation. If there is a change, the smallest of REPLACE, REPLACE_BZ, 98// or BSDIFF wins. |new_extents| must not be empty. 99// If |src_ops_allowed| is true, it will emit SOURCE_COPY and SOURCE_BSDIFF 100// operations instead of MOVE and BSDIFF, respectively. 101// Returns true on success. 102bool ReadExtentsToDiff(const std::string& old_part, 103 const std::string& new_part, 104 const std::vector<Extent>& old_extents, 105 const std::vector<Extent>& new_extents, 106 bool bsdiff_allowed, 107 chromeos::Blob* out_data, 108 InstallOperation* out_op, 109 bool src_ops_allowed); 110 111// Runs the bsdiff tool on two files and returns the resulting delta in 112// |out|. Returns true on success. 113bool BsdiffFiles(const std::string& old_file, 114 const std::string& new_file, 115 chromeos::Blob* out); 116 117// Returns true if |op| is a no-op operation that doesn't do any useful work 118// (e.g., a move operation that copies blocks onto themselves). 119bool IsNoopOperation(const InstallOperation& op); 120 121// Filters all the operations that are no-op, maintaining the relative order 122// of the rest of the operations. 123void FilterNoopOperations(std::vector<AnnotatedOperation>* ops); 124 125bool InitializePartitionInfo(const PartitionConfig& partition, 126 PartitionInfo* info); 127 128// Compare two AnnotatedOperations by the start block of the first Extent in 129// their destination extents. 130bool CompareAopsByDestination(AnnotatedOperation first_aop, 131 AnnotatedOperation second_aop); 132 133} // namespace diff_utils 134 135} // namespace chromeos_update_engine 136 137#endif // UPDATE_ENGINE_PAYLOAD_GENERATOR_DELTA_DIFF_UTILS_H_ 138