blob: c3c64cda4c5f403ed4b4ee5b52b445353f87d400 [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 <cstdint>
18#include <vector>
19
20#include <libsnapshot/cow_writer.h>
21
22#include "update_engine/payload_consumer/extent_writer.h"
Kelvin Zhang9754f172020-09-25 15:22:35 -040023#include "update_engine/update_metadata.pb.h"
Kelvin Zhang9b10dba2020-09-25 17:09:11 -040024
25namespace chromeos_update_engine {
Kelvin Zhang9754f172020-09-25 15:22:35 -040026
Kelvin Zhang9b10dba2020-09-25 17:09:11 -040027class SnapshotExtentWriter : public chromeos_update_engine::ExtentWriter {
28 public:
29 explicit SnapshotExtentWriter(android::snapshot::ICowWriter* cow_writer);
30 ~SnapshotExtentWriter();
31 // Returns true on success.
Kelvin Zhang4d22ca22021-02-09 14:06:25 -050032 bool Init(const google::protobuf::RepeatedPtrField<Extent>& extents,
Kelvin Zhang9b10dba2020-09-25 17:09:11 -040033 uint32_t block_size) override;
34 // Returns true on success.
35 // This will construct a COW_REPLACE operation and forward it to CowWriter. It
36 // is important that caller does not perform SOURCE_COPY operation on this
37 // class, otherwise raw data will be stored. Caller should find ways to use
38 // COW_COPY whenever possible.
39 bool Write(const void* bytes, size_t count) override;
40
41 private:
Kelvin Zhang9754f172020-09-25 15:22:35 -040042 bool next_extent();
Kelvin Zhange3293c72020-11-19 17:10:56 -050043 [[nodiscard]] size_t ConsumeWithBuffer(const uint8_t* bytes, size_t count);
Kelvin Zhang9b10dba2020-09-25 17:09:11 -040044 // It's a non-owning pointer, because PartitionWriter owns the CowWruter. This
45 // allows us to use a single instance of CowWriter for all operations applied
46 // to the same partition.
Kelvin Zhang9754f172020-09-25 15:22:35 -040047 android::snapshot::ICowWriter* cow_writer_;
48 google::protobuf::RepeatedPtrField<Extent> extents_;
49 size_t cur_extent_idx_;
50 std::vector<uint8_t> buffer_;
51 size_t block_size_;
Kelvin Zhang9b10dba2020-09-25 17:09:11 -040052};
Kelvin Zhang9754f172020-09-25 15:22:35 -040053
Kelvin Zhang9b10dba2020-09-25 17:09:11 -040054} // namespace chromeos_update_engine