blob: c9e6f312d74fab3de86f798ba06024f890ab4e43 [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//
Kelvin Zhang9754f172020-09-25 15:22:35 -040016
Kelvin Zhang9b10dba2020-09-25 17:09:11 -040017#include "update_engine/payload_consumer/snapshot_extent_writer.h"
18
19#include <algorithm>
20#include <cstdint>
21
22#include <libsnapshot/cow_writer.h>
23
24#include "update_engine/update_metadata.pb.h"
25
26namespace chromeos_update_engine {
Kelvin Zhang9754f172020-09-25 15:22:35 -040027
Kelvin Zhang9b10dba2020-09-25 17:09:11 -040028SnapshotExtentWriter::SnapshotExtentWriter(
29 android::snapshot::ICowWriter* cow_writer)
30 : cow_writer_(cow_writer) {
31 CHECK_NE(cow_writer, nullptr);
32}
33
34SnapshotExtentWriter::~SnapshotExtentWriter() {
Kelvin Zhang9754f172020-09-25 15:22:35 -040035 CHECK(buffer_.empty()) << buffer_.size();
Kelvin Zhang9b10dba2020-09-25 17:09:11 -040036}
37
38bool SnapshotExtentWriter::Init(
39 FileDescriptorPtr /*fd*/,
40 const google::protobuf::RepeatedPtrField<Extent>& extents,
Kelvin Zhang9754f172020-09-25 15:22:35 -040041 uint32_t block_size) {
42 extents_ = extents;
43 cur_extent_idx_ = 0;
44 buffer_.clear();
45 buffer_.reserve(block_size);
46 block_size_ = block_size;
Kelvin Zhang9b10dba2020-09-25 17:09:11 -040047 return true;
48}
49
Kelvin Zhang9754f172020-09-25 15:22:35 -040050size_t SnapshotExtentWriter::ConsumeWithBuffer(const uint8_t* data,
51 size_t count) {
52 CHECK_LT(cur_extent_idx_, static_cast<size_t>(extents_.size()));
53 const auto& cur_extent = extents_[cur_extent_idx_];
54 const auto cur_extent_size = cur_extent.num_blocks() * block_size_;
55
56 if (buffer_.empty() && count >= cur_extent_size) {
Kelvin Zhange3293c72020-11-19 17:10:56 -050057 if (!cow_writer_->AddRawBlocks(
58 cur_extent.start_block(), data, cur_extent_size)) {
59 LOG(ERROR) << "AddRawBlocks(" << cur_extent.start_block() << ", " << data
60 << ", " << cur_extent_size << ") failed.";
61 // return value is expected to be greater than 0. Return 0 to signal error
62 // condition
63 return 0;
64 }
Kelvin Zhang9754f172020-09-25 15:22:35 -040065 if (!next_extent()) {
66 CHECK_EQ(count, cur_extent_size)
67 << "Exhausted all blocks, but still have " << count - cur_extent_size
68 << " bytes left";
69 }
70 return cur_extent_size;
71 }
72 CHECK_LT(buffer_.size(), cur_extent_size)
73 << "Data left in buffer should never be >= cur_extent_size, otherwise "
74 "we should have send that data to CowWriter. Buffer size: "
75 << buffer_.size() << " current extent size: " << cur_extent_size;
76 size_t bytes_to_copy =
77 std::min<size_t>(count, cur_extent_size - buffer_.size());
78 CHECK_GT(bytes_to_copy, 0U);
79
80 buffer_.insert(buffer_.end(), data, data + bytes_to_copy);
81 CHECK_LE(buffer_.size(), cur_extent_size);
82
83 if (buffer_.size() == cur_extent_size) {
Kelvin Zhange3293c72020-11-19 17:10:56 -050084 if (!cow_writer_->AddRawBlocks(
85 cur_extent.start_block(), buffer_.data(), buffer_.size())) {
86 LOG(ERROR) << "AddRawBlocks(" << cur_extent.start_block() << ", "
87 << buffer_.data() << ", " << buffer_.size() << ") failed.";
88 return 0;
89 }
Kelvin Zhang9754f172020-09-25 15:22:35 -040090 buffer_.clear();
91 if (!next_extent()) {
92 CHECK_EQ(count, bytes_to_copy) << "Exhausted all blocks, but still have "
93 << count - bytes_to_copy << " bytes left";
94 }
95 }
96 return bytes_to_copy;
97}
98
Kelvin Zhang9b10dba2020-09-25 17:09:11 -040099// Returns true on success.
100// This will construct a COW_REPLACE operation and forward it to CowWriter. It
101// is important that caller does not perform SOURCE_COPY operation on this
102// class, otherwise raw data will be stored. Caller should find ways to use
103// COW_COPY whenever possible.
104bool SnapshotExtentWriter::Write(const void* bytes, size_t count) {
Kelvin Zhang9754f172020-09-25 15:22:35 -0400105 if (count == 0) {
106 return true;
107 }
108 CHECK_NE(extents_.size(), 0);
109
110 auto data = static_cast<const uint8_t*>(bytes);
111 while (count > 0) {
112 auto bytes_written = ConsumeWithBuffer(data, count);
Kelvin Zhange3293c72020-11-19 17:10:56 -0500113 TEST_AND_RETURN_FALSE(bytes_written > 0);
Kelvin Zhang9754f172020-09-25 15:22:35 -0400114 data += bytes_written;
115 count -= bytes_written;
116 }
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400117 return true;
118}
119
Kelvin Zhang9754f172020-09-25 15:22:35 -0400120bool SnapshotExtentWriter::next_extent() {
121 cur_extent_idx_++;
122 return cur_extent_idx_ < static_cast<size_t>(extents_.size());
123}
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400124} // namespace chromeos_update_engine