Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // |
| 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 Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 16 | |
| 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 Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 26 | #include <chromeos/streams/stream.h> |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 27 | #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 | |
| 36 | namespace chromeos_update_engine { |
| 37 | |
| 38 | class SystemState; |
| 39 | |
| 40 | // The type of filesystem that we are verifying. |
| 41 | enum class PartitionType { |
| 42 | kSourceRootfs, |
| 43 | kSourceKernel, |
| 44 | kRootfs, |
| 45 | kKernel, |
| 46 | }; |
| 47 | |
Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 48 | // Return the partition name string for the passed partition type. |
| 49 | std::string PartitionTypeToString(const PartitionType partition_type); |
| 50 | |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 51 | class 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 Deymo | 0b3db6b | 2015-08-10 15:19:37 -0700 | [diff] [blame] | 62 | // terminating the main loop. |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 63 | 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 Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 74 | // Schedules the asynchronous read of the filesystem. |
| 75 | void ScheduleRead(); |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 76 | |
Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 77 | // 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 Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 85 | |
| 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 Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 94 | void DetermineFilesystemSize(const std::string& path); |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 95 | |
| 96 | // The type of the partition that we are verifying. |
| 97 | PartitionType partition_type_; |
| 98 | |
Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 99 | // If not null, the FileStream used to read from the device. |
| 100 | chromeos::StreamPtr src_stream_; |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 101 | |
| 102 | // Buffer for storing data we read. |
| 103 | chromeos::Blob buffer_; |
| 104 | |
Alex Deymo | 20c9920 | 2015-07-09 16:14:16 -0700 | [diff] [blame] | 105 | bool read_done_{false}; // true if reached EOF on the input stream. |
Alex Deymo | 20c9920 | 2015-07-09 16:14:16 -0700 | [diff] [blame] | 106 | bool cancelled_{false}; // true if the action has been cancelled. |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 107 | |
| 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_ |