• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 PAYLOAD_GENERATOR_DELTA_DIFF_UTILS_H_
18 #define PAYLOAD_GENERATOR_DELTA_DIFF_UTILS_H_
19 
20 #include <map>
21 #include <string>
22 #include <utility>
23 #include <vector>
24 
25 #include <brillo/secure_blob.h>
26 #include <puffin/puffdiff.h>
27 
28 #include "payload_generator/deflate_utils.h"
29 #include "update_engine/payload_generator/annotated_operation.h"
30 #include "update_engine/payload_generator/extent_ranges.h"
31 #include "update_engine/payload_generator/payload_generation_config.h"
32 #include "update_engine/update_metadata.pb.h"
33 
34 namespace chromeos_update_engine {
35 
36 namespace diff_utils {
37 using File = FilesystemInterface::File;
38 
39 // Create operations in |aops| to produce all the blocks in the |new_part|
40 // partition using the filesystem opened in that PartitionConfig.
41 // It uses the files reported by the filesystem in |old_part| and the data
42 // blocks in that partition (if available) to determine the best way to compress
43 // the new files (REPLACE, REPLACE_BZ, COPY, BSDIFF) and writes any necessary
44 // data to |blob_file|. |hard_chunk_blocks| and |soft_chunk_blocks| are the hard
45 // and soft chunk limits in number of blocks respectively. The soft chunk limit
46 // is used to split MOVE and SOURCE_COPY operations and REPLACE_BZ of zeroed
47 // blocks, while the hard limit is used to split a file when generating other
48 // operations. A value of -1 in |hard_chunk_blocks| means whole files.
49 bool DeltaReadPartition(std::vector<AnnotatedOperation>* aops,
50                         const PartitionConfig& old_part,
51                         const PartitionConfig& new_part,
52                         ssize_t hard_chunk_blocks,
53                         size_t soft_chunk_blocks,
54                         const PayloadGenerationConfig& version,
55                         BlobFileWriter* blob_file);
56 
57 // Create operations in |aops| for identical blocks that moved around in the old
58 // and new partition and also handle zeroed blocks. The old and new partition
59 // are stored in the |old_part| and |new_part| files and have |old_num_blocks|
60 // and |new_num_blocks| respectively. The maximum operation size is
61 // |chunk_blocks| blocks, or unlimited if |chunk_blocks| is -1. The blobs of the
62 // produced operations are stored in the |blob_file|.
63 // The collections |old_visited_blocks| and |new_visited_blocks| state what
64 // blocks already have operations reading or writing them and only operations
65 // for unvisited blocks are produced by this function updating both collections
66 // with the used blocks.
67 bool DeltaMovedAndZeroBlocks(std::vector<AnnotatedOperation>* aops,
68                              const std::string& old_part,
69                              const std::string& new_part,
70                              size_t old_num_blocks,
71                              size_t new_num_blocks,
72                              ssize_t chunk_blocks,
73                              const PayloadGenerationConfig& version,
74                              BlobFileWriter* blob_file,
75                              ExtentRanges* old_visited_blocks,
76                              ExtentRanges* new_visited_blocks,
77                              ExtentRanges* old_zero_blocks);
78 
79 // For a given file |name| append operations to |aops| to produce it in the
80 // |new_part|. The file will be split in chunks of |chunk_blocks| blocks each
81 // or treated as a single chunk if |chunk_blocks| is -1. The file data is
82 // stored in |new_part| in the blocks described by |new_extents| and, if it
83 // exists, the old version exists in |old_part| in the blocks described by
84 // |old_extents|. The operations added to |aops| reference the data blob
85 // in the |blob_file|. |old_deflates| and |new_deflates| are all deflate
86 // locations in |old_part| and |new_part|. Returns true on success.
87 bool DeltaReadFile(std::vector<AnnotatedOperation>* aops,
88                    const std::string& old_part,
89                    const std::string& new_part,
90                    const File& old_file,
91                    const File& new_file,
92                    ssize_t chunk_blocks,
93                    const PayloadGenerationConfig& config,
94                    BlobFileWriter* blob_file);
95 
96 // Reads the blocks |old_extents| from |old_part| (if it exists) and the
97 // |new_extents| from |new_part| and determines the smallest way to encode
98 // this |new_extents| for the diff. It stores necessary data in |out_data| and
99 // fills in |out_op|. If there's no change in old and new files, it creates a
100 // MOVE or SOURCE_COPY operation. If there is a change, the smallest of the
101 // operations allowed in the given |version| (REPLACE, REPLACE_BZ, BSDIFF,
102 // SOURCE_BSDIFF, PUFFDIFF or ZUCCHINI) wins.
103 // |new_extents| must not be empty. |old_deflates| and |new_deflates| are all
104 // the deflate locations in |old_part| and |new_part|. Returns true on success.
105 // TODO(197361113) Move logic to calculate deflates inside puffin.
106 bool ReadExtentsToDiff(const std::string& old_part,
107                        const std::string& new_part,
108                        const std::vector<Extent>& old_extents,
109                        const std::vector<Extent>& new_extents,
110                        const File& old_file,
111                        const File& new_file,
112                        const PayloadGenerationConfig& config,
113                        brillo::Blob* out_data,
114                        AnnotatedOperation* out_op);
115 
116 // Generates the best allowed full operation to produce |new_data|. The allowed
117 // operations are based on |payload_version|. The operation blob will be stored
118 // in |out_blob| and the resulting operation type in |out_type|. Returns whether
119 // a valid full operation was generated.
120 bool GenerateBestFullOperation(const brillo::Blob& new_data,
121                                const PayloadVersion& version,
122                                brillo::Blob* out_blob,
123                                InstallOperation::Type* out_type);
124 
125 // Returns whether |op_type| is one of the REPLACE full operations.
126 bool IsAReplaceOperation(InstallOperation::Type op_type);
127 
128 // Returns true if an operation with type |op_type| has no |src_extents|.
129 bool IsNoSourceOperation(InstallOperation::Type op_type);
130 
131 bool InitializePartitionInfo(const PartitionConfig& partition,
132                              PartitionInfo* info);
133 
134 // Compare two AnnotatedOperations by the start block of the first Extent in
135 // their destination extents.
136 bool CompareAopsByDestination(AnnotatedOperation first_aop,
137                               AnnotatedOperation second_aop);
138 
139 // Returns whether the filesystem is an ext[234] filesystem. In case of failure,
140 // such as if the file |device| doesn't exists or can't be read, it returns
141 // false.
142 bool IsExtFilesystem(const std::string& device);
143 
144 // Returns the max number of threads to process the files(chunks) in parallel.
145 size_t GetMaxThreads();
146 
147 // Returns the old file which file name has the shortest levenshtein distance to
148 // |new_file_name|.
149 FilesystemInterface::File GetOldFile(
150     const std::map<std::string, FilesystemInterface::File>& old_files_map,
151     const std::string& new_file_name);
152 
153 // Read BSDIFF patch data in |data|, compute list of blocks that can be COW_XOR,
154 // store these blocks in |aop|.
155 bool PopulateXorOps(AnnotatedOperation* aop, const uint8_t* data, size_t size);
156 
PopulateXorOps(AnnotatedOperation * aop,const brillo::Blob & patch_data)157 inline bool PopulateXorOps(AnnotatedOperation* aop,
158                            const brillo::Blob& patch_data) {
159   return PopulateXorOps(aop, patch_data.data(), patch_data.size());
160 }
161 
162 // A utility class that tries different algorithms and pick the patch with the
163 // smallest size.
164 
165 class BestDiffGenerator {
166  public:
BestDiffGenerator(const brillo::Blob & old_data,const brillo::Blob & new_data,const std::vector<Extent> & src_extents,const std::vector<Extent> & dst_extents,const File & old_file,const File & new_file,const PayloadGenerationConfig & config)167   BestDiffGenerator(const brillo::Blob& old_data,
168                     const brillo::Blob& new_data,
169                     const std::vector<Extent>& src_extents,
170                     const std::vector<Extent>& dst_extents,
171                     const File& old_file,
172                     const File& new_file,
173                     const PayloadGenerationConfig& config)
174       : old_data_(old_data),
175         new_data_(new_data),
176         src_extents_(src_extents),
177         dst_extents_(dst_extents),
178         old_deflates_(old_file.deflates),
179         new_deflates_(new_file.deflates),
180         old_block_info_(old_file.compressed_file_info),
181         new_block_info_(new_file.compressed_file_info),
182         config_(config) {
183     using std::vector;
184     // Find all deflate positions inside the given extents and then put all
185     // deflates together because we have already read all the extents into
186     // one buffer.
187     vector<puffin::BitExtent> src_deflates;
188     TEST_AND_RETURN(deflate_utils::FindAndCompactDeflates(
189         src_extents_, old_deflates_, &src_deflates));
190 
191     vector<puffin::BitExtent> dst_deflates;
192     TEST_AND_RETURN(deflate_utils::FindAndCompactDeflates(
193         dst_extents_, new_deflates_, &dst_deflates));
194     puffin::RemoveEqualBitExtents(
195         old_data_, new_data_, &src_deflates, &dst_deflates);
196     // See crbug.com/915559.
197     if (config.version.minor <= kPuffdiffMinorPayloadVersion) {
198       CHECK(
199           puffin::RemoveDeflatesWithBadDistanceCaches(old_data, &src_deflates));
200 
201       CHECK(
202           puffin::RemoveDeflatesWithBadDistanceCaches(new_data, &dst_deflates));
203     }
204     old_deflates_ = std::move(src_deflates);
205     new_deflates_ = std::move(dst_deflates);
206   }
207 
208   // Tries different algorithms and compares their patch sizes with the
209   // compressed full operation data in |data_blob|. If the size is smaller,
210   // updates the operation type in |aop| and bytes in |data_blob|.
211   bool GenerateBestDiffOperation(AnnotatedOperation* aop,
212                                  brillo::Blob* data_blob);
213 
214   bool GenerateBestDiffOperation(
215       const std::vector<std::pair<InstallOperation_Type, size_t>>&
216           diff_candidates,
217       AnnotatedOperation* aop,
218       brillo::Blob* data_blob);
219 
220  private:
221   std::vector<bsdiff::CompressorType> GetUsableCompressorTypes() const;
222   bool TryBsdiffAndUpdateOperation(InstallOperation_Type operation_type,
223                                    AnnotatedOperation* aop,
224                                    brillo::Blob* data_blob);
225   bool TryPuffdiffAndUpdateOperation(AnnotatedOperation* aop,
226                                      brillo::Blob* data_blob);
227   bool TryZucchiniAndUpdateOperation(AnnotatedOperation* aop,
228                                      brillo::Blob* data_blob);
229 
230   const brillo::Blob& old_data_;
231   const brillo::Blob& new_data_;
232   const std::vector<Extent>& src_extents_;
233   const std::vector<Extent>& dst_extents_;
234   std::vector<puffin::BitExtent> old_deflates_;
235   std::vector<puffin::BitExtent> new_deflates_;
236   const CompressedFile& old_block_info_;
237   const CompressedFile& new_block_info_;
238   const PayloadGenerationConfig& config_;
239 };
240 
241 }  // namespace diff_utils
242 
243 }  // namespace chromeos_update_engine
244 
245 #endif  // PAYLOAD_GENERATOR_DELTA_DIFF_UTILS_H_
246