| Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // | 
|  | 2 | // Copyright (C) 2009 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 | // | 
| Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 16 |  | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 17 | #ifndef UPDATE_ENGINE_PAYLOAD_CONSUMER_EXTENT_WRITER_H_ | 
|  | 18 | #define UPDATE_ENGINE_PAYLOAD_CONSUMER_EXTENT_WRITER_H_ | 
| Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 19 |  | 
| Amin Hassani | cd7edbe | 2017-09-18 17:05:02 -0700 | [diff] [blame] | 20 | #include <memory> | 
|  | 21 | #include <utility> | 
| Alex Deymo | 8427b4a | 2014-11-05 14:00:32 -0800 | [diff] [blame] | 22 |  | 
|  | 23 | #include <base/logging.h> | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 24 | #include <brillo/secure_blob.h> | 
| Alex Deymo | 8427b4a | 2014-11-05 14:00:32 -0800 | [diff] [blame] | 25 |  | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 26 | #include "update_engine/payload_consumer/file_descriptor.h" | 
| Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 27 | #include "update_engine/update_metadata.pb.h" | 
| Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 28 |  | 
|  | 29 | // ExtentWriter is an abstract class which synchronously writes to a given | 
|  | 30 | // file descriptor at the extents given. | 
|  | 31 |  | 
|  | 32 | namespace chromeos_update_engine { | 
|  | 33 |  | 
| Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 34 | class ExtentWriter { | 
|  | 35 | public: | 
| Alex Deymo | 0532287 | 2015-09-30 09:50:24 -0700 | [diff] [blame] | 36 | ExtentWriter() = default; | 
| Sen Jiang | 5e1af98 | 2018-11-01 15:01:45 -0700 | [diff] [blame] | 37 | virtual ~ExtentWriter() = default; | 
| Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 38 |  | 
|  | 39 | // Returns true on success. | 
| Kelvin Zhang | 4d22ca2 | 2021-02-09 14:06:25 -0500 | [diff] [blame] | 40 | virtual bool Init(const google::protobuf::RepeatedPtrField<Extent>& extents, | 
| Andrew de los Reyes | 08c4e27 | 2010-04-15 14:02:17 -0700 | [diff] [blame] | 41 | uint32_t block_size) = 0; | 
| Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 42 |  | 
|  | 43 | // Returns true on success. | 
|  | 44 | virtual bool Write(const void* bytes, size_t count) = 0; | 
| Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 45 | }; | 
|  | 46 |  | 
|  | 47 | // DirectExtentWriter is probably the simplest ExtentWriter implementation. | 
|  | 48 | // It writes the data directly into the extents. | 
|  | 49 |  | 
|  | 50 | class DirectExtentWriter : public ExtentWriter { | 
|  | 51 | public: | 
| Kelvin Zhang | 4d22ca2 | 2021-02-09 14:06:25 -0500 | [diff] [blame] | 52 | explicit DirectExtentWriter(FileDescriptorPtr fd) : fd_(fd) {} | 
| Alex Deymo | 0532287 | 2015-09-30 09:50:24 -0700 | [diff] [blame] | 53 | ~DirectExtentWriter() override = default; | 
| Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 54 |  | 
| Kelvin Zhang | 4d22ca2 | 2021-02-09 14:06:25 -0500 | [diff] [blame] | 55 | bool Init(const google::protobuf::RepeatedPtrField<Extent>& extents, | 
| Alex Deymo | 0532287 | 2015-09-30 09:50:24 -0700 | [diff] [blame] | 56 | uint32_t block_size) override { | 
| Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 57 | block_size_ = block_size; | 
|  | 58 | extents_ = extents; | 
| Amin Hassani | cd7edbe | 2017-09-18 17:05:02 -0700 | [diff] [blame] | 59 | cur_extent_ = extents_.begin(); | 
| Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 60 | return true; | 
|  | 61 | } | 
| Alex Deymo | 0532287 | 2015-09-30 09:50:24 -0700 | [diff] [blame] | 62 | bool Write(const void* bytes, size_t count) override; | 
| Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 63 |  | 
|  | 64 | private: | 
| Alex Deymo | 0532287 | 2015-09-30 09:50:24 -0700 | [diff] [blame] | 65 | FileDescriptorPtr fd_{nullptr}; | 
| Chris Masone | 4dc2ada | 2010-09-23 12:43:03 -0700 | [diff] [blame] | 66 |  | 
| Alex Deymo | 0532287 | 2015-09-30 09:50:24 -0700 | [diff] [blame] | 67 | size_t block_size_{0}; | 
| Amin Hassani | cd7edbe | 2017-09-18 17:05:02 -0700 | [diff] [blame] | 68 | // Bytes written into |cur_extent_| thus far. | 
| Alex Deymo | 0532287 | 2015-09-30 09:50:24 -0700 | [diff] [blame] | 69 | uint64_t extent_bytes_written_{0}; | 
| Amin Hassani | cd7edbe | 2017-09-18 17:05:02 -0700 | [diff] [blame] | 70 | google::protobuf::RepeatedPtrField<Extent> extents_; | 
|  | 71 | // The next call to write should correspond to |cur_extents_|. | 
|  | 72 | google::protobuf::RepeatedPtrField<Extent>::iterator cur_extent_; | 
| Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 73 | }; | 
|  | 74 |  | 
| Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 75 | }  // namespace chromeos_update_engine | 
|  | 76 |  | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 77 | #endif  // UPDATE_ENGINE_PAYLOAD_CONSUMER_EXTENT_WRITER_H_ |