extent_utils.h 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. //
  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. //
  16. #ifndef UPDATE_ENGINE_PAYLOAD_GENERATOR_EXTENT_UTILS_H_
  17. #define UPDATE_ENGINE_PAYLOAD_GENERATOR_EXTENT_UTILS_H_
  18. #include <string>
  19. #include <vector>
  20. #include "update_engine/payload_consumer/payload_constants.h"
  21. #include "update_engine/update_metadata.pb.h"
  22. // Utility functions for manipulating Extents and lists of blocks.
  23. namespace chromeos_update_engine {
  24. // |block| must either be the next block in the last extent or a block
  25. // in the next extent. This function will not handle inserting block
  26. // into an arbitrary place in the extents.
  27. void AppendBlockToExtents(std::vector<Extent>* extents, uint64_t block);
  28. // Takes a collection (vector or RepeatedPtrField) of Extent and
  29. // returns a vector of the blocks referenced, in order.
  30. template <typename T>
  31. std::vector<uint64_t> ExpandExtents(const T& extents) {
  32. std::vector<uint64_t> ret;
  33. for (const auto& extent : extents) {
  34. if (extent.start_block() == kSparseHole) {
  35. ret.resize(ret.size() + extent.num_blocks(), kSparseHole);
  36. } else {
  37. for (uint64_t block = extent.start_block();
  38. block < (extent.start_block() + extent.num_blocks());
  39. block++) {
  40. ret.push_back(block);
  41. }
  42. }
  43. }
  44. return ret;
  45. }
  46. // Stores all Extents in 'extents' into 'out'.
  47. void StoreExtents(const std::vector<Extent>& extents,
  48. google::protobuf::RepeatedPtrField<Extent>* out);
  49. // Stores all extents in |extents| into |out_vector|.
  50. void ExtentsToVector(const google::protobuf::RepeatedPtrField<Extent>& extents,
  51. std::vector<Extent>* out_vector);
  52. // Returns a string representing all extents in |extents|.
  53. std::string ExtentsToString(const std::vector<Extent>& extents);
  54. // Takes a pointer to extents |extents| and extents |extents_to_add|, and
  55. // merges them by adding |extents_to_add| to |extents| and normalizing.
  56. void ExtendExtents(
  57. google::protobuf::RepeatedPtrField<Extent>* extents,
  58. const google::protobuf::RepeatedPtrField<Extent>& extents_to_add);
  59. // Takes a vector of extents and normalizes those extents. Expects the extents
  60. // to be sorted by start block. E.g. if |extents| is [(1, 2), (3, 5), (10, 2)]
  61. // then |extents| will be changed to [(1, 7), (10, 2)].
  62. void NormalizeExtents(std::vector<Extent>* extents);
  63. // Return a subsequence of the list of blocks passed. Both the passed list of
  64. // blocks |extents| and the return value are expressed as a list of Extent, not
  65. // blocks. The returned list skips the first |block_offset| blocks from the
  66. // |extents| and cotains |block_count| blocks (or less if |extents| is shorter).
  67. std::vector<Extent> ExtentsSublist(const std::vector<Extent>& extents,
  68. uint64_t block_offset,
  69. uint64_t block_count);
  70. bool operator==(const Extent& a, const Extent& b);
  71. } // namespace chromeos_update_engine
  72. #endif // UPDATE_ENGINE_PAYLOAD_GENERATOR_EXTENT_UTILS_H_