blob: c73bd4dd959a828da74d3e9ea0eef7f8188785fa [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
55 [[nodiscard]] bool PerformSourceBsdiffOperation(
56 const InstallOperation& operation,
57 ErrorCode* error,
58 const void* data,
59 size_t count) override;
60 [[nodiscard]] bool PerformPuffDiffOperation(const InstallOperation& operation,
61 ErrorCode* error,
62 const void* data,
63 size_t count) override;
64
Kelvin Zhang52cb1d72020-10-27 13:44:25 -040065 void CheckpointUpdateProgress(size_t next_op_index) override;
Kelvin Zhang94f51cc2020-09-25 11:34:49 -040066
Kelvin Zhang7a265752020-10-29 15:51:35 -040067 static bool WriteAllCowOps(size_t block_size,
68 const std::vector<CowOperation>& converted,
69 android::snapshot::ICowWriter* cow_writer,
70 FileDescriptorPtr source_fd);
71
Kelvin Zhangec205cf2020-09-28 13:23:40 -040072 [[nodiscard]] bool FinishedInstallOps() override;
Kelvin Zhange52b6cd2021-02-09 15:28:40 -050073 int Close() override;
Kelvin Zhanga37aafc2021-06-14 13:21:37 -040074 // Send merge sequence data to cow writer
75 static bool WriteMergeSequence(
76 const ::google::protobuf::RepeatedPtrField<CowMergeOperation>& merge_ops,
77 android::snapshot::ICowWriter* cow_writer);
Kelvin Zhangec205cf2020-09-28 13:23:40 -040078
Kelvin Zhang94f51cc2020-09-25 11:34:49 -040079 private:
Kelvin Zhang9b10dba2020-09-25 17:09:11 -040080 std::unique_ptr<android::snapshot::ISnapshotWriter> cow_writer_;
Kelvin Zhange52b6cd2021-02-09 15:28:40 -050081
Kelvin Zhange52b6cd2021-02-09 15:28:40 -050082 [[nodiscard]] std::unique_ptr<ExtentWriter> CreateBaseExtentWriter();
83
84 const PartitionUpdate& partition_update_;
85 const InstallPlan::Partition& install_part_;
86 DynamicPartitionControlInterface* dynamic_control_;
87 // Path to source partition
88 std::string source_path_;
89
Kelvin Zhange52b6cd2021-02-09 15:28:40 -050090 const size_t block_size_;
91 InstallOperationExecutor executor_;
92 VerifiedSourceFd verified_source_fd_;
Kelvin Zhang94f51cc2020-09-25 11:34:49 -040093};
94
95} // namespace chromeos_update_engine
96
97#endif