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