• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
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 
26 namespace chromeos_update_engine {
27 
28 // Cache data upto size of one extent before writing.
29 class BlockExtentWriter : public chromeos_update_engine::ExtentWriter {
30  public:
31   BlockExtentWriter() = default;
32   ~BlockExtentWriter() = default;
33   // Returns true on success.
34   bool Init(const google::protobuf::RepeatedPtrField<Extent>& extents,
35             uint32_t block_size) override;
36   // Returns true on success.
37   bool Write(const void* bytes, size_t count) final;
38   // Write data for 1 extent. |bytes| will be a pointer which points to data of
39   // size |extent.num_blocks()*block_size|. |extent| is the current extent we
40   // are writing to.
41   virtual bool WriteExtent(const void* bytes,
42                            const Extent& extent,
43                            size_t block_size) = 0;
BlockSize()44   size_t BlockSize() const { return block_size_; }
45 
46  private:
47   bool NextExtent();
48   [[nodiscard]] size_t ConsumeWithBuffer(const uint8_t* bytes, size_t count);
49   // It's a non-owning pointer, because PartitionWriter owns the CowWruter. This
50   // allows us to use a single instance of CowWriter for all operations applied
51   // to the same partition.
52   google::protobuf::RepeatedPtrField<Extent> extents_;
53   size_t cur_extent_idx_;
54   std::vector<uint8_t> buffer_;
55   size_t block_size_;
56 };
57 
58 }  // namespace chromeos_update_engine
59 
60 #endif
61