Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2009 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 | #include "update_engine/payload_generator/extent_utils.h" |
| 18 | |
Sen Jiang | 55c4f9b | 2016-02-10 11:26:20 -0800 | [diff] [blame] | 19 | #include <inttypes.h> |
| 20 | |
Kokoa Matsuda | 1783eb3 | 2024-10-16 13:49:17 +0900 | [diff] [blame] | 21 | #include <set> |
Alex Deymo | 5c6c655 | 2015-06-03 19:06:50 +0200 | [diff] [blame] | 22 | #include <string> |
Alex Deymo | 5c6c655 | 2015-06-03 19:06:50 +0200 | [diff] [blame] | 23 | #include <vector> |
| 24 | |
| 25 | #include <base/logging.h> |
Kelvin Zhang | b9a9aa2 | 2024-10-15 10:38:35 -0700 | [diff] [blame^] | 26 | #include <base/macros.h> |
| 27 | #include <android-base/stringprintf.h> |
Alex Deymo | 5c6c655 | 2015-06-03 19:06:50 +0200 | [diff] [blame] | 28 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 29 | #include "update_engine/payload_consumer/payload_constants.h" |
Alex Deymo | 1beda78 | 2015-06-07 23:01:25 +0200 | [diff] [blame] | 30 | #include "update_engine/payload_generator/extent_ranges.h" |
Alex Deymo | 5c6c655 | 2015-06-03 19:06:50 +0200 | [diff] [blame] | 31 | |
| 32 | using std::string; |
| 33 | using std::vector; |
| 34 | |
| 35 | namespace chromeos_update_engine { |
| 36 | |
| 37 | void AppendBlockToExtents(vector<Extent>* extents, uint64_t block) { |
| 38 | // First try to extend the last extent in |extents|, if any. |
| 39 | if (!extents->empty()) { |
| 40 | Extent& extent = extents->back(); |
Amin Hassani | 232f8f9 | 2019-01-14 16:15:31 -0800 | [diff] [blame] | 41 | uint64_t next_block = extent.start_block() == kSparseHole |
| 42 | ? kSparseHole |
| 43 | : extent.start_block() + extent.num_blocks(); |
Alex Deymo | 5c6c655 | 2015-06-03 19:06:50 +0200 | [diff] [blame] | 44 | if (next_block == block) { |
| 45 | extent.set_num_blocks(extent.num_blocks() + 1); |
| 46 | return; |
| 47 | } |
| 48 | } |
| 49 | // If unable to extend the last extent, append a new single-block extent. |
| 50 | Extent new_extent; |
| 51 | new_extent.set_start_block(block); |
| 52 | new_extent.set_num_blocks(1); |
| 53 | extents->push_back(new_extent); |
| 54 | } |
| 55 | |
Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 56 | void ExtendExtents( |
| 57 | google::protobuf::RepeatedPtrField<Extent>* extents, |
| 58 | const google::protobuf::RepeatedPtrField<Extent>& extents_to_add) { |
| 59 | vector<Extent> extents_vector; |
| 60 | vector<Extent> extents_to_add_vector; |
| 61 | ExtentsToVector(*extents, &extents_vector); |
| 62 | ExtentsToVector(extents_to_add, &extents_to_add_vector); |
| 63 | extents_vector.insert(extents_vector.end(), |
| 64 | extents_to_add_vector.begin(), |
| 65 | extents_to_add_vector.end()); |
| 66 | NormalizeExtents(&extents_vector); |
| 67 | extents->Clear(); |
| 68 | StoreExtents(extents_vector, extents); |
| 69 | } |
| 70 | |
| 71 | // Stores all Extents in 'extents' into 'out'. |
| 72 | void StoreExtents(const vector<Extent>& extents, |
| 73 | google::protobuf::RepeatedPtrField<Extent>* out) { |
| 74 | for (const Extent& extent : extents) { |
| 75 | Extent* new_extent = out->Add(); |
| 76 | *new_extent = extent; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | // Stores all extents in |extents| into |out_vector|. |
| 81 | void ExtentsToVector(const google::protobuf::RepeatedPtrField<Extent>& extents, |
| 82 | vector<Extent>* out_vector) { |
| 83 | out_vector->clear(); |
| 84 | for (int i = 0; i < extents.size(); i++) { |
| 85 | out_vector->push_back(extents.Get(i)); |
| 86 | } |
| 87 | } |
| 88 | |
Kelvin Zhang | 40d9666 | 2021-02-24 14:21:29 -0500 | [diff] [blame] | 89 | template <typename Container> |
| 90 | string ExtentsToStringTemplate(const Container& extents) { |
Sen Jiang | 55c4f9b | 2016-02-10 11:26:20 -0800 | [diff] [blame] | 91 | string ext_str; |
| 92 | for (const Extent& e : extents) |
Kelvin Zhang | b9a9aa2 | 2024-10-15 10:38:35 -0700 | [diff] [blame^] | 93 | ext_str += |
| 94 | android::base::StringPrintf("[%" PRIu64 ", %" PRIu64 "] ", |
| 95 | static_cast<uint64_t>(e.start_block()), |
| 96 | static_cast<uint64_t>(e.num_blocks())); |
Sen Jiang | 55c4f9b | 2016-02-10 11:26:20 -0800 | [diff] [blame] | 97 | return ext_str; |
| 98 | } |
| 99 | |
Kelvin Zhang | 40d9666 | 2021-02-24 14:21:29 -0500 | [diff] [blame] | 100 | std::string ExtentsToString(const std::vector<Extent>& extents) { |
| 101 | return ExtentsToStringTemplate(extents); |
| 102 | } |
| 103 | |
| 104 | std::string ExtentsToString( |
| 105 | const google::protobuf::RepeatedPtrField<Extent>& extents) { |
| 106 | return ExtentsToStringTemplate(extents); |
| 107 | } |
| 108 | |
Alex Deymo | 52490e7 | 2015-06-04 14:53:44 +0200 | [diff] [blame] | 109 | void NormalizeExtents(vector<Extent>* extents) { |
| 110 | vector<Extent> new_extents; |
| 111 | for (const Extent& curr_ext : *extents) { |
| 112 | if (new_extents.empty()) { |
| 113 | new_extents.push_back(curr_ext); |
| 114 | continue; |
| 115 | } |
| 116 | Extent& last_ext = new_extents.back(); |
| 117 | if (last_ext.start_block() + last_ext.num_blocks() == |
| 118 | curr_ext.start_block()) { |
| 119 | // If the extents are touching, we want to combine them. |
| 120 | last_ext.set_num_blocks(last_ext.num_blocks() + curr_ext.num_blocks()); |
| 121 | } else { |
| 122 | // Otherwise just include the extent as is. |
| 123 | new_extents.push_back(curr_ext); |
| 124 | } |
| 125 | } |
| 126 | *extents = new_extents; |
| 127 | } |
| 128 | |
| 129 | vector<Extent> ExtentsSublist(const vector<Extent>& extents, |
Amin Hassani | 232f8f9 | 2019-01-14 16:15:31 -0800 | [diff] [blame] | 130 | uint64_t block_offset, |
| 131 | uint64_t block_count) { |
Alex Deymo | 52490e7 | 2015-06-04 14:53:44 +0200 | [diff] [blame] | 132 | vector<Extent> result; |
| 133 | uint64_t scanned_blocks = 0; |
| 134 | if (block_count == 0) |
| 135 | return result; |
| 136 | uint64_t end_block_offset = block_offset + block_count; |
| 137 | for (const Extent& extent : extents) { |
| 138 | // The loop invariant is that if |extents| has enough blocks, there's |
| 139 | // still some extent to add to |result|. This implies that at the beginning |
| 140 | // of the loop scanned_blocks < block_offset + block_count. |
| 141 | if (scanned_blocks + extent.num_blocks() > block_offset) { |
| 142 | // This case implies that |extent| has some overlapping with the requested |
| 143 | // subsequence. |
| 144 | uint64_t new_start = extent.start_block(); |
| 145 | uint64_t new_num_blocks = extent.num_blocks(); |
| 146 | if (scanned_blocks + new_num_blocks > end_block_offset) { |
| 147 | // Cut the end part of the extent. |
| 148 | new_num_blocks = end_block_offset - scanned_blocks; |
| 149 | } |
| 150 | if (block_offset > scanned_blocks) { |
| 151 | // Cut the begin part of the extent. |
| 152 | new_num_blocks -= block_offset - scanned_blocks; |
| 153 | new_start += block_offset - scanned_blocks; |
| 154 | } |
| 155 | result.push_back(ExtentForRange(new_start, new_num_blocks)); |
| 156 | } |
| 157 | scanned_blocks += extent.num_blocks(); |
| 158 | if (scanned_blocks >= end_block_offset) |
| 159 | break; |
| 160 | } |
| 161 | return result; |
| 162 | } |
| 163 | |
Kelvin Zhang | 4bb4920 | 2021-07-08 21:39:05 -0400 | [diff] [blame] | 164 | bool operator==(const Extent& a, const Extent& b) noexcept { |
Alex Deymo | 5c6c655 | 2015-06-03 19:06:50 +0200 | [diff] [blame] | 165 | return a.start_block() == b.start_block() && a.num_blocks() == b.num_blocks(); |
| 166 | } |
| 167 | |
Kelvin Zhang | 4bb4920 | 2021-07-08 21:39:05 -0400 | [diff] [blame] | 168 | bool operator!=(const Extent& a, const Extent& b) noexcept { |
| 169 | return !(a == b); |
| 170 | } |
| 171 | |
Kelvin Zhang | eb8703b | 2020-12-10 14:17:21 -0500 | [diff] [blame] | 172 | std::ostream& operator<<(std::ostream& out, const Extent& extent) { |
Kelvin Zhang | e532af3 | 2024-01-10 15:29:02 -0800 | [diff] [blame] | 173 | out << "(" << extent.start_block() << " - " << extent.num_blocks() << ")"; |
Kelvin Zhang | eb8703b | 2020-12-10 14:17:21 -0500 | [diff] [blame] | 174 | return out; |
| 175 | } |
| 176 | |
Kelvin Zhang | 4bb4920 | 2021-07-08 21:39:05 -0400 | [diff] [blame] | 177 | template <typename T> |
| 178 | std::ostream& PrintExtents(std::ostream& out, const T& extents) { |
| 179 | if (extents.begin() == extents.end()) { |
| 180 | out << "{}"; |
| 181 | return out; |
| 182 | } |
| 183 | out << "{"; |
| 184 | auto begin = extents.begin(); |
| 185 | out << *begin; |
| 186 | for (const auto& ext : Range{++begin, extents.end()}) { |
| 187 | out << ", " << ext; |
| 188 | } |
| 189 | out << "}"; |
| 190 | return out; |
| 191 | } |
| 192 | |
| 193 | std::ostream& operator<<(std::ostream& out, |
| 194 | const std::vector<Extent>& extents) { |
| 195 | return PrintExtents(out, extents); |
| 196 | } |
| 197 | std::ostream& operator<<( |
| 198 | std::ostream& out, |
| 199 | const google::protobuf::RepeatedPtrField<Extent>& extents) { |
| 200 | return PrintExtents(out, extents); |
| 201 | } |
| 202 | |
Kelvin Zhang | e532af3 | 2024-01-10 15:29:02 -0800 | [diff] [blame] | 203 | std::ostream& operator<<(std::ostream& out, const std::set<Extent>& extents) { |
| 204 | return PrintExtents(out, extents); |
| 205 | } |
| 206 | |
| 207 | std::ostream& operator<<(std::ostream& out, |
| 208 | const std::set<Extent, ExtentLess>& extents) { |
| 209 | return PrintExtents(out, extents); |
| 210 | } |
| 211 | |
| 212 | std::ostream& operator<<( |
| 213 | std::ostream& out, |
| 214 | Range<std::set<Extent, ExtentLess>::const_iterator> range) { |
| 215 | return PrintExtents(out, range); |
| 216 | } |
| 217 | |
Alex Deymo | 5c6c655 | 2015-06-03 19:06:50 +0200 | [diff] [blame] | 218 | } // namespace chromeos_update_engine |