1*5a923131SAndroid Build Coastguard Worker //
2*5a923131SAndroid Build Coastguard Worker // Copyright (C) 2020 The Android Open Source Project
3*5a923131SAndroid Build Coastguard Worker //
4*5a923131SAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License");
5*5a923131SAndroid Build Coastguard Worker // you may not use this file except in compliance with the License.
6*5a923131SAndroid Build Coastguard Worker // You may obtain a copy of the License at
7*5a923131SAndroid Build Coastguard Worker //
8*5a923131SAndroid Build Coastguard Worker // http://www.apache.org/licenses/LICENSE-2.0
9*5a923131SAndroid Build Coastguard Worker //
10*5a923131SAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
11*5a923131SAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS,
12*5a923131SAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*5a923131SAndroid Build Coastguard Worker // See the License for the specific language governing permissions and
14*5a923131SAndroid Build Coastguard Worker // limitations under the License.
15*5a923131SAndroid Build Coastguard Worker //
16*5a923131SAndroid Build Coastguard Worker
17*5a923131SAndroid Build Coastguard Worker #ifndef __COW_OPERATION_CONVERT_H
18*5a923131SAndroid Build Coastguard Worker #define __COW_OPERATION_CONVERT_H
19*5a923131SAndroid Build Coastguard Worker
20*5a923131SAndroid Build Coastguard Worker #include <vector>
21*5a923131SAndroid Build Coastguard Worker
22*5a923131SAndroid Build Coastguard Worker #include <libsnapshot/cow_format.h>
23*5a923131SAndroid Build Coastguard Worker
24*5a923131SAndroid Build Coastguard Worker #include "update_engine/update_metadata.pb.h"
25*5a923131SAndroid Build Coastguard Worker
26*5a923131SAndroid Build Coastguard Worker namespace chromeos_update_engine {
27*5a923131SAndroid Build Coastguard Worker struct CowOperation {
28*5a923131SAndroid Build Coastguard Worker enum Type {
29*5a923131SAndroid Build Coastguard Worker CowCopy = static_cast<int>(android::snapshot::kCowCopyOp),
30*5a923131SAndroid Build Coastguard Worker CowReplace = static_cast<int>(android::snapshot::kCowReplaceOp),
31*5a923131SAndroid Build Coastguard Worker };
32*5a923131SAndroid Build Coastguard Worker Type op{};
33*5a923131SAndroid Build Coastguard Worker uint64_t src_block{};
34*5a923131SAndroid Build Coastguard Worker uint64_t dst_block{};
35*5a923131SAndroid Build Coastguard Worker uint64_t block_count{1};
36*5a923131SAndroid Build Coastguard Worker };
37*5a923131SAndroid Build Coastguard Worker
38*5a923131SAndroid Build Coastguard Worker // Convert SOURCE_COPY operations in `operations` list to a list of
39*5a923131SAndroid Build Coastguard Worker // CowOperations according to the merge sequence. This function only converts
40*5a923131SAndroid Build Coastguard Worker // SOURCE_COPY, other operations are ignored. If there's a merge conflict in
41*5a923131SAndroid Build Coastguard Worker // SOURCE_COPY operations, some blocks may be converted to COW_REPLACE instead
42*5a923131SAndroid Build Coastguard Worker // of COW_COPY.
43*5a923131SAndroid Build Coastguard Worker
44*5a923131SAndroid Build Coastguard Worker // The list returned does not necessarily preserve the order of
45*5a923131SAndroid Build Coastguard Worker // SOURCE_COPY in `operations`. The only guarantee about ordering in the
46*5a923131SAndroid Build Coastguard Worker // returned list is that if operations are applied in such order, there would be
47*5a923131SAndroid Build Coastguard Worker // no merge conflicts.
48*5a923131SAndroid Build Coastguard Worker
49*5a923131SAndroid Build Coastguard Worker // This funnction is intended to be used by delta_performer to perform
50*5a923131SAndroid Build Coastguard Worker // SOURCE_COPY operations on Virtual AB Compression devices.
51*5a923131SAndroid Build Coastguard Worker std::vector<CowOperation> ConvertToCowOperations(
52*5a923131SAndroid Build Coastguard Worker const ::google::protobuf::RepeatedPtrField<
53*5a923131SAndroid Build Coastguard Worker ::chromeos_update_engine::InstallOperation>& operations,
54*5a923131SAndroid Build Coastguard Worker const ::google::protobuf::RepeatedPtrField<CowMergeOperation>&
55*5a923131SAndroid Build Coastguard Worker merge_operations);
56*5a923131SAndroid Build Coastguard Worker
IsConsecutive(const CowOperation & op1,const CowOperation & op2)57*5a923131SAndroid Build Coastguard Worker constexpr bool IsConsecutive(const CowOperation& op1, const CowOperation& op2) {
58*5a923131SAndroid Build Coastguard Worker return op1.op == op2.op && op1.dst_block + op1.block_count == op2.dst_block &&
59*5a923131SAndroid Build Coastguard Worker op1.src_block + op1.block_count == op2.src_block;
60*5a923131SAndroid Build Coastguard Worker }
61*5a923131SAndroid Build Coastguard Worker
62*5a923131SAndroid Build Coastguard Worker void push_back(std::vector<CowOperation>* converted, const CowOperation& op);
63*5a923131SAndroid Build Coastguard Worker
64*5a923131SAndroid Build Coastguard Worker } // namespace chromeos_update_engine
65*5a923131SAndroid Build Coastguard Worker #endif
66