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