blob: 6b1fba719fef090223e52cab47bacb8f59996ce2 [file] [log] [blame]
Kelvin Zhangb1706762021-06-25 15:05:22 -04001//
2// Copyright (C) 2021 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/payload_consumer/block_extent_writer.h"
18
19#include <algorithm>
20#include <cstdint>
21
22#include "update_engine/update_metadata.pb.h"
23
24namespace chromeos_update_engine {
25
Kelvin Zhangb1706762021-06-25 15:05:22 -040026bool BlockExtentWriter::Init(
27 const google::protobuf::RepeatedPtrField<Extent>& extents,
28 uint32_t block_size) {
Kelvin Zhang243d2a12022-01-26 09:48:40 -080029 TEST_NE(extents.size(), 0);
Kelvin Zhangb1706762021-06-25 15:05:22 -040030 extents_ = extents;
31 cur_extent_idx_ = 0;
32 buffer_.clear();
33 buffer_.reserve(block_size);
34 block_size_ = block_size;
35 return true;
36}
37
38size_t BlockExtentWriter::ConsumeWithBuffer(const uint8_t* data, size_t count) {
39 CHECK_LT(cur_extent_idx_, static_cast<size_t>(extents_.size()));
40 const auto& cur_extent = extents_[cur_extent_idx_];
41 const auto cur_extent_size = cur_extent.num_blocks() * block_size_;
42
43 if (buffer_.empty() && count >= cur_extent_size) {
44 if (!WriteExtent(data, cur_extent, block_size_)) {
Kelvin Zhang4bb49202021-07-08 21:39:05 -040045 LOG(ERROR) << "WriteExtent(" << cur_extent.start_block() << ", " << data
Kelvin Zhangb1706762021-06-25 15:05:22 -040046 << ", " << cur_extent_size << ") failed.";
47 // return value is expected to be greater than 0. Return 0 to signal error
48 // condition
49 return 0;
50 }
51 if (!NextExtent()) {
Kelvin Zhang243d2a12022-01-26 09:48:40 -080052 if (count != cur_extent_size) {
53 LOG(ERROR) << "Exhausted all blocks, but still have "
54 << count - cur_extent_size << " bytes left";
55 return 0;
56 }
Kelvin Zhangb1706762021-06-25 15:05:22 -040057 }
58 return cur_extent_size;
59 }
Kelvin Zhang243d2a12022-01-26 09:48:40 -080060 if (buffer_.size() >= cur_extent_size) {
61 LOG(ERROR)
62 << "Data left in buffer should never be >= cur_extent_size, otherwise "
63 "we should have send that data to CowWriter. Buffer size: "
64 << buffer_.size() << " current extent size: " << cur_extent_size;
65 }
66 const size_t bytes_to_copy =
Kelvin Zhangb1706762021-06-25 15:05:22 -040067 std::min<size_t>(count, cur_extent_size - buffer_.size());
Kelvin Zhang243d2a12022-01-26 09:48:40 -080068 TEST_GT(bytes_to_copy, 0U);
Kelvin Zhangb1706762021-06-25 15:05:22 -040069
70 buffer_.insert(buffer_.end(), data, data + bytes_to_copy);
Kelvin Zhang243d2a12022-01-26 09:48:40 -080071 TEST_LE(buffer_.size(), cur_extent_size);
Kelvin Zhangb1706762021-06-25 15:05:22 -040072
73 if (buffer_.size() == cur_extent_size) {
74 if (!WriteExtent(buffer_.data(), cur_extent, block_size_)) {
75 LOG(ERROR) << "WriteExtent(" << buffer_.data() << ", "
76 << cur_extent.start_block() << ", " << cur_extent.num_blocks()
77 << ") failed.";
78 return 0;
79 }
80 buffer_.clear();
81 if (!NextExtent()) {
Kelvin Zhang243d2a12022-01-26 09:48:40 -080082 if (count != bytes_to_copy) {
83 LOG(ERROR) << "Exhausted all blocks, but still have "
84 << count - bytes_to_copy << " bytes left";
85 }
Kelvin Zhangb1706762021-06-25 15:05:22 -040086 }
87 }
88 return bytes_to_copy;
89}
90
91// Returns true on success.
92// This will construct a COW_REPLACE operation and forward it to CowWriter. It
93// is important that caller does not perform SOURCE_COPY operation on this
94// class, otherwise raw data will be stored. Caller should find ways to use
95// COW_COPY whenever possible.
96bool BlockExtentWriter::Write(const void* bytes, size_t count) {
97 if (count == 0) {
98 return true;
99 }
Kelvin Zhangb1706762021-06-25 15:05:22 -0400100
101 auto data = static_cast<const uint8_t*>(bytes);
102 while (count > 0) {
Kelvin Zhang243d2a12022-01-26 09:48:40 -0800103 const auto bytes_written = ConsumeWithBuffer(data, count);
Kelvin Zhangb1706762021-06-25 15:05:22 -0400104 TEST_AND_RETURN_FALSE(bytes_written > 0);
105 data += bytes_written;
106 count -= bytes_written;
107 }
108 return true;
109}
110
111bool BlockExtentWriter::NextExtent() {
112 cur_extent_idx_++;
113 return cur_extent_idx_ < static_cast<size_t>(extents_.size());
114}
115} // namespace chromeos_update_engine