delta_diff_utils.h revision 3cd4df127c29eb90c0e203373edfbc9534c79169
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 <brillo/secure_blob.h> 24#include <puffin/puffdiff.h> 25 26#include "update_engine/payload_generator/annotated_operation.h" 27#include "update_engine/payload_generator/extent_ranges.h" 28#include "update_engine/payload_generator/payload_generation_config.h" 29#include "update_engine/update_metadata.pb.h" 30 31namespace chromeos_update_engine { 32 33namespace diff_utils { 34 35// Create operations in |aops| to produce all the blocks in the |new_part| 36// partition using the filesystem opened in that PartitionConfig. 37// It uses the files reported by the filesystem in |old_part| and the data 38// blocks in that partition (if available) to determine the best way to compress 39// the new files (REPLACE, REPLACE_BZ, COPY, BSDIFF) and writes any necessary 40// data to |blob_file|. |hard_chunk_blocks| and |soft_chunk_blocks| are the hard 41// and soft chunk limits in number of blocks respectively. The soft chunk limit 42// is used to split MOVE and SOURCE_COPY operations and REPLACE_BZ of zeroed 43// blocks, while the hard limit is used to split a file when generating other 44// operations. A 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 const PayloadVersion& version, 51 BlobFileWriter* blob_file); 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 |blob_file|. 59// The collections |old_visited_blocks| and |new_visited_blocks| state what 60// blocks already have operations reading or writing them and only operations 61// for unvisited blocks are produced by this function updating both collections 62// with the used blocks. 63bool DeltaMovedAndZeroBlocks(std::vector<AnnotatedOperation>* aops, 64 const std::string& old_part, 65 const std::string& new_part, 66 size_t old_num_blocks, 67 size_t new_num_blocks, 68 ssize_t chunk_blocks, 69 const PayloadVersion& version, 70 BlobFileWriter* blob_file, 71 ExtentRanges* old_visited_blocks, 72 ExtentRanges* new_visited_blocks); 73 74// For a given file |name| append operations to |aops| to produce it in the 75// |new_part|. The file will be split in chunks of |chunk_blocks| blocks each 76// or treated as a single chunk if |chunk_blocks| is -1. The file data is 77// stored in |new_part| in the blocks described by |new_extents| and, if it 78// exists, the old version exists in |old_part| in the blocks described by 79// |old_extents|. The operations added to |aops| reference the data blob 80// in the |blob_file|. |old_deflates| and |new_deflates| are all deflate 81// locations in |old_part| and |new_part|. Returns true on success. 82bool DeltaReadFile(std::vector<AnnotatedOperation>* aops, 83 const std::string& old_part, 84 const std::string& new_part, 85 const std::vector<Extent>& old_extents, 86 const std::vector<Extent>& new_extents, 87 const std::vector<puffin::BitExtent>& old_deflates, 88 const std::vector<puffin::BitExtent>& new_deflates, 89 const std::string& name, 90 ssize_t chunk_blocks, 91 const PayloadVersion& version, 92 BlobFileWriter* blob_file); 93 94// Reads the blocks |old_extents| from |old_part| (if it exists) and the 95// |new_extents| from |new_part| and determines the smallest way to encode 96// this |new_extents| for the diff. It stores necessary data in |out_data| and 97// fills in |out_op|. If there's no change in old and new files, it creates a 98// MOVE or SOURCE_COPY operation. If there is a change, the smallest of the 99// operations allowed in the given |version| (REPLACE, REPLACE_BZ, BSDIFF, 100// SOURCE_BSDIFF, or PUFFDIFF) wins. 101// |new_extents| must not be empty. |old_deflates| and |new_deflates| are all 102// the deflate locations in |old_part| and |new_part|. Returns true on success. 103bool ReadExtentsToDiff(const std::string& old_part, 104 const std::string& new_part, 105 const std::vector<Extent>& old_extents, 106 const std::vector<Extent>& new_extents, 107 const std::vector<puffin::BitExtent>& old_deflates, 108 const std::vector<puffin::BitExtent>& new_deflates, 109 const PayloadVersion& version, 110 brillo::Blob* out_data, 111 InstallOperation* out_op); 112 113// Generates the best allowed full operation to produce |new_data|. The allowed 114// operations are based on |payload_version|. The operation blob will be stored 115// in |out_blob| and the resulting operation type in |out_type|. Returns whether 116// a valid full operation was generated. 117bool GenerateBestFullOperation(const brillo::Blob& new_data, 118 const PayloadVersion& version, 119 brillo::Blob* out_blob, 120 InstallOperation_Type* out_type); 121 122// Returns whether |op_type| is one of the REPLACE full operations. 123bool IsAReplaceOperation(InstallOperation_Type op_type); 124 125// Returns true if an operation with type |op_type| has no |src_extents|. 126bool IsNoSourceOperation(InstallOperation_Type op_type); 127 128// Returns true if |op| is a no-op operation that doesn't do any useful work 129// (e.g., a move operation that copies blocks onto themselves). 130bool IsNoopOperation(const InstallOperation& op); 131 132// Filters all the operations that are no-op, maintaining the relative order 133// of the rest of the operations. 134void FilterNoopOperations(std::vector<AnnotatedOperation>* ops); 135 136bool InitializePartitionInfo(const PartitionConfig& partition, 137 PartitionInfo* info); 138 139// Compare two AnnotatedOperations by the start block of the first Extent in 140// their destination extents. 141bool CompareAopsByDestination(AnnotatedOperation first_aop, 142 AnnotatedOperation second_aop); 143 144// Returns whether the filesystem is an ext[234] filesystem. In case of failure, 145// such as if the file |device| doesn't exists or can't be read, it returns 146// false. 147bool IsExtFilesystem(const std::string& device); 148 149// Returns the max number of threads to process the files(chunks) in parallel. 150size_t GetMaxThreads(); 151 152} // namespace diff_utils 153 154} // namespace chromeos_update_engine 155 156#endif // UPDATE_ENGINE_PAYLOAD_GENERATOR_DELTA_DIFF_UTILS_H_ 157