blob: fb63f8cf30267ad1dd69d1bbbd1bbb4091e12c01 [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
20#include <memory>
Kelvin Zhange52b6cd2021-02-09 15:28:40 -050021#include <string>
Kelvin Zhang7a265752020-10-29 15:51:35 -040022#include <vector>
Kelvin Zhang94f51cc2020-09-25 11:34:49 -040023
Kelvin Zhang9b10dba2020-09-25 17:09:11 -040024#include <libsnapshot/snapshot_writer.h>
Kelvin Zhang94f51cc2020-09-25 11:34:49 -040025
Kelvin Zhang7a265752020-10-29 15:51:35 -040026#include "update_engine/common/cow_operation_convert.h"
Kelvin Zhange52b6cd2021-02-09 15:28:40 -050027#include "update_engine/payload_consumer/install_operation_executor.h"
Kelvin Zhang94f51cc2020-09-25 11:34:49 -040028#include "update_engine/payload_consumer/install_plan.h"
29#include "update_engine/payload_consumer/partition_writer.h"
30
31namespace chromeos_update_engine {
Kelvin Zhange52b6cd2021-02-09 15:28:40 -050032class VABCPartitionWriter final : public PartitionWriterInterface {
Kelvin Zhang94f51cc2020-09-25 11:34:49 -040033 public:
Kelvin Zhange52b6cd2021-02-09 15:28:40 -050034 VABCPartitionWriter(const PartitionUpdate& partition_update,
35 const InstallPlan::Partition& install_part,
36 DynamicPartitionControlInterface* dynamic_control,
Kelvin Zhanga37aafc2021-06-14 13:21:37 -040037 size_t block_size);
Kelvin Zhang94f51cc2020-09-25 11:34:49 -040038 [[nodiscard]] bool Init(const InstallPlan* install_plan,
Kelvin Zhang52cb1d72020-10-27 13:44:25 -040039 bool source_may_exist,
40 size_t next_op_index) override;
Kelvin Zhang94f51cc2020-09-25 11:34:49 -040041 ~VABCPartitionWriter() override;
42
Kelvin Zhang94f51cc2020-09-25 11:34:49 -040043 // Only ZERO and SOURCE_COPY InstallOperations are treated special by VABC
44 // Partition Writer. These operations correspond to COW_ZERO and COW_COPY. All
45 // other operations just get converted to COW_REPLACE.
46 [[nodiscard]] bool PerformZeroOrDiscardOperation(
47 const InstallOperation& operation) override;
48 [[nodiscard]] bool PerformSourceCopyOperation(
49 const InstallOperation& operation, ErrorCode* error) override;
Kelvin Zhang52cb1d72020-10-27 13:44:25 -040050
Kelvin Zhange52b6cd2021-02-09 15:28:40 -050051 [[nodiscard]] bool PerformReplaceOperation(const InstallOperation& operation,
52 const void* data,
53 size_t count) override;
54
Tianjie8e0090d2021-08-30 22:35:21 -070055 [[nodiscard]] bool PerformDiffOperation(const InstallOperation& operation,
56 ErrorCode* error,
57 const void* data,
58 size_t count) override;
Kelvin Zhange52b6cd2021-02-09 15:28:40 -050059
Kelvin Zhang52cb1d72020-10-27 13:44:25 -040060 void CheckpointUpdateProgress(size_t next_op_index) override;
Kelvin Zhang94f51cc2020-09-25 11:34:49 -040061
Kelvin Zhang7a265752020-10-29 15:51:35 -040062 static bool WriteAllCowOps(size_t block_size,
63 const std::vector<CowOperation>& converted,
64 android::snapshot::ICowWriter* cow_writer,
65 FileDescriptorPtr source_fd);
66
Kelvin Zhangec205cf2020-09-28 13:23:40 -040067 [[nodiscard]] bool FinishedInstallOps() override;
Kelvin Zhange52b6cd2021-02-09 15:28:40 -050068 int Close() override;
Kelvin Zhanga37aafc2021-06-14 13:21:37 -040069 // Send merge sequence data to cow writer
70 static bool WriteMergeSequence(
71 const ::google::protobuf::RepeatedPtrField<CowMergeOperation>& merge_ops,
72 android::snapshot::ICowWriter* cow_writer);
Kelvin Zhangec205cf2020-09-28 13:23:40 -040073
Kelvin Zhang94f51cc2020-09-25 11:34:49 -040074 private:
Kelvin Zhang9b10dba2020-09-25 17:09:11 -040075 std::unique_ptr<android::snapshot::ISnapshotWriter> cow_writer_;
Kelvin Zhange52b6cd2021-02-09 15:28:40 -050076
Kelvin Zhange52b6cd2021-02-09 15:28:40 -050077 [[nodiscard]] std::unique_ptr<ExtentWriter> CreateBaseExtentWriter();
78
79 const PartitionUpdate& partition_update_;
80 const InstallPlan::Partition& install_part_;
81 DynamicPartitionControlInterface* dynamic_control_;
82 // Path to source partition
83 std::string source_path_;
84
Kelvin Zhange52b6cd2021-02-09 15:28:40 -050085 const size_t block_size_;
86 InstallOperationExecutor executor_;
87 VerifiedSourceFd verified_source_fd_;
Kelvin Zhang94f51cc2020-09-25 11:34:49 -040088};
89
90} // namespace chromeos_update_engine
91
92#endif