blob: 1379277fd7fb88f26ffd6d3ddc3622392a671a0e [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
Kelvin Zhange47767a2023-05-16 13:00:58 -070019#include <stdint.h>
Kelvin Zhangb1706762021-06-25 15:05:22 -040020
Kelvin Zhange47767a2023-05-16 13:00:58 -070021#include <algorithm>
22
23#include "update_engine/common/utils.h"
24#include "update_engine/payload_generator/delta_diff_generator.h"
25#include "update_engine/payload_generator/extent_ranges.h"
Kelvin Zhangb1706762021-06-25 15:05:22 -040026#include "update_engine/update_metadata.pb.h"
27
28namespace chromeos_update_engine {
29
Kelvin Zhangb1706762021-06-25 15:05:22 -040030bool BlockExtentWriter::Init(
31 const google::protobuf::RepeatedPtrField<Extent>& extents,
32 uint32_t block_size) {
Kelvin Zhang243d2a12022-01-26 09:48:40 -080033 TEST_NE(extents.size(), 0);
Kelvin Zhangb1706762021-06-25 15:05:22 -040034 extents_ = extents;
35 cur_extent_idx_ = 0;
36 buffer_.clear();
37 buffer_.reserve(block_size);
38 block_size_ = block_size;
39 return true;
40}
41
Kelvin Zhange47767a2023-05-16 13:00:58 -070042bool BlockExtentWriter::WriteExtent(const void* bytes, const size_t count) {
Kelvin Zhangb1706762021-06-25 15:05:22 -040043 const auto& cur_extent = extents_[cur_extent_idx_];
Kelvin Zhange47767a2023-05-16 13:00:58 -070044 const auto write_extent =
45 ExtentForRange(cur_extent.start_block() + offset_in_extent_ / block_size_,
46 count / kBlockSize);
47 offset_in_extent_ += count;
48 if (offset_in_extent_ == cur_extent.num_blocks() * block_size_) {
49 NextExtent();
50 }
51 return WriteExtent(bytes, write_extent, block_size_);
52}
Kelvin Zhangb1706762021-06-25 15:05:22 -040053
Kelvin Zhange47767a2023-05-16 13:00:58 -070054size_t BlockExtentWriter::ConsumeWithBuffer(const uint8_t* const data,
55 const size_t count) {
56 if (cur_extent_idx_ >= static_cast<size_t>(extents_.size())) {
57 if (count > 0) {
58 LOG(ERROR) << "Exhausted all blocks, but still have " << count
59 << " bytes pending for write";
60 }
61 return 0;
62 }
63 const auto& cur_extent = extents_[cur_extent_idx_];
64 const auto cur_extent_size =
65 static_cast<size_t>(cur_extent.num_blocks() * block_size_);
66
67 const auto write_size =
68 std::min(cur_extent_size - offset_in_extent_, BUFFER_SIZE);
69 if (buffer_.empty() && count >= write_size) {
70 if (!WriteExtent(data, write_size)) {
Kelvin Zhang636ba2f2022-02-09 14:40:22 -080071 LOG(ERROR) << "WriteExtent(" << cur_extent.start_block() << ", "
Kelvin Zhang8ddadd32023-12-12 12:54:30 -080072 << write_size << ") failed.";
Kelvin Zhangb1706762021-06-25 15:05:22 -040073 // return value is expected to be greater than 0. Return 0 to signal error
74 // condition
75 return 0;
76 }
Kelvin Zhange47767a2023-05-16 13:00:58 -070077 return write_size;
Kelvin Zhangb1706762021-06-25 15:05:22 -040078 }
Kelvin Zhange47767a2023-05-16 13:00:58 -070079 if (buffer_.size() >= write_size) {
Kelvin Zhang243d2a12022-01-26 09:48:40 -080080 LOG(ERROR)
Kelvin Zhange47767a2023-05-16 13:00:58 -070081 << "Data left in buffer should never be >= write_size, otherwise "
Kelvin Zhang243d2a12022-01-26 09:48:40 -080082 "we should have send that data to CowWriter. Buffer size: "
Kelvin Zhange47767a2023-05-16 13:00:58 -070083 << buffer_.size() << " write_size: " << write_size;
Kelvin Zhang243d2a12022-01-26 09:48:40 -080084 }
85 const size_t bytes_to_copy =
Kelvin Zhange47767a2023-05-16 13:00:58 -070086 std::min<size_t>(count, write_size - buffer_.size());
Kelvin Zhang243d2a12022-01-26 09:48:40 -080087 TEST_GT(bytes_to_copy, 0U);
Kelvin Zhangb1706762021-06-25 15:05:22 -040088
89 buffer_.insert(buffer_.end(), data, data + bytes_to_copy);
Kelvin Zhange47767a2023-05-16 13:00:58 -070090 TEST_LE(buffer_.size(), write_size);
Kelvin Zhangb1706762021-06-25 15:05:22 -040091
Kelvin Zhange47767a2023-05-16 13:00:58 -070092 if (buffer_.size() == write_size) {
93 if (!WriteExtent(buffer_.data(), write_size)) {
Kelvin Zhang8ddadd32023-12-12 12:54:30 -080094 LOG(ERROR) << "WriteExtent(" << cur_extent.start_block() << ", "
95 << cur_extent.num_blocks() << ") failed.";
Kelvin Zhangb1706762021-06-25 15:05:22 -040096 return 0;
97 }
98 buffer_.clear();
Kelvin Zhangb1706762021-06-25 15:05:22 -040099 }
100 return bytes_to_copy;
101}
102
103// Returns true on success.
104// This will construct a COW_REPLACE operation and forward it to CowWriter. It
105// is important that caller does not perform SOURCE_COPY operation on this
106// class, otherwise raw data will be stored. Caller should find ways to use
107// COW_COPY whenever possible.
108bool BlockExtentWriter::Write(const void* bytes, size_t count) {
109 if (count == 0) {
110 return true;
111 }
Kelvin Zhangb1706762021-06-25 15:05:22 -0400112
113 auto data = static_cast<const uint8_t*>(bytes);
114 while (count > 0) {
Kelvin Zhang243d2a12022-01-26 09:48:40 -0800115 const auto bytes_written = ConsumeWithBuffer(data, count);
Kelvin Zhangb1706762021-06-25 15:05:22 -0400116 TEST_AND_RETURN_FALSE(bytes_written > 0);
117 data += bytes_written;
118 count -= bytes_written;
119 }
120 return true;
121}
122
123bool BlockExtentWriter::NextExtent() {
124 cur_extent_idx_++;
Kelvin Zhange47767a2023-05-16 13:00:58 -0700125 offset_in_extent_ = 0;
Kelvin Zhangb1706762021-06-25 15:05:22 -0400126 return cur_extent_idx_ < static_cast<size_t>(extents_.size());
127}
128} // namespace chromeos_update_engine