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 // 16 17 #ifndef UPDATE_ENGINE_VABC_PARTITION_WRITER_H_ 18 #define UPDATE_ENGINE_VABC_PARTITION_WRITER_H_ 19 20 #include <map> 21 #include <memory> 22 #include <string> 23 #include <vector> 24 25 #include <libsnapshot/snapshot_writer.h> 26 27 #include "update_engine/payload_consumer/extent_map.h" 28 #include "update_engine/payload_consumer/install_operation_executor.h" 29 #include "update_engine/payload_consumer/install_plan.h" 30 #include "update_engine/payload_consumer/partition_writer_interface.h" 31 #include "update_engine/payload_consumer/verified_source_fd.h" 32 #include "update_engine/payload_generator/extent_ranges.h" 33 34 namespace chromeos_update_engine { 35 class VABCPartitionWriter final : public PartitionWriterInterface { 36 public: 37 VABCPartitionWriter(const PartitionUpdate& partition_update, 38 const InstallPlan::Partition& install_part, 39 DynamicPartitionControlInterface* dynamic_control, 40 size_t block_size); 41 [[nodiscard]] bool Init(const InstallPlan* install_plan, 42 bool source_may_exist, 43 size_t next_op_index) override; 44 ~VABCPartitionWriter() override; 45 46 // Only ZERO and SOURCE_COPY InstallOperations are treated special by VABC 47 // Partition Writer. These operations correspond to COW_ZERO and COW_COPY. All 48 // other operations just get converted to COW_REPLACE. 49 [[nodiscard]] bool PerformZeroOrDiscardOperation( 50 const InstallOperation& operation) override; 51 [[nodiscard]] bool PerformSourceCopyOperation( 52 const InstallOperation& operation, ErrorCode* error) override; 53 54 [[nodiscard]] bool PerformReplaceOperation(const InstallOperation& operation, 55 const void* data, 56 size_t count) override; 57 58 [[nodiscard]] bool PerformDiffOperation(const InstallOperation& operation, 59 ErrorCode* error, 60 const void* data, 61 size_t count) override; 62 63 void CheckpointUpdateProgress(size_t next_op_index) override; 64 65 [[nodiscard]] bool FinishedInstallOps() override; 66 int Close() override; 67 // Send merge sequence data to cow writer 68 static bool WriteMergeSequence( 69 const ::google::protobuf::RepeatedPtrField<CowMergeOperation>& merge_ops, 70 android::snapshot::ICowWriter* cow_writer); 71 72 private: 73 [[nodiscard]] bool DoesDeviceSupportsXor(); IsXorEnabled()74 bool IsXorEnabled() const noexcept { return xor_map_.size() > 0; } 75 [[nodiscard]] bool WriteAllCopyOps(); 76 std::unique_ptr<android::snapshot::ISnapshotWriter> cow_writer_; 77 78 [[nodiscard]] std::unique_ptr<ExtentWriter> CreateBaseExtentWriter(); 79 80 const PartitionUpdate& partition_update_; 81 const InstallPlan::Partition& install_part_; 82 DynamicPartitionControlInterface* const dynamic_control_; 83 // Path to source partition 84 const std::string source_path_; 85 86 const size_t block_size_; 87 InstallOperationExecutor executor_; 88 VerifiedSourceFd verified_source_fd_; 89 ExtentMap<const CowMergeOperation*, ExtentLess> xor_map_; 90 ExtentRanges copy_blocks_; 91 }; 92 93 } // namespace chromeos_update_engine 94 95 #endif 96