blob: f7d062077a92e3da316afcd7cd95092cb42c56b4 [file] [log] [blame]
Kelvin Zhangab3ce602021-02-24 14:46:40 -05001//
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
30namespace chromeos_update_engine {
31
32class VerifiedSourceFd {
33 public:
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 OpenCurrentECCPartition();
43 const size_t block_size_;
44 const std::string source_path_;
45 FileDescriptorPtr source_ecc_fd_;
46 FileDescriptorPtr source_fd_;
47
48 friend class PartitionWriterTest;
49 FRIEND_TEST(PartitionWriterTest, ChooseSourceFDTest);
50 // The total number of operations that failed source hash verification but
51 // passed after falling back to the error-corrected |source_ecc_fd_| device.
52 uint64_t source_ecc_recovered_failures_{0};
53
54 // Whether opening the current partition as an error-corrected device failed.
55 // Used to avoid re-opening the same source partition if it is not actually
56 // error corrected.
57 bool source_ecc_open_failure_{false};
58};
59} // namespace chromeos_update_engine
60
61#endif