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