Gilad Arnold | 6dbbd39 | 2012-07-10 16:19:11 -0700 | [diff] [blame] | 1 | // Copyright (c) 2012 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 <errno.h> |
| 8 | #include <fcntl.h> |
Alex Deymo | 161c4a1 | 2014-05-16 15:56:21 -0700 | [diff] [blame] | 9 | #include <sys/stat.h> |
| 10 | #include <sys/types.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 | |
Alex Deymo | 44666f9 | 2014-07-22 20:29:24 -0700 | [diff] [blame] | 23 | #include "update_engine/glib_utils.h" |
Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 24 | #include "update_engine/hardware_interface.h" |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 25 | #include "update_engine/subprocess.h" |
Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 26 | #include "update_engine/system_state.h" |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 27 | #include "update_engine/utils.h" |
| 28 | |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 29 | using std::map; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 30 | using std::min; |
| 31 | using std::string; |
| 32 | using std::vector; |
| 33 | |
| 34 | namespace chromeos_update_engine { |
| 35 | |
| 36 | namespace { |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 37 | const off_t kCopyFileBufferSize = 128 * 1024; |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 38 | } // namespace |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 39 | |
Darin Petkov | c2e4a7d | 2010-12-02 14:47:06 -0800 | [diff] [blame] | 40 | FilesystemCopierAction::FilesystemCopierAction( |
Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 41 | SystemState* system_state, |
Gilad Arnold | 581c2ea | 2012-07-19 12:33:49 -0700 | [diff] [blame] | 42 | bool copying_kernel_install_path, |
| 43 | bool verify_hash) |
Darin Petkov | c2e4a7d | 2010-12-02 14:47:06 -0800 | [diff] [blame] | 44 | : copying_kernel_install_path_(copying_kernel_install_path), |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 45 | verify_hash_(verify_hash), |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame^] | 46 | src_stream_(nullptr), |
| 47 | dst_stream_(nullptr), |
Darin Petkov | c2e4a7d | 2010-12-02 14:47:06 -0800 | [diff] [blame] | 48 | read_done_(false), |
| 49 | failed_(false), |
| 50 | cancelled_(false), |
Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 51 | filesystem_size_(kint64max), |
| 52 | system_state_(system_state) { |
Darin Petkov | c2e4a7d | 2010-12-02 14:47:06 -0800 | [diff] [blame] | 53 | // A lot of code works on the implicit assumption that processing is done on |
| 54 | // exactly 2 ping-pong buffers. |
| 55 | COMPILE_ASSERT(arraysize(buffer_) == 2 && |
| 56 | arraysize(buffer_state_) == 2 && |
| 57 | arraysize(buffer_valid_size_) == 2 && |
| 58 | arraysize(canceller_) == 2, |
| 59 | ping_pong_buffers_not_two); |
| 60 | for (int i = 0; i < 2; ++i) { |
| 61 | buffer_state_[i] = kBufferStateEmpty; |
| 62 | buffer_valid_size_[i] = 0; |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame^] | 63 | canceller_[i] = nullptr; |
Darin Petkov | c2e4a7d | 2010-12-02 14:47:06 -0800 | [diff] [blame] | 64 | } |
| 65 | } |
| 66 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 67 | void FilesystemCopierAction::PerformAction() { |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 68 | // Will tell the ActionProcessor we've failed if we return. |
| 69 | ScopedActionCompleter abort_action_completer(processor_, this); |
| 70 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 71 | if (!HasInputObject()) { |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 72 | LOG(ERROR) << "FilesystemCopierAction missing input object."; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 73 | return; |
| 74 | } |
| 75 | install_plan_ = GetInputObject(); |
Don Garrett | 83692e4 | 2013-11-08 10:11:30 -0800 | [diff] [blame] | 76 | |
| 77 | // No need to copy on a resume. |
| 78 | if (!verify_hash_ && install_plan_.is_resume) { |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 79 | // No copy or hash verification needed. Done! |
Don Garrett | 83692e4 | 2013-11-08 10:11:30 -0800 | [diff] [blame] | 80 | LOG(INFO) << "filesystem copying skipped on resumed update."; |
| 81 | if (HasOutputPipe()) |
| 82 | SetOutputObject(install_plan_); |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 83 | abort_action_completer.set_code(ErrorCode::kSuccess); |
Don Garrett | 83692e4 | 2013-11-08 10:11:30 -0800 | [diff] [blame] | 84 | return; |
| 85 | } |
| 86 | |
| 87 | if (copying_kernel_install_path_) { |
| 88 | if (!system_state_->hardware()->MarkKernelUnbootable( |
| 89 | install_plan_.kernel_install_path)) { |
| 90 | PLOG(ERROR) << "Unable to clear kernel GPT boot flags: " << |
| 91 | install_plan_.kernel_install_path; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | if (!verify_hash_ && install_plan_.is_full_update) { |
| 96 | // No copy or hash verification needed. Done! |
| 97 | LOG(INFO) << "filesystem copying skipped on full update."; |
Andrew de los Reyes | f98bff8 | 2010-05-06 13:33:25 -0700 | [diff] [blame] | 98 | if (HasOutputPipe()) |
| 99 | SetOutputObject(install_plan_); |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 100 | abort_action_completer.set_code(ErrorCode::kSuccess); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 101 | return; |
| 102 | } |
| 103 | |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 104 | const string destination = copying_kernel_install_path_ ? |
| 105 | install_plan_.kernel_install_path : |
| 106 | install_plan_.install_path; |
| 107 | string source = verify_hash_ ? destination : copy_source_; |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 108 | if (source.empty()) { |
| 109 | source = copying_kernel_install_path_ ? |
J. Richard Barnette | 3084293 | 2013-10-28 15:04:23 -0700 | [diff] [blame] | 110 | utils::KernelDeviceOfBootDevice( |
Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 111 | system_state_->hardware()->BootDevice()) : |
| 112 | system_state_->hardware()->BootDevice(); |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 113 | } |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 114 | int src_fd = open(source.c_str(), O_RDONLY); |
| 115 | if (src_fd < 0) { |
| 116 | PLOG(ERROR) << "Unable to open " << source << " for reading:"; |
| 117 | return; |
| 118 | } |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 119 | |
| 120 | if (!verify_hash_) { |
| 121 | int dst_fd = open(destination.c_str(), |
| 122 | O_WRONLY | O_TRUNC | O_CREAT, |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 123 | 0644); |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 124 | if (dst_fd < 0) { |
| 125 | close(src_fd); |
| 126 | PLOG(ERROR) << "Unable to open " << install_plan_.install_path |
| 127 | << " for writing:"; |
| 128 | return; |
| 129 | } |
| 130 | |
| 131 | dst_stream_ = g_unix_output_stream_new(dst_fd, TRUE); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 134 | DetermineFilesystemSize(src_fd); |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 135 | src_stream_ = g_unix_input_stream_new(src_fd, TRUE); |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 136 | |
Darin Petkov | c2e4a7d | 2010-12-02 14:47:06 -0800 | [diff] [blame] | 137 | for (int i = 0; i < 2; i++) { |
| 138 | buffer_[i].resize(kCopyFileBufferSize); |
| 139 | canceller_[i] = g_cancellable_new(); |
| 140 | } |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 141 | |
Darin Petkov | c2e4a7d | 2010-12-02 14:47:06 -0800 | [diff] [blame] | 142 | // Start the first read. |
| 143 | SpawnAsyncActions(); |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 144 | |
| 145 | abort_action_completer.set_should_complete(false); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | void FilesystemCopierAction::TerminateProcessing() { |
Darin Petkov | c2e4a7d | 2010-12-02 14:47:06 -0800 | [diff] [blame] | 149 | for (int i = 0; i < 2; i++) { |
| 150 | if (canceller_[i]) { |
| 151 | g_cancellable_cancel(canceller_[i]); |
| 152 | } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | |
Ben Chan | 2add7d7 | 2012-10-08 19:28:37 -0700 | [diff] [blame] | 156 | bool FilesystemCopierAction::IsCleanupPending() const { |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame^] | 157 | return (src_stream_ != nullptr); |
Ben Chan | 2add7d7 | 2012-10-08 19:28:37 -0700 | [diff] [blame] | 158 | } |
| 159 | |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 160 | void FilesystemCopierAction::Cleanup(ErrorCode code) { |
Darin Petkov | c2e4a7d | 2010-12-02 14:47:06 -0800 | [diff] [blame] | 161 | for (int i = 0; i < 2; i++) { |
| 162 | g_object_unref(canceller_[i]); |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame^] | 163 | canceller_[i] = nullptr; |
Darin Petkov | c2e4a7d | 2010-12-02 14:47:06 -0800 | [diff] [blame] | 164 | } |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 165 | g_object_unref(src_stream_); |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame^] | 166 | src_stream_ = nullptr; |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 167 | if (dst_stream_) { |
| 168 | g_object_unref(dst_stream_); |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame^] | 169 | dst_stream_ = nullptr; |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 170 | } |
Darin Petkov | c2e4a7d | 2010-12-02 14:47:06 -0800 | [diff] [blame] | 171 | if (cancelled_) |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 172 | return; |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 173 | if (code == ErrorCode::kSuccess && HasOutputPipe()) |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 174 | SetOutputObject(install_plan_); |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 175 | processor_->ActionComplete(this, code); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 176 | } |
| 177 | |
Darin Petkov | c2e4a7d | 2010-12-02 14:47:06 -0800 | [diff] [blame] | 178 | void FilesystemCopierAction::AsyncReadReadyCallback(GObject *source_object, |
| 179 | GAsyncResult *res) { |
| 180 | int index = buffer_state_[0] == kBufferStateReading ? 0 : 1; |
| 181 | CHECK(buffer_state_[index] == kBufferStateReading); |
| 182 | |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame^] | 183 | GError* error = nullptr; |
Darin Petkov | c2e4a7d | 2010-12-02 14:47:06 -0800 | [diff] [blame] | 184 | CHECK(canceller_[index]); |
| 185 | cancelled_ = g_cancellable_is_cancelled(canceller_[index]) == TRUE; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 186 | |
Darin Petkov | c2e4a7d | 2010-12-02 14:47:06 -0800 | [diff] [blame] | 187 | ssize_t bytes_read = g_input_stream_read_finish(src_stream_, res, &error); |
| 188 | if (bytes_read < 0) { |
Darin Petkov | a0b9e77 | 2011-10-06 05:05:56 -0700 | [diff] [blame] | 189 | LOG(ERROR) << "Read failed: " << utils::GetAndFreeGError(&error); |
Darin Petkov | c2e4a7d | 2010-12-02 14:47:06 -0800 | [diff] [blame] | 190 | failed_ = true; |
| 191 | buffer_state_[index] = kBufferStateEmpty; |
| 192 | } else if (bytes_read == 0) { |
| 193 | read_done_ = true; |
| 194 | buffer_state_[index] = kBufferStateEmpty; |
| 195 | } else { |
| 196 | buffer_valid_size_[index] = bytes_read; |
| 197 | buffer_state_[index] = kBufferStateFull; |
Darin Petkov | c2e4a7d | 2010-12-02 14:47:06 -0800 | [diff] [blame] | 198 | filesystem_size_ -= bytes_read; |
| 199 | } |
Darin Petkov | c2e4a7d | 2010-12-02 14:47:06 -0800 | [diff] [blame] | 200 | SpawnAsyncActions(); |
| 201 | |
| 202 | if (bytes_read > 0) { |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 203 | // If read_done_ is set, SpawnAsyncActions may finalize the hash so the hash |
| 204 | // update below would happen too late. |
| 205 | CHECK(!read_done_); |
Darin Petkov | c2e4a7d | 2010-12-02 14:47:06 -0800 | [diff] [blame] | 206 | if (!hasher_.Update(buffer_[index].data(), bytes_read)) { |
| 207 | LOG(ERROR) << "Unable to update the hash."; |
| 208 | failed_ = true; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 209 | } |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 210 | if (verify_hash_) { |
| 211 | buffer_state_[index] = kBufferStateEmpty; |
| 212 | } |
Darin Petkov | c2e4a7d | 2010-12-02 14:47:06 -0800 | [diff] [blame] | 213 | } |
| 214 | } |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 215 | |
Darin Petkov | c2e4a7d | 2010-12-02 14:47:06 -0800 | [diff] [blame] | 216 | void FilesystemCopierAction::StaticAsyncReadReadyCallback( |
| 217 | GObject *source_object, |
| 218 | GAsyncResult *res, |
| 219 | gpointer user_data) { |
| 220 | reinterpret_cast<FilesystemCopierAction*>(user_data)-> |
| 221 | AsyncReadReadyCallback(source_object, res); |
| 222 | } |
| 223 | |
| 224 | void FilesystemCopierAction::AsyncWriteReadyCallback(GObject *source_object, |
| 225 | GAsyncResult *res) { |
| 226 | int index = buffer_state_[0] == kBufferStateWriting ? 0 : 1; |
| 227 | CHECK(buffer_state_[index] == kBufferStateWriting); |
Gilad Arnold | 581c2ea | 2012-07-19 12:33:49 -0700 | [diff] [blame] | 228 | buffer_state_[index] = kBufferStateEmpty; |
Darin Petkov | c2e4a7d | 2010-12-02 14:47:06 -0800 | [diff] [blame] | 229 | |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame^] | 230 | GError* error = nullptr; |
Darin Petkov | c2e4a7d | 2010-12-02 14:47:06 -0800 | [diff] [blame] | 231 | CHECK(canceller_[index]); |
| 232 | cancelled_ = g_cancellable_is_cancelled(canceller_[index]) == TRUE; |
| 233 | |
| 234 | ssize_t bytes_written = g_output_stream_write_finish(dst_stream_, |
| 235 | res, |
| 236 | &error); |
Gilad Arnold | 6dbbd39 | 2012-07-10 16:19:11 -0700 | [diff] [blame] | 237 | |
Darin Petkov | c2e4a7d | 2010-12-02 14:47:06 -0800 | [diff] [blame] | 238 | if (bytes_written < static_cast<ssize_t>(buffer_valid_size_[index])) { |
| 239 | if (bytes_written < 0) { |
Gilad Arnold | 6dbbd39 | 2012-07-10 16:19:11 -0700 | [diff] [blame] | 240 | LOG(ERROR) << "Write error: " << utils::GetAndFreeGError(&error); |
Darin Petkov | c2e4a7d | 2010-12-02 14:47:06 -0800 | [diff] [blame] | 241 | } else { |
Gilad Arnold | 6dbbd39 | 2012-07-10 16:19:11 -0700 | [diff] [blame] | 242 | LOG(ERROR) << "Wrote too few bytes: " << bytes_written |
Gilad Arnold | 581c2ea | 2012-07-19 12:33:49 -0700 | [diff] [blame] | 243 | << " < " << buffer_valid_size_[index]; |
Darin Petkov | c2e4a7d | 2010-12-02 14:47:06 -0800 | [diff] [blame] | 244 | } |
Gilad Arnold | 581c2ea | 2012-07-19 12:33:49 -0700 | [diff] [blame] | 245 | failed_ = true; |
Darin Petkov | c2e4a7d | 2010-12-02 14:47:06 -0800 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | SpawnAsyncActions(); |
| 249 | } |
| 250 | |
| 251 | void FilesystemCopierAction::StaticAsyncWriteReadyCallback( |
| 252 | GObject *source_object, |
| 253 | GAsyncResult *res, |
| 254 | gpointer user_data) { |
| 255 | reinterpret_cast<FilesystemCopierAction*>(user_data)-> |
| 256 | AsyncWriteReadyCallback(source_object, res); |
| 257 | } |
| 258 | |
| 259 | void FilesystemCopierAction::SpawnAsyncActions() { |
| 260 | bool reading = false; |
| 261 | bool writing = false; |
| 262 | for (int i = 0; i < 2; i++) { |
| 263 | if (buffer_state_[i] == kBufferStateReading) { |
| 264 | reading = true; |
| 265 | } |
| 266 | if (buffer_state_[i] == kBufferStateWriting) { |
| 267 | writing = true; |
| 268 | } |
| 269 | } |
| 270 | if (failed_ || cancelled_) { |
| 271 | if (!reading && !writing) { |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 272 | Cleanup(ErrorCode::kError); |
Darin Petkov | c2e4a7d | 2010-12-02 14:47:06 -0800 | [diff] [blame] | 273 | } |
| 274 | return; |
| 275 | } |
| 276 | for (int i = 0; i < 2; i++) { |
| 277 | if (!reading && !read_done_ && buffer_state_[i] == kBufferStateEmpty) { |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 278 | int64_t bytes_to_read = std::min(static_cast<int64_t>(buffer_[0].size()), |
| 279 | filesystem_size_); |
Darin Petkov | c2e4a7d | 2010-12-02 14:47:06 -0800 | [diff] [blame] | 280 | g_input_stream_read_async( |
| 281 | src_stream_, |
| 282 | buffer_[i].data(), |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 283 | bytes_to_read, |
Darin Petkov | c2e4a7d | 2010-12-02 14:47:06 -0800 | [diff] [blame] | 284 | G_PRIORITY_DEFAULT, |
| 285 | canceller_[i], |
| 286 | &FilesystemCopierAction::StaticAsyncReadReadyCallback, |
| 287 | this); |
| 288 | reading = true; |
| 289 | buffer_state_[i] = kBufferStateReading; |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 290 | } else if (!writing && !verify_hash_ && |
| 291 | buffer_state_[i] == kBufferStateFull) { |
Darin Petkov | c2e4a7d | 2010-12-02 14:47:06 -0800 | [diff] [blame] | 292 | g_output_stream_write_async( |
| 293 | dst_stream_, |
| 294 | buffer_[i].data(), |
| 295 | buffer_valid_size_[i], |
| 296 | G_PRIORITY_DEFAULT, |
| 297 | canceller_[i], |
| 298 | &FilesystemCopierAction::StaticAsyncWriteReadyCallback, |
| 299 | this); |
| 300 | writing = true; |
| 301 | buffer_state_[i] = kBufferStateWriting; |
| 302 | } |
| 303 | } |
| 304 | if (!reading && !writing) { |
| 305 | // We're done! |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 306 | ErrorCode code = ErrorCode::kSuccess; |
Darin Petkov | c2e4a7d | 2010-12-02 14:47:06 -0800 | [diff] [blame] | 307 | if (hasher_.Finalize()) { |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 308 | LOG(INFO) << "Hash: " << hasher_.hash(); |
| 309 | if (verify_hash_) { |
| 310 | if (copying_kernel_install_path_) { |
| 311 | if (install_plan_.kernel_hash != hasher_.raw_hash()) { |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 312 | code = ErrorCode::kNewKernelVerificationError; |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 313 | LOG(ERROR) << "New kernel verification failed."; |
| 314 | } |
| 315 | } else { |
| 316 | if (install_plan_.rootfs_hash != hasher_.raw_hash()) { |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 317 | code = ErrorCode::kNewRootfsVerificationError; |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 318 | LOG(ERROR) << "New rootfs verification failed."; |
| 319 | } |
| 320 | } |
Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 321 | } else { |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 322 | if (copying_kernel_install_path_) { |
| 323 | install_plan_.kernel_hash = hasher_.raw_hash(); |
| 324 | } else { |
| 325 | install_plan_.rootfs_hash = hasher_.raw_hash(); |
| 326 | } |
Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 327 | } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 328 | } else { |
Darin Petkov | c2e4a7d | 2010-12-02 14:47:06 -0800 | [diff] [blame] | 329 | LOG(ERROR) << "Unable to finalize the hash."; |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 330 | code = ErrorCode::kError; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 331 | } |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 332 | Cleanup(code); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 333 | } |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 334 | } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 335 | |
Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 336 | void FilesystemCopierAction::DetermineFilesystemSize(int fd) { |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 337 | if (verify_hash_) { |
| 338 | filesystem_size_ = copying_kernel_install_path_ ? |
| 339 | install_plan_.kernel_size : install_plan_.rootfs_size; |
| 340 | LOG(INFO) << "Filesystem size: " << filesystem_size_; |
| 341 | return; |
| 342 | } |
Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 343 | filesystem_size_ = kint64max; |
| 344 | int block_count = 0, block_size = 0; |
| 345 | if (!copying_kernel_install_path_ && |
| 346 | utils::GetFilesystemSizeFromFD(fd, &block_count, &block_size)) { |
| 347 | filesystem_size_ = static_cast<int64_t>(block_count) * block_size; |
| 348 | LOG(INFO) << "Filesystem size: " << filesystem_size_ << " bytes (" |
| 349 | << block_count << "x" << block_size << ")."; |
| 350 | } |
| 351 | } |
| 352 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 353 | } // namespace chromeos_update_engine |