blob: 9763b1f1bd16daaffaf589f0db799419a4893304 [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
Alex Deymo39910dc2015-11-09 17:04:30 -080023#include "update_engine/payload_consumer/payload_constants.h"
Alex Deymo5c6c6552015-06-03 19:06:50 +020024#include "update_engine/update_metadata.pb.h"
25
26// Utility functions for manipulating Extents and lists of blocks.
27
28namespace 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.
33void AppendBlockToExtents(std::vector<Extent>* extents, uint64_t block);
34
Alex Deymo14158572015-06-13 03:37:08 -070035// Takes a collection (vector or RepeatedPtrField) of Extent and
36// returns a vector of the blocks referenced, in order.
Amin Hassani232f8f92019-01-14 16:15:31 -080037template <typename T>
Alex Deymo14158572015-06-13 03:37:08 -070038std::vector<uint64_t> ExpandExtents(const T& extents) {
39 std::vector<uint64_t> ret;
Amin Hassanid8b67f42017-12-06 13:47:52 -080040 for (const auto& extent : extents) {
Alex Deymo14158572015-06-13 03:37:08 -070041 if (extent.start_block() == kSparseHole) {
42 ret.resize(ret.size() + extent.num_blocks(), kSparseHole);
43 } else {
44 for (uint64_t block = extent.start_block();
Amin Hassani232f8f92019-01-14 16:15:31 -080045 block < (extent.start_block() + extent.num_blocks());
46 block++) {
Alex Deymo14158572015-06-13 03:37:08 -070047 ret.push_back(block);
48 }
49 }
50 }
51 return ret;
52}
53
54// Stores all Extents in 'extents' into 'out'.
55void StoreExtents(const std::vector<Extent>& extents,
56 google::protobuf::RepeatedPtrField<Extent>* out);
57
58// Stores all extents in |extents| into |out_vector|.
59void ExtentsToVector(const google::protobuf::RepeatedPtrField<Extent>& extents,
60 std::vector<Extent>* out_vector);
61
Sen Jiang55c4f9b2016-02-10 11:26:20 -080062// Returns a string representing all extents in |extents|.
63std::string ExtentsToString(const std::vector<Extent>& extents);
64
Alex Deymo14158572015-06-13 03:37:08 -070065// Takes a pointer to extents |extents| and extents |extents_to_add|, and
66// merges them by adding |extents_to_add| to |extents| and normalizing.
67void ExtendExtents(
Amin Hassani232f8f92019-01-14 16:15:31 -080068 google::protobuf::RepeatedPtrField<Extent>* extents,
69 const google::protobuf::RepeatedPtrField<Extent>& extents_to_add);
Alex Deymo14158572015-06-13 03:37:08 -070070
Alex Deymo52490e72015-06-04 14:53:44 +020071// Takes a vector of extents and normalizes those extents. Expects the extents
72// to be sorted by start block. E.g. if |extents| is [(1, 2), (3, 5), (10, 2)]
73// then |extents| will be changed to [(1, 7), (10, 2)].
74void NormalizeExtents(std::vector<Extent>* extents);
75
76// Return a subsequence of the list of blocks passed. Both the passed list of
77// blocks |extents| and the return value are expressed as a list of Extent, not
78// blocks. The returned list skips the first |block_offset| blocks from the
79// |extents| and cotains |block_count| blocks (or less if |extents| is shorter).
80std::vector<Extent> ExtentsSublist(const std::vector<Extent>& extents,
Amin Hassani232f8f92019-01-14 16:15:31 -080081 uint64_t block_offset,
82 uint64_t block_count);
Alex Deymo52490e72015-06-04 14:53:44 +020083
Alex Deymo5c6c6552015-06-03 19:06:50 +020084bool operator==(const Extent& a, const Extent& b);
85
86} // namespace chromeos_update_engine
87
88#endif // UPDATE_ENGINE_PAYLOAD_GENERATOR_EXTENT_UTILS_H_