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