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_PAYLOAD_CONSUMER_XOR_EXTENT_WRITER_H_
18 #define UPDATE_ENGINE_PAYLOAD_CONSUMER_XOR_EXTENT_WRITER_H_
19 
20 #include <vector>
21 
22 #include "common/utils.h"
23 #include "update_engine/payload_consumer/block_extent_writer.h"
24 #include "update_engine/payload_consumer/extent_map.h"
25 
26 #include <update_engine/update_metadata.pb.h>
27 #include <libsnapshot/cow_writer.h>
28 
29 namespace chromeos_update_engine {
30 
31 // An extent writer that will selectively convert some of the blocks into an XOR
32 // block. All blocks that appear in |xor_map| will be converted,
33 class XORExtentWriter : public BlockExtentWriter {
34  public:
XORExtentWriter(const InstallOperation & op,FileDescriptorPtr source_fd,android::snapshot::ICowWriter * cow_writer,const ExtentMap<const CowMergeOperation * > & xor_map,size_t partition_size)35   XORExtentWriter(const InstallOperation& op,
36                   FileDescriptorPtr source_fd,
37                   android::snapshot::ICowWriter* cow_writer,
38                   const ExtentMap<const CowMergeOperation*>& xor_map,
39                   size_t partition_size)
40       : src_extents_(op.src_extents()),
41         source_fd_(source_fd),
42         xor_map_(xor_map),
43         cow_writer_(cow_writer),
44         partition_size_(partition_size) {
45     CHECK(source_fd->IsOpen());
46   }
47   ~XORExtentWriter() = default;
48 
49   // Returns true on success.
50   bool WriteExtent(const void* bytes,
51                    const Extent& extent,
52                    size_t size) override;
53 
54  private:
55   bool WriteReplaceExtents(const std::vector<Extent>& replace_extents,
56                            const Extent& extent,
57                            const void* bytes,
58                            size_t size);
59   bool WriteXorExtent(const uint8_t* bytes,
60                       const size_t size,
61                       const Extent& xor_ext,
62                       const CowMergeOperation* merge_op);
63   bool WriteXorCowOp(const uint8_t* bytes,
64                      const size_t size,
65                      const Extent& xor_ext,
66                      size_t src_offset);
67   const google::protobuf::RepeatedPtrField<Extent>& src_extents_;
68   const FileDescriptorPtr source_fd_;
69   const ExtentMap<const CowMergeOperation*>& xor_map_;
70   android::snapshot::ICowWriter* cow_writer_;
71   std::vector<uint8_t> xor_block_data;
72   const size_t partition_size_;
73 };
74 
75 }  // namespace chromeos_update_engine
76 
77 #endif  // UPDATE_ENGINE_PAYLOAD_CONSUMER_XOR_EXTENT_WRITER_H_
78