blob: 69e497278cdf2b3326a2e4a67b08dcffcf58299a [file] [log] [blame]
Allie Woodeb9e6d82015-04-17 13:55:30 -07001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef UPDATE_ENGINE_FILESYSTEM_VERIFIER_ACTION_H_
6#define UPDATE_ENGINE_FILESYSTEM_VERIFIER_ACTION_H_
7
8#include <sys/stat.h>
9#include <sys/types.h>
10
11#include <string>
12#include <vector>
13
Alex Deymo20c99202015-07-09 16:14:16 -070014#include <chromeos/message_loops/message_loop.h>
Allie Woodeb9e6d82015-04-17 13:55:30 -070015#include <gtest/gtest_prod.h> // for FRIEND_TEST
16
17#include "update_engine/action.h"
18#include "update_engine/install_plan.h"
19#include "update_engine/omaha_hash_calculator.h"
20
21// This action will only do real work if it's a delta update. It will
22// copy the root partition to install partition, and then terminate.
23
24namespace chromeos_update_engine {
25
26class SystemState;
27
28// The type of filesystem that we are verifying.
29enum class PartitionType {
30 kSourceRootfs,
31 kSourceKernel,
32 kRootfs,
33 kKernel,
34};
35
36class FilesystemVerifierAction : public InstallPlanAction {
37 public:
38 FilesystemVerifierAction(SystemState* system_state,
39 PartitionType partition_type);
40
41 void PerformAction() override;
42 void TerminateProcessing() override;
43
44 // Used for testing. Return true if Cleanup() has not yet been called due
45 // to a callback upon the completion or cancellation of the verifier action.
46 // A test should wait until IsCleanupPending() returns false before
47 // terminating the glib main loop.
48 bool IsCleanupPending() const;
49
50 // Debugging/logging
51 static std::string StaticType() { return "FilesystemVerifierAction"; }
52 std::string Type() const override { return StaticType(); }
53
54 private:
55 friend class FilesystemVerifierActionTest;
56 FRIEND_TEST(FilesystemVerifierActionTest,
57 RunAsRootDetermineFilesystemSizeTest);
58
Alex Deymo20c99202015-07-09 16:14:16 -070059 // Callback from the main loop when there's data to read from the file
60 // descriptor.
61 void OnReadReadyCallback();
Allie Woodeb9e6d82015-04-17 13:55:30 -070062
Alex Deymo20c99202015-07-09 16:14:16 -070063 // Based on the state of the read buffers, terminates read process and the
64 // action.
65 void CheckTerminationConditions();
Allie Woodeb9e6d82015-04-17 13:55:30 -070066
67 // Cleans up all the variables we use for async operations and tells the
68 // ActionProcessor we're done w/ |code| as passed in. |cancelled_| should be
69 // true if TerminateProcessing() was called.
70 void Cleanup(ErrorCode code);
71
72 // Determine, if possible, the source file system size to avoid copying the
73 // whole partition. Currently this supports only the root file system assuming
74 // it's ext3-compatible.
75 void DetermineFilesystemSize(int fd);
76
77 // The type of the partition that we are verifying.
78 PartitionType partition_type_;
79
80 // If non-null, this is the GUnixInputStream object for the opened source
81 // partition.
Alex Deymo20c99202015-07-09 16:14:16 -070082 int src_fd_{-1};
Allie Woodeb9e6d82015-04-17 13:55:30 -070083
84 // Buffer for storing data we read.
85 chromeos::Blob buffer_;
86
Alex Deymo20c99202015-07-09 16:14:16 -070087 // The task id for the the in-flight async call.
88 chromeos::MessageLoop::TaskId read_task_{chromeos::MessageLoop::kTaskIdNull};
Allie Woodeb9e6d82015-04-17 13:55:30 -070089
Alex Deymo20c99202015-07-09 16:14:16 -070090 bool read_done_{false}; // true if reached EOF on the input stream.
91 bool failed_{false}; // true if the action has failed.
92 bool cancelled_{false}; // true if the action has been cancelled.
Allie Woodeb9e6d82015-04-17 13:55:30 -070093
94 // The install plan we're passed in via the input pipe.
95 InstallPlan install_plan_;
96
97 // Calculates the hash of the data.
98 OmahaHashCalculator hasher_;
99
100 // Reads and hashes this many bytes from the head of the input stream. This
101 // field is initialized when the action is started and decremented as more
102 // bytes get read.
103 int64_t remaining_size_;
104
105 // The global context for update_engine.
106 SystemState* system_state_;
107
108 DISALLOW_COPY_AND_ASSIGN(FilesystemVerifierAction);
109};
110
111} // namespace chromeos_update_engine
112
113#endif // UPDATE_ENGINE_FILESYSTEM_VERIFIER_ACTION_H_