1aea4c1cea20dda7ae7e85fc8924a2d784f70d806Alex Deymo// 2aea4c1cea20dda7ae7e85fc8924a2d784f70d806Alex Deymo// Copyright (C) 2015 The Android Open Source Project 3aea4c1cea20dda7ae7e85fc8924a2d784f70d806Alex Deymo// 4aea4c1cea20dda7ae7e85fc8924a2d784f70d806Alex Deymo// Licensed under the Apache License, Version 2.0 (the "License"); 5aea4c1cea20dda7ae7e85fc8924a2d784f70d806Alex Deymo// you may not use this file except in compliance with the License. 6aea4c1cea20dda7ae7e85fc8924a2d784f70d806Alex Deymo// You may obtain a copy of the License at 7aea4c1cea20dda7ae7e85fc8924a2d784f70d806Alex Deymo// 8aea4c1cea20dda7ae7e85fc8924a2d784f70d806Alex Deymo// http://www.apache.org/licenses/LICENSE-2.0 9aea4c1cea20dda7ae7e85fc8924a2d784f70d806Alex Deymo// 10aea4c1cea20dda7ae7e85fc8924a2d784f70d806Alex Deymo// Unless required by applicable law or agreed to in writing, software 11aea4c1cea20dda7ae7e85fc8924a2d784f70d806Alex Deymo// distributed under the License is distributed on an "AS IS" BASIS, 12aea4c1cea20dda7ae7e85fc8924a2d784f70d806Alex Deymo// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13aea4c1cea20dda7ae7e85fc8924a2d784f70d806Alex Deymo// See the License for the specific language governing permissions and 14aea4c1cea20dda7ae7e85fc8924a2d784f70d806Alex Deymo// limitations under the License. 15aea4c1cea20dda7ae7e85fc8924a2d784f70d806Alex Deymo// 165c6c65570013bbdbd67f9bf6391dd295ef5b5ee6Alex Deymo 175c6c65570013bbdbd67f9bf6391dd295ef5b5ee6Alex Deymo#ifndef UPDATE_ENGINE_PAYLOAD_GENERATOR_EXTENT_UTILS_H_ 185c6c65570013bbdbd67f9bf6391dd295ef5b5ee6Alex Deymo#define UPDATE_ENGINE_PAYLOAD_GENERATOR_EXTENT_UTILS_H_ 195c6c65570013bbdbd67f9bf6391dd295ef5b5ee6Alex Deymo 20f96cc198264161749d60169cb0b8d5d0015303d1Sen Jiang#include <string> 215c6c65570013bbdbd67f9bf6391dd295ef5b5ee6Alex Deymo#include <vector> 225c6c65570013bbdbd67f9bf6391dd295ef5b5ee6Alex Deymo 2339910dcd1d68987ccee7c3031dc269233a8490bbAlex Deymo#include "update_engine/payload_consumer/payload_constants.h" 245c6c65570013bbdbd67f9bf6391dd295ef5b5ee6Alex Deymo#include "update_engine/update_metadata.pb.h" 255c6c65570013bbdbd67f9bf6391dd295ef5b5ee6Alex Deymo 265c6c65570013bbdbd67f9bf6391dd295ef5b5ee6Alex Deymo// Utility functions for manipulating Extents and lists of blocks. 275c6c65570013bbdbd67f9bf6391dd295ef5b5ee6Alex Deymo 285c6c65570013bbdbd67f9bf6391dd295ef5b5ee6Alex Deymonamespace chromeos_update_engine { 295c6c65570013bbdbd67f9bf6391dd295ef5b5ee6Alex Deymo 305c6c65570013bbdbd67f9bf6391dd295ef5b5ee6Alex Deymo// |block| must either be the next block in the last extent or a block 315c6c65570013bbdbd67f9bf6391dd295ef5b5ee6Alex Deymo// in the next extent. This function will not handle inserting block 325c6c65570013bbdbd67f9bf6391dd295ef5b5ee6Alex Deymo// into an arbitrary place in the extents. 335c6c65570013bbdbd67f9bf6391dd295ef5b5ee6Alex Deymovoid AppendBlockToExtents(std::vector<Extent>* extents, uint64_t block); 345c6c65570013bbdbd67f9bf6391dd295ef5b5ee6Alex Deymo 355c6c65570013bbdbd67f9bf6391dd295ef5b5ee6Alex Deymo// Get/SetElement are intentionally overloaded so that templated functions 365c6c65570013bbdbd67f9bf6391dd295ef5b5ee6Alex Deymo// can accept either type of collection of Extents. 375c6c65570013bbdbd67f9bf6391dd295ef5b5ee6Alex DeymoExtent GetElement(const std::vector<Extent>& collection, size_t index); 385c6c65570013bbdbd67f9bf6391dd295ef5b5ee6Alex DeymoExtent GetElement( 395c6c65570013bbdbd67f9bf6391dd295ef5b5ee6Alex Deymo const google::protobuf::RepeatedPtrField<Extent>& collection, 405c6c65570013bbdbd67f9bf6391dd295ef5b5ee6Alex Deymo size_t index); 415c6c65570013bbdbd67f9bf6391dd295ef5b5ee6Alex Deymo 4214158570d3995008dc93a628004118b87a6bca01Alex Deymo// Return the total number of blocks in a collection (vector or 4314158570d3995008dc93a628004118b87a6bca01Alex Deymo// RepeatedPtrField) of Extents. 445c6c65570013bbdbd67f9bf6391dd295ef5b5ee6Alex Deymotemplate<typename T> 455c6c65570013bbdbd67f9bf6391dd295ef5b5ee6Alex Deymouint64_t BlocksInExtents(const T& collection) { 465c6c65570013bbdbd67f9bf6391dd295ef5b5ee6Alex Deymo uint64_t ret = 0; 475c6c65570013bbdbd67f9bf6391dd295ef5b5ee6Alex Deymo for (size_t i = 0; i < static_cast<size_t>(collection.size()); ++i) { 485c6c65570013bbdbd67f9bf6391dd295ef5b5ee6Alex Deymo ret += GetElement(collection, i).num_blocks(); 495c6c65570013bbdbd67f9bf6391dd295ef5b5ee6Alex Deymo } 505c6c65570013bbdbd67f9bf6391dd295ef5b5ee6Alex Deymo return ret; 515c6c65570013bbdbd67f9bf6391dd295ef5b5ee6Alex Deymo} 525c6c65570013bbdbd67f9bf6391dd295ef5b5ee6Alex Deymo 5314158570d3995008dc93a628004118b87a6bca01Alex Deymo// Takes a collection (vector or RepeatedPtrField) of Extent and 5414158570d3995008dc93a628004118b87a6bca01Alex Deymo// returns a vector of the blocks referenced, in order. 5514158570d3995008dc93a628004118b87a6bca01Alex Deymotemplate<typename T> 5614158570d3995008dc93a628004118b87a6bca01Alex Deymostd::vector<uint64_t> ExpandExtents(const T& extents) { 5714158570d3995008dc93a628004118b87a6bca01Alex Deymo std::vector<uint64_t> ret; 5814158570d3995008dc93a628004118b87a6bca01Alex Deymo for (size_t i = 0, e = static_cast<size_t>(extents.size()); i != e; ++i) { 5914158570d3995008dc93a628004118b87a6bca01Alex Deymo const Extent extent = GetElement(extents, i); 6014158570d3995008dc93a628004118b87a6bca01Alex Deymo if (extent.start_block() == kSparseHole) { 6114158570d3995008dc93a628004118b87a6bca01Alex Deymo ret.resize(ret.size() + extent.num_blocks(), kSparseHole); 6214158570d3995008dc93a628004118b87a6bca01Alex Deymo } else { 6314158570d3995008dc93a628004118b87a6bca01Alex Deymo for (uint64_t block = extent.start_block(); 6414158570d3995008dc93a628004118b87a6bca01Alex Deymo block < (extent.start_block() + extent.num_blocks()); block++) { 6514158570d3995008dc93a628004118b87a6bca01Alex Deymo ret.push_back(block); 6614158570d3995008dc93a628004118b87a6bca01Alex Deymo } 6714158570d3995008dc93a628004118b87a6bca01Alex Deymo } 6814158570d3995008dc93a628004118b87a6bca01Alex Deymo } 6914158570d3995008dc93a628004118b87a6bca01Alex Deymo return ret; 7014158570d3995008dc93a628004118b87a6bca01Alex Deymo} 7114158570d3995008dc93a628004118b87a6bca01Alex Deymo 7214158570d3995008dc93a628004118b87a6bca01Alex Deymo// Stores all Extents in 'extents' into 'out'. 7314158570d3995008dc93a628004118b87a6bca01Alex Deymovoid StoreExtents(const std::vector<Extent>& extents, 7414158570d3995008dc93a628004118b87a6bca01Alex Deymo google::protobuf::RepeatedPtrField<Extent>* out); 7514158570d3995008dc93a628004118b87a6bca01Alex Deymo 7614158570d3995008dc93a628004118b87a6bca01Alex Deymo// Stores all extents in |extents| into |out_vector|. 7714158570d3995008dc93a628004118b87a6bca01Alex Deymovoid ExtentsToVector(const google::protobuf::RepeatedPtrField<Extent>& extents, 7814158570d3995008dc93a628004118b87a6bca01Alex Deymo std::vector<Extent>* out_vector); 7914158570d3995008dc93a628004118b87a6bca01Alex Deymo 80f96cc198264161749d60169cb0b8d5d0015303d1Sen Jiang// Returns a string representing all extents in |extents|. 81f96cc198264161749d60169cb0b8d5d0015303d1Sen Jiangstd::string ExtentsToString(const std::vector<Extent>& extents); 82f96cc198264161749d60169cb0b8d5d0015303d1Sen Jiang 8314158570d3995008dc93a628004118b87a6bca01Alex Deymo// Takes a pointer to extents |extents| and extents |extents_to_add|, and 8414158570d3995008dc93a628004118b87a6bca01Alex Deymo// merges them by adding |extents_to_add| to |extents| and normalizing. 8514158570d3995008dc93a628004118b87a6bca01Alex Deymovoid ExtendExtents( 8614158570d3995008dc93a628004118b87a6bca01Alex Deymo google::protobuf::RepeatedPtrField<Extent>* extents, 8714158570d3995008dc93a628004118b87a6bca01Alex Deymo const google::protobuf::RepeatedPtrField<Extent>& extents_to_add); 8814158570d3995008dc93a628004118b87a6bca01Alex Deymo 8952490e776a9d34a94fb18ab1fe7d416425e94ddaAlex Deymo// Takes a vector of extents and normalizes those extents. Expects the extents 9052490e776a9d34a94fb18ab1fe7d416425e94ddaAlex Deymo// to be sorted by start block. E.g. if |extents| is [(1, 2), (3, 5), (10, 2)] 9152490e776a9d34a94fb18ab1fe7d416425e94ddaAlex Deymo// then |extents| will be changed to [(1, 7), (10, 2)]. 9252490e776a9d34a94fb18ab1fe7d416425e94ddaAlex Deymovoid NormalizeExtents(std::vector<Extent>* extents); 9352490e776a9d34a94fb18ab1fe7d416425e94ddaAlex Deymo 9452490e776a9d34a94fb18ab1fe7d416425e94ddaAlex Deymo// Return a subsequence of the list of blocks passed. Both the passed list of 9552490e776a9d34a94fb18ab1fe7d416425e94ddaAlex Deymo// blocks |extents| and the return value are expressed as a list of Extent, not 9652490e776a9d34a94fb18ab1fe7d416425e94ddaAlex Deymo// blocks. The returned list skips the first |block_offset| blocks from the 9752490e776a9d34a94fb18ab1fe7d416425e94ddaAlex Deymo// |extents| and cotains |block_count| blocks (or less if |extents| is shorter). 9852490e776a9d34a94fb18ab1fe7d416425e94ddaAlex Deymostd::vector<Extent> ExtentsSublist(const std::vector<Extent>& extents, 9952490e776a9d34a94fb18ab1fe7d416425e94ddaAlex Deymo uint64_t block_offset, uint64_t block_count); 10052490e776a9d34a94fb18ab1fe7d416425e94ddaAlex Deymo 1015c6c65570013bbdbd67f9bf6391dd295ef5b5ee6Alex Deymobool operator==(const Extent& a, const Extent& b); 1025c6c65570013bbdbd67f9bf6391dd295ef5b5ee6Alex Deymo 1035c6c65570013bbdbd67f9bf6391dd295ef5b5ee6Alex Deymo} // namespace chromeos_update_engine 1045c6c65570013bbdbd67f9bf6391dd295ef5b5ee6Alex Deymo 1055c6c65570013bbdbd67f9bf6391dd295ef5b5ee6Alex Deymo#endif // UPDATE_ENGINE_PAYLOAD_GENERATOR_EXTENT_UTILS_H_ 106