blob: db17b5fda7505696613850e70492bc419b0429c1 [file] [log] [blame]
Kelvin Zhang9b10dba2020-09-25 17:09:11 -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#include "update_engine/common/cow_operation_convert.h"
18
Kelvin Zhangb05e4e22020-09-25 16:16:19 -040019#include <base/logging.h>
20
Kelvin Zhang9b10dba2020-09-25 17:09:11 -040021#include "update_engine/payload_generator/extent_ranges.h"
Kelvin Zhangb05e4e22020-09-25 16:16:19 -040022#include "update_engine/payload_generator/extent_utils.h"
Kelvin Zhang9b10dba2020-09-25 17:09:11 -040023
24namespace chromeos_update_engine {
Kelvin Zhangb05e4e22020-09-25 16:16:19 -040025
Kelvin Zhang9b10dba2020-09-25 17:09:11 -040026std::vector<CowOperation> ConvertToCowOperations(
27 const ::google::protobuf::RepeatedPtrField<
28 ::chromeos_update_engine::InstallOperation>& operations,
29 const ::google::protobuf::RepeatedPtrField<CowMergeOperation>&
30 merge_operations) {
Kelvin Zhangb05e4e22020-09-25 16:16:19 -040031 ExtentRanges merge_extents;
32 std::vector<CowOperation> converted;
33
34 // We want all CowCopy ops to be done first, before any COW_REPLACE happen.
35 // Therefore we add these ops in 2 separate loops. This is because during
36 // merge, a CowReplace might modify a block needed by CowCopy, so we always
37 // perform CowCopy first.
38
39 // This loop handles CowCopy blocks within SOURCE_COPY, and the next loop
40 // converts the leftover blocks to CowReplace?
41 for (const auto& merge_op : merge_operations) {
42 merge_extents.AddExtent(merge_op.dst_extent());
43 const auto& src_extent = merge_op.src_extent();
44 const auto& dst_extent = merge_op.dst_extent();
45 for (uint64_t i = 0; i < src_extent.num_blocks(); i++) {
46 converted.push_back({CowOperation::CowCopy,
47 src_extent.start_block() + i,
48 dst_extent.start_block() + i});
49 }
50 }
51 // COW_REPLACE are added after COW_COPY, because replace might modify blocks
52 // needed by COW_COPY. Please don't merge this loop with the previous one.
53 for (const auto& operation : operations) {
54 if (operation.type() != InstallOperation::SOURCE_COPY) {
55 continue;
56 }
57 const auto& src_extents = operation.src_extents();
58 const auto& dst_extents = operation.dst_extents();
59 BlockIterator it1{src_extents};
60 BlockIterator it2{dst_extents};
61 while (!it1.is_end() && !it2.is_end()) {
62 auto src_block = *it1;
63 auto dst_block = *it2;
64 if (!merge_extents.ContainsBlock(dst_block)) {
65 converted.push_back({CowOperation::CowReplace, src_block, dst_block});
66 }
67 ++it1;
68 ++it2;
69 }
70 }
71 return converted;
Kelvin Zhang9b10dba2020-09-25 17:09:11 -040072}
73} // namespace chromeos_update_engine