blob: 77dadd1afb232042861deaf1c3b942552eece846 [file] [log] [blame]
Alex Deymo2c131bb2016-05-26 16:43:13 -07001//
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 <base/strings/string_util.h>
27#include <base/strings/stringprintf.h>
28#include <brillo/streams/file_stream.h>
29
30#include "update_engine/common/certificate_checker.h"
31#include "update_engine/common/hardware_interface.h"
32#include "update_engine/common/platform_constants.h"
33
34using std::string;
35
36namespace {
37
38size_t kReadBufferSize = 16 * 1024;
39
40} // namespace
41
42namespace chromeos_update_engine {
43
44// static
45bool FileFetcher::SupportedUrl(const string& url) {
46 // Note that we require the file path to start with a "/".
47 return base::StartsWith(
48 url, "file:///", base::CompareCase::INSENSITIVE_ASCII);
49}
50
51FileFetcher::~FileFetcher() {
52 LOG_IF(ERROR, transfer_in_progress_)
53 << "Destroying the fetcher while a transfer is in progress.";
54 CleanUp();
55}
56
57// Begins the transfer, which must not have already been started.
58void FileFetcher::BeginTransfer(const string& url) {
59 CHECK(!transfer_in_progress_);
60
61 if (!SupportedUrl(url)) {
62 LOG(ERROR) << "Unsupported file URL: " << url;
63 // No HTTP error code when the URL is not supported.
64 http_response_code_ = 0;
65 CleanUp();
66 if (delegate_)
67 delegate_->TransferComplete(this, false);
68 return;
69 }
70
71 string file_path = url.substr(strlen("file://"));
72 stream_ =
73 brillo::FileStream::Open(base::FilePath(file_path),
74 brillo::Stream::AccessMode::READ,
75 brillo::FileStream::Disposition::OPEN_EXISTING,
76 nullptr);
77
78 if (!stream_) {
79 LOG(ERROR) << "Couldn't open " << file_path;
80 http_response_code_ = kHttpResponseNotFound;
81 CleanUp();
82 if (delegate_)
83 delegate_->TransferComplete(this, false);
84 return;
85 }
86 http_response_code_ = kHttpResponseOk;
87
88 if (offset_)
89 stream_->SetPosition(offset_, nullptr);
90 bytes_copied_ = 0;
91 transfer_in_progress_ = true;
92 ScheduleRead();
93}
94
95void FileFetcher::TerminateTransfer() {
96 CleanUp();
97 if (delegate_) {
98 // Note that after the callback returns this object may be destroyed.
99 delegate_->TransferTerminated(this);
100 }
101}
102
103void FileFetcher::ScheduleRead() {
104 if (transfer_paused_ || ongoing_read_ || !transfer_in_progress_)
105 return;
106
107 buffer_.resize(kReadBufferSize);
108 size_t bytes_to_read = buffer_.size();
109 if (data_length_ >= 0) {
110 bytes_to_read = std::min(static_cast<uint64_t>(bytes_to_read),
111 data_length_ - bytes_copied_);
112 }
113
114 if (!bytes_to_read) {
115 OnReadDoneCallback(0);
116 return;
117 }
118
119 ongoing_read_ = stream_->ReadAsync(
120 buffer_.data(),
121 bytes_to_read,
122 base::Bind(&FileFetcher::OnReadDoneCallback, base::Unretained(this)),
123 base::Bind(&FileFetcher::OnReadErrorCallback, base::Unretained(this)),
124 nullptr);
125
126 if (!ongoing_read_) {
127 LOG(ERROR) << "Unable to schedule an asynchronous read from the stream.";
128 CleanUp();
129 if (delegate_)
130 delegate_->TransferComplete(this, false);
131 }
132}
133
134void FileFetcher::OnReadDoneCallback(size_t bytes_read) {
135 ongoing_read_ = false;
136 if (bytes_read == 0) {
137 CleanUp();
138 if (delegate_)
139 delegate_->TransferComplete(this, true);
140 } else {
141 bytes_copied_ += bytes_read;
142 if (delegate_)
143 delegate_->ReceivedBytes(this, buffer_.data(), bytes_read);
144 ScheduleRead();
145 }
146}
147
148void FileFetcher::OnReadErrorCallback(const brillo::Error* error) {
149 LOG(ERROR) << "Asynchronous read failed: " << error->GetMessage();
150 CleanUp();
151 if (delegate_)
152 delegate_->TransferComplete(this, false);
153}
154
155void FileFetcher::Pause() {
156 if (transfer_paused_) {
157 LOG(ERROR) << "Fetcher already paused.";
158 return;
159 }
160 transfer_paused_ = true;
161}
162
163void FileFetcher::Unpause() {
164 if (!transfer_paused_) {
165 LOG(ERROR) << "Resume attempted when fetcher not paused.";
166 return;
167 }
168 transfer_paused_ = false;
169 ScheduleRead();
170}
171
172void FileFetcher::CleanUp() {
173 if (stream_) {
174 stream_->CancelPendingAsyncOperations();
175 stream_->CloseBlocking(nullptr);
176 stream_.reset();
177 }
178 // Destroying the |stream_| releases the callback, so we don't have any
179 // ongoing read at this point.
180 ongoing_read_ = false;
181 buffer_ = brillo::Blob();
182
183 transfer_in_progress_ = false;
184 transfer_paused_ = false;
185}
186
187} // namespace chromeos_update_engine