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