blob: 712cc92ccf4cb7f8e619b11b21e5934b70b686ae [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
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 Deymo5c6c6552015-06-03 19:06:50 +020016
17#ifndef UPDATE_ENGINE_PAYLOAD_GENERATOR_EXTENT_UTILS_H_
18#define UPDATE_ENGINE_PAYLOAD_GENERATOR_EXTENT_UTILS_H_
19
Sen Jiang55c4f9b2016-02-10 11:26:20 -080020#include <string>
Alex Deymo5c6c6552015-06-03 19:06:50 +020021#include <vector>
22
Kelvin Zhangb05e4e22020-09-25 16:16:19 -040023#include <base/logging.h>
24
Kelvin Zhang40d96662021-02-24 14:21:29 -050025#include "google/protobuf/repeated_field.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080026#include "update_engine/payload_consumer/payload_constants.h"
Alex Deymo5c6c6552015-06-03 19:06:50 +020027#include "update_engine/update_metadata.pb.h"
28
29// Utility functions for manipulating Extents and lists of blocks.
30
31namespace chromeos_update_engine {
32
33// |block| must either be the next block in the last extent or a block
34// in the next extent. This function will not handle inserting block
35// into an arbitrary place in the extents.
36void AppendBlockToExtents(std::vector<Extent>* extents, uint64_t block);
37
Alex Deymo14158572015-06-13 03:37:08 -070038// Takes a collection (vector or RepeatedPtrField) of Extent and
39// returns a vector of the blocks referenced, in order.
Amin Hassani232f8f92019-01-14 16:15:31 -080040template <typename T>
Alex Deymo14158572015-06-13 03:37:08 -070041std::vector<uint64_t> ExpandExtents(const T& extents) {
42 std::vector<uint64_t> ret;
Amin Hassanid8b67f42017-12-06 13:47:52 -080043 for (const auto& extent : extents) {
Alex Deymo14158572015-06-13 03:37:08 -070044 if (extent.start_block() == kSparseHole) {
45 ret.resize(ret.size() + extent.num_blocks(), kSparseHole);
46 } else {
47 for (uint64_t block = extent.start_block();
Amin Hassani232f8f92019-01-14 16:15:31 -080048 block < (extent.start_block() + extent.num_blocks());
49 block++) {
Alex Deymo14158572015-06-13 03:37:08 -070050 ret.push_back(block);
51 }
52 }
53 }
54 return ret;
55}
56
57// Stores all Extents in 'extents' into 'out'.
58void StoreExtents(const std::vector<Extent>& extents,
59 google::protobuf::RepeatedPtrField<Extent>* out);
60
61// Stores all extents in |extents| into |out_vector|.
62void ExtentsToVector(const google::protobuf::RepeatedPtrField<Extent>& extents,
63 std::vector<Extent>* out_vector);
64
Sen Jiang55c4f9b2016-02-10 11:26:20 -080065// Returns a string representing all extents in |extents|.
66std::string ExtentsToString(const std::vector<Extent>& extents);
Kelvin Zhang40d96662021-02-24 14:21:29 -050067std::string ExtentsToString(
68 const google::protobuf::RepeatedPtrField<Extent>& extents);
Sen Jiang55c4f9b2016-02-10 11:26:20 -080069
Alex Deymo14158572015-06-13 03:37:08 -070070// Takes a pointer to extents |extents| and extents |extents_to_add|, and
71// merges them by adding |extents_to_add| to |extents| and normalizing.
72void ExtendExtents(
Amin Hassani232f8f92019-01-14 16:15:31 -080073 google::protobuf::RepeatedPtrField<Extent>* extents,
74 const google::protobuf::RepeatedPtrField<Extent>& extents_to_add);
Alex Deymo14158572015-06-13 03:37:08 -070075
Alex Deymo52490e72015-06-04 14:53:44 +020076// Takes a vector of extents and normalizes those extents. Expects the extents
77// to be sorted by start block. E.g. if |extents| is [(1, 2), (3, 5), (10, 2)]
78// then |extents| will be changed to [(1, 7), (10, 2)].
79void NormalizeExtents(std::vector<Extent>* extents);
80
81// Return a subsequence of the list of blocks passed. Both the passed list of
82// blocks |extents| and the return value are expressed as a list of Extent, not
83// blocks. The returned list skips the first |block_offset| blocks from the
84// |extents| and cotains |block_count| blocks (or less if |extents| is shorter).
85std::vector<Extent> ExtentsSublist(const std::vector<Extent>& extents,
Amin Hassani232f8f92019-01-14 16:15:31 -080086 uint64_t block_offset,
87 uint64_t block_count);
Alex Deymo52490e72015-06-04 14:53:44 +020088
Kelvin Zhang4bb49202021-07-08 21:39:05 -040089bool operator==(const Extent& a, const Extent& b) noexcept;
90
91bool operator!=(const Extent& a, const Extent& b) noexcept;
Alex Deymo5c6c6552015-06-03 19:06:50 +020092
Kelvin Zhangb05e4e22020-09-25 16:16:19 -040093// TODO(zhangkelvin) This is ugly. Rewrite using C++20's coroutine once
94// that's available. Unfortunately with C++17 this is the best I could do.
95
96// An iterator that takes a sequence of extents, and iterate over blocks
97// inside this sequence of extents.
98// Example usage:
99
100// BlockIterator it1{src_extents};
101// while(!it1.is_end()) {
102// auto block = *it1;
103// Do stuff with |block|
104// }
105struct BlockIterator {
106 explicit BlockIterator(
107 const google::protobuf::RepeatedPtrField<Extent>& src_extents)
108 : src_extents_(src_extents) {}
109
110 BlockIterator& operator++() {
111 CHECK_LT(cur_extent_, src_extents_.size());
112 block_offset_++;
113 if (block_offset_ >= src_extents_[cur_extent_].num_blocks()) {
114 cur_extent_++;
115 block_offset_ = 0;
116 }
117 return *this;
118 }
119
120 [[nodiscard]] bool is_end() { return cur_extent_ >= src_extents_.size(); }
121 [[nodiscard]] uint64_t operator*() {
122 return src_extents_[cur_extent_].start_block() + block_offset_;
123 }
124
125 const google::protobuf::RepeatedPtrField<Extent>& src_extents_;
126 int cur_extent_ = 0;
127 size_t block_offset_ = 0;
128};
129
Kelvin Zhangeb8703b2020-12-10 14:17:21 -0500130std::ostream& operator<<(std::ostream& out, const Extent& extent);
Kelvin Zhang4bb49202021-07-08 21:39:05 -0400131std::ostream& operator<<(std::ostream& out, const std::vector<Extent>& extent);
132std::ostream& operator<<(
133 std::ostream& out,
134 const google::protobuf::RepeatedPtrField<Extent>& extent);
Kelvin Zhangeb8703b2020-12-10 14:17:21 -0500135
Kelvin Zhang02df21b2021-01-07 14:55:18 -0500136template <typename Container>
137size_t GetNthBlock(const Container& extents, const size_t n) {
138 size_t cur_block_count = 0;
139 for (const auto& extent : extents) {
Kelvin Zhangf7f7cc32021-07-15 16:14:00 -0400140 if (n - cur_block_count < extent.num_blocks()) {
Kelvin Zhang02df21b2021-01-07 14:55:18 -0500141 return extent.start_block() + (n - cur_block_count);
142 }
143 cur_block_count += extent.num_blocks();
144 }
145 return std::numeric_limits<size_t>::max();
146}
147
Kelvin Zhang52c4b792021-08-18 18:32:47 -0700148constexpr bool ExtentContains(const Extent& extent, size_t block) {
149 return extent.start_block() <= block &&
150 block < extent.start_block() + extent.num_blocks();
151}
152
Alex Deymo5c6c6552015-06-03 19:06:50 +0200153} // namespace chromeos_update_engine
154
155#endif // UPDATE_ENGINE_PAYLOAD_GENERATOR_EXTENT_UTILS_H_