• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright (C) 2020 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_MERGE_SEQUENCE_GENERATOR_H_
18 #define UPDATE_ENGINE_PAYLOAD_GENERATOR_MERGE_SEQUENCE_GENERATOR_H_
19 
20 #include <map>
21 #include <memory>
22 #include <set>
23 #include <utility>
24 #include <vector>
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/extent_utils.h"
29 #include "update_engine/update_metadata.pb.h"
30 
31 namespace chromeos_update_engine {
32 // Constructs CowMergeOperation from src & dst extents
33 CowMergeOperation CreateCowMergeOperation(const Extent& src_extent,
34                                           const Extent& dst_extent,
35                                           CowMergeOperation::Type op_type,
36                                           uint32_t src_offset = 0);
37 
38 // Comparator for CowMergeOperation.
39 bool operator<(const CowMergeOperation& op1, const CowMergeOperation& op2);
40 bool operator==(const CowMergeOperation& op1, const CowMergeOperation& op2);
41 
42 std::ostream& operator<<(std::ostream& os,
43                          const CowMergeOperation& merge_operation);
44 
45 // This class takes a list of CowMergeOperations; and sorts them so that no
46 // read after write will happen by following the sequence. When there is a
47 // cycle, we will omit some operations in the list. Therefore, the result
48 // sequence may not contain all blocks in the input list.
49 class MergeSequenceGenerator {
50  public:
51   // Creates an object from a list of OTA InstallOperations. Returns nullptr on
52   // failure.
53   static std::unique_ptr<MergeSequenceGenerator> Create(
54       const std::vector<AnnotatedOperation>& aops);
55   // Checks that no read after write happens in the given sequence.
56   static bool ValidateSequence(const std::vector<CowMergeOperation>& sequence);
57 
58   // Generates a merge sequence from |operations_|, puts the result in
59   // |sequence|. Returns false on failure.
60   bool Generate(std::vector<CowMergeOperation>* sequence) const;
61 
62  private:
63   friend class MergeSequenceGeneratorTest;
MergeSequenceGenerator(std::vector<CowMergeOperation> transfers)64   explicit MergeSequenceGenerator(std::vector<CowMergeOperation> transfers)
65       : operations_(std::move(transfers)) {}
66 
67   // For a given merge operation, finds all the operations that should merge
68   // after myself. Put the result in |merge_after|.
69   bool FindDependency(std::map<CowMergeOperation, std::set<CowMergeOperation>>*
70                           merge_after) const;
71   // The list of CowMergeOperations to sort.
72   const std::vector<CowMergeOperation> operations_;
73 };
74 
75 void SplitSelfOverlapping(const Extent& src_extent,
76                           const Extent& dst_extent,
77                           std::vector<CowMergeOperation>* sequence);
78 
79 }  // namespace chromeos_update_engine
80 #endif
81