blob: a5c990c2d335cdbad08cbe9c0f2d1e7aeeec49af [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
26BlockExtentWriter::~BlockExtentWriter() {
27 CHECK(buffer_.empty()) << buffer_.size();
28}
29
30bool BlockExtentWriter::Init(
31 const google::protobuf::RepeatedPtrField<Extent>& extents,
32 uint32_t block_size) {
33 extents_ = extents;
34 cur_extent_idx_ = 0;
35 buffer_.clear();
36 buffer_.reserve(block_size);
37 block_size_ = block_size;
38 return true;
39}
40
41size_t BlockExtentWriter::ConsumeWithBuffer(const uint8_t* data, size_t count) {
42 CHECK_LT(cur_extent_idx_, static_cast<size_t>(extents_.size()));
43 const auto& cur_extent = extents_[cur_extent_idx_];
44 const auto cur_extent_size = cur_extent.num_blocks() * block_size_;
45
46 if (buffer_.empty() && count >= cur_extent_size) {
47 if (!WriteExtent(data, cur_extent, block_size_)) {
48 LOG(ERROR) << "AddRawBlocks(" << cur_extent.start_block() << ", " << data
49 << ", " << cur_extent_size << ") failed.";
50 // return value is expected to be greater than 0. Return 0 to signal error
51 // condition
52 return 0;
53 }
54 if (!NextExtent()) {
55 CHECK_EQ(count, cur_extent_size)
56 << "Exhausted all blocks, but still have " << count - cur_extent_size
57 << " bytes left";
58 }
59 return cur_extent_size;
60 }
61 CHECK_LT(buffer_.size(), cur_extent_size)
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 size_t bytes_to_copy =
66 std::min<size_t>(count, cur_extent_size - buffer_.size());
67 CHECK_GT(bytes_to_copy, 0U);
68
69 buffer_.insert(buffer_.end(), data, data + bytes_to_copy);
70 CHECK_LE(buffer_.size(), cur_extent_size);
71
72 if (buffer_.size() == cur_extent_size) {
73 if (!WriteExtent(buffer_.data(), cur_extent, block_size_)) {
74 LOG(ERROR) << "WriteExtent(" << buffer_.data() << ", "
75 << cur_extent.start_block() << ", " << cur_extent.num_blocks()
76 << ") failed.";
77 return 0;
78 }
79 buffer_.clear();
80 if (!NextExtent()) {
81 CHECK_EQ(count, bytes_to_copy) << "Exhausted all blocks, but still have "
82 << count - bytes_to_copy << " bytes left";
83 }
84 }
85 return bytes_to_copy;
86}
87
88// Returns true on success.
89// This will construct a COW_REPLACE operation and forward it to CowWriter. It
90// is important that caller does not perform SOURCE_COPY operation on this
91// class, otherwise raw data will be stored. Caller should find ways to use
92// COW_COPY whenever possible.
93bool BlockExtentWriter::Write(const void* bytes, size_t count) {
94 if (count == 0) {
95 return true;
96 }
97 CHECK_NE(extents_.size(), 0);
98
99 auto data = static_cast<const uint8_t*>(bytes);
100 while (count > 0) {
101 auto bytes_written = ConsumeWithBuffer(data, count);
102 TEST_AND_RETURN_FALSE(bytes_written > 0);
103 data += bytes_written;
104 count -= bytes_written;
105 }
106 return true;
107}
108
109bool BlockExtentWriter::NextExtent() {
110 cur_extent_idx_++;
111 return cur_extent_idx_ < static_cast<size_t>(extents_.size());
112}
113} // namespace chromeos_update_engine