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