blob: 9935dea77a052d4f9701914743547e0f15a3bba3 [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
48class FilesystemVerifierAction : public InstallPlanAction {
49 public:
50 FilesystemVerifierAction(SystemState* system_state,
51 PartitionType partition_type);
52
53 void PerformAction() override;
54 void TerminateProcessing() override;
55
56 // Used for testing. Return true if Cleanup() has not yet been called due
57 // to a callback upon the completion or cancellation of the verifier action.
58 // A test should wait until IsCleanupPending() returns false before
Alex Deymo0b3db6b2015-08-10 15:19:37 -070059 // terminating the main loop.
Allie Woodeb9e6d82015-04-17 13:55:30 -070060 bool IsCleanupPending() const;
61
62 // Debugging/logging
63 static std::string StaticType() { return "FilesystemVerifierAction"; }
64 std::string Type() const override { return StaticType(); }
65
66 private:
67 friend class FilesystemVerifierActionTest;
68 FRIEND_TEST(FilesystemVerifierActionTest,
69 RunAsRootDetermineFilesystemSizeTest);
70
Alex Deymob9e8e262015-08-03 20:23:03 -070071 // Schedules the asynchronous read of the filesystem.
72 void ScheduleRead();
Allie Woodeb9e6d82015-04-17 13:55:30 -070073
Alex Deymob9e8e262015-08-03 20:23:03 -070074 // Called from the main loop when a single read from |src_stream_| succeeds or
75 // fails, calling OnReadDoneCallback() and OnReadErrorCallback() respectively.
76 void OnReadDoneCallback(size_t bytes_read);
77 void OnReadErrorCallback(const chromeos::Error* error);
78
79 // Based on the state of the read buffer, terminates read process and the
80 // action. Return whether the action was terminated.
81 bool CheckTerminationConditions();
Allie Woodeb9e6d82015-04-17 13:55:30 -070082
83 // Cleans up all the variables we use for async operations and tells the
84 // ActionProcessor we're done w/ |code| as passed in. |cancelled_| should be
85 // true if TerminateProcessing() was called.
86 void Cleanup(ErrorCode code);
87
88 // Determine, if possible, the source file system size to avoid copying the
89 // whole partition. Currently this supports only the root file system assuming
90 // it's ext3-compatible.
Alex Deymob9e8e262015-08-03 20:23:03 -070091 void DetermineFilesystemSize(const std::string& path);
Allie Woodeb9e6d82015-04-17 13:55:30 -070092
93 // The type of the partition that we are verifying.
94 PartitionType partition_type_;
95
Alex Deymob9e8e262015-08-03 20:23:03 -070096 // If not null, the FileStream used to read from the device.
97 chromeos::StreamPtr src_stream_;
Allie Woodeb9e6d82015-04-17 13:55:30 -070098
99 // Buffer for storing data we read.
100 chromeos::Blob buffer_;
101
Alex Deymo20c99202015-07-09 16:14:16 -0700102 bool read_done_{false}; // true if reached EOF on the input stream.
Alex Deymo20c99202015-07-09 16:14:16 -0700103 bool cancelled_{false}; // true if the action has been cancelled.
Allie Woodeb9e6d82015-04-17 13:55:30 -0700104
105 // The install plan we're passed in via the input pipe.
106 InstallPlan install_plan_;
107
108 // Calculates the hash of the data.
109 OmahaHashCalculator hasher_;
110
111 // Reads and hashes this many bytes from the head of the input stream. This
112 // field is initialized when the action is started and decremented as more
113 // bytes get read.
114 int64_t remaining_size_;
115
116 // The global context for update_engine.
117 SystemState* system_state_;
118
119 DISALLOW_COPY_AND_ASSIGN(FilesystemVerifierAction);
120};
121
122} // namespace chromeos_update_engine
123
124#endif // UPDATE_ENGINE_FILESYSTEM_VERIFIER_ACTION_H_