1 //
2 // Copyright (C) 2016 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 #include "update_engine/common/file_fetcher.h"
18
19 #include <algorithm>
20 #include <string>
21
22 #include <base/bind.h>
23 #include <base/format_macros.h>
24 #include <base/location.h>
25 #include <base/logging.h>
26 #include <android-base/stringprintf.h>
27 #include <brillo/streams/file_stream.h>
28
29 #include "update_engine/common/utils.h"
30
31 using std::string;
32
33 namespace {
34
35 size_t kReadBufferSize = 16 * 1024;
36
37 } // namespace
38
39 namespace chromeos_update_engine {
40
41 // static
SupportedUrl(const string & url)42 bool FileFetcher::SupportedUrl(const string& url) {
43 // Note that we require the file path to start with a "/".
44 return (android::base::StartsWith(ToLower(url), "file:///") ||
45 android::base::StartsWith(ToLower(url), "fd://"));
46 }
47
~FileFetcher()48 FileFetcher::~FileFetcher() {
49 LOG_IF(ERROR, transfer_in_progress_)
50 << "Destroying the fetcher while a transfer is in progress.";
51 CleanUp();
52 }
53
54 // Begins the transfer, which must not have already been started.
BeginTransfer(const string & url)55 void FileFetcher::BeginTransfer(const string& url) {
56 CHECK(!transfer_in_progress_);
57
58 if (!SupportedUrl(url)) {
59 LOG(ERROR) << "Unsupported file URL: " << url;
60 // No HTTP error code when the URL is not supported.
61 http_response_code_ = 0;
62 CleanUp();
63 if (delegate_)
64 delegate_->TransferComplete(this, false);
65 return;
66 }
67
68 string file_path;
69
70 if (android::base::StartsWith(ToLower(url), "fd://")) {
71 int fd = std::stoi(url.substr(strlen("fd://")));
72 file_path = url;
73 stream_ = brillo::FileStream::FromFileDescriptor(fd, false, nullptr);
74 } else {
75 file_path = url.substr(strlen("file://"));
76 stream_ =
77 brillo::FileStream::Open(base::FilePath(file_path),
78 brillo::Stream::AccessMode::READ,
79 brillo::FileStream::Disposition::OPEN_EXISTING,
80 nullptr);
81 }
82
83 if (!stream_) {
84 LOG(ERROR) << "Couldn't open " << file_path;
85 http_response_code_ = kHttpResponseNotFound;
86 CleanUp();
87 if (delegate_)
88 delegate_->TransferComplete(this, false);
89 return;
90 }
91 http_response_code_ = kHttpResponseOk;
92
93 if (offset_)
94 stream_->SetPosition(offset_, nullptr);
95 bytes_copied_ = 0;
96 transfer_in_progress_ = true;
97 ScheduleRead();
98 }
99
TerminateTransfer()100 void FileFetcher::TerminateTransfer() {
101 CleanUp();
102 if (delegate_) {
103 // Note that after the callback returns this object may be destroyed.
104 delegate_->TransferTerminated(this);
105 }
106 }
107
ScheduleRead()108 void FileFetcher::ScheduleRead() {
109 if (transfer_paused_ || ongoing_read_ || !transfer_in_progress_)
110 return;
111
112 buffer_.resize(kReadBufferSize);
113 size_t bytes_to_read = buffer_.size();
114 if (data_length_ >= 0) {
115 bytes_to_read = std::min(static_cast<uint64_t>(bytes_to_read),
116 data_length_ - bytes_copied_);
117 }
118
119 if (!bytes_to_read) {
120 OnReadDoneCallback(0);
121 return;
122 }
123
124 ongoing_read_ = stream_->ReadAsync(
125 buffer_.data(),
126 bytes_to_read,
127 base::Bind(&FileFetcher::OnReadDoneCallback, base::Unretained(this)),
128 base::Bind(&FileFetcher::OnReadErrorCallback, base::Unretained(this)),
129 nullptr);
130
131 if (!ongoing_read_) {
132 LOG(ERROR) << "Unable to schedule an asynchronous read from the stream.";
133 CleanUp();
134 if (delegate_)
135 delegate_->TransferComplete(this, false);
136 }
137 }
138
OnReadDoneCallback(size_t bytes_read)139 void FileFetcher::OnReadDoneCallback(size_t bytes_read) {
140 ongoing_read_ = false;
141 if (bytes_read == 0) {
142 CleanUp();
143 if (delegate_)
144 delegate_->TransferComplete(this, true);
145 } else {
146 bytes_copied_ += bytes_read;
147 if (delegate_ &&
148 !delegate_->ReceivedBytes(this, buffer_.data(), bytes_read))
149 return;
150 ScheduleRead();
151 }
152 }
153
OnReadErrorCallback(const brillo::Error * error)154 void FileFetcher::OnReadErrorCallback(const brillo::Error* error) {
155 LOG(ERROR) << "Asynchronous read failed: " << error->GetMessage();
156 CleanUp();
157 if (delegate_)
158 delegate_->TransferComplete(this, false);
159 }
160
Pause()161 void FileFetcher::Pause() {
162 if (transfer_paused_) {
163 LOG(ERROR) << "Fetcher already paused.";
164 return;
165 }
166 transfer_paused_ = true;
167 }
168
Unpause()169 void FileFetcher::Unpause() {
170 if (!transfer_paused_) {
171 LOG(ERROR) << "Resume attempted when fetcher not paused.";
172 return;
173 }
174 transfer_paused_ = false;
175 ScheduleRead();
176 }
177
CleanUp()178 void FileFetcher::CleanUp() {
179 if (stream_) {
180 stream_->CancelPendingAsyncOperations();
181 stream_->CloseBlocking(nullptr);
182 stream_.reset();
183 }
184 // Destroying the |stream_| releases the callback, so we don't have any
185 // ongoing read at this point.
186 ongoing_read_ = false;
187 buffer_ = brillo::Blob();
188
189 transfer_in_progress_ = false;
190 transfer_paused_ = false;
191 }
192 } // namespace chromeos_update_engine
193