extent_reader.cc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. //
  2. // Copyright (C) 2017 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. #include "update_engine/payload_consumer/extent_reader.h"
  17. #include <algorithm>
  18. #include <sys/types.h>
  19. #include <unistd.h>
  20. #include "update_engine/common/utils.h"
  21. #include "update_engine/payload_consumer/payload_constants.h"
  22. using google::protobuf::RepeatedPtrField;
  23. namespace chromeos_update_engine {
  24. bool DirectExtentReader::Init(FileDescriptorPtr fd,
  25. const RepeatedPtrField<Extent>& extents,
  26. uint32_t block_size) {
  27. fd_ = fd;
  28. extents_ = extents;
  29. block_size_ = block_size;
  30. cur_extent_ = extents_.begin();
  31. extents_upper_bounds_.reserve(extents_.size() + 1);
  32. // We add this pad as the first element to not bother with boundary checks
  33. // later.
  34. extents_upper_bounds_.emplace_back(0);
  35. for (const auto& extent : extents_) {
  36. total_size_ += extent.num_blocks() * block_size_;
  37. extents_upper_bounds_.emplace_back(total_size_);
  38. }
  39. return true;
  40. }
  41. bool DirectExtentReader::Seek(uint64_t offset) {
  42. TEST_AND_RETURN_FALSE(offset <= total_size_);
  43. if (offset_ == offset) {
  44. return true;
  45. }
  46. // The first item is zero and upper_bound never returns it because it always
  47. // return the item which is greater than the given value.
  48. auto extent_idx =
  49. std::upper_bound(
  50. extents_upper_bounds_.begin(), extents_upper_bounds_.end(), offset) -
  51. extents_upper_bounds_.begin() - 1;
  52. cur_extent_ = std::next(extents_.begin(), extent_idx);
  53. offset_ = offset;
  54. cur_extent_bytes_read_ = offset_ - extents_upper_bounds_[extent_idx];
  55. return true;
  56. }
  57. bool DirectExtentReader::Read(void* buffer, size_t count) {
  58. auto bytes = reinterpret_cast<uint8_t*>(buffer);
  59. uint64_t bytes_read = 0;
  60. while (bytes_read < count) {
  61. if (cur_extent_ == extents_.end()) {
  62. TEST_AND_RETURN_FALSE(bytes_read == count);
  63. }
  64. uint64_t cur_extent_bytes_left =
  65. cur_extent_->num_blocks() * block_size_ - cur_extent_bytes_read_;
  66. uint64_t bytes_to_read =
  67. std::min(count - bytes_read, cur_extent_bytes_left);
  68. ssize_t out_bytes_read;
  69. TEST_AND_RETURN_FALSE(utils::PReadAll(
  70. fd_,
  71. bytes + bytes_read,
  72. bytes_to_read,
  73. cur_extent_->start_block() * block_size_ + cur_extent_bytes_read_,
  74. &out_bytes_read));
  75. TEST_AND_RETURN_FALSE(out_bytes_read ==
  76. static_cast<ssize_t>(bytes_to_read));
  77. bytes_read += bytes_to_read;
  78. cur_extent_bytes_read_ += bytes_to_read;
  79. offset_ += bytes_to_read;
  80. if (cur_extent_bytes_read_ == cur_extent_->num_blocks() * block_size_) {
  81. // We have to advance the cur_extent_;
  82. cur_extent_++;
  83. cur_extent_bytes_read_ = 0;
  84. }
  85. }
  86. return true;
  87. }
  88. } // namespace chromeos_update_engine