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"
25 #include "update_engine/payload_consumer/extent_reader.h"
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.
36 const uint64_t kMaxCopyBufferSize = 1024 * 1024;
37
38 } // namespace
39 namespace fd_utils {
40
CommonHashExtents(FileDescriptorPtr source,const RepeatedPtrField<Extent> & src_extents,ExtentWriter * writer,uint64_t block_size,brillo::Blob * hash_out)41 bool CommonHashExtents(FileDescriptorPtr source,
42 const RepeatedPtrField<Extent>& src_extents,
43 ExtentWriter* writer,
44 uint64_t block_size,
45 brillo::Blob* hash_out) {
46 auto total_blocks = utils::BlocksInExtents(src_extents);
47 auto buffer_blocks = kMaxCopyBufferSize / block_size;
48 // Ensure we copy at least one block at a time.
49 if (buffer_blocks < 1)
50 buffer_blocks = 1;
51 brillo::Blob buf(buffer_blocks * block_size);
52
53 DirectExtentReader reader;
54 TEST_AND_RETURN_FALSE(reader.Init(source, src_extents, block_size));
55
56 HashCalculator source_hasher;
57 while (total_blocks > 0) {
58 auto read_blocks = std::min(total_blocks, buffer_blocks);
59 TEST_AND_RETURN_FALSE(reader.Read(buf.data(), read_blocks * block_size));
60 if (hash_out != nullptr) {
61 TEST_AND_RETURN_FALSE(
62 source_hasher.Update(buf.data(), read_blocks * block_size));
63 }
64 if (writer) {
65 TEST_AND_RETURN_FALSE(
66 writer->Write(buf.data(), read_blocks * block_size));
67 }
68 total_blocks -= read_blocks;
69 }
70
71 if (hash_out != nullptr) {
72 TEST_AND_RETURN_FALSE(source_hasher.Finalize());
73 *hash_out = source_hasher.raw_hash();
74 }
75 return true;
76 }
77
CopyAndHashExtents(FileDescriptorPtr source,const RepeatedPtrField<Extent> & src_extents,FileDescriptorPtr target,const RepeatedPtrField<Extent> & tgt_extents,uint64_t block_size,brillo::Blob * hash_out)78 bool CopyAndHashExtents(FileDescriptorPtr source,
79 const RepeatedPtrField<Extent>& src_extents,
80 FileDescriptorPtr target,
81 const RepeatedPtrField<Extent>& tgt_extents,
82 uint64_t block_size,
83 brillo::Blob* hash_out) {
84 DirectExtentWriter writer{target};
85 TEST_AND_RETURN_FALSE(writer.Init(tgt_extents, block_size));
86 TEST_AND_RETURN_FALSE(utils::BlocksInExtents(src_extents) ==
87 utils::BlocksInExtents(tgt_extents));
88 TEST_AND_RETURN_FALSE(
89 CommonHashExtents(source, src_extents, &writer, block_size, hash_out));
90 return true;
91 }
92
ReadAndHashExtents(FileDescriptorPtr source,const RepeatedPtrField<Extent> & extents,uint64_t block_size,brillo::Blob * hash_out)93 bool ReadAndHashExtents(FileDescriptorPtr source,
94 const RepeatedPtrField<Extent>& extents,
95 uint64_t block_size,
96 brillo::Blob* hash_out) {
97 return CommonHashExtents(source, extents, nullptr, block_size, hash_out);
98 }
99
100 } // namespace fd_utils
101
102 } // namespace chromeos_update_engine
103