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 // limi
15 
16 #ifndef UPDATE_ENGINE_VERIFIED_SOURCE_FD_H__
17 #define UPDATE_ENGINE_VERIFIED_SOURCE_FD_H__
18 
19 #include <cstddef>
20 
21 #include <string>
22 #include <utility>
23 
24 #include <gtest/gtest_prod.h>
25 #include <update_engine/update_metadata.pb.h>
26 
27 #include "update_engine/common/error_code.h"
28 #include "update_engine/payload_consumer/file_descriptor.h"
29 
30 namespace chromeos_update_engine {
31 
32 class VerifiedSourceFd {
33  public:
VerifiedSourceFd(size_t block_size,std::string source_path)34   explicit VerifiedSourceFd(size_t block_size, std::string source_path)
35       : block_size_(block_size), source_path_(std::move(source_path)) {}
36   FileDescriptorPtr ChooseSourceFD(const InstallOperation& operation,
37                                    ErrorCode* error);
38 
39   [[nodiscard]] bool Open();
40 
41  private:
42   bool WriteBackCorrectedSourceBlocks(
43       const std::vector<unsigned char>& source_data,
44       const google::protobuf::RepeatedPtrField<Extent>& extents);
45   bool OpenCurrentECCPartition();
46   const size_t block_size_;
47   const std::string source_path_;
48   FileDescriptorPtr source_ecc_fd_;
49   FileDescriptorPtr source_fd_;
50 
51   friend class PartitionWriterTest;
52   FRIEND_TEST(PartitionWriterTest, ChooseSourceFDTest);
53   // The total number of operations that failed source hash verification but
54   // passed after falling back to the error-corrected |source_ecc_fd_| device.
55   uint64_t source_ecc_recovered_failures_{0};
56 
57   // Whether opening the current partition as an error-corrected device failed.
58   // Used to avoid re-opening the same source partition if it is not actually
59   // error corrected.
60   bool source_ecc_open_failure_{false};
61 };
62 }  // namespace chromeos_update_engine
63 
64 #endif
65