blob: 731790a74701d03b2b14b790dab373c0e4631b23 [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 Deymob9e8e262015-08-03 20:23:03 -070014#include <chromeos/streams/stream.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 Deymob9e8e262015-08-03 20:23:03 -070059 // Schedules the asynchronous read of the filesystem.
60 void ScheduleRead();
Allie Woodeb9e6d82015-04-17 13:55:30 -070061
Alex Deymob9e8e262015-08-03 20:23:03 -070062 // Called from the main loop when a single read from |src_stream_| succeeds or
63 // fails, calling OnReadDoneCallback() and OnReadErrorCallback() respectively.
64 void OnReadDoneCallback(size_t bytes_read);
65 void OnReadErrorCallback(const chromeos::Error* error);
66
67 // Based on the state of the read buffer, terminates read process and the
68 // action. Return whether the action was terminated.
69 bool CheckTerminationConditions();
Allie Woodeb9e6d82015-04-17 13:55:30 -070070
71 // Cleans up all the variables we use for async operations and tells the
72 // ActionProcessor we're done w/ |code| as passed in. |cancelled_| should be
73 // true if TerminateProcessing() was called.
74 void Cleanup(ErrorCode code);
75
76 // Determine, if possible, the source file system size to avoid copying the
77 // whole partition. Currently this supports only the root file system assuming
78 // it's ext3-compatible.
Alex Deymob9e8e262015-08-03 20:23:03 -070079 void DetermineFilesystemSize(const std::string& path);
Allie Woodeb9e6d82015-04-17 13:55:30 -070080
81 // The type of the partition that we are verifying.
82 PartitionType partition_type_;
83
Alex Deymob9e8e262015-08-03 20:23:03 -070084 // If not null, the FileStream used to read from the device.
85 chromeos::StreamPtr src_stream_;
Allie Woodeb9e6d82015-04-17 13:55:30 -070086
87 // Buffer for storing data we read.
88 chromeos::Blob buffer_;
89
Alex Deymo20c99202015-07-09 16:14:16 -070090 bool read_done_{false}; // true if reached EOF on the input stream.
Alex Deymo20c99202015-07-09 16:14:16 -070091 bool cancelled_{false}; // true if the action has been cancelled.
Allie Woodeb9e6d82015-04-17 13:55:30 -070092
93 // The install plan we're passed in via the input pipe.
94 InstallPlan install_plan_;
95
96 // Calculates the hash of the data.
97 OmahaHashCalculator hasher_;
98
99 // Reads and hashes this many bytes from the head of the input stream. This
100 // field is initialized when the action is started and decremented as more
101 // bytes get read.
102 int64_t remaining_size_;
103
104 // The global context for update_engine.
105 SystemState* system_state_;
106
107 DISALLOW_COPY_AND_ASSIGN(FilesystemVerifierAction);
108};
109
110} // namespace chromeos_update_engine
111
112#endif // UPDATE_ENGINE_FILESYSTEM_VERIFIER_ACTION_H_