| Gilad Arnold | 6dbbd39 | 2012-07-10 16:19:11 -0700 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 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 CHROMEOS_PLATFORM_UPDATE_ENGINE_FILESYSTEM_COPIER_ACTION_H__ | 
|  | 6 | #define CHROMEOS_PLATFORM_UPDATE_ENGINE_FILESYSTEM_COPIER_ACTION_H__ | 
|  | 7 |  | 
|  | 8 | #include <sys/stat.h> | 
|  | 9 | #include <sys/types.h> | 
| Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 10 |  | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 11 | #include <string> | 
| Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 12 | #include <vector> | 
| Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 13 |  | 
| Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 14 | #include <gio/gio.h> | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 15 | #include <glib.h> | 
| Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 16 | #include <gtest/gtest_prod.h>  // for FRIEND_TEST | 
|  | 17 |  | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 18 | #include "update_engine/action.h" | 
|  | 19 | #include "update_engine/install_plan.h" | 
| Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 20 | #include "update_engine/omaha_hash_calculator.h" | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 21 |  | 
|  | 22 | // This action will only do real work if it's a delta update. It will | 
| Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 23 | // copy the root partition to install partition, and then terminate. | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 24 |  | 
|  | 25 | namespace chromeos_update_engine { | 
|  | 26 |  | 
| Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 27 | class SystemState; | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 28 |  | 
| Chris Sosa | d317e40 | 2013-06-12 13:47:09 -0700 | [diff] [blame] | 29 | class FilesystemCopierAction : public InstallPlanAction { | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 30 | public: | 
| Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 31 | FilesystemCopierAction(SystemState* system_state, | 
|  | 32 | bool copying_kernel_install_path, | 
|  | 33 | bool verify_hash); | 
| Darin Petkov | c2e4a7d | 2010-12-02 14:47:06 -0800 | [diff] [blame] | 34 |  | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 35 | void PerformAction(); | 
|  | 36 | void TerminateProcessing(); | 
|  | 37 |  | 
| Ben Chan | 2add7d7 | 2012-10-08 19:28:37 -0700 | [diff] [blame] | 38 | // Used for testing. Return true if Cleanup() has not yet been called due | 
|  | 39 | // to a callback upon the completion or cancellation of the copier action. | 
|  | 40 | // A test should wait until IsCleanupPending() returns false before | 
|  | 41 | // terminating the glib main loop. | 
|  | 42 | bool IsCleanupPending() const; | 
|  | 43 |  | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 44 | // Used for testing, so we can copy from somewhere other than root | 
| Darin Petkov | c2e4a7d | 2010-12-02 14:47:06 -0800 | [diff] [blame] | 45 | void set_copy_source(const std::string& path) { copy_source_ = path; } | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 46 |  | 
|  | 47 | // Debugging/logging | 
|  | 48 | static std::string StaticType() { return "FilesystemCopierAction"; } | 
|  | 49 | std::string Type() const { return StaticType(); } | 
|  | 50 |  | 
|  | 51 | private: | 
| Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 52 | friend class FilesystemCopierActionTest; | 
|  | 53 | FRIEND_TEST(FilesystemCopierActionTest, RunAsRootDetermineFilesystemSizeTest); | 
|  | 54 |  | 
| Darin Petkov | c2e4a7d | 2010-12-02 14:47:06 -0800 | [diff] [blame] | 55 | // Ping-pong buffers generally cycle through the following states: | 
| Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 56 | // Empty->Reading->Full->Writing->Empty. In hash verification mode the state | 
|  | 57 | // is never set to Writing. | 
| Darin Petkov | c2e4a7d | 2010-12-02 14:47:06 -0800 | [diff] [blame] | 58 | enum BufferState { | 
|  | 59 | kBufferStateEmpty, | 
|  | 60 | kBufferStateReading, | 
|  | 61 | kBufferStateFull, | 
|  | 62 | kBufferStateWriting | 
|  | 63 | }; | 
| Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 64 |  | 
| Darin Petkov | c2e4a7d | 2010-12-02 14:47:06 -0800 | [diff] [blame] | 65 | // Callbacks from glib when the read/write operation is done. | 
|  | 66 | void AsyncReadReadyCallback(GObject *source_object, GAsyncResult *res); | 
|  | 67 | static void StaticAsyncReadReadyCallback(GObject *source_object, | 
|  | 68 | GAsyncResult *res, | 
|  | 69 | gpointer user_data); | 
|  | 70 |  | 
|  | 71 | void AsyncWriteReadyCallback(GObject *source_object, GAsyncResult *res); | 
|  | 72 | static void StaticAsyncWriteReadyCallback(GObject *source_object, | 
|  | 73 | GAsyncResult *res, | 
|  | 74 | gpointer user_data); | 
|  | 75 |  | 
|  | 76 | // Based on the state of the ping-pong buffers spawns appropriate read/write | 
|  | 77 | // actions asynchronously. | 
|  | 78 | void SpawnAsyncActions(); | 
|  | 79 |  | 
|  | 80 | // Cleans up all the variables we use for async operations and tells the | 
| Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 81 | // ActionProcessor we're done w/ |code| as passed in. |cancelled_| should be | 
| Darin Petkov | c2e4a7d | 2010-12-02 14:47:06 -0800 | [diff] [blame] | 82 | // true if TerminateProcessing() was called. | 
| David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 83 | void Cleanup(ErrorCode code); | 
| Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 84 |  | 
|  | 85 | // Determine, if possible, the source file system size to avoid copying the | 
|  | 86 | // whole partition. Currently this supports only the root file system assuming | 
|  | 87 | // it's ext3-compatible. | 
|  | 88 | void DetermineFilesystemSize(int fd); | 
|  | 89 |  | 
| Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 90 | // If true, this action is copying to the kernel_install_path from | 
|  | 91 | // the install plan, otherwise it's copying just to the install_path. | 
|  | 92 | const bool copying_kernel_install_path_; | 
| Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 93 |  | 
| Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 94 | // If true, this action is running in applied update hash verification mode -- | 
|  | 95 | // it computes a hash for the target install path and compares it against the | 
|  | 96 | // expected value. | 
|  | 97 | const bool verify_hash_; | 
|  | 98 |  | 
| Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 99 | // The path to copy from. If empty (the default), the source is from the | 
|  | 100 | // passed in InstallPlan. | 
| Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 101 | std::string copy_source_; | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 102 |  | 
| Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 103 | // If non-NULL, these are GUnixInputStream objects for the opened | 
|  | 104 | // source/destination partitions. | 
|  | 105 | GInputStream* src_stream_; | 
|  | 106 | GOutputStream* dst_stream_; | 
| Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 107 |  | 
| Darin Petkov | c2e4a7d | 2010-12-02 14:47:06 -0800 | [diff] [blame] | 108 | // Ping-pong buffers for storing data we read/write. Only one buffer is being | 
|  | 109 | // read at a time and only one buffer is being written at a time. | 
|  | 110 | std::vector<char> buffer_[2]; | 
| Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 111 |  | 
| Darin Petkov | c2e4a7d | 2010-12-02 14:47:06 -0800 | [diff] [blame] | 112 | // The state of each buffer. | 
|  | 113 | BufferState buffer_state_[2]; | 
| Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 114 |  | 
| Darin Petkov | c2e4a7d | 2010-12-02 14:47:06 -0800 | [diff] [blame] | 115 | // Number of valid elements in |buffer_| if its state is kBufferStateFull. | 
|  | 116 | std::vector<char>::size_type buffer_valid_size_[2]; | 
| Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 117 |  | 
| Darin Petkov | c2e4a7d | 2010-12-02 14:47:06 -0800 | [diff] [blame] | 118 | // The cancellable objects for the in-flight async calls. | 
|  | 119 | GCancellable* canceller_[2]; | 
|  | 120 |  | 
|  | 121 | bool read_done_;  // true if reached EOF on the input stream. | 
|  | 122 | bool failed_;  // true if the action has failed. | 
|  | 123 | bool cancelled_;  // true if the action has been cancelled. | 
| Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 124 |  | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 125 | // The install plan we're passed in via the input pipe. | 
|  | 126 | InstallPlan install_plan_; | 
| Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 127 |  | 
|  | 128 | // Calculates the hash of the copied data. | 
|  | 129 | OmahaHashCalculator hasher_; | 
|  | 130 |  | 
|  | 131 | // Copies and hashes this many bytes from the head of the input stream. This | 
|  | 132 | // field is initialized when the action is started and decremented as more | 
|  | 133 | // bytes get copied. | 
|  | 134 | int64_t filesystem_size_; | 
|  | 135 |  | 
| Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 136 | // The global context for update_engine. | 
|  | 137 | SystemState* system_state_; | 
|  | 138 |  | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 139 | DISALLOW_COPY_AND_ASSIGN(FilesystemCopierAction); | 
|  | 140 | }; | 
|  | 141 |  | 
|  | 142 | }  // namespace chromeos_update_engine | 
|  | 143 |  | 
|  | 144 | #endif  // CHROMEOS_PLATFORM_UPDATE_ENGINE_FILESYSTEM_COPIER_ACTION_H__ |