blob: 8393fe5b72227a759358272f6d9b3475df20403d [file] [log] [blame]
Kelvin Zhang94f51cc2020-09-25 11:34:49 -04001//
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
Kelvin Zhang76f10b82021-06-25 18:45:46 -040020#include <map>
Kelvin Zhang94f51cc2020-09-25 11:34:49 -040021#include <memory>
Kelvin Zhange52b6cd2021-02-09 15:28:40 -050022#include <string>
Kelvin Zhang7a265752020-10-29 15:51:35 -040023#include <vector>
Kelvin Zhang94f51cc2020-09-25 11:34:49 -040024
Kelvin Zhang9b10dba2020-09-25 17:09:11 -040025#include <libsnapshot/snapshot_writer.h>
Kelvin Zhang94f51cc2020-09-25 11:34:49 -040026
Kelvin Zhang76f10b82021-06-25 18:45:46 -040027#include "update_engine/payload_consumer/extent_map.h"
Kelvin Zhange52b6cd2021-02-09 15:28:40 -050028#include "update_engine/payload_consumer/install_operation_executor.h"
Kelvin Zhang94f51cc2020-09-25 11:34:49 -040029#include "update_engine/payload_consumer/install_plan.h"
Kelvin Zhangd11e2fc2022-10-24 15:40:30 -070030#include "update_engine/payload_consumer/partition_writer_interface.h"
31#include "update_engine/payload_consumer/verified_source_fd.h"
Kelvin Zhang76f10b82021-06-25 18:45:46 -040032#include "update_engine/payload_generator/extent_ranges.h"
Kelvin Zhang94f51cc2020-09-25 11:34:49 -040033
34namespace chromeos_update_engine {
Kelvin Zhange52b6cd2021-02-09 15:28:40 -050035class VABCPartitionWriter final : public PartitionWriterInterface {
Kelvin Zhang94f51cc2020-09-25 11:34:49 -040036 public:
Kelvin Zhange52b6cd2021-02-09 15:28:40 -050037 VABCPartitionWriter(const PartitionUpdate& partition_update,
38 const InstallPlan::Partition& install_part,
39 DynamicPartitionControlInterface* dynamic_control,
Kelvin Zhanga37aafc2021-06-14 13:21:37 -040040 size_t block_size);
Kelvin Zhang94f51cc2020-09-25 11:34:49 -040041 [[nodiscard]] bool Init(const InstallPlan* install_plan,
Kelvin Zhang52cb1d72020-10-27 13:44:25 -040042 bool source_may_exist,
43 size_t next_op_index) override;
Kelvin Zhang94f51cc2020-09-25 11:34:49 -040044 ~VABCPartitionWriter() override;
45
Kelvin Zhang94f51cc2020-09-25 11:34:49 -040046 // 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;
Kelvin Zhang52cb1d72020-10-27 13:44:25 -040053
Kelvin Zhange52b6cd2021-02-09 15:28:40 -050054 [[nodiscard]] bool PerformReplaceOperation(const InstallOperation& operation,
55 const void* data,
56 size_t count) override;
57
Tianjie8e0090d2021-08-30 22:35:21 -070058 [[nodiscard]] bool PerformDiffOperation(const InstallOperation& operation,
59 ErrorCode* error,
60 const void* data,
61 size_t count) override;
Kelvin Zhange52b6cd2021-02-09 15:28:40 -050062
Kelvin Zhang52cb1d72020-10-27 13:44:25 -040063 void CheckpointUpdateProgress(size_t next_op_index) override;
Kelvin Zhang94f51cc2020-09-25 11:34:49 -040064
Kelvin Zhangec205cf2020-09-28 13:23:40 -040065 [[nodiscard]] bool FinishedInstallOps() override;
Kelvin Zhange52b6cd2021-02-09 15:28:40 -050066 int Close() override;
Kelvin Zhanga37aafc2021-06-14 13:21:37 -040067 // 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);
Kelvin Zhangec205cf2020-09-28 13:23:40 -040071
Kelvin Zhang94f51cc2020-09-25 11:34:49 -040072 private:
Kelvin Zhang76f10b82021-06-25 18:45:46 -040073 bool IsXorEnabled() const noexcept { return xor_map_.size() > 0; }
Kelvin Zhang9b10dba2020-09-25 17:09:11 -040074 std::unique_ptr<android::snapshot::ISnapshotWriter> cow_writer_;
Kelvin Zhange52b6cd2021-02-09 15:28:40 -050075
Kelvin Zhange52b6cd2021-02-09 15:28:40 -050076 [[nodiscard]] std::unique_ptr<ExtentWriter> CreateBaseExtentWriter();
77
78 const PartitionUpdate& partition_update_;
79 const InstallPlan::Partition& install_part_;
Kelvin Zhang76f10b82021-06-25 18:45:46 -040080 DynamicPartitionControlInterface* const dynamic_control_;
Kelvin Zhange52b6cd2021-02-09 15:28:40 -050081 // Path to source partition
Kelvin Zhang76f10b82021-06-25 18:45:46 -040082 const std::string source_path_;
Kelvin Zhange52b6cd2021-02-09 15:28:40 -050083
Kelvin Zhange52b6cd2021-02-09 15:28:40 -050084 const size_t block_size_;
85 InstallOperationExecutor executor_;
86 VerifiedSourceFd verified_source_fd_;
Kelvin Zhang76f10b82021-06-25 18:45:46 -040087 ExtentMap<const CowMergeOperation*, ExtentLess> xor_map_;
Kelvin Zhangd11e2fc2022-10-24 15:40:30 -070088 ExtentRanges copy_blocks_;
Kelvin Zhang94f51cc2020-09-25 11:34:49 -040089};
90
91} // namespace chromeos_update_engine
92
93#endif