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