blob: 516c24b18946c94b9a1777f76934dc313dfabb35 [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#ifndef UPDATE_ENGINE_BLOCK_EXTENT_WRITER_H_
18#define UPDATE_ENGINE_BLOCK_EXTENT_WRITER_H_
19
20#include <cstdint>
21#include <vector>
22
23#include "update_engine/payload_consumer/extent_writer.h"
24#include "update_engine/update_metadata.pb.h"
25
26namespace chromeos_update_engine {
27
28// Cache data upto size of one extent before writing.
29class BlockExtentWriter : public chromeos_update_engine::ExtentWriter {
30 public:
Kelvin Zhange47767a2023-05-16 13:00:58 -070031 static constexpr size_t BUFFER_SIZE = 1024 * 1024;
Kelvin Zhangb1706762021-06-25 15:05:22 -040032 BlockExtentWriter() = default;
Kelvin Zhang243d2a12022-01-26 09:48:40 -080033 ~BlockExtentWriter() = default;
Kelvin Zhangb1706762021-06-25 15:05:22 -040034 // Returns true on success.
35 bool Init(const google::protobuf::RepeatedPtrField<Extent>& extents,
36 uint32_t block_size) override;
37 // Returns true on success.
38 bool Write(const void* bytes, size_t count) final;
39 // Write data for 1 extent. |bytes| will be a pointer which points to data of
40 // size |extent.num_blocks()*block_size|. |extent| is the current extent we
41 // are writing to.
42 virtual bool WriteExtent(const void* bytes,
43 const Extent& extent,
44 size_t block_size) = 0;
Kelvin Zhang4bb49202021-07-08 21:39:05 -040045 size_t BlockSize() const { return block_size_; }
Kelvin Zhangb1706762021-06-25 15:05:22 -040046
47 private:
Kelvin Zhange47767a2023-05-16 13:00:58 -070048 bool WriteExtent(const void* bytes, size_t count);
Kelvin Zhangb1706762021-06-25 15:05:22 -040049 bool NextExtent();
Kelvin Zhange47767a2023-05-16 13:00:58 -070050 [[nodiscard]] size_t ConsumeWithBuffer(const uint8_t* const bytes,
51 const size_t count);
Daniel Zhengb66e62c2024-02-21 13:35:56 -080052 // It's a non-owning pointer, because PartitionWriter owns the CowWriter. This
Kelvin Zhangb1706762021-06-25 15:05:22 -040053 // allows us to use a single instance of CowWriter for all operations applied
54 // to the same partition.
55 google::protobuf::RepeatedPtrField<Extent> extents_;
Kelvin Zhange47767a2023-05-16 13:00:58 -070056 size_t cur_extent_idx_{};
Kelvin Zhangb1706762021-06-25 15:05:22 -040057 std::vector<uint8_t> buffer_;
Kelvin Zhange47767a2023-05-16 13:00:58 -070058 size_t block_size_{};
59 size_t offset_in_extent_{};
Kelvin Zhangb1706762021-06-25 15:05:22 -040060};
61
62} // namespace chromeos_update_engine
63
64#endif