Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium 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 | #include "update_engine/filesystem_copier_action.h" |
| 6 | #include <sys/stat.h> |
| 7 | #include <sys/types.h> |
| 8 | #include <errno.h> |
| 9 | #include <fcntl.h> |
| 10 | #include <stdlib.h> |
| 11 | #include <algorithm> |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 12 | #include <map> |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 13 | #include <string> |
| 14 | #include <vector> |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 15 | #include <gio/gio.h> |
| 16 | #include <gio/gunixinputstream.h> |
| 17 | #include <gio/gunixoutputstream.h> |
| 18 | #include <glib.h> |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 19 | #include "update_engine/filesystem_iterator.h" |
| 20 | #include "update_engine/subprocess.h" |
| 21 | #include "update_engine/utils.h" |
| 22 | |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 23 | using std::map; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 24 | using std::min; |
| 25 | using std::string; |
| 26 | using std::vector; |
| 27 | |
| 28 | namespace chromeos_update_engine { |
| 29 | |
| 30 | namespace { |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 31 | const off_t kCopyFileBufferSize = 2 * 1024 * 1024; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 32 | } // namespace {} |
| 33 | |
| 34 | void FilesystemCopierAction::PerformAction() { |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 35 | // Will tell the ActionProcessor we've failed if we return. |
| 36 | ScopedActionCompleter abort_action_completer(processor_, this); |
| 37 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 38 | if (!HasInputObject()) { |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 39 | LOG(ERROR) << "FilesystemCopierAction missing input object."; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 40 | return; |
| 41 | } |
| 42 | install_plan_ = GetInputObject(); |
| 43 | |
| 44 | if (install_plan_.is_full_update) { |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 45 | // No copy needed. Done! |
| 46 | abort_action_completer.set_success(true); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 47 | return; |
| 48 | } |
| 49 | |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame^] | 50 | string source = copy_source_; |
| 51 | if (source.empty()) { |
| 52 | source = copying_kernel_install_path_ ? |
| 53 | utils::BootKernelDevice(utils::BootDevice()) : |
| 54 | utils::BootDevice(); |
| 55 | } |
| 56 | |
| 57 | const string destination = copying_kernel_install_path_ ? |
| 58 | install_plan_.kernel_install_path : |
| 59 | install_plan_.install_path; |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 60 | |
| 61 | int src_fd = open(source.c_str(), O_RDONLY); |
| 62 | if (src_fd < 0) { |
| 63 | PLOG(ERROR) << "Unable to open " << source << " for reading:"; |
| 64 | return; |
| 65 | } |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame^] | 66 | int dst_fd = open(destination.c_str(), |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 67 | O_WRONLY | O_TRUNC | O_CREAT, |
| 68 | 0644); |
| 69 | if (dst_fd < 0) { |
| 70 | close(src_fd); |
| 71 | PLOG(ERROR) << "Unable to open " << install_plan_.install_path |
| 72 | << " for writing:"; |
| 73 | return; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 76 | src_stream_ = g_unix_input_stream_new(src_fd, TRUE); |
| 77 | dst_stream_ = g_unix_output_stream_new(dst_fd, TRUE); |
| 78 | |
| 79 | buffer_.resize(kCopyFileBufferSize); |
| 80 | |
| 81 | // Set up the first read |
| 82 | canceller_ = g_cancellable_new(); |
| 83 | |
| 84 | g_input_stream_read_async(src_stream_, |
| 85 | &buffer_[0], |
| 86 | buffer_.size(), |
| 87 | G_PRIORITY_DEFAULT, |
| 88 | canceller_, |
| 89 | &FilesystemCopierAction::StaticAsyncReadyCallback, |
| 90 | this); |
| 91 | read_in_flight_ = true; |
| 92 | |
| 93 | abort_action_completer.set_should_complete(false); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | void FilesystemCopierAction::TerminateProcessing() { |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 97 | if (canceller_) { |
| 98 | g_cancellable_cancel(canceller_); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 102 | void FilesystemCopierAction::Cleanup(bool success, bool was_cancelled) { |
| 103 | g_object_unref(src_stream_); |
| 104 | src_stream_ = NULL; |
| 105 | g_object_unref(dst_stream_); |
| 106 | dst_stream_ = NULL; |
| 107 | if (was_cancelled) |
| 108 | return; |
| 109 | if (success && HasOutputPipe()) |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 110 | SetOutputObject(install_plan_); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 111 | processor_->ActionComplete(this, success); |
| 112 | } |
| 113 | |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 114 | void FilesystemCopierAction::AsyncReadyCallback(GObject *source_object, |
| 115 | GAsyncResult *res) { |
| 116 | GError* error = NULL; |
| 117 | CHECK(canceller_); |
| 118 | bool was_cancelled = g_cancellable_is_cancelled(canceller_) == TRUE; |
| 119 | g_object_unref(canceller_); |
| 120 | canceller_ = NULL; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 121 | |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 122 | if (read_in_flight_) { |
| 123 | ssize_t bytes_read = g_input_stream_read_finish(src_stream_, res, &error); |
| 124 | if (bytes_read < 0) { |
| 125 | LOG(ERROR) << "Read failed:" << utils::GetGErrorMessage(error); |
| 126 | Cleanup(false, was_cancelled); |
| 127 | return; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 128 | } |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 129 | |
| 130 | if (bytes_read == 0) { |
| 131 | // We're done! |
| 132 | Cleanup(true, was_cancelled); |
| 133 | return; |
| 134 | } |
| 135 | // Kick off a write |
| 136 | read_in_flight_ = false; |
| 137 | buffer_valid_size_ = bytes_read; |
| 138 | canceller_ = g_cancellable_new(); |
| 139 | g_output_stream_write_async( |
| 140 | dst_stream_, |
| 141 | &buffer_[0], |
| 142 | bytes_read, |
| 143 | G_PRIORITY_DEFAULT, |
| 144 | canceller_, |
| 145 | &FilesystemCopierAction::StaticAsyncReadyCallback, |
| 146 | this); |
| 147 | return; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 148 | } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 149 | |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 150 | ssize_t bytes_written = g_output_stream_write_finish(dst_stream_, |
| 151 | res, |
| 152 | &error); |
| 153 | if (bytes_written < static_cast<ssize_t>(buffer_valid_size_)) { |
| 154 | if (bytes_written < 0) { |
| 155 | LOG(ERROR) << "Write failed:" << utils::GetGErrorMessage(error); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 156 | } else { |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 157 | LOG(ERROR) << "Write was short: wrote " << bytes_written |
| 158 | << " but expected to write " << buffer_valid_size_; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 159 | } |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 160 | Cleanup(false, was_cancelled); |
| 161 | return; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 162 | } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 163 | |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 164 | // Kick off a read |
| 165 | read_in_flight_ = true; |
| 166 | canceller_ = g_cancellable_new(); |
| 167 | g_input_stream_read_async( |
| 168 | src_stream_, |
| 169 | &buffer_[0], |
| 170 | buffer_.size(), |
| 171 | G_PRIORITY_DEFAULT, |
| 172 | canceller_, |
| 173 | &FilesystemCopierAction::StaticAsyncReadyCallback, |
| 174 | this); |
| 175 | } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 176 | |
| 177 | } // namespace chromeos_update_engine |