blob: bd4486730b08db0191a5772ca72ec5bbd2e8647c [file] [log] [blame]
Andrew de los Reyesc7020782010-04-28 10:46:04 -07001// Copyright (c) 2010 The Chromium 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"
6#include <sys/stat.h>
7#include <sys/types.h>
8#include <errno.h>
9#include <fcntl.h>
10#include <stdlib.h>
11#include <algorithm>
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080012#include <map>
adlr@google.com3defe6a2009-12-04 20:57:17 +000013#include <string>
14#include <vector>
Andrew de los Reyesc7020782010-04-28 10:46:04 -070015#include <gio/gio.h>
16#include <gio/gunixinputstream.h>
17#include <gio/gunixoutputstream.h>
18#include <glib.h>
adlr@google.com3defe6a2009-12-04 20:57:17 +000019#include "update_engine/filesystem_iterator.h"
20#include "update_engine/subprocess.h"
21#include "update_engine/utils.h"
22
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080023using std::map;
adlr@google.com3defe6a2009-12-04 20:57:17 +000024using std::min;
25using std::string;
26using std::vector;
27
28namespace chromeos_update_engine {
29
30namespace {
Andrew de los Reyesc7020782010-04-28 10:46:04 -070031const off_t kCopyFileBufferSize = 2 * 1024 * 1024;
adlr@google.com3defe6a2009-12-04 20:57:17 +000032} // namespace {}
33
34void FilesystemCopierAction::PerformAction() {
Andrew de los Reyesc7020782010-04-28 10:46:04 -070035 // Will tell the ActionProcessor we've failed if we return.
36 ScopedActionCompleter abort_action_completer(processor_, this);
37
adlr@google.com3defe6a2009-12-04 20:57:17 +000038 if (!HasInputObject()) {
Andrew de los Reyesc7020782010-04-28 10:46:04 -070039 LOG(ERROR) << "FilesystemCopierAction missing input object.";
adlr@google.com3defe6a2009-12-04 20:57:17 +000040 return;
41 }
42 install_plan_ = GetInputObject();
43
44 if (install_plan_.is_full_update) {
Andrew de los Reyesc7020782010-04-28 10:46:04 -070045 // No copy needed. Done!
46 abort_action_completer.set_success(true);
adlr@google.com3defe6a2009-12-04 20:57:17 +000047 return;
48 }
49
Andrew de los Reyesf9185172010-05-03 11:07:05 -070050 string source = copy_source_;
51 if (source.empty()) {
52 source = copying_kernel_install_path_ ?
53 utils::BootKernelDevice(utils::BootDevice()) :
54 utils::BootDevice();
55 }
56
57 const string destination = copying_kernel_install_path_ ?
58 install_plan_.kernel_install_path :
59 install_plan_.install_path;
Andrew de los Reyesc7020782010-04-28 10:46:04 -070060
61 int src_fd = open(source.c_str(), O_RDONLY);
62 if (src_fd < 0) {
63 PLOG(ERROR) << "Unable to open " << source << " for reading:";
64 return;
65 }
Andrew de los Reyesf9185172010-05-03 11:07:05 -070066 int dst_fd = open(destination.c_str(),
Andrew de los Reyesc7020782010-04-28 10:46:04 -070067 O_WRONLY | O_TRUNC | O_CREAT,
68 0644);
69 if (dst_fd < 0) {
70 close(src_fd);
71 PLOG(ERROR) << "Unable to open " << install_plan_.install_path
72 << " for writing:";
73 return;
adlr@google.com3defe6a2009-12-04 20:57:17 +000074 }
75
Andrew de los Reyesc7020782010-04-28 10:46:04 -070076 src_stream_ = g_unix_input_stream_new(src_fd, TRUE);
77 dst_stream_ = g_unix_output_stream_new(dst_fd, TRUE);
78
79 buffer_.resize(kCopyFileBufferSize);
80
81 // Set up the first read
82 canceller_ = g_cancellable_new();
83
84 g_input_stream_read_async(src_stream_,
85 &buffer_[0],
86 buffer_.size(),
87 G_PRIORITY_DEFAULT,
88 canceller_,
89 &FilesystemCopierAction::StaticAsyncReadyCallback,
90 this);
91 read_in_flight_ = true;
92
93 abort_action_completer.set_should_complete(false);
adlr@google.com3defe6a2009-12-04 20:57:17 +000094}
95
96void FilesystemCopierAction::TerminateProcessing() {
Andrew de los Reyesc7020782010-04-28 10:46:04 -070097 if (canceller_) {
98 g_cancellable_cancel(canceller_);
adlr@google.com3defe6a2009-12-04 20:57:17 +000099 }
100}
101
Andrew de los Reyesc7020782010-04-28 10:46:04 -0700102void FilesystemCopierAction::Cleanup(bool success, bool was_cancelled) {
103 g_object_unref(src_stream_);
104 src_stream_ = NULL;
105 g_object_unref(dst_stream_);
106 dst_stream_ = NULL;
107 if (was_cancelled)
108 return;
109 if (success && HasOutputPipe())
adlr@google.com3defe6a2009-12-04 20:57:17 +0000110 SetOutputObject(install_plan_);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000111 processor_->ActionComplete(this, success);
112}
113
Andrew de los Reyesc7020782010-04-28 10:46:04 -0700114void FilesystemCopierAction::AsyncReadyCallback(GObject *source_object,
115 GAsyncResult *res) {
116 GError* error = NULL;
117 CHECK(canceller_);
118 bool was_cancelled = g_cancellable_is_cancelled(canceller_) == TRUE;
119 g_object_unref(canceller_);
120 canceller_ = NULL;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000121
Andrew de los Reyesc7020782010-04-28 10:46:04 -0700122 if (read_in_flight_) {
123 ssize_t bytes_read = g_input_stream_read_finish(src_stream_, res, &error);
124 if (bytes_read < 0) {
125 LOG(ERROR) << "Read failed:" << utils::GetGErrorMessage(error);
126 Cleanup(false, was_cancelled);
127 return;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000128 }
Andrew de los Reyesc7020782010-04-28 10:46:04 -0700129
130 if (bytes_read == 0) {
131 // We're done!
132 Cleanup(true, was_cancelled);
133 return;
134 }
135 // Kick off a write
136 read_in_flight_ = false;
137 buffer_valid_size_ = bytes_read;
138 canceller_ = g_cancellable_new();
139 g_output_stream_write_async(
140 dst_stream_,
141 &buffer_[0],
142 bytes_read,
143 G_PRIORITY_DEFAULT,
144 canceller_,
145 &FilesystemCopierAction::StaticAsyncReadyCallback,
146 this);
147 return;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000148 }
adlr@google.com3defe6a2009-12-04 20:57:17 +0000149
Andrew de los Reyesc7020782010-04-28 10:46:04 -0700150 ssize_t bytes_written = g_output_stream_write_finish(dst_stream_,
151 res,
152 &error);
153 if (bytes_written < static_cast<ssize_t>(buffer_valid_size_)) {
154 if (bytes_written < 0) {
155 LOG(ERROR) << "Write failed:" << utils::GetGErrorMessage(error);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000156 } else {
Andrew de los Reyesc7020782010-04-28 10:46:04 -0700157 LOG(ERROR) << "Write was short: wrote " << bytes_written
158 << " but expected to write " << buffer_valid_size_;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000159 }
Andrew de los Reyesc7020782010-04-28 10:46:04 -0700160 Cleanup(false, was_cancelled);
161 return;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000162 }
adlr@google.com3defe6a2009-12-04 20:57:17 +0000163
Andrew de los Reyesc7020782010-04-28 10:46:04 -0700164 // Kick off a read
165 read_in_flight_ = true;
166 canceller_ = g_cancellable_new();
167 g_input_stream_read_async(
168 src_stream_,
169 &buffer_[0],
170 buffer_.size(),
171 G_PRIORITY_DEFAULT,
172 canceller_,
173 &FilesystemCopierAction::StaticAsyncReadyCallback,
174 this);
175}
adlr@google.com3defe6a2009-12-04 20:57:17 +0000176
177} // namespace chromeos_update_engine