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! |
Andrew de los Reyes | f98bff8 | 2010-05-06 13:33:25 -0700 | [diff] [blame] | 46 | if (HasOutputPipe()) |
| 47 | SetOutputObject(install_plan_); |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 48 | abort_action_completer.set_success(true); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 49 | return; |
| 50 | } |
| 51 | |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 52 | string source = copy_source_; |
| 53 | if (source.empty()) { |
| 54 | source = copying_kernel_install_path_ ? |
| 55 | utils::BootKernelDevice(utils::BootDevice()) : |
| 56 | utils::BootDevice(); |
| 57 | } |
| 58 | |
| 59 | const string destination = copying_kernel_install_path_ ? |
| 60 | install_plan_.kernel_install_path : |
| 61 | install_plan_.install_path; |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 62 | |
| 63 | int src_fd = open(source.c_str(), O_RDONLY); |
| 64 | if (src_fd < 0) { |
| 65 | PLOG(ERROR) << "Unable to open " << source << " for reading:"; |
| 66 | return; |
| 67 | } |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 68 | int dst_fd = open(destination.c_str(), |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 69 | O_WRONLY | O_TRUNC | O_CREAT, |
| 70 | 0644); |
| 71 | if (dst_fd < 0) { |
| 72 | close(src_fd); |
| 73 | PLOG(ERROR) << "Unable to open " << install_plan_.install_path |
| 74 | << " for writing:"; |
| 75 | return; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 78 | src_stream_ = g_unix_input_stream_new(src_fd, TRUE); |
| 79 | dst_stream_ = g_unix_output_stream_new(dst_fd, TRUE); |
| 80 | |
| 81 | buffer_.resize(kCopyFileBufferSize); |
| 82 | |
| 83 | // Set up the first read |
| 84 | canceller_ = g_cancellable_new(); |
| 85 | |
| 86 | g_input_stream_read_async(src_stream_, |
| 87 | &buffer_[0], |
| 88 | buffer_.size(), |
| 89 | G_PRIORITY_DEFAULT, |
| 90 | canceller_, |
| 91 | &FilesystemCopierAction::StaticAsyncReadyCallback, |
| 92 | this); |
| 93 | read_in_flight_ = true; |
| 94 | |
| 95 | abort_action_completer.set_should_complete(false); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | void FilesystemCopierAction::TerminateProcessing() { |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 99 | if (canceller_) { |
| 100 | g_cancellable_cancel(canceller_); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 101 | } |
| 102 | } |
| 103 | |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 104 | void FilesystemCopierAction::Cleanup(bool success, bool was_cancelled) { |
| 105 | g_object_unref(src_stream_); |
| 106 | src_stream_ = NULL; |
| 107 | g_object_unref(dst_stream_); |
| 108 | dst_stream_ = NULL; |
| 109 | if (was_cancelled) |
| 110 | return; |
| 111 | if (success && HasOutputPipe()) |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 112 | SetOutputObject(install_plan_); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 113 | processor_->ActionComplete(this, success); |
| 114 | } |
| 115 | |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 116 | void FilesystemCopierAction::AsyncReadyCallback(GObject *source_object, |
| 117 | GAsyncResult *res) { |
| 118 | GError* error = NULL; |
| 119 | CHECK(canceller_); |
| 120 | bool was_cancelled = g_cancellable_is_cancelled(canceller_) == TRUE; |
| 121 | g_object_unref(canceller_); |
| 122 | canceller_ = NULL; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 123 | |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 124 | if (read_in_flight_) { |
| 125 | ssize_t bytes_read = g_input_stream_read_finish(src_stream_, res, &error); |
| 126 | if (bytes_read < 0) { |
| 127 | LOG(ERROR) << "Read failed:" << utils::GetGErrorMessage(error); |
| 128 | Cleanup(false, was_cancelled); |
| 129 | return; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 130 | } |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 131 | |
| 132 | if (bytes_read == 0) { |
| 133 | // We're done! |
| 134 | Cleanup(true, was_cancelled); |
| 135 | return; |
| 136 | } |
| 137 | // Kick off a write |
| 138 | read_in_flight_ = false; |
| 139 | buffer_valid_size_ = bytes_read; |
| 140 | canceller_ = g_cancellable_new(); |
| 141 | g_output_stream_write_async( |
| 142 | dst_stream_, |
| 143 | &buffer_[0], |
| 144 | bytes_read, |
| 145 | G_PRIORITY_DEFAULT, |
| 146 | canceller_, |
| 147 | &FilesystemCopierAction::StaticAsyncReadyCallback, |
| 148 | this); |
| 149 | return; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 150 | } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 151 | |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 152 | ssize_t bytes_written = g_output_stream_write_finish(dst_stream_, |
| 153 | res, |
| 154 | &error); |
| 155 | if (bytes_written < static_cast<ssize_t>(buffer_valid_size_)) { |
| 156 | if (bytes_written < 0) { |
| 157 | LOG(ERROR) << "Write failed:" << utils::GetGErrorMessage(error); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 158 | } else { |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 159 | LOG(ERROR) << "Write was short: wrote " << bytes_written |
| 160 | << " but expected to write " << buffer_valid_size_; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 161 | } |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 162 | Cleanup(false, was_cancelled); |
| 163 | return; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 164 | } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 165 | |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 166 | // Kick off a read |
| 167 | read_in_flight_ = true; |
| 168 | canceller_ = g_cancellable_new(); |
| 169 | g_input_stream_read_async( |
| 170 | src_stream_, |
| 171 | &buffer_[0], |
| 172 | buffer_.size(), |
| 173 | G_PRIORITY_DEFAULT, |
| 174 | canceller_, |
| 175 | &FilesystemCopierAction::StaticAsyncReadyCallback, |
| 176 | this); |
| 177 | } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 178 | |
| 179 | } // namespace chromeos_update_engine |