extent_reader.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. #ifndef UPDATE_ENGINE_PAYLOAD_CONSUMER_EXTENT_READER_H_
  17. #define UPDATE_ENGINE_PAYLOAD_CONSUMER_EXTENT_READER_H_
  18. #include <vector>
  19. #include "update_engine/payload_consumer/file_descriptor.h"
  20. #include "update_engine/update_metadata.pb.h"
  21. namespace chromeos_update_engine {
  22. // ExtentReader is an abstract class with reads from a given file descriptor at
  23. // the extents given.
  24. class ExtentReader {
  25. public:
  26. virtual ~ExtentReader() = default;
  27. // Initializes |ExtentReader|
  28. virtual bool Init(FileDescriptorPtr fd,
  29. const google::protobuf::RepeatedPtrField<Extent>& extents,
  30. uint32_t block_size) = 0;
  31. // Seeks to the given |offset| assuming all extents are concatenated together.
  32. virtual bool Seek(uint64_t offset) = 0;
  33. // Returns true on success.
  34. virtual bool Read(void* buffer, size_t count) = 0;
  35. };
  36. // DirectExtentReader is probably the simplest ExtentReader implementation.
  37. // It reads the data directly from the extents.
  38. class DirectExtentReader : public ExtentReader {
  39. public:
  40. DirectExtentReader() = default;
  41. ~DirectExtentReader() override = default;
  42. bool Init(FileDescriptorPtr fd,
  43. const google::protobuf::RepeatedPtrField<Extent>& extents,
  44. uint32_t block_size) override;
  45. bool Seek(uint64_t offset) override;
  46. bool Read(void* bytes, size_t count) override;
  47. private:
  48. FileDescriptorPtr fd_{nullptr};
  49. google::protobuf::RepeatedPtrField<Extent> extents_;
  50. size_t block_size_{0};
  51. // Current extent being read from |fd_|.
  52. google::protobuf::RepeatedPtrField<Extent>::iterator cur_extent_;
  53. // Bytes read from |cur_extent_| thus far.
  54. uint64_t cur_extent_bytes_read_{0};
  55. // Offset assuming all extents are concatenated.
  56. uint64_t offset_{0};
  57. // The accelaring upper bounds for |extents_| if we assume all extents are
  58. // concatenated.
  59. std::vector<uint64_t> extents_upper_bounds_;
  60. uint64_t total_size_{0};
  61. DISALLOW_COPY_AND_ASSIGN(DirectExtentReader);
  62. };
  63. } // namespace chromeos_update_engine
  64. #endif // UPDATE_ENGINE_PAYLOAD_CONSUMER_EXTENT_READER_H_