1 // 2 // Copyright (C) 2017 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_FILE_DESCRIPTOR_UTILS_H_ 18 #define UPDATE_ENGINE_PAYLOAD_CONSUMER_FILE_DESCRIPTOR_UTILS_H_ 19 20 #include <brillo/secure_blob.h> 21 22 #include "update_engine/payload_consumer/extent_writer.h" 23 #include "update_engine/payload_consumer/file_descriptor.h" 24 #include "update_engine/update_metadata.pb.h" 25 26 namespace chromeos_update_engine { 27 namespace fd_utils { 28 29 bool CommonHashExtents( 30 FileDescriptorPtr source, 31 const google::protobuf::RepeatedPtrField<Extent>& src_extents, 32 ExtentWriter* writer, 33 uint64_t block_size, 34 brillo::Blob* hash_out); 35 36 // Copy blocks from the |source| file to the |target| file and hashes the 37 // contents. The blocks to copy from the |source| to the |target| files are 38 // specified by the |src_extents| and |tgt_extents| list of Extents, which 39 // must have the same length in number of blocks. Stores the hash of the 40 // copied blocks in Blob pointed by |hash_out| if not null. The block size 41 // is passed as |block_size|. In case of error reading or writing, returns 42 // false and the value pointed by |hash_out| is undefined. 43 // The |source| and |target| files must be different, or otherwise |src_extents| 44 // and |tgt_extents| must not overlap. 45 bool CopyAndHashExtents( 46 FileDescriptorPtr source, 47 const google::protobuf::RepeatedPtrField<Extent>& src_extents, 48 FileDescriptorPtr target, 49 const google::protobuf::RepeatedPtrField<Extent>& tgt_extents, 50 uint64_t block_size, 51 brillo::Blob* hash_out); 52 53 // Reads blocks from |source| and calculates the hash. The blocks to read are 54 // specified by |extents|. Stores the hash in |hash_out| if it is not null. The 55 // block sizes are passed as |block_size|. In case of error reading, it returns 56 // false and the value pointed by |hash_out| is undefined. 57 bool ReadAndHashExtents( 58 FileDescriptorPtr source, 59 const google::protobuf::RepeatedPtrField<Extent>& extents, 60 uint64_t block_size, 61 brillo::Blob* hash_out); 62 63 } // namespace fd_utils 64 } // namespace chromeos_update_engine 65 66 #endif // UPDATE_ENGINE_PAYLOAD_CONSUMER_FILE_DESCRIPTOR_UTILS_H_ 67