blob: 0a66b01b2aeceaef960d631a601341e87bf6f53e [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
2// Copyright (C) 2012 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//
Allie Woodeb9e6d82015-04-17 13:55:30 -070016
Alex Deymo39910dc2015-11-09 17:04:30 -080017#ifndef UPDATE_ENGINE_PAYLOAD_CONSUMER_FILESYSTEM_VERIFIER_ACTION_H_
18#define UPDATE_ENGINE_PAYLOAD_CONSUMER_FILESYSTEM_VERIFIER_ACTION_H_
Allie Woodeb9e6d82015-04-17 13:55:30 -070019
20#include <sys/stat.h>
21#include <sys/types.h>
22
23#include <string>
24#include <vector>
25
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070026#include <brillo/streams/stream.h>
Allie Woodeb9e6d82015-04-17 13:55:30 -070027#include <gtest/gtest_prod.h> // for FRIEND_TEST
28
Alex Deymo39910dc2015-11-09 17:04:30 -080029#include "update_engine/common/action.h"
30#include "update_engine/common/hash_calculator.h"
31#include "update_engine/payload_consumer/install_plan.h"
Allie Woodeb9e6d82015-04-17 13:55:30 -070032
Alex Deymoe5e5fe92015-10-05 09:28:19 -070033// This action will hash all the partitions of a single slot involved in the
34// update (either source or target slot). The hashes are then either stored in
35// the InstallPlan (for source partitions) or verified against it (for target
36// partitions).
Allie Woodeb9e6d82015-04-17 13:55:30 -070037
38namespace chromeos_update_engine {
39
Alex Deymoe5e5fe92015-10-05 09:28:19 -070040// The mode we are running the FilesystemVerifier on. On kComputeSourceHash mode
41// it computes the source_hash of all the partitions in the InstallPlan, based
42// on the already populated source_size values. On kVerifyTargetHash it computes
43// the hash on the target partitions based on the already populated size and
44// verifies it matches the one in the target_hash in the InstallPlan.
45enum class VerifierMode {
46 kComputeSourceHash,
47 kVerifyTargetHash,
Allie Woodeb9e6d82015-04-17 13:55:30 -070048};
49
50class FilesystemVerifierAction : public InstallPlanAction {
51 public:
Alex Deymoe5e5fe92015-10-05 09:28:19 -070052 FilesystemVerifierAction(const BootControlInterface* boot_control,
53 VerifierMode verifier_mode);
Allie Woodeb9e6d82015-04-17 13:55:30 -070054
55 void PerformAction() override;
56 void TerminateProcessing() override;
57
58 // Used for testing. Return true if Cleanup() has not yet been called due
59 // to a callback upon the completion or cancellation of the verifier action.
60 // A test should wait until IsCleanupPending() returns false before
Alex Deymo0b3db6b2015-08-10 15:19:37 -070061 // terminating the main loop.
Allie Woodeb9e6d82015-04-17 13:55:30 -070062 bool IsCleanupPending() const;
63
64 // Debugging/logging
65 static std::string StaticType() { return "FilesystemVerifierAction"; }
66 std::string Type() const override { return StaticType(); }
67
68 private:
69 friend class FilesystemVerifierActionTest;
70 FRIEND_TEST(FilesystemVerifierActionTest,
71 RunAsRootDetermineFilesystemSizeTest);
72
Alex Deymoe5e5fe92015-10-05 09:28:19 -070073 // Starts the hashing of the current partition. If there aren't any partitions
74 // remaining to be hashed, if finishes the action.
75 void StartPartitionHashing();
76
Alex Deymob9e8e262015-08-03 20:23:03 -070077 // Schedules the asynchronous read of the filesystem.
78 void ScheduleRead();
Allie Woodeb9e6d82015-04-17 13:55:30 -070079
Alex Deymob9e8e262015-08-03 20:23:03 -070080 // Called from the main loop when a single read from |src_stream_| succeeds or
81 // fails, calling OnReadDoneCallback() and OnReadErrorCallback() respectively.
82 void OnReadDoneCallback(size_t bytes_read);
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070083 void OnReadErrorCallback(const brillo::Error* error);
Alex Deymob9e8e262015-08-03 20:23:03 -070084
Alex Deymoe5e5fe92015-10-05 09:28:19 -070085 // When the read is done, finalize the hash checking of the current partition
86 // and continue checking the next one.
87 void FinishPartitionHashing();
Allie Woodeb9e6d82015-04-17 13:55:30 -070088
89 // Cleans up all the variables we use for async operations and tells the
90 // ActionProcessor we're done w/ |code| as passed in. |cancelled_| should be
91 // true if TerminateProcessing() was called.
92 void Cleanup(ErrorCode code);
93
Allie Woodeb9e6d82015-04-17 13:55:30 -070094 // The type of the partition that we are verifying.
Alex Deymoe5e5fe92015-10-05 09:28:19 -070095 const VerifierMode verifier_mode_;
96
97 // The BootControlInterface used to get the partitions based on the slots.
98 const BootControlInterface* const boot_control_;
99
100 // The index in the install_plan_.partitions vector of the partition currently
101 // being hashed.
102 size_t partition_index_{0};
Allie Woodeb9e6d82015-04-17 13:55:30 -0700103
Alex Deymob9e8e262015-08-03 20:23:03 -0700104 // If not null, the FileStream used to read from the device.
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700105 brillo::StreamPtr src_stream_;
Allie Woodeb9e6d82015-04-17 13:55:30 -0700106
107 // Buffer for storing data we read.
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700108 brillo::Blob buffer_;
Allie Woodeb9e6d82015-04-17 13:55:30 -0700109
Alex Deymo20c99202015-07-09 16:14:16 -0700110 bool read_done_{false}; // true if reached EOF on the input stream.
Alex Deymo20c99202015-07-09 16:14:16 -0700111 bool cancelled_{false}; // true if the action has been cancelled.
Allie Woodeb9e6d82015-04-17 13:55:30 -0700112
113 // The install plan we're passed in via the input pipe.
114 InstallPlan install_plan_;
115
116 // Calculates the hash of the data.
Alex Deymo39910dc2015-11-09 17:04:30 -0800117 std::unique_ptr<HashCalculator> hasher_;
Allie Woodeb9e6d82015-04-17 13:55:30 -0700118
119 // Reads and hashes this many bytes from the head of the input stream. This
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700120 // field is initialized from the corresponding InstallPlan::Partition size,
121 // when the partition starts to be hashed.
122 int64_t remaining_size_{0};
Allie Woodeb9e6d82015-04-17 13:55:30 -0700123
124 DISALLOW_COPY_AND_ASSIGN(FilesystemVerifierAction);
125};
126
127} // namespace chromeos_update_engine
128
Alex Deymo39910dc2015-11-09 17:04:30 -0800129#endif // UPDATE_ENGINE_PAYLOAD_CONSUMER_FILESYSTEM_VERIFIER_ACTION_H_