blob: 7a6930e7aed72125f0337061ac82a4d300cbc93e [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
17#ifndef UPDATE_ENGINE_FILESYSTEM_VERIFIER_ACTION_H_
18#define UPDATE_ENGINE_FILESYSTEM_VERIFIER_ACTION_H_
19
20#include <sys/stat.h>
21#include <sys/types.h>
22
23#include <string>
24#include <vector>
25
Alex Deymob9e8e262015-08-03 20:23:03 -070026#include <chromeos/streams/stream.h>
Allie Woodeb9e6d82015-04-17 13:55:30 -070027#include <gtest/gtest_prod.h> // for FRIEND_TEST
28
29#include "update_engine/action.h"
30#include "update_engine/install_plan.h"
31#include "update_engine/omaha_hash_calculator.h"
32
33// This action will only do real work if it's a delta update. It will
34// copy the root partition to install partition, and then terminate.
35
36namespace chromeos_update_engine {
37
38class SystemState;
39
40// The type of filesystem that we are verifying.
41enum class PartitionType {
42 kSourceRootfs,
43 kSourceKernel,
44 kRootfs,
45 kKernel,
46};
47
Alex Deymo763e7db2015-08-27 21:08:08 -070048// Return the partition name string for the passed partition type.
49std::string PartitionTypeToString(const PartitionType partition_type);
50
Allie Woodeb9e6d82015-04-17 13:55:30 -070051class FilesystemVerifierAction : public InstallPlanAction {
52 public:
53 FilesystemVerifierAction(SystemState* system_state,
54 PartitionType partition_type);
55
56 void PerformAction() override;
57 void TerminateProcessing() override;
58
59 // Used for testing. Return true if Cleanup() has not yet been called due
60 // to a callback upon the completion or cancellation of the verifier action.
61 // A test should wait until IsCleanupPending() returns false before
Alex Deymo0b3db6b2015-08-10 15:19:37 -070062 // terminating the main loop.
Allie Woodeb9e6d82015-04-17 13:55:30 -070063 bool IsCleanupPending() const;
64
65 // Debugging/logging
66 static std::string StaticType() { return "FilesystemVerifierAction"; }
67 std::string Type() const override { return StaticType(); }
68
69 private:
70 friend class FilesystemVerifierActionTest;
71 FRIEND_TEST(FilesystemVerifierActionTest,
72 RunAsRootDetermineFilesystemSizeTest);
73
Alex Deymob9e8e262015-08-03 20:23:03 -070074 // Schedules the asynchronous read of the filesystem.
75 void ScheduleRead();
Allie Woodeb9e6d82015-04-17 13:55:30 -070076
Alex Deymob9e8e262015-08-03 20:23:03 -070077 // Called from the main loop when a single read from |src_stream_| succeeds or
78 // fails, calling OnReadDoneCallback() and OnReadErrorCallback() respectively.
79 void OnReadDoneCallback(size_t bytes_read);
80 void OnReadErrorCallback(const chromeos::Error* error);
81
82 // Based on the state of the read buffer, terminates read process and the
83 // action. Return whether the action was terminated.
84 bool CheckTerminationConditions();
Allie Woodeb9e6d82015-04-17 13:55:30 -070085
86 // Cleans up all the variables we use for async operations and tells the
87 // ActionProcessor we're done w/ |code| as passed in. |cancelled_| should be
88 // true if TerminateProcessing() was called.
89 void Cleanup(ErrorCode code);
90
91 // Determine, if possible, the source file system size to avoid copying the
92 // whole partition. Currently this supports only the root file system assuming
93 // it's ext3-compatible.
Alex Deymob9e8e262015-08-03 20:23:03 -070094 void DetermineFilesystemSize(const std::string& path);
Allie Woodeb9e6d82015-04-17 13:55:30 -070095
96 // The type of the partition that we are verifying.
97 PartitionType partition_type_;
98
Alex Deymob9e8e262015-08-03 20:23:03 -070099 // If not null, the FileStream used to read from the device.
100 chromeos::StreamPtr src_stream_;
Allie Woodeb9e6d82015-04-17 13:55:30 -0700101
102 // Buffer for storing data we read.
103 chromeos::Blob buffer_;
104
Alex Deymo20c99202015-07-09 16:14:16 -0700105 bool read_done_{false}; // true if reached EOF on the input stream.
Alex Deymo20c99202015-07-09 16:14:16 -0700106 bool cancelled_{false}; // true if the action has been cancelled.
Allie Woodeb9e6d82015-04-17 13:55:30 -0700107
108 // The install plan we're passed in via the input pipe.
109 InstallPlan install_plan_;
110
111 // Calculates the hash of the data.
112 OmahaHashCalculator hasher_;
113
114 // Reads and hashes this many bytes from the head of the input stream. This
115 // field is initialized when the action is started and decremented as more
116 // bytes get read.
117 int64_t remaining_size_;
118
119 // The global context for update_engine.
120 SystemState* system_state_;
121
122 DISALLOW_COPY_AND_ASSIGN(FilesystemVerifierAction);
123};
124
125} // namespace chromeos_update_engine
126
127#endif // UPDATE_ENGINE_FILESYSTEM_VERIFIER_ACTION_H_