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 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 23 | #include "update_engine/payload_consumer/payload_constants.h" |
Alex Deymo | 5c6c655 | 2015-06-03 19:06:50 +0200 | [diff] [blame] | 24 | #include "update_engine/update_metadata.pb.h" |
| 25 | |
| 26 | // Utility functions for manipulating Extents and lists of blocks. |
| 27 | |
| 28 | namespace chromeos_update_engine { |
| 29 | |
| 30 | // |block| must either be the next block in the last extent or a block |
| 31 | // in the next extent. This function will not handle inserting block |
| 32 | // into an arbitrary place in the extents. |
| 33 | void AppendBlockToExtents(std::vector<Extent>* extents, uint64_t block); |
| 34 | |
| 35 | // Get/SetElement are intentionally overloaded so that templated functions |
| 36 | // can accept either type of collection of Extents. |
| 37 | Extent GetElement(const std::vector<Extent>& collection, size_t index); |
| 38 | Extent GetElement( |
| 39 | const google::protobuf::RepeatedPtrField<Extent>& collection, |
| 40 | size_t index); |
| 41 | |
Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 42 | // Return the total number of blocks in a collection (vector or |
| 43 | // RepeatedPtrField) of Extents. |
Alex Deymo | 5c6c655 | 2015-06-03 19:06:50 +0200 | [diff] [blame] | 44 | template<typename T> |
| 45 | uint64_t BlocksInExtents(const T& collection) { |
| 46 | uint64_t ret = 0; |
| 47 | for (size_t i = 0; i < static_cast<size_t>(collection.size()); ++i) { |
| 48 | ret += GetElement(collection, i).num_blocks(); |
| 49 | } |
| 50 | return ret; |
| 51 | } |
| 52 | |
Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 53 | // Takes a collection (vector or RepeatedPtrField) of Extent and |
| 54 | // returns a vector of the blocks referenced, in order. |
| 55 | template<typename T> |
| 56 | std::vector<uint64_t> ExpandExtents(const T& extents) { |
| 57 | std::vector<uint64_t> ret; |
| 58 | for (size_t i = 0, e = static_cast<size_t>(extents.size()); i != e; ++i) { |
| 59 | const Extent extent = GetElement(extents, i); |
| 60 | if (extent.start_block() == kSparseHole) { |
| 61 | ret.resize(ret.size() + extent.num_blocks(), kSparseHole); |
| 62 | } else { |
| 63 | for (uint64_t block = extent.start_block(); |
| 64 | block < (extent.start_block() + extent.num_blocks()); block++) { |
| 65 | ret.push_back(block); |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | return ret; |
| 70 | } |
| 71 | |
| 72 | // Stores all Extents in 'extents' into 'out'. |
| 73 | void StoreExtents(const std::vector<Extent>& extents, |
| 74 | google::protobuf::RepeatedPtrField<Extent>* out); |
| 75 | |
| 76 | // Stores all extents in |extents| into |out_vector|. |
| 77 | void ExtentsToVector(const google::protobuf::RepeatedPtrField<Extent>& extents, |
| 78 | std::vector<Extent>* out_vector); |
| 79 | |
Sen Jiang | 55c4f9b | 2016-02-10 11:26:20 -0800 | [diff] [blame^] | 80 | // Returns a string representing all extents in |extents|. |
| 81 | std::string ExtentsToString(const std::vector<Extent>& extents); |
| 82 | |
Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 83 | // Takes a pointer to extents |extents| and extents |extents_to_add|, and |
| 84 | // merges them by adding |extents_to_add| to |extents| and normalizing. |
| 85 | void ExtendExtents( |
| 86 | google::protobuf::RepeatedPtrField<Extent>* extents, |
| 87 | const google::protobuf::RepeatedPtrField<Extent>& extents_to_add); |
| 88 | |
Alex Deymo | 52490e7 | 2015-06-04 14:53:44 +0200 | [diff] [blame] | 89 | // Takes a vector of extents and normalizes those extents. Expects the extents |
| 90 | // to be sorted by start block. E.g. if |extents| is [(1, 2), (3, 5), (10, 2)] |
| 91 | // then |extents| will be changed to [(1, 7), (10, 2)]. |
| 92 | void NormalizeExtents(std::vector<Extent>* extents); |
| 93 | |
| 94 | // Return a subsequence of the list of blocks passed. Both the passed list of |
| 95 | // blocks |extents| and the return value are expressed as a list of Extent, not |
| 96 | // blocks. The returned list skips the first |block_offset| blocks from the |
| 97 | // |extents| and cotains |block_count| blocks (or less if |extents| is shorter). |
| 98 | std::vector<Extent> ExtentsSublist(const std::vector<Extent>& extents, |
| 99 | uint64_t block_offset, uint64_t block_count); |
| 100 | |
Alex Deymo | 5c6c655 | 2015-06-03 19:06:50 +0200 | [diff] [blame] | 101 | bool operator==(const Extent& a, const Extent& b); |
| 102 | |
| 103 | } // namespace chromeos_update_engine |
| 104 | |
| 105 | #endif // UPDATE_ENGINE_PAYLOAD_GENERATOR_EXTENT_UTILS_H_ |