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 | |
Sen Jiang | 55c4f9b | 2016-02-10 11:26:20 -0800 | [diff] [blame] | 20 | #include <string> |
Alex Deymo | 5c6c655 | 2015-06-03 19:06:50 +0200 | [diff] [blame] | 21 | #include <vector> |
| 22 | |
Kelvin Zhang | b05e4e2 | 2020-09-25 16:16:19 -0400 | [diff] [blame^] | 23 | #include <base/logging.h> |
| 24 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 25 | #include "update_engine/payload_consumer/payload_constants.h" |
Alex Deymo | 5c6c655 | 2015-06-03 19:06:50 +0200 | [diff] [blame] | 26 | #include "update_engine/update_metadata.pb.h" |
| 27 | |
| 28 | // Utility functions for manipulating Extents and lists of blocks. |
| 29 | |
| 30 | namespace chromeos_update_engine { |
| 31 | |
| 32 | // |block| must either be the next block in the last extent or a block |
| 33 | // in the next extent. This function will not handle inserting block |
| 34 | // into an arbitrary place in the extents. |
| 35 | void AppendBlockToExtents(std::vector<Extent>* extents, uint64_t block); |
| 36 | |
Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 37 | // Takes a collection (vector or RepeatedPtrField) of Extent and |
| 38 | // returns a vector of the blocks referenced, in order. |
Amin Hassani | 232f8f9 | 2019-01-14 16:15:31 -0800 | [diff] [blame] | 39 | template <typename T> |
Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 40 | std::vector<uint64_t> ExpandExtents(const T& extents) { |
| 41 | std::vector<uint64_t> ret; |
Amin Hassani | d8b67f4 | 2017-12-06 13:47:52 -0800 | [diff] [blame] | 42 | for (const auto& extent : extents) { |
Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 43 | if (extent.start_block() == kSparseHole) { |
| 44 | ret.resize(ret.size() + extent.num_blocks(), kSparseHole); |
| 45 | } else { |
| 46 | for (uint64_t block = extent.start_block(); |
Amin Hassani | 232f8f9 | 2019-01-14 16:15:31 -0800 | [diff] [blame] | 47 | block < (extent.start_block() + extent.num_blocks()); |
| 48 | block++) { |
Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 49 | ret.push_back(block); |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | return ret; |
| 54 | } |
| 55 | |
| 56 | // Stores all Extents in 'extents' into 'out'. |
| 57 | void StoreExtents(const std::vector<Extent>& extents, |
| 58 | google::protobuf::RepeatedPtrField<Extent>* out); |
| 59 | |
| 60 | // Stores all extents in |extents| into |out_vector|. |
| 61 | void ExtentsToVector(const google::protobuf::RepeatedPtrField<Extent>& extents, |
| 62 | std::vector<Extent>* out_vector); |
| 63 | |
Sen Jiang | 55c4f9b | 2016-02-10 11:26:20 -0800 | [diff] [blame] | 64 | // Returns a string representing all extents in |extents|. |
| 65 | std::string ExtentsToString(const std::vector<Extent>& extents); |
| 66 | |
Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 67 | // Takes a pointer to extents |extents| and extents |extents_to_add|, and |
| 68 | // merges them by adding |extents_to_add| to |extents| and normalizing. |
| 69 | void ExtendExtents( |
Amin Hassani | 232f8f9 | 2019-01-14 16:15:31 -0800 | [diff] [blame] | 70 | google::protobuf::RepeatedPtrField<Extent>* extents, |
| 71 | const google::protobuf::RepeatedPtrField<Extent>& extents_to_add); |
Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 72 | |
Alex Deymo | 52490e7 | 2015-06-04 14:53:44 +0200 | [diff] [blame] | 73 | // Takes a vector of extents and normalizes those extents. Expects the extents |
| 74 | // to be sorted by start block. E.g. if |extents| is [(1, 2), (3, 5), (10, 2)] |
| 75 | // then |extents| will be changed to [(1, 7), (10, 2)]. |
| 76 | void NormalizeExtents(std::vector<Extent>* extents); |
| 77 | |
| 78 | // Return a subsequence of the list of blocks passed. Both the passed list of |
| 79 | // blocks |extents| and the return value are expressed as a list of Extent, not |
| 80 | // blocks. The returned list skips the first |block_offset| blocks from the |
| 81 | // |extents| and cotains |block_count| blocks (or less if |extents| is shorter). |
| 82 | std::vector<Extent> ExtentsSublist(const std::vector<Extent>& extents, |
Amin Hassani | 232f8f9 | 2019-01-14 16:15:31 -0800 | [diff] [blame] | 83 | uint64_t block_offset, |
| 84 | uint64_t block_count); |
Alex Deymo | 52490e7 | 2015-06-04 14:53:44 +0200 | [diff] [blame] | 85 | |
Alex Deymo | 5c6c655 | 2015-06-03 19:06:50 +0200 | [diff] [blame] | 86 | bool operator==(const Extent& a, const Extent& b); |
| 87 | |
Kelvin Zhang | b05e4e2 | 2020-09-25 16:16:19 -0400 | [diff] [blame^] | 88 | // TODO(zhangkelvin) This is ugly. Rewrite using C++20's coroutine once |
| 89 | // that's available. Unfortunately with C++17 this is the best I could do. |
| 90 | |
| 91 | // An iterator that takes a sequence of extents, and iterate over blocks |
| 92 | // inside this sequence of extents. |
| 93 | // Example usage: |
| 94 | |
| 95 | // BlockIterator it1{src_extents}; |
| 96 | // while(!it1.is_end()) { |
| 97 | // auto block = *it1; |
| 98 | // Do stuff with |block| |
| 99 | // } |
| 100 | struct BlockIterator { |
| 101 | explicit BlockIterator( |
| 102 | const google::protobuf::RepeatedPtrField<Extent>& src_extents) |
| 103 | : src_extents_(src_extents) {} |
| 104 | |
| 105 | BlockIterator& operator++() { |
| 106 | CHECK_LT(cur_extent_, src_extents_.size()); |
| 107 | block_offset_++; |
| 108 | if (block_offset_ >= src_extents_[cur_extent_].num_blocks()) { |
| 109 | cur_extent_++; |
| 110 | block_offset_ = 0; |
| 111 | } |
| 112 | return *this; |
| 113 | } |
| 114 | |
| 115 | [[nodiscard]] bool is_end() { return cur_extent_ >= src_extents_.size(); } |
| 116 | [[nodiscard]] uint64_t operator*() { |
| 117 | return src_extents_[cur_extent_].start_block() + block_offset_; |
| 118 | } |
| 119 | |
| 120 | const google::protobuf::RepeatedPtrField<Extent>& src_extents_; |
| 121 | int cur_extent_ = 0; |
| 122 | size_t block_offset_ = 0; |
| 123 | }; |
| 124 | |
Alex Deymo | 5c6c655 | 2015-06-03 19:06:50 +0200 | [diff] [blame] | 125 | } // namespace chromeos_update_engine |
| 126 | |
| 127 | #endif // UPDATE_ENGINE_PAYLOAD_GENERATOR_EXTENT_UTILS_H_ |