Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 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 | // |
Alex Deymo | 5c6c655 | 2015-06-03 19:06:50 +0200 | [diff] [blame] | 16 | |
| 17 | #ifndef UPDATE_ENGINE_PAYLOAD_GENERATOR_EXTENT_UTILS_H_ |
| 18 | #define UPDATE_ENGINE_PAYLOAD_GENERATOR_EXTENT_UTILS_H_ |
| 19 | |
Colin Cross | 26b82b1 | 2021-12-22 10:09:19 -0800 | [diff] [blame] | 20 | #include <limits> |
Sen Jiang | 55c4f9b | 2016-02-10 11:26:20 -0800 | [diff] [blame] | 21 | #include <string> |
Alex Deymo | 5c6c655 | 2015-06-03 19:06:50 +0200 | [diff] [blame] | 22 | #include <vector> |
| 23 | |
Kelvin Zhang | b05e4e2 | 2020-09-25 16:16:19 -0400 | [diff] [blame] | 24 | #include <base/logging.h> |
| 25 | |
Kelvin Zhang | e532af3 | 2024-01-10 15:29:02 -0800 | [diff] [blame] | 26 | #include "update_engine/common/utils.h" |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 27 | #include "update_engine/payload_consumer/payload_constants.h" |
Alex Deymo | 5c6c655 | 2015-06-03 19:06:50 +0200 | [diff] [blame] | 28 | #include "update_engine/update_metadata.pb.h" |
| 29 | |
| 30 | // Utility functions for manipulating Extents and lists of blocks. |
| 31 | |
| 32 | namespace chromeos_update_engine { |
Kelvin Zhang | e532af3 | 2024-01-10 15:29:02 -0800 | [diff] [blame] | 33 | struct ExtentLess { |
| 34 | constexpr bool operator()(const Extent& x, const Extent& y) const { |
| 35 | if (x.start_block() == y.start_block()) { |
| 36 | return x.num_blocks() < y.num_blocks(); |
| 37 | } |
| 38 | return x.start_block() < y.start_block(); |
| 39 | } |
| 40 | }; |
Alex Deymo | 5c6c655 | 2015-06-03 19:06:50 +0200 | [diff] [blame] | 41 | |
| 42 | // |block| must either be the next block in the last extent or a block |
| 43 | // in the next extent. This function will not handle inserting block |
| 44 | // into an arbitrary place in the extents. |
| 45 | void AppendBlockToExtents(std::vector<Extent>* extents, uint64_t block); |
| 46 | |
Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 47 | // Takes a collection (vector or RepeatedPtrField) of Extent and |
| 48 | // returns a vector of the blocks referenced, in order. |
Amin Hassani | 232f8f9 | 2019-01-14 16:15:31 -0800 | [diff] [blame] | 49 | template <typename T> |
Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 50 | std::vector<uint64_t> ExpandExtents(const T& extents) { |
| 51 | std::vector<uint64_t> ret; |
Amin Hassani | d8b67f4 | 2017-12-06 13:47:52 -0800 | [diff] [blame] | 52 | for (const auto& extent : extents) { |
Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 53 | if (extent.start_block() == kSparseHole) { |
| 54 | ret.resize(ret.size() + extent.num_blocks(), kSparseHole); |
| 55 | } else { |
| 56 | for (uint64_t block = extent.start_block(); |
Amin Hassani | 232f8f9 | 2019-01-14 16:15:31 -0800 | [diff] [blame] | 57 | block < (extent.start_block() + extent.num_blocks()); |
| 58 | block++) { |
Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 59 | ret.push_back(block); |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | return ret; |
| 64 | } |
| 65 | |
| 66 | // Stores all Extents in 'extents' into 'out'. |
| 67 | void StoreExtents(const std::vector<Extent>& extents, |
| 68 | google::protobuf::RepeatedPtrField<Extent>* out); |
| 69 | |
| 70 | // Stores all extents in |extents| into |out_vector|. |
| 71 | void ExtentsToVector(const google::protobuf::RepeatedPtrField<Extent>& extents, |
| 72 | std::vector<Extent>* out_vector); |
| 73 | |
Sen Jiang | 55c4f9b | 2016-02-10 11:26:20 -0800 | [diff] [blame] | 74 | // Returns a string representing all extents in |extents|. |
| 75 | std::string ExtentsToString(const std::vector<Extent>& extents); |
Kelvin Zhang | 40d9666 | 2021-02-24 14:21:29 -0500 | [diff] [blame] | 76 | std::string ExtentsToString( |
| 77 | const google::protobuf::RepeatedPtrField<Extent>& extents); |
Sen Jiang | 55c4f9b | 2016-02-10 11:26:20 -0800 | [diff] [blame] | 78 | |
Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 79 | // Takes a pointer to extents |extents| and extents |extents_to_add|, and |
| 80 | // merges them by adding |extents_to_add| to |extents| and normalizing. |
| 81 | void ExtendExtents( |
Amin Hassani | 232f8f9 | 2019-01-14 16:15:31 -0800 | [diff] [blame] | 82 | google::protobuf::RepeatedPtrField<Extent>* extents, |
| 83 | const google::protobuf::RepeatedPtrField<Extent>& extents_to_add); |
Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 84 | |
Alex Deymo | 52490e7 | 2015-06-04 14:53:44 +0200 | [diff] [blame] | 85 | // Takes a vector of extents and normalizes those extents. Expects the extents |
| 86 | // to be sorted by start block. E.g. if |extents| is [(1, 2), (3, 5), (10, 2)] |
| 87 | // then |extents| will be changed to [(1, 7), (10, 2)]. |
| 88 | void NormalizeExtents(std::vector<Extent>* extents); |
| 89 | |
| 90 | // Return a subsequence of the list of blocks passed. Both the passed list of |
| 91 | // blocks |extents| and the return value are expressed as a list of Extent, not |
| 92 | // blocks. The returned list skips the first |block_offset| blocks from the |
| 93 | // |extents| and cotains |block_count| blocks (or less if |extents| is shorter). |
| 94 | std::vector<Extent> ExtentsSublist(const std::vector<Extent>& extents, |
Amin Hassani | 232f8f9 | 2019-01-14 16:15:31 -0800 | [diff] [blame] | 95 | uint64_t block_offset, |
| 96 | uint64_t block_count); |
Alex Deymo | 52490e7 | 2015-06-04 14:53:44 +0200 | [diff] [blame] | 97 | |
Kelvin Zhang | 4bb4920 | 2021-07-08 21:39:05 -0400 | [diff] [blame] | 98 | bool operator==(const Extent& a, const Extent& b) noexcept; |
| 99 | |
| 100 | bool operator!=(const Extent& a, const Extent& b) noexcept; |
Alex Deymo | 5c6c655 | 2015-06-03 19:06:50 +0200 | [diff] [blame] | 101 | |
Kelvin Zhang | b05e4e2 | 2020-09-25 16:16:19 -0400 | [diff] [blame] | 102 | // TODO(zhangkelvin) This is ugly. Rewrite using C++20's coroutine once |
| 103 | // that's available. Unfortunately with C++17 this is the best I could do. |
| 104 | |
| 105 | // An iterator that takes a sequence of extents, and iterate over blocks |
| 106 | // inside this sequence of extents. |
| 107 | // Example usage: |
| 108 | |
| 109 | // BlockIterator it1{src_extents}; |
| 110 | // while(!it1.is_end()) { |
| 111 | // auto block = *it1; |
| 112 | // Do stuff with |block| |
| 113 | // } |
| 114 | struct BlockIterator { |
| 115 | explicit BlockIterator( |
Kelvin Zhang | d11e2fc | 2022-10-24 15:40:30 -0700 | [diff] [blame] | 116 | const google::protobuf::RepeatedPtrField<Extent>& extents) |
| 117 | : extents_(extents) {} |
Kelvin Zhang | b05e4e2 | 2020-09-25 16:16:19 -0400 | [diff] [blame] | 118 | |
| 119 | BlockIterator& operator++() { |
Kelvin Zhang | d11e2fc | 2022-10-24 15:40:30 -0700 | [diff] [blame] | 120 | CHECK_LT(cur_extent_, extents_.size()); |
Kelvin Zhang | b05e4e2 | 2020-09-25 16:16:19 -0400 | [diff] [blame] | 121 | block_offset_++; |
Kelvin Zhang | d11e2fc | 2022-10-24 15:40:30 -0700 | [diff] [blame] | 122 | if (block_offset_ >= extents_[cur_extent_].num_blocks()) { |
Kelvin Zhang | b05e4e2 | 2020-09-25 16:16:19 -0400 | [diff] [blame] | 123 | cur_extent_++; |
| 124 | block_offset_ = 0; |
| 125 | } |
| 126 | return *this; |
| 127 | } |
| 128 | |
Kelvin Zhang | d11e2fc | 2022-10-24 15:40:30 -0700 | [diff] [blame] | 129 | [[nodiscard]] bool is_end() { return cur_extent_ >= extents_.size(); } |
Kelvin Zhang | b05e4e2 | 2020-09-25 16:16:19 -0400 | [diff] [blame] | 130 | [[nodiscard]] uint64_t operator*() { |
Kelvin Zhang | d11e2fc | 2022-10-24 15:40:30 -0700 | [diff] [blame] | 131 | return extents_[cur_extent_].start_block() + block_offset_; |
Kelvin Zhang | b05e4e2 | 2020-09-25 16:16:19 -0400 | [diff] [blame] | 132 | } |
| 133 | |
Kelvin Zhang | d11e2fc | 2022-10-24 15:40:30 -0700 | [diff] [blame] | 134 | const google::protobuf::RepeatedPtrField<Extent>& extents_; |
Kelvin Zhang | b05e4e2 | 2020-09-25 16:16:19 -0400 | [diff] [blame] | 135 | int cur_extent_ = 0; |
| 136 | size_t block_offset_ = 0; |
| 137 | }; |
| 138 | |
Kelvin Zhang | eb8703b | 2020-12-10 14:17:21 -0500 | [diff] [blame] | 139 | std::ostream& operator<<(std::ostream& out, const Extent& extent); |
Kelvin Zhang | 4bb4920 | 2021-07-08 21:39:05 -0400 | [diff] [blame] | 140 | std::ostream& operator<<(std::ostream& out, const std::vector<Extent>& extent); |
Kelvin Zhang | e532af3 | 2024-01-10 15:29:02 -0800 | [diff] [blame] | 141 | std::ostream& operator<<(std::ostream& out, const std::set<Extent>& extents); |
| 142 | |
| 143 | std::ostream& operator<<(std::ostream& out, |
| 144 | const std::set<Extent, ExtentLess>& extents); |
| 145 | std::ostream& operator<<( |
| 146 | std::ostream& out, |
| 147 | Range<std::set<Extent, ExtentLess>::const_iterator> range); |
| 148 | |
Kelvin Zhang | 4bb4920 | 2021-07-08 21:39:05 -0400 | [diff] [blame] | 149 | std::ostream& operator<<( |
| 150 | std::ostream& out, |
| 151 | const google::protobuf::RepeatedPtrField<Extent>& extent); |
Kelvin Zhang | eb8703b | 2020-12-10 14:17:21 -0500 | [diff] [blame] | 152 | |
Kelvin Zhang | 02df21b | 2021-01-07 14:55:18 -0500 | [diff] [blame] | 153 | template <typename Container> |
| 154 | size_t GetNthBlock(const Container& extents, const size_t n) { |
| 155 | size_t cur_block_count = 0; |
| 156 | for (const auto& extent : extents) { |
Kelvin Zhang | f7f7cc3 | 2021-07-15 16:14:00 -0400 | [diff] [blame] | 157 | if (n - cur_block_count < extent.num_blocks()) { |
Kelvin Zhang | 02df21b | 2021-01-07 14:55:18 -0500 | [diff] [blame] | 158 | return extent.start_block() + (n - cur_block_count); |
| 159 | } |
| 160 | cur_block_count += extent.num_blocks(); |
| 161 | } |
| 162 | return std::numeric_limits<size_t>::max(); |
| 163 | } |
| 164 | |
Kelvin Zhang | 52c4b79 | 2021-08-18 18:32:47 -0700 | [diff] [blame] | 165 | constexpr bool ExtentContains(const Extent& extent, size_t block) { |
| 166 | return extent.start_block() <= block && |
| 167 | block < extent.start_block() + extent.num_blocks(); |
| 168 | } |
| 169 | |
Kelvin Zhang | cb9932f | 2023-01-20 15:39:33 -0800 | [diff] [blame] | 170 | // return true iff |big| extent contains |small| extent |
| 171 | constexpr bool ExtentContains(const Extent& big, const Extent& small) { |
| 172 | return big.start_block() <= small.start_block() && |
| 173 | small.start_block() + small.num_blocks() <= |
| 174 | big.start_block() + big.num_blocks(); |
| 175 | } |
| 176 | |
Kelvin Zhang | 7949fce | 2024-03-12 10:17:40 -0700 | [diff] [blame] | 177 | template <typename T> |
| 178 | constexpr void Dedup(T* container) { |
| 179 | std::sort(container->begin(), container->end()); |
| 180 | container->erase(std::unique(container->begin(), container->end()), |
| 181 | container->end()); |
| 182 | } |
| 183 | |
Alex Deymo | 5c6c655 | 2015-06-03 19:06:50 +0200 | [diff] [blame] | 184 | } // namespace chromeos_update_engine |
| 185 | |
| 186 | #endif // UPDATE_ENGINE_PAYLOAD_GENERATOR_EXTENT_UTILS_H_ |