Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 1 | // 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 Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 14 | #include <chromeos/streams/stream.h> |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 15 | #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 | |
| 24 | namespace chromeos_update_engine { |
| 25 | |
| 26 | class SystemState; |
| 27 | |
| 28 | // The type of filesystem that we are verifying. |
| 29 | enum class PartitionType { |
| 30 | kSourceRootfs, |
| 31 | kSourceKernel, |
| 32 | kRootfs, |
| 33 | kKernel, |
| 34 | }; |
| 35 | |
| 36 | class 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 Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 59 | // Schedules the asynchronous read of the filesystem. |
| 60 | void ScheduleRead(); |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 61 | |
Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 62 | // 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 Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 70 | |
| 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 Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 79 | void DetermineFilesystemSize(const std::string& path); |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 80 | |
| 81 | // The type of the partition that we are verifying. |
| 82 | PartitionType partition_type_; |
| 83 | |
Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 84 | // If not null, the FileStream used to read from the device. |
| 85 | chromeos::StreamPtr src_stream_; |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 86 | |
| 87 | // Buffer for storing data we read. |
| 88 | chromeos::Blob buffer_; |
| 89 | |
Alex Deymo | 20c9920 | 2015-07-09 16:14:16 -0700 | [diff] [blame] | 90 | bool read_done_{false}; // true if reached EOF on the input stream. |
Alex Deymo | 20c9920 | 2015-07-09 16:14:16 -0700 | [diff] [blame] | 91 | bool cancelled_{false}; // true if the action has been cancelled. |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 92 | |
| 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_ |