blob: 5628ec3517055ae1058b083ba714795440187840 [file] [log] [blame]
Darin Petkov9b230572010-10-08 10:20:09 -07001// Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
adlr@google.com3defe6a2009-12-04 20:57:17 +00002// 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 Petkov9b230572010-10-08 10:20:09 -07006
adlr@google.com3defe6a2009-12-04 20:57:17 +00007#include <sys/stat.h>
8#include <sys/types.h>
9#include <errno.h>
10#include <fcntl.h>
Darin Petkov9b230572010-10-08 10:20:09 -070011
adlr@google.com3defe6a2009-12-04 20:57:17 +000012#include <algorithm>
Darin Petkov9b230572010-10-08 10:20:09 -070013#include <cstdlib>
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080014#include <map>
adlr@google.com3defe6a2009-12-04 20:57:17 +000015#include <string>
16#include <vector>
Darin Petkov9b230572010-10-08 10:20:09 -070017
Andrew de los Reyesc7020782010-04-28 10:46:04 -070018#include <gio/gio.h>
19#include <gio/gunixinputstream.h>
20#include <gio/gunixoutputstream.h>
21#include <glib.h>
Darin Petkov9b230572010-10-08 10:20:09 -070022
adlr@google.com3defe6a2009-12-04 20:57:17 +000023#include "update_engine/filesystem_iterator.h"
24#include "update_engine/subprocess.h"
25#include "update_engine/utils.h"
26
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080027using std::map;
adlr@google.com3defe6a2009-12-04 20:57:17 +000028using std::min;
29using std::string;
30using std::vector;
31
32namespace chromeos_update_engine {
33
34namespace {
Andrew de los Reyesc7020782010-04-28 10:46:04 -070035const off_t kCopyFileBufferSize = 2 * 1024 * 1024;
adlr@google.com3defe6a2009-12-04 20:57:17 +000036} // namespace {}
37
38void FilesystemCopierAction::PerformAction() {
Andrew de los Reyesc7020782010-04-28 10:46:04 -070039 // Will tell the ActionProcessor we've failed if we return.
40 ScopedActionCompleter abort_action_completer(processor_, this);
41
adlr@google.com3defe6a2009-12-04 20:57:17 +000042 if (!HasInputObject()) {
Andrew de los Reyesc7020782010-04-28 10:46:04 -070043 LOG(ERROR) << "FilesystemCopierAction missing input object.";
adlr@google.com3defe6a2009-12-04 20:57:17 +000044 return;
45 }
46 install_plan_ = GetInputObject();
47
Darin Petkov9b230572010-10-08 10:20:09 -070048 if (install_plan_.is_full_update || install_plan_.is_resume) {
Andrew de los Reyesc7020782010-04-28 10:46:04 -070049 // No copy needed. Done!
Andrew de los Reyesf98bff82010-05-06 13:33:25 -070050 if (HasOutputPipe())
51 SetOutputObject(install_plan_);
Darin Petkovc1a8b422010-07-19 11:34:49 -070052 abort_action_completer.set_code(kActionCodeSuccess);
adlr@google.com3defe6a2009-12-04 20:57:17 +000053 return;
54 }
55
Andrew de los Reyesf9185172010-05-03 11:07:05 -070056 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 Petkovc1a8b422010-07-19 11:34:49 -070066
Andrew de los Reyesc7020782010-04-28 10:46:04 -070067 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 Reyesf9185172010-05-03 11:07:05 -070072 int dst_fd = open(destination.c_str(),
Andrew de los Reyesc7020782010-04-28 10:46:04 -070073 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.com3defe6a2009-12-04 20:57:17 +000080 }
81
Andrew de los Reyesc7020782010-04-28 10:46:04 -070082 src_stream_ = g_unix_input_stream_new(src_fd, TRUE);
83 dst_stream_ = g_unix_output_stream_new(dst_fd, TRUE);
84
85 buffer_.resize(kCopyFileBufferSize);
86
87 // Set up the first read
88 canceller_ = g_cancellable_new();
89
90 g_input_stream_read_async(src_stream_,
91 &buffer_[0],
92 buffer_.size(),
93 G_PRIORITY_DEFAULT,
94 canceller_,
95 &FilesystemCopierAction::StaticAsyncReadyCallback,
96 this);
97 read_in_flight_ = true;
98
99 abort_action_completer.set_should_complete(false);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000100}
101
102void FilesystemCopierAction::TerminateProcessing() {
Andrew de los Reyesc7020782010-04-28 10:46:04 -0700103 if (canceller_) {
104 g_cancellable_cancel(canceller_);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000105 }
106}
107
Andrew de los Reyesc7020782010-04-28 10:46:04 -0700108void FilesystemCopierAction::Cleanup(bool success, bool was_cancelled) {
109 g_object_unref(src_stream_);
110 src_stream_ = NULL;
111 g_object_unref(dst_stream_);
112 dst_stream_ = NULL;
113 if (was_cancelled)
114 return;
115 if (success && HasOutputPipe())
adlr@google.com3defe6a2009-12-04 20:57:17 +0000116 SetOutputObject(install_plan_);
Darin Petkovc1a8b422010-07-19 11:34:49 -0700117 processor_->ActionComplete(
118 this,
119 success ? kActionCodeSuccess : kActionCodeError);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000120}
121
Andrew de los Reyesc7020782010-04-28 10:46:04 -0700122void FilesystemCopierAction::AsyncReadyCallback(GObject *source_object,
123 GAsyncResult *res) {
124 GError* error = NULL;
125 CHECK(canceller_);
126 bool was_cancelled = g_cancellable_is_cancelled(canceller_) == TRUE;
127 g_object_unref(canceller_);
128 canceller_ = NULL;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000129
Andrew de los Reyesc7020782010-04-28 10:46:04 -0700130 if (read_in_flight_) {
131 ssize_t bytes_read = g_input_stream_read_finish(src_stream_, res, &error);
132 if (bytes_read < 0) {
133 LOG(ERROR) << "Read failed:" << utils::GetGErrorMessage(error);
134 Cleanup(false, was_cancelled);
135 return;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000136 }
Andrew de los Reyesc7020782010-04-28 10:46:04 -0700137
138 if (bytes_read == 0) {
139 // We're done!
140 Cleanup(true, was_cancelled);
141 return;
142 }
143 // Kick off a write
144 read_in_flight_ = false;
145 buffer_valid_size_ = bytes_read;
146 canceller_ = g_cancellable_new();
147 g_output_stream_write_async(
148 dst_stream_,
149 &buffer_[0],
150 bytes_read,
151 G_PRIORITY_DEFAULT,
152 canceller_,
153 &FilesystemCopierAction::StaticAsyncReadyCallback,
154 this);
155 return;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000156 }
adlr@google.com3defe6a2009-12-04 20:57:17 +0000157
Andrew de los Reyesc7020782010-04-28 10:46:04 -0700158 ssize_t bytes_written = g_output_stream_write_finish(dst_stream_,
159 res,
160 &error);
161 if (bytes_written < static_cast<ssize_t>(buffer_valid_size_)) {
162 if (bytes_written < 0) {
163 LOG(ERROR) << "Write failed:" << utils::GetGErrorMessage(error);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000164 } else {
Andrew de los Reyesc7020782010-04-28 10:46:04 -0700165 LOG(ERROR) << "Write was short: wrote " << bytes_written
166 << " but expected to write " << buffer_valid_size_;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000167 }
Andrew de los Reyesc7020782010-04-28 10:46:04 -0700168 Cleanup(false, was_cancelled);
169 return;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000170 }
adlr@google.com3defe6a2009-12-04 20:57:17 +0000171
Andrew de los Reyesc7020782010-04-28 10:46:04 -0700172 // Kick off a read
173 read_in_flight_ = true;
174 canceller_ = g_cancellable_new();
175 g_input_stream_read_async(
176 src_stream_,
177 &buffer_[0],
178 buffer_.size(),
179 G_PRIORITY_DEFAULT,
180 canceller_,
181 &FilesystemCopierAction::StaticAsyncReadyCallback,
182 this);
183}
adlr@google.com3defe6a2009-12-04 20:57:17 +0000184
185} // namespace chromeos_update_engine