blob: cb8e89a4ccd3d371eaf0768eb2da08a6461252cc [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>
Kelvin Zhangb9a9aa22024-10-15 10:38:35 -070026#include <android-base/stringprintf.h>
Alex Deymo2c131bb2016-05-26 16:43:13 -070027#include <brillo/streams/file_stream.h>
28
Kelvin Zhang0c184242024-10-25 11:19:27 -070029#include "update_engine/common/utils.h"
Alex Deymo2c131bb2016-05-26 16:43:13 -070030
31using std::string;
32
33namespace {
34
35size_t kReadBufferSize = 16 * 1024;
36
37} // namespace
38
39namespace chromeos_update_engine {
40
41// static
42bool FileFetcher::SupportedUrl(const string& url) {
43 // Note that we require the file path to start with a "/".
Kelvin Zhang0c184242024-10-25 11:19:27 -070044 return (android::base::StartsWith(ToLower(url), "file:///") ||
45 android::base::StartsWith(ToLower(url), "fd://"));
Alex Deymo2c131bb2016-05-26 16:43:13 -070046}
47
48FileFetcher::~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.
55void 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
Kyeongkab.Nam500ca132019-06-26 13:48:07 +090068 string file_path;
69
Kelvin Zhang0c184242024-10-25 11:19:27 -070070 if (android::base::StartsWith(ToLower(url), "fd://")) {
Kyeongkab.Nam500ca132019-06-26 13:48:07 +090071 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 }
Alex Deymo2c131bb2016-05-26 16:43:13 -070082
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
100void 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
108void 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
139void 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;
Amin Hassani0cd9d772018-07-31 23:55:43 -0700147 if (delegate_ &&
148 !delegate_->ReceivedBytes(this, buffer_.data(), bytes_read))
149 return;
Alex Deymo2c131bb2016-05-26 16:43:13 -0700150 ScheduleRead();
151 }
152}
153
154void 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
161void FileFetcher::Pause() {
162 if (transfer_paused_) {
163 LOG(ERROR) << "Fetcher already paused.";
164 return;
165 }
166 transfer_paused_ = true;
167}
168
169void 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
178void 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}
Alex Deymo2c131bb2016-05-26 16:43:13 -0700192} // namespace chromeos_update_engine