blob: d318fd2f65e0ef93358e1dd3c6d5660348cad524 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
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 Deymo5c6c6552015-06-03 19:06:50 +020016
17#include "update_engine/payload_generator/extent_utils.h"
18
Sen Jiang55c4f9b2016-02-10 11:26:20 -080019#include <inttypes.h>
20
Kokoa Matsuda1783eb32024-10-16 13:49:17 +090021#include <set>
Alex Deymo5c6c6552015-06-03 19:06:50 +020022#include <string>
Alex Deymo5c6c6552015-06-03 19:06:50 +020023#include <vector>
24
25#include <base/logging.h>
Kokoa Matsuda1783eb32024-10-16 13:49:17 +090026#include <android-base/macros.h>
Sen Jiang55c4f9b2016-02-10 11:26:20 -080027#include <base/strings/stringprintf.h>
Alex Deymo5c6c6552015-06-03 19:06:50 +020028
Alex Deymo39910dc2015-11-09 17:04:30 -080029#include "update_engine/payload_consumer/payload_constants.h"
Alex Deymo1beda782015-06-07 23:01:25 +020030#include "update_engine/payload_generator/extent_ranges.h"
Alex Deymo5c6c6552015-06-03 19:06:50 +020031
32using std::string;
33using std::vector;
34
35namespace chromeos_update_engine {
36
37void 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 Hassani232f8f92019-01-14 16:15:31 -080041 uint64_t next_block = extent.start_block() == kSparseHole
42 ? kSparseHole
43 : extent.start_block() + extent.num_blocks();
Alex Deymo5c6c6552015-06-03 19:06:50 +020044 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 Deymo14158572015-06-13 03:37:08 -070056void 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'.
72void 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|.
81void 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 Zhang40d96662021-02-24 14:21:29 -050089template <typename Container>
90string ExtentsToStringTemplate(const Container& extents) {
Sen Jiang55c4f9b2016-02-10 11:26:20 -080091 string ext_str;
92 for (const Extent& e : extents)
Tamas Berghammer9d66d762016-07-15 14:19:04 +010093 ext_str += base::StringPrintf("[%" PRIu64 ", %" PRIu64 "] ",
94 static_cast<uint64_t>(e.start_block()),
95 static_cast<uint64_t>(e.num_blocks()));
Sen Jiang55c4f9b2016-02-10 11:26:20 -080096 return ext_str;
97}
98
Kelvin Zhang40d96662021-02-24 14:21:29 -050099std::string ExtentsToString(const std::vector<Extent>& extents) {
100 return ExtentsToStringTemplate(extents);
101}
102
103std::string ExtentsToString(
104 const google::protobuf::RepeatedPtrField<Extent>& extents) {
105 return ExtentsToStringTemplate(extents);
106}
107
Alex Deymo52490e72015-06-04 14:53:44 +0200108void NormalizeExtents(vector<Extent>* extents) {
109 vector<Extent> new_extents;
110 for (const Extent& curr_ext : *extents) {
111 if (new_extents.empty()) {
112 new_extents.push_back(curr_ext);
113 continue;
114 }
115 Extent& last_ext = new_extents.back();
116 if (last_ext.start_block() + last_ext.num_blocks() ==
117 curr_ext.start_block()) {
118 // If the extents are touching, we want to combine them.
119 last_ext.set_num_blocks(last_ext.num_blocks() + curr_ext.num_blocks());
120 } else {
121 // Otherwise just include the extent as is.
122 new_extents.push_back(curr_ext);
123 }
124 }
125 *extents = new_extents;
126}
127
128vector<Extent> ExtentsSublist(const vector<Extent>& extents,
Amin Hassani232f8f92019-01-14 16:15:31 -0800129 uint64_t block_offset,
130 uint64_t block_count) {
Alex Deymo52490e72015-06-04 14:53:44 +0200131 vector<Extent> result;
132 uint64_t scanned_blocks = 0;
133 if (block_count == 0)
134 return result;
135 uint64_t end_block_offset = block_offset + block_count;
136 for (const Extent& extent : extents) {
137 // The loop invariant is that if |extents| has enough blocks, there's
138 // still some extent to add to |result|. This implies that at the beginning
139 // of the loop scanned_blocks < block_offset + block_count.
140 if (scanned_blocks + extent.num_blocks() > block_offset) {
141 // This case implies that |extent| has some overlapping with the requested
142 // subsequence.
143 uint64_t new_start = extent.start_block();
144 uint64_t new_num_blocks = extent.num_blocks();
145 if (scanned_blocks + new_num_blocks > end_block_offset) {
146 // Cut the end part of the extent.
147 new_num_blocks = end_block_offset - scanned_blocks;
148 }
149 if (block_offset > scanned_blocks) {
150 // Cut the begin part of the extent.
151 new_num_blocks -= block_offset - scanned_blocks;
152 new_start += block_offset - scanned_blocks;
153 }
154 result.push_back(ExtentForRange(new_start, new_num_blocks));
155 }
156 scanned_blocks += extent.num_blocks();
157 if (scanned_blocks >= end_block_offset)
158 break;
159 }
160 return result;
161}
162
Kelvin Zhang4bb49202021-07-08 21:39:05 -0400163bool operator==(const Extent& a, const Extent& b) noexcept {
Alex Deymo5c6c6552015-06-03 19:06:50 +0200164 return a.start_block() == b.start_block() && a.num_blocks() == b.num_blocks();
165}
166
Kelvin Zhang4bb49202021-07-08 21:39:05 -0400167bool operator!=(const Extent& a, const Extent& b) noexcept {
168 return !(a == b);
169}
170
Kelvin Zhangeb8703b2020-12-10 14:17:21 -0500171std::ostream& operator<<(std::ostream& out, const Extent& extent) {
Kelvin Zhange532af32024-01-10 15:29:02 -0800172 out << "(" << extent.start_block() << " - " << extent.num_blocks() << ")";
Kelvin Zhangeb8703b2020-12-10 14:17:21 -0500173 return out;
174}
175
Kelvin Zhang4bb49202021-07-08 21:39:05 -0400176template <typename T>
177std::ostream& PrintExtents(std::ostream& out, const T& extents) {
178 if (extents.begin() == extents.end()) {
179 out << "{}";
180 return out;
181 }
182 out << "{";
183 auto begin = extents.begin();
184 out << *begin;
185 for (const auto& ext : Range{++begin, extents.end()}) {
186 out << ", " << ext;
187 }
188 out << "}";
189 return out;
190}
191
192std::ostream& operator<<(std::ostream& out,
193 const std::vector<Extent>& extents) {
194 return PrintExtents(out, extents);
195}
196std::ostream& operator<<(
197 std::ostream& out,
198 const google::protobuf::RepeatedPtrField<Extent>& extents) {
199 return PrintExtents(out, extents);
200}
201
Kelvin Zhange532af32024-01-10 15:29:02 -0800202std::ostream& operator<<(std::ostream& out, const std::set<Extent>& extents) {
203 return PrintExtents(out, extents);
204}
205
206std::ostream& operator<<(std::ostream& out,
207 const std::set<Extent, ExtentLess>& extents) {
208 return PrintExtents(out, extents);
209}
210
211std::ostream& operator<<(
212 std::ostream& out,
213 Range<std::set<Extent, ExtentLess>::const_iterator> range) {
214 return PrintExtents(out, range);
215}
216
Alex Deymo5c6c6552015-06-03 19:06:50 +0200217} // namespace chromeos_update_engine