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 { |
Darin Petkov | c2e4a7d | 2010-12-02 14:47:06 -0800 | [diff] [blame^] | 35 | const off_t kCopyFileBufferSize = 512 * 1024; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 36 | } // namespace {} |
| 37 | |
Darin Petkov | c2e4a7d | 2010-12-02 14:47:06 -0800 | [diff] [blame^] | 38 | FilesystemCopierAction::FilesystemCopierAction( |
| 39 | bool copying_kernel_install_path) |
| 40 | : copying_kernel_install_path_(copying_kernel_install_path), |
| 41 | src_stream_(NULL), |
| 42 | dst_stream_(NULL), |
| 43 | read_done_(false), |
| 44 | failed_(false), |
| 45 | cancelled_(false), |
| 46 | filesystem_size_(kint64max) { |
| 47 | // A lot of code works on the implicit assumption that processing is done on |
| 48 | // exactly 2 ping-pong buffers. |
| 49 | COMPILE_ASSERT(arraysize(buffer_) == 2 && |
| 50 | arraysize(buffer_state_) == 2 && |
| 51 | arraysize(buffer_valid_size_) == 2 && |
| 52 | arraysize(canceller_) == 2, |
| 53 | ping_pong_buffers_not_two); |
| 54 | for (int i = 0; i < 2; ++i) { |
| 55 | buffer_state_[i] = kBufferStateEmpty; |
| 56 | buffer_valid_size_[i] = 0; |
| 57 | canceller_[i] = NULL; |
| 58 | } |
| 59 | } |
| 60 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 61 | void FilesystemCopierAction::PerformAction() { |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 62 | // Will tell the ActionProcessor we've failed if we return. |
| 63 | ScopedActionCompleter abort_action_completer(processor_, this); |
| 64 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 65 | if (!HasInputObject()) { |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 66 | LOG(ERROR) << "FilesystemCopierAction missing input object."; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 67 | return; |
| 68 | } |
| 69 | install_plan_ = GetInputObject(); |
| 70 | |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 71 | if (install_plan_.is_full_update || install_plan_.is_resume) { |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 72 | // No copy needed. Done! |
Andrew de los Reyes | f98bff8 | 2010-05-06 13:33:25 -0700 | [diff] [blame] | 73 | if (HasOutputPipe()) |
| 74 | SetOutputObject(install_plan_); |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 75 | abort_action_completer.set_code(kActionCodeSuccess); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 76 | return; |
| 77 | } |
| 78 | |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 79 | string source = copy_source_; |
| 80 | if (source.empty()) { |
| 81 | source = copying_kernel_install_path_ ? |
| 82 | utils::BootKernelDevice(utils::BootDevice()) : |
| 83 | utils::BootDevice(); |
| 84 | } |
| 85 | |
| 86 | const string destination = copying_kernel_install_path_ ? |
| 87 | install_plan_.kernel_install_path : |
| 88 | install_plan_.install_path; |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 89 | |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 90 | int src_fd = open(source.c_str(), O_RDONLY); |
| 91 | if (src_fd < 0) { |
| 92 | PLOG(ERROR) << "Unable to open " << source << " for reading:"; |
| 93 | return; |
| 94 | } |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 95 | int dst_fd = open(destination.c_str(), |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 96 | O_WRONLY | O_TRUNC | O_CREAT, |
| 97 | 0644); |
| 98 | if (dst_fd < 0) { |
| 99 | close(src_fd); |
| 100 | PLOG(ERROR) << "Unable to open " << install_plan_.install_path |
| 101 | << " for writing:"; |
| 102 | return; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 105 | DetermineFilesystemSize(src_fd); |
| 106 | |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 107 | src_stream_ = g_unix_input_stream_new(src_fd, TRUE); |
| 108 | dst_stream_ = g_unix_output_stream_new(dst_fd, TRUE); |
| 109 | |
Darin Petkov | c2e4a7d | 2010-12-02 14:47:06 -0800 | [diff] [blame^] | 110 | for (int i = 0; i < 2; i++) { |
| 111 | buffer_[i].resize(kCopyFileBufferSize); |
| 112 | canceller_[i] = g_cancellable_new(); |
| 113 | } |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 114 | |
Darin Petkov | c2e4a7d | 2010-12-02 14:47:06 -0800 | [diff] [blame^] | 115 | // Start the first read. |
| 116 | SpawnAsyncActions(); |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 117 | |
| 118 | abort_action_completer.set_should_complete(false); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | void FilesystemCopierAction::TerminateProcessing() { |
Darin Petkov | c2e4a7d | 2010-12-02 14:47:06 -0800 | [diff] [blame^] | 122 | for (int i = 0; i < 2; i++) { |
| 123 | if (canceller_[i]) { |
| 124 | g_cancellable_cancel(canceller_[i]); |
| 125 | } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | |
Darin Petkov | c2e4a7d | 2010-12-02 14:47:06 -0800 | [diff] [blame^] | 129 | void FilesystemCopierAction::Cleanup(bool success) { |
| 130 | for (int i = 0; i < 2; i++) { |
| 131 | g_object_unref(canceller_[i]); |
| 132 | canceller_[i] = NULL; |
| 133 | } |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 134 | g_object_unref(src_stream_); |
| 135 | src_stream_ = NULL; |
| 136 | g_object_unref(dst_stream_); |
| 137 | dst_stream_ = NULL; |
Darin Petkov | c2e4a7d | 2010-12-02 14:47:06 -0800 | [diff] [blame^] | 138 | if (cancelled_) |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 139 | return; |
| 140 | if (success && HasOutputPipe()) |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 141 | SetOutputObject(install_plan_); |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 142 | processor_->ActionComplete( |
| 143 | this, |
| 144 | success ? kActionCodeSuccess : kActionCodeError); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Darin Petkov | c2e4a7d | 2010-12-02 14:47:06 -0800 | [diff] [blame^] | 147 | void FilesystemCopierAction::AsyncReadReadyCallback(GObject *source_object, |
| 148 | GAsyncResult *res) { |
| 149 | int index = buffer_state_[0] == kBufferStateReading ? 0 : 1; |
| 150 | CHECK(buffer_state_[index] == kBufferStateReading); |
| 151 | |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 152 | GError* error = NULL; |
Darin Petkov | c2e4a7d | 2010-12-02 14:47:06 -0800 | [diff] [blame^] | 153 | CHECK(canceller_[index]); |
| 154 | cancelled_ = g_cancellable_is_cancelled(canceller_[index]) == TRUE; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 155 | |
Darin Petkov | c2e4a7d | 2010-12-02 14:47:06 -0800 | [diff] [blame^] | 156 | ssize_t bytes_read = g_input_stream_read_finish(src_stream_, res, &error); |
| 157 | if (bytes_read < 0) { |
| 158 | LOG(ERROR) << "Read failed: " << utils::GetGErrorMessage(error); |
| 159 | failed_ = true; |
| 160 | buffer_state_[index] = kBufferStateEmpty; |
| 161 | } else if (bytes_read == 0) { |
| 162 | read_done_ = true; |
| 163 | buffer_state_[index] = kBufferStateEmpty; |
| 164 | } else { |
| 165 | buffer_valid_size_[index] = bytes_read; |
| 166 | buffer_state_[index] = kBufferStateFull; |
| 167 | } |
| 168 | |
| 169 | if (bytes_read > 0) { |
| 170 | filesystem_size_ -= bytes_read; |
| 171 | } |
| 172 | |
| 173 | SpawnAsyncActions(); |
| 174 | |
| 175 | if (bytes_read > 0) { |
| 176 | if (!hasher_.Update(buffer_[index].data(), bytes_read)) { |
| 177 | LOG(ERROR) << "Unable to update the hash."; |
| 178 | failed_ = true; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 179 | } |
Darin Petkov | c2e4a7d | 2010-12-02 14:47:06 -0800 | [diff] [blame^] | 180 | } |
| 181 | } |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 182 | |
Darin Petkov | c2e4a7d | 2010-12-02 14:47:06 -0800 | [diff] [blame^] | 183 | void FilesystemCopierAction::StaticAsyncReadReadyCallback( |
| 184 | GObject *source_object, |
| 185 | GAsyncResult *res, |
| 186 | gpointer user_data) { |
| 187 | reinterpret_cast<FilesystemCopierAction*>(user_data)-> |
| 188 | AsyncReadReadyCallback(source_object, res); |
| 189 | } |
| 190 | |
| 191 | void FilesystemCopierAction::AsyncWriteReadyCallback(GObject *source_object, |
| 192 | GAsyncResult *res) { |
| 193 | int index = buffer_state_[0] == kBufferStateWriting ? 0 : 1; |
| 194 | CHECK(buffer_state_[index] == kBufferStateWriting); |
| 195 | buffer_state_[index] = kBufferStateEmpty; |
| 196 | |
| 197 | GError* error = NULL; |
| 198 | CHECK(canceller_[index]); |
| 199 | cancelled_ = g_cancellable_is_cancelled(canceller_[index]) == TRUE; |
| 200 | |
| 201 | ssize_t bytes_written = g_output_stream_write_finish(dst_stream_, |
| 202 | res, |
| 203 | &error); |
| 204 | if (bytes_written < static_cast<ssize_t>(buffer_valid_size_[index])) { |
| 205 | if (bytes_written < 0) { |
| 206 | LOG(ERROR) << "Write failed: " << utils::GetGErrorMessage(error); |
| 207 | } else { |
| 208 | LOG(ERROR) << "Write was short: wrote " << bytes_written |
| 209 | << " but expected to write " << buffer_valid_size_[index]; |
| 210 | } |
| 211 | failed_ = true; |
| 212 | } |
| 213 | |
| 214 | SpawnAsyncActions(); |
| 215 | } |
| 216 | |
| 217 | void FilesystemCopierAction::StaticAsyncWriteReadyCallback( |
| 218 | GObject *source_object, |
| 219 | GAsyncResult *res, |
| 220 | gpointer user_data) { |
| 221 | reinterpret_cast<FilesystemCopierAction*>(user_data)-> |
| 222 | AsyncWriteReadyCallback(source_object, res); |
| 223 | } |
| 224 | |
| 225 | void FilesystemCopierAction::SpawnAsyncActions() { |
| 226 | bool reading = false; |
| 227 | bool writing = false; |
| 228 | for (int i = 0; i < 2; i++) { |
| 229 | if (buffer_state_[i] == kBufferStateReading) { |
| 230 | reading = true; |
| 231 | } |
| 232 | if (buffer_state_[i] == kBufferStateWriting) { |
| 233 | writing = true; |
| 234 | } |
| 235 | } |
| 236 | if (failed_ || cancelled_) { |
| 237 | if (!reading && !writing) { |
| 238 | Cleanup(false); |
| 239 | } |
| 240 | return; |
| 241 | } |
| 242 | for (int i = 0; i < 2; i++) { |
| 243 | if (!reading && !read_done_ && buffer_state_[i] == kBufferStateEmpty) { |
| 244 | g_input_stream_read_async( |
| 245 | src_stream_, |
| 246 | buffer_[i].data(), |
| 247 | GetBytesToRead(), |
| 248 | G_PRIORITY_DEFAULT, |
| 249 | canceller_[i], |
| 250 | &FilesystemCopierAction::StaticAsyncReadReadyCallback, |
| 251 | this); |
| 252 | reading = true; |
| 253 | buffer_state_[i] = kBufferStateReading; |
| 254 | } else if (!writing && buffer_state_[i] == kBufferStateFull) { |
| 255 | g_output_stream_write_async( |
| 256 | dst_stream_, |
| 257 | buffer_[i].data(), |
| 258 | buffer_valid_size_[i], |
| 259 | G_PRIORITY_DEFAULT, |
| 260 | canceller_[i], |
| 261 | &FilesystemCopierAction::StaticAsyncWriteReadyCallback, |
| 262 | this); |
| 263 | writing = true; |
| 264 | buffer_state_[i] = kBufferStateWriting; |
| 265 | } |
| 266 | } |
| 267 | if (!reading && !writing) { |
| 268 | // We're done! |
| 269 | if (hasher_.Finalize()) { |
Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 270 | LOG(INFO) << "hash: " << hasher_.hash(); |
| 271 | if (copying_kernel_install_path_) { |
| 272 | install_plan_.current_kernel_hash = hasher_.raw_hash(); |
| 273 | } else { |
| 274 | install_plan_.current_rootfs_hash = hasher_.raw_hash(); |
| 275 | } |
Darin Petkov | c2e4a7d | 2010-12-02 14:47:06 -0800 | [diff] [blame^] | 276 | Cleanup(true); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 277 | } else { |
Darin Petkov | c2e4a7d | 2010-12-02 14:47:06 -0800 | [diff] [blame^] | 278 | LOG(ERROR) << "Unable to finalize the hash."; |
| 279 | Cleanup(false); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 280 | } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 281 | } |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 282 | } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 283 | |
Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 284 | void FilesystemCopierAction::DetermineFilesystemSize(int fd) { |
| 285 | filesystem_size_ = kint64max; |
| 286 | int block_count = 0, block_size = 0; |
| 287 | if (!copying_kernel_install_path_ && |
| 288 | utils::GetFilesystemSizeFromFD(fd, &block_count, &block_size)) { |
| 289 | filesystem_size_ = static_cast<int64_t>(block_count) * block_size; |
| 290 | LOG(INFO) << "Filesystem size: " << filesystem_size_ << " bytes (" |
| 291 | << block_count << "x" << block_size << ")."; |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | int64_t FilesystemCopierAction::GetBytesToRead() { |
Darin Petkov | c2e4a7d | 2010-12-02 14:47:06 -0800 | [diff] [blame^] | 296 | return std::min(static_cast<int64_t>(buffer_[0].size()), filesystem_size_); |
Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 297 | } |
| 298 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 299 | } // namespace chromeos_update_engine |