| Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 1 | // | 
|  | 2 | // Copyright (C) 2020 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 | // | 
| Kelvin Zhang | 9754f17 | 2020-09-25 15:22:35 -0400 | [diff] [blame] | 16 |  | 
| Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 17 | #include <cstdint> | 
|  | 18 | #include <vector> | 
|  | 19 |  | 
|  | 20 | #include <libsnapshot/cow_writer.h> | 
|  | 21 |  | 
|  | 22 | #include "update_engine/payload_consumer/extent_writer.h" | 
| Kelvin Zhang | 9754f17 | 2020-09-25 15:22:35 -0400 | [diff] [blame] | 23 | #include "update_engine/update_metadata.pb.h" | 
| Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 24 |  | 
|  | 25 | namespace chromeos_update_engine { | 
| Kelvin Zhang | 9754f17 | 2020-09-25 15:22:35 -0400 | [diff] [blame] | 26 |  | 
| Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 27 | class SnapshotExtentWriter : public chromeos_update_engine::ExtentWriter { | 
|  | 28 | public: | 
|  | 29 | explicit SnapshotExtentWriter(android::snapshot::ICowWriter* cow_writer); | 
|  | 30 | ~SnapshotExtentWriter(); | 
|  | 31 | // Returns true on success. | 
|  | 32 | bool Init(FileDescriptorPtr fd, | 
|  | 33 | const google::protobuf::RepeatedPtrField<Extent>& extents, | 
|  | 34 | uint32_t block_size) override; | 
|  | 35 | // Returns true on success. | 
|  | 36 | // This will construct a COW_REPLACE operation and forward it to CowWriter. It | 
|  | 37 | // is important that caller does not perform SOURCE_COPY operation on this | 
|  | 38 | // class, otherwise raw data will be stored. Caller should find ways to use | 
|  | 39 | // COW_COPY whenever possible. | 
|  | 40 | bool Write(const void* bytes, size_t count) override; | 
|  | 41 |  | 
|  | 42 | private: | 
| Kelvin Zhang | 9754f17 | 2020-09-25 15:22:35 -0400 | [diff] [blame] | 43 | bool next_extent(); | 
| Kelvin Zhang | e3293c7 | 2020-11-19 17:10:56 -0500 | [diff] [blame] | 44 | [[nodiscard]] size_t ConsumeWithBuffer(const uint8_t* bytes, size_t count); | 
| Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 45 | // It's a non-owning pointer, because PartitionWriter owns the CowWruter. This | 
|  | 46 | // allows us to use a single instance of CowWriter for all operations applied | 
|  | 47 | // to the same partition. | 
| Kelvin Zhang | 9754f17 | 2020-09-25 15:22:35 -0400 | [diff] [blame] | 48 | android::snapshot::ICowWriter* cow_writer_; | 
|  | 49 | google::protobuf::RepeatedPtrField<Extent> extents_; | 
|  | 50 | size_t cur_extent_idx_; | 
|  | 51 | std::vector<uint8_t> buffer_; | 
|  | 52 | size_t block_size_; | 
| Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 53 | }; | 
| Kelvin Zhang | 9754f17 | 2020-09-25 15:22:35 -0400 | [diff] [blame] | 54 |  | 
| Kelvin Zhang | 9b10dba | 2020-09-25 17:09:11 -0400 | [diff] [blame] | 55 | }  // namespace chromeos_update_engine |