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