Alex Deymo | 21ec92f | 2016-11-04 15:49:53 -0700 | [diff] [blame] | 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 | #include "update_engine/payload_consumer/file_descriptor_utils.h" |
| 18 | |
| 19 | #include <algorithm> |
| 20 | |
| 21 | #include <base/logging.h> |
| 22 | |
| 23 | #include "update_engine/common/hash_calculator.h" |
| 24 | #include "update_engine/common/utils.h" |
Amin Hassani | db56be9 | 2017-09-06 12:41:23 -0700 | [diff] [blame] | 25 | #include "update_engine/payload_consumer/extent_reader.h" |
Alex Deymo | 21ec92f | 2016-11-04 15:49:53 -0700 | [diff] [blame] | 26 | #include "update_engine/payload_consumer/extent_writer.h" |
| 27 | |
| 28 | using google::protobuf::RepeatedPtrField; |
| 29 | using std::min; |
| 30 | |
| 31 | namespace chromeos_update_engine { |
| 32 | |
| 33 | namespace { |
| 34 | |
| 35 | // Size of the buffer used to copy blocks. |
Amin Hassani | b379d19 | 2017-12-06 15:31:17 -0800 | [diff] [blame] | 36 | const uint64_t kMaxCopyBufferSize = 1024 * 1024; |
| 37 | |
| 38 | bool CommonHashExtents(FileDescriptorPtr source, |
| 39 | const RepeatedPtrField<Extent>& src_extents, |
Sen Jiang | bcb15aa | 2018-02-07 18:01:48 -0800 | [diff] [blame^] | 40 | DirectExtentWriter* writer, |
Amin Hassani | b379d19 | 2017-12-06 15:31:17 -0800 | [diff] [blame] | 41 | uint64_t block_size, |
| 42 | brillo::Blob* hash_out) { |
| 43 | auto total_blocks = utils::BlocksInExtents(src_extents); |
| 44 | auto buffer_blocks = kMaxCopyBufferSize / block_size; |
| 45 | // Ensure we copy at least one block at a time. |
| 46 | if (buffer_blocks < 1) |
| 47 | buffer_blocks = 1; |
| 48 | brillo::Blob buf(buffer_blocks * block_size); |
| 49 | |
| 50 | DirectExtentReader reader; |
| 51 | TEST_AND_RETURN_FALSE(reader.Init(source, src_extents, block_size)); |
Amin Hassani | b379d19 | 2017-12-06 15:31:17 -0800 | [diff] [blame] | 52 | |
| 53 | HashCalculator source_hasher; |
| 54 | while (total_blocks > 0) { |
| 55 | auto read_blocks = std::min(total_blocks, buffer_blocks); |
| 56 | TEST_AND_RETURN_FALSE(reader.Read(buf.data(), read_blocks * block_size)); |
| 57 | if (hash_out != nullptr) { |
| 58 | TEST_AND_RETURN_FALSE( |
| 59 | source_hasher.Update(buf.data(), read_blocks * block_size)); |
| 60 | } |
Sen Jiang | bcb15aa | 2018-02-07 18:01:48 -0800 | [diff] [blame^] | 61 | if (writer) { |
| 62 | TEST_AND_RETURN_FALSE( |
| 63 | writer->Write(buf.data(), read_blocks * block_size)); |
Amin Hassani | b379d19 | 2017-12-06 15:31:17 -0800 | [diff] [blame] | 64 | } |
| 65 | total_blocks -= read_blocks; |
| 66 | } |
Amin Hassani | b379d19 | 2017-12-06 15:31:17 -0800 | [diff] [blame] | 67 | |
| 68 | if (hash_out != nullptr) { |
| 69 | TEST_AND_RETURN_FALSE(source_hasher.Finalize()); |
| 70 | *hash_out = source_hasher.raw_hash(); |
| 71 | } |
| 72 | return true; |
| 73 | } |
Alex Deymo | 21ec92f | 2016-11-04 15:49:53 -0700 | [diff] [blame] | 74 | |
Alex Deymo | 21ec92f | 2016-11-04 15:49:53 -0700 | [diff] [blame] | 75 | } // namespace |
| 76 | |
| 77 | namespace fd_utils { |
| 78 | |
| 79 | bool CopyAndHashExtents(FileDescriptorPtr source, |
| 80 | const RepeatedPtrField<Extent>& src_extents, |
| 81 | FileDescriptorPtr target, |
| 82 | const RepeatedPtrField<Extent>& tgt_extents, |
Amin Hassani | b379d19 | 2017-12-06 15:31:17 -0800 | [diff] [blame] | 83 | uint64_t block_size, |
Alex Deymo | 21ec92f | 2016-11-04 15:49:53 -0700 | [diff] [blame] | 84 | brillo::Blob* hash_out) { |
Sen Jiang | bcb15aa | 2018-02-07 18:01:48 -0800 | [diff] [blame^] | 85 | DirectExtentWriter writer; |
| 86 | TEST_AND_RETURN_FALSE(writer.Init(target, tgt_extents, block_size)); |
| 87 | TEST_AND_RETURN_FALSE(utils::BlocksInExtents(src_extents) == |
| 88 | utils::BlocksInExtents(tgt_extents)); |
| 89 | TEST_AND_RETURN_FALSE( |
| 90 | CommonHashExtents(source, src_extents, &writer, block_size, hash_out)); |
| 91 | TEST_AND_RETURN_FALSE(writer.End()); |
Amin Hassani | b379d19 | 2017-12-06 15:31:17 -0800 | [diff] [blame] | 92 | return true; |
| 93 | } |
Amin Hassani | db56be9 | 2017-09-06 12:41:23 -0700 | [diff] [blame] | 94 | |
Amin Hassani | b379d19 | 2017-12-06 15:31:17 -0800 | [diff] [blame] | 95 | bool ReadAndHashExtents(FileDescriptorPtr source, |
| 96 | const RepeatedPtrField<Extent>& extents, |
| 97 | uint64_t block_size, |
| 98 | brillo::Blob* hash_out) { |
| 99 | TEST_AND_RETURN_FALSE(hash_out != nullptr); |
| 100 | TEST_AND_RETURN_FALSE( |
Sen Jiang | bcb15aa | 2018-02-07 18:01:48 -0800 | [diff] [blame^] | 101 | CommonHashExtents(source, extents, nullptr, block_size, hash_out)); |
Alex Deymo | 21ec92f | 2016-11-04 15:49:53 -0700 | [diff] [blame] | 102 | return true; |
| 103 | } |
| 104 | |
| 105 | } // namespace fd_utils |
| 106 | |
| 107 | } // namespace chromeos_update_engine |