• 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 UPDATE_ENGINE_PAYLOAD_GENERATOR_DELTA_DIFF_UTILS_H_
18 #define UPDATE_ENGINE_PAYLOAD_GENERATOR_DELTA_DIFF_UTILS_H_
19 
20 #include <map>
21 #include <string>
22 #include <vector>
23 
24 #include <brillo/secure_blob.h>
25 #include <puffin/puffdiff.h>
26 
27 #include "update_engine/payload_generator/annotated_operation.h"
28 #include "update_engine/payload_generator/extent_ranges.h"
29 #include "update_engine/payload_generator/payload_generation_config.h"
30 #include "update_engine/update_metadata.pb.h"
31 
32 namespace chromeos_update_engine {
33 
34 namespace diff_utils {
35 
36 // Create operations in |aops| to produce all the blocks in the |new_part|
37 // partition using the filesystem opened in that PartitionConfig.
38 // It uses the files reported by the filesystem in |old_part| and the data
39 // blocks in that partition (if available) to determine the best way to compress
40 // the new files (REPLACE, REPLACE_BZ, COPY, BSDIFF) and writes any necessary
41 // data to |blob_file|. |hard_chunk_blocks| and |soft_chunk_blocks| are the hard
42 // and soft chunk limits in number of blocks respectively. The soft chunk limit
43 // is used to split MOVE and SOURCE_COPY operations and REPLACE_BZ of zeroed
44 // blocks, while the hard limit is used to split a file when generating other
45 // operations. A value of -1 in |hard_chunk_blocks| means whole files.
46 bool DeltaReadPartition(std::vector<AnnotatedOperation>* aops,
47                         const PartitionConfig& old_part,
48                         const PartitionConfig& new_part,
49                         ssize_t hard_chunk_blocks,
50                         size_t soft_chunk_blocks,
51                         const PayloadVersion& version,
52                         BlobFileWriter* blob_file);
53 
54 // Create operations in |aops| for identical blocks that moved around in the old
55 // and new partition and also handle zeroed blocks. The old and new partition
56 // are stored in the |old_part| and |new_part| files and have |old_num_blocks|
57 // and |new_num_blocks| respectively. The maximum operation size is
58 // |chunk_blocks| blocks, or unlimited if |chunk_blocks| is -1. The blobs of the
59 // produced operations are stored in the |blob_file|.
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.
64 bool 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                              const PayloadVersion& version,
71                              BlobFileWriter* blob_file,
72                              ExtentRanges* old_visited_blocks,
73                              ExtentRanges* new_visited_blocks,
74                              ExtentRanges* old_zero_blocks);
75 
76 // For a given file |name| append operations to |aops| to produce it in the
77 // |new_part|. The file will be split in chunks of |chunk_blocks| blocks each
78 // or treated as a single chunk if |chunk_blocks| is -1. The file data is
79 // stored in |new_part| in the blocks described by |new_extents| and, if it
80 // exists, the old version exists in |old_part| in the blocks described by
81 // |old_extents|. The operations added to |aops| reference the data blob
82 // in the |blob_file|. |old_deflates| and |new_deflates| are all deflate
83 // locations in |old_part| and |new_part|. Returns true on success.
84 bool DeltaReadFile(std::vector<AnnotatedOperation>* aops,
85                    const std::string& old_part,
86                    const std::string& new_part,
87                    const std::vector<Extent>& old_extents,
88                    const std::vector<Extent>& new_extents,
89                    const std::vector<puffin::BitExtent>& old_deflates,
90                    const std::vector<puffin::BitExtent>& new_deflates,
91                    const std::string& name,
92                    ssize_t chunk_blocks,
93                    const PayloadVersion& version,
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, or PUFFDIFF) 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 bool ReadExtentsToDiff(const std::string& old_part,
106                        const std::string& new_part,
107                        const std::vector<Extent>& old_extents,
108                        const std::vector<Extent>& new_extents,
109                        const std::vector<puffin::BitExtent>& old_deflates,
110                        const std::vector<puffin::BitExtent>& new_deflates,
111                        const PayloadVersion& version,
112                        brillo::Blob* out_data,
113                        InstallOperation* out_op);
114 
115 // Generates the best allowed full operation to produce |new_data|. The allowed
116 // operations are based on |payload_version|. The operation blob will be stored
117 // in |out_blob| and the resulting operation type in |out_type|. Returns whether
118 // a valid full operation was generated.
119 bool GenerateBestFullOperation(const brillo::Blob& new_data,
120                                const PayloadVersion& version,
121                                brillo::Blob* out_blob,
122                                InstallOperation::Type* out_type);
123 
124 // Returns whether |op_type| is one of the REPLACE full operations.
125 bool IsAReplaceOperation(InstallOperation::Type op_type);
126 
127 // Returns true if an operation with type |op_type| has no |src_extents|.
128 bool IsNoSourceOperation(InstallOperation::Type op_type);
129 
130 bool InitializePartitionInfo(const PartitionConfig& partition,
131                              PartitionInfo* info);
132 
133 // Compare two AnnotatedOperations by the start block of the first Extent in
134 // their destination extents.
135 bool CompareAopsByDestination(AnnotatedOperation first_aop,
136                               AnnotatedOperation second_aop);
137 
138 // Returns whether the filesystem is an ext[234] filesystem. In case of failure,
139 // such as if the file |device| doesn't exists or can't be read, it returns
140 // false.
141 bool IsExtFilesystem(const std::string& device);
142 
143 // Returns the max number of threads to process the files(chunks) in parallel.
144 size_t GetMaxThreads();
145 
146 // Returns the old file which file name has the shortest levenshtein distance to
147 // |new_file_name|.
148 FilesystemInterface::File GetOldFile(
149     const std::map<std::string, FilesystemInterface::File>& old_files_map,
150     const std::string& new_file_name);
151 
152 }  // namespace diff_utils
153 
154 }  // namespace chromeos_update_engine
155 
156 #endif  // UPDATE_ENGINE_PAYLOAD_GENERATOR_DELTA_DIFF_UTILS_H_
157