blob: f4a9ff0ec6730c4048fd44e54309875e3211815a [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
Alex Deymo5c6c6552015-06-03 19:06:50 +020021#include <string>
22#include <utility>
23#include <vector>
24
25#include <base/logging.h>
26#include <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 Deymo5c6c6552015-06-03 19:06:50 +020030#include "update_engine/payload_generator/annotated_operation.h"
Alex Deymo1beda782015-06-07 23:01:25 +020031#include "update_engine/payload_generator/extent_ranges.h"
Alex Deymo5c6c6552015-06-03 19:06:50 +020032
33using std::string;
34using std::vector;
35
36namespace chromeos_update_engine {
37
38void AppendBlockToExtents(vector<Extent>* extents, uint64_t block) {
39 // First try to extend the last extent in |extents|, if any.
40 if (!extents->empty()) {
41 Extent& extent = extents->back();
Amin Hassani232f8f92019-01-14 16:15:31 -080042 uint64_t next_block = extent.start_block() == kSparseHole
43 ? kSparseHole
44 : extent.start_block() + extent.num_blocks();
Alex Deymo5c6c6552015-06-03 19:06:50 +020045 if (next_block == block) {
46 extent.set_num_blocks(extent.num_blocks() + 1);
47 return;
48 }
49 }
50 // If unable to extend the last extent, append a new single-block extent.
51 Extent new_extent;
52 new_extent.set_start_block(block);
53 new_extent.set_num_blocks(1);
54 extents->push_back(new_extent);
55}
56
Alex Deymo14158572015-06-13 03:37:08 -070057void ExtendExtents(
58 google::protobuf::RepeatedPtrField<Extent>* extents,
59 const google::protobuf::RepeatedPtrField<Extent>& extents_to_add) {
60 vector<Extent> extents_vector;
61 vector<Extent> extents_to_add_vector;
62 ExtentsToVector(*extents, &extents_vector);
63 ExtentsToVector(extents_to_add, &extents_to_add_vector);
64 extents_vector.insert(extents_vector.end(),
65 extents_to_add_vector.begin(),
66 extents_to_add_vector.end());
67 NormalizeExtents(&extents_vector);
68 extents->Clear();
69 StoreExtents(extents_vector, extents);
70}
71
72// Stores all Extents in 'extents' into 'out'.
73void StoreExtents(const vector<Extent>& extents,
74 google::protobuf::RepeatedPtrField<Extent>* out) {
75 for (const Extent& extent : extents) {
76 Extent* new_extent = out->Add();
77 *new_extent = extent;
78 }
79}
80
81// Stores all extents in |extents| into |out_vector|.
82void ExtentsToVector(const google::protobuf::RepeatedPtrField<Extent>& extents,
83 vector<Extent>* out_vector) {
84 out_vector->clear();
85 for (int i = 0; i < extents.size(); i++) {
86 out_vector->push_back(extents.Get(i));
87 }
88}
89
Kelvin Zhang40d96662021-02-24 14:21:29 -050090template <typename Container>
91string ExtentsToStringTemplate(const Container& extents) {
Sen Jiang55c4f9b2016-02-10 11:26:20 -080092 string ext_str;
93 for (const Extent& e : extents)
Tamas Berghammer9d66d762016-07-15 14:19:04 +010094 ext_str += base::StringPrintf("[%" PRIu64 ", %" PRIu64 "] ",
95 static_cast<uint64_t>(e.start_block()),
96 static_cast<uint64_t>(e.num_blocks()));
Sen Jiang55c4f9b2016-02-10 11:26:20 -080097 return ext_str;
98}
99
Kelvin Zhang40d96662021-02-24 14:21:29 -0500100std::string ExtentsToString(const std::vector<Extent>& extents) {
101 return ExtentsToStringTemplate(extents);
102}
103
104std::string ExtentsToString(
105 const google::protobuf::RepeatedPtrField<Extent>& extents) {
106 return ExtentsToStringTemplate(extents);
107}
108
Alex Deymo52490e72015-06-04 14:53:44 +0200109void 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
129vector<Extent> ExtentsSublist(const vector<Extent>& extents,
Amin Hassani232f8f92019-01-14 16:15:31 -0800130 uint64_t block_offset,
131 uint64_t block_count) {
Alex Deymo52490e72015-06-04 14:53:44 +0200132 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
Alex Deymo5c6c6552015-06-03 19:06:50 +0200164bool operator==(const Extent& a, const Extent& b) {
165 return a.start_block() == b.start_block() && a.num_blocks() == b.num_blocks();
166}
167
Kelvin Zhangeb8703b2020-12-10 14:17:21 -0500168std::ostream& operator<<(std::ostream& out, const Extent& extent) {
169 out << "[" << extent.start_block() << " - "
170 << extent.start_block() + extent.num_blocks() - 1 << "]";
171 return out;
172}
173
Alex Deymo5c6c6552015-06-03 19:06:50 +0200174} // namespace chromeos_update_engine