| 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 |  | 
|  | 14 | #include <gio/gio.h> | 
|  | 15 | #include <glib.h> | 
|  | 16 | #include <gtest/gtest_prod.h>  // for FRIEND_TEST | 
|  | 17 |  | 
|  | 18 | #include "update_engine/action.h" | 
|  | 19 | #include "update_engine/install_plan.h" | 
|  | 20 | #include "update_engine/omaha_hash_calculator.h" | 
|  | 21 |  | 
|  | 22 | // This action will only do real work if it's a delta update. It will | 
|  | 23 | // copy the root partition to install partition, and then terminate. | 
|  | 24 |  | 
|  | 25 | namespace chromeos_update_engine { | 
|  | 26 |  | 
|  | 27 | class SystemState; | 
|  | 28 |  | 
|  | 29 | // The type of filesystem that we are verifying. | 
|  | 30 | enum class PartitionType { | 
|  | 31 | kSourceRootfs, | 
|  | 32 | kSourceKernel, | 
|  | 33 | kRootfs, | 
|  | 34 | kKernel, | 
|  | 35 | }; | 
|  | 36 |  | 
|  | 37 | class FilesystemVerifierAction : public InstallPlanAction { | 
|  | 38 | public: | 
|  | 39 | FilesystemVerifierAction(SystemState* system_state, | 
|  | 40 | PartitionType partition_type); | 
|  | 41 |  | 
|  | 42 | void PerformAction() override; | 
|  | 43 | void TerminateProcessing() override; | 
|  | 44 |  | 
|  | 45 | // Used for testing. Return true if Cleanup() has not yet been called due | 
|  | 46 | // to a callback upon the completion or cancellation of the verifier action. | 
|  | 47 | // A test should wait until IsCleanupPending() returns false before | 
|  | 48 | // terminating the glib main loop. | 
|  | 49 | bool IsCleanupPending() const; | 
|  | 50 |  | 
|  | 51 | // Debugging/logging | 
|  | 52 | static std::string StaticType() { return "FilesystemVerifierAction"; } | 
|  | 53 | std::string Type() const override { return StaticType(); } | 
|  | 54 |  | 
|  | 55 | private: | 
|  | 56 | friend class FilesystemVerifierActionTest; | 
|  | 57 | FRIEND_TEST(FilesystemVerifierActionTest, | 
|  | 58 | RunAsRootDetermineFilesystemSizeTest); | 
|  | 59 |  | 
|  | 60 | // Callbacks from glib when the read operation is done. | 
|  | 61 | void AsyncReadReadyCallback(GObject *source_object, GAsyncResult *res); | 
|  | 62 | static void StaticAsyncReadReadyCallback(GObject *source_object, | 
|  | 63 | GAsyncResult *res, | 
|  | 64 | gpointer user_data); | 
|  | 65 |  | 
|  | 66 | // Based on the state of the ping-pong buffers spawns appropriate read | 
|  | 67 | // actions asynchronously. | 
|  | 68 | void SpawnAsyncActions(); | 
|  | 69 |  | 
|  | 70 | // Cleans up all the variables we use for async operations and tells the | 
|  | 71 | // ActionProcessor we're done w/ |code| as passed in. |cancelled_| should be | 
|  | 72 | // true if TerminateProcessing() was called. | 
|  | 73 | void Cleanup(ErrorCode code); | 
|  | 74 |  | 
|  | 75 | // Determine, if possible, the source file system size to avoid copying the | 
|  | 76 | // whole partition. Currently this supports only the root file system assuming | 
|  | 77 | // it's ext3-compatible. | 
|  | 78 | void DetermineFilesystemSize(int fd); | 
|  | 79 |  | 
|  | 80 | // The type of the partition that we are verifying. | 
|  | 81 | PartitionType partition_type_; | 
|  | 82 |  | 
|  | 83 | // If non-null, this is the GUnixInputStream object for the opened source | 
|  | 84 | // partition. | 
|  | 85 | GInputStream* src_stream_; | 
|  | 86 |  | 
|  | 87 | // Buffer for storing data we read. | 
|  | 88 | chromeos::Blob buffer_; | 
|  | 89 |  | 
|  | 90 | // The cancellable object for the in-flight async calls. | 
|  | 91 | GCancellable* canceller_; | 
|  | 92 |  | 
|  | 93 | bool read_done_;  // true if reached EOF on the input stream. | 
|  | 94 | bool failed_;  // true if the action has failed. | 
|  | 95 | bool cancelled_;  // true if the action has been cancelled. | 
|  | 96 |  | 
|  | 97 | // The install plan we're passed in via the input pipe. | 
|  | 98 | InstallPlan install_plan_; | 
|  | 99 |  | 
|  | 100 | // Calculates the hash of the data. | 
|  | 101 | OmahaHashCalculator hasher_; | 
|  | 102 |  | 
|  | 103 | // Reads and hashes this many bytes from the head of the input stream. This | 
|  | 104 | // field is initialized when the action is started and decremented as more | 
|  | 105 | // bytes get read. | 
|  | 106 | int64_t remaining_size_; | 
|  | 107 |  | 
|  | 108 | // The global context for update_engine. | 
|  | 109 | SystemState* system_state_; | 
|  | 110 |  | 
|  | 111 | DISALLOW_COPY_AND_ASSIGN(FilesystemVerifierAction); | 
|  | 112 | }; | 
|  | 113 |  | 
|  | 114 | }  // namespace chromeos_update_engine | 
|  | 115 |  | 
|  | 116 | #endif  // UPDATE_ENGINE_FILESYSTEM_VERIFIER_ACTION_H_ |