blob: 5693c9b0e211ab52a385a83875a95e63ac9ea3c8 [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) {
57 TEST_AND_RETURN_FALSE(cow_writer_->AddRawBlocks(
58 cur_extent.start_block(), data, cur_extent_size));
59 if (!next_extent()) {
60 CHECK_EQ(count, cur_extent_size)
61 << "Exhausted all blocks, but still have " << count - cur_extent_size
62 << " bytes left";
63 }
64 return cur_extent_size;
65 }
66 CHECK_LT(buffer_.size(), cur_extent_size)
67 << "Data left in buffer should never be >= cur_extent_size, otherwise "
68 "we should have send that data to CowWriter. Buffer size: "
69 << buffer_.size() << " current extent size: " << cur_extent_size;
70 size_t bytes_to_copy =
71 std::min<size_t>(count, cur_extent_size - buffer_.size());
72 CHECK_GT(bytes_to_copy, 0U);
73
74 buffer_.insert(buffer_.end(), data, data + bytes_to_copy);
75 CHECK_LE(buffer_.size(), cur_extent_size);
76
77 if (buffer_.size() == cur_extent_size) {
78 TEST_AND_RETURN_FALSE(cow_writer_->AddRawBlocks(
79 cur_extent.start_block(), buffer_.data(), buffer_.size()));
80 buffer_.clear();
81 if (!next_extent()) {
82 CHECK_EQ(count, bytes_to_copy) << "Exhausted all blocks, but still have "
83 << count - bytes_to_copy << " bytes left";
84 }
85 }
86 return bytes_to_copy;
87}
88
Kelvin Zhang9b10dba2020-09-25 17:09:11 -040089// Returns true on success.
90// This will construct a COW_REPLACE operation and forward it to CowWriter. It
91// is important that caller does not perform SOURCE_COPY operation on this
92// class, otherwise raw data will be stored. Caller should find ways to use
93// COW_COPY whenever possible.
94bool SnapshotExtentWriter::Write(const void* bytes, size_t count) {
Kelvin Zhang9754f172020-09-25 15:22:35 -040095 if (count == 0) {
96 return true;
97 }
98 CHECK_NE(extents_.size(), 0);
99
100 auto data = static_cast<const uint8_t*>(bytes);
101 while (count > 0) {
102 auto bytes_written = ConsumeWithBuffer(data, count);
103 data += bytes_written;
104 count -= bytes_written;
105 }
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400106 return true;
107}
108
Kelvin Zhang9754f172020-09-25 15:22:35 -0400109bool SnapshotExtentWriter::next_extent() {
110 cur_extent_idx_++;
111 return cur_extent_idx_ < static_cast<size_t>(extents_.size());
112}
Kelvin Zhang9b10dba2020-09-25 17:09:11 -0400113} // namespace chromeos_update_engine