blob: 028a99acf249fb888846e43f2b47235830115bc9 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
2// Copyright (C) 2011 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//
rspangler@google.com49fdf182009-10-10 00:57:34 +000016
Alex Deymo39910dc2015-11-09 17:04:30 -080017#ifndef UPDATE_ENGINE_PAYLOAD_CONSUMER_DOWNLOAD_ACTION_H_
18#define UPDATE_ENGINE_PAYLOAD_CONSUMER_DOWNLOAD_ACTION_H_
rspangler@google.com49fdf182009-10-10 00:57:34 +000019
rspangler@google.com49fdf182009-10-10 00:57:34 +000020#include <fcntl.h>
Alex Vakulenko44cab302014-07-23 13:12:15 -070021#include <sys/stat.h>
22#include <sys/types.h>
rspangler@google.com49fdf182009-10-10 00:57:34 +000023
Ben Chan02f7c1d2014-10-18 15:18:02 -070024#include <memory>
rspangler@google.com49fdf182009-10-10 00:57:34 +000025#include <string>
26
Alex Deymo39910dc2015-11-09 17:04:30 -080027#include "update_engine/common/action.h"
Alex Deymo1b3556c2016-02-03 09:54:02 -080028#include "update_engine/common/boot_control_interface.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080029#include "update_engine/common/http_fetcher.h"
Sen Jiang5ae865b2017-04-18 14:24:40 -070030#include "update_engine/common/multi_range_http_fetcher.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080031#include "update_engine/payload_consumer/delta_performer.h"
32#include "update_engine/payload_consumer/install_plan.h"
Jay Srinivasanf0572052012-10-23 18:12:56 -070033#include "update_engine/system_state.h"
rspangler@google.com49fdf182009-10-10 00:57:34 +000034
Darin Petkov7ed561b2011-10-04 02:59:03 -070035// The Download Action downloads a specified url to disk. The url should point
36// to an update in a delta payload format. The payload will be piped into a
Andrew de los Reyesf9185172010-05-03 11:07:05 -070037// DeltaPerformer that will apply the delta to the disk.
rspangler@google.com49fdf182009-10-10 00:57:34 +000038
rspangler@google.com49fdf182009-10-10 00:57:34 +000039namespace chromeos_update_engine {
40
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070041class DownloadActionDelegate {
42 public:
Alex Deymoe8948702014-11-11 21:44:45 -080043 virtual ~DownloadActionDelegate() = default;
44
Alex Deymo22ad8612015-11-20 17:59:11 -030045 // Called periodically after bytes are received. This method will be invoked
Alex Deymo542c19b2015-12-03 07:43:31 -030046 // only if the DownloadAction is running. |bytes_progressed| is the number of
47 // bytes downloaded since the last call of this method, |bytes_received|
48 // the number of bytes downloaded thus far and |total| is the number of bytes
49 // expected.
50 virtual void BytesReceived(uint64_t bytes_progressed,
51 uint64_t bytes_received,
52 uint64_t total) = 0;
53
54 // Returns whether the download should be canceled, in which case the
55 // |cancel_reason| error should be set to the reason why the download was
56 // canceled.
57 virtual bool ShouldCancel(ErrorCode* cancel_reason) = 0;
58
59 // Called once the complete payload has been downloaded. Note that any errors
60 // while applying or downloading the partial payload will result in this
61 // method not being called.
62 virtual void DownloadComplete() = 0;
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070063};
64
Darin Petkov73058b42010-10-06 16:32:19 -070065class PrefsInterface;
rspangler@google.com49fdf182009-10-10 00:57:34 +000066
Chris Sosad317e402013-06-12 13:47:09 -070067class DownloadAction : public InstallPlanAction,
rspangler@google.com49fdf182009-10-10 00:57:34 +000068 public HttpFetcherDelegate {
69 public:
Alex Deymof2858572016-02-25 11:20:13 -080070 // Debugging/logging
71 static std::string StaticType() { return "DownloadAction"; }
72
rspangler@google.com49fdf182009-10-10 00:57:34 +000073 // Takes ownership of the passed in HttpFetcher. Useful for testing.
74 // A good calling pattern is:
Alex Deymo1b3556c2016-02-03 09:54:02 -080075 // DownloadAction(prefs, boot_contol, hardware, system_state,
Amin Hassani7ecda262017-07-11 17:10:50 -070076 // new WhateverHttpFetcher, false);
Jay Srinivasanf0572052012-10-23 18:12:56 -070077 DownloadAction(PrefsInterface* prefs,
Alex Deymo1b3556c2016-02-03 09:54:02 -080078 BootControlInterface* boot_control,
79 HardwareInterface* hardware,
Jay Srinivasanf0572052012-10-23 18:12:56 -070080 SystemState* system_state,
Amin Hassani7ecda262017-07-11 17:10:50 -070081 HttpFetcher* http_fetcher,
Amin Hassanied37d682018-04-06 13:22:00 -070082 bool interactive);
Alex Deymo610277e2014-11-11 21:18:11 -080083 ~DownloadAction() override;
Alex Deymof2858572016-02-25 11:20:13 -080084
85 // InstallPlanAction overrides.
Alex Deymo610277e2014-11-11 21:18:11 -080086 void PerformAction() override;
Alex Deymof2858572016-02-25 11:20:13 -080087 void SuspendAction() override;
88 void ResumeAction() override;
Alex Deymo610277e2014-11-11 21:18:11 -080089 void TerminateProcessing() override;
Alex Deymof2858572016-02-25 11:20:13 -080090 std::string Type() const override { return StaticType(); }
rspangler@google.com49fdf182009-10-10 00:57:34 +000091
Andrew de los Reyesf9185172010-05-03 11:07:05 -070092 // Testing
93 void SetTestFileWriter(FileWriter* writer) {
94 writer_ = writer;
95 }
96
Darin Petkov1023a602010-08-30 13:47:51 -070097 int GetHTTPResponseCode() { return http_fetcher_->http_response_code(); }
98
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070099 // HttpFetcherDelegate methods (see http_fetcher.h)
Amin Hassani0cd9d772018-07-31 23:55:43 -0700100 bool ReceivedBytes(HttpFetcher* fetcher,
101 const void* bytes,
102 size_t length) override;
Alex Deymo610277e2014-11-11 21:18:11 -0800103 void SeekToOffset(off_t offset) override;
Alex Deymo60ca1a72015-06-18 18:19:15 -0700104 void TransferComplete(HttpFetcher* fetcher, bool successful) override;
105 void TransferTerminated(HttpFetcher* fetcher) override;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000106
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700107 DownloadActionDelegate* delegate() const { return delegate_; }
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700108 void set_delegate(DownloadActionDelegate* delegate) {
109 delegate_ = delegate;
110 }
111
Sen Jiang5ae865b2017-04-18 14:24:40 -0700112 void set_base_offset(int64_t base_offset) { base_offset_ = base_offset; }
113
Darin Petkov9b230572010-10-08 10:20:09 -0700114 HttpFetcher* http_fetcher() { return http_fetcher_.get(); }
115
David Zeuthen8f191b22013-08-06 12:27:50 -0700116 // Returns the p2p file id for the file being written or the empty
117 // string if we're not writing to a p2p file.
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700118 std::string p2p_file_id() { return p2p_file_id_; }
David Zeuthen8f191b22013-08-06 12:27:50 -0700119
rspangler@google.com49fdf182009-10-10 00:57:34 +0000120 private:
David Zeuthen8f191b22013-08-06 12:27:50 -0700121 // Closes the file descriptor for the p2p file being written and
122 // clears |p2p_file_id_| to indicate that we're no longer sharing
123 // the file. If |delete_p2p_file| is True, also deletes the file.
124 // If there is no p2p file descriptor, this method does nothing.
125 void CloseP2PSharingFd(bool delete_p2p_file);
126
127 // Starts sharing the p2p file. Must be called before
128 // WriteToP2PFile(). Returns True if this worked.
129 bool SetupP2PSharingFd();
130
131 // Writes |length| bytes of payload from |data| into |file_offset|
132 // of the p2p file. Also does sanity checks; for example ensures we
133 // don't end up with a file with holes in it.
134 //
135 // This method does nothing if SetupP2PSharingFd() hasn't been
136 // called or if CloseP2PSharingFd() has been called.
Alex Deymo60ca1a72015-06-18 18:19:15 -0700137 void WriteToP2PFile(const void* data, size_t length, off_t file_offset);
David Zeuthen8f191b22013-08-06 12:27:50 -0700138
Sen Jiang6c736682017-03-10 15:01:36 -0800139 // Start downloading the current payload using delta_performer.
140 void StartDownloading();
141
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700142 // The InstallPlan passed in
143 InstallPlan install_plan_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000144
Sen Jiang0affc2c2017-02-10 15:55:05 -0800145 // Pointer to the current payload in install_plan_.payloads.
146 InstallPlan::Payload* payload_{nullptr};
147
Alex Deymo1b3556c2016-02-03 09:54:02 -0800148 // SystemState required pointers.
Darin Petkov73058b42010-10-06 16:32:19 -0700149 PrefsInterface* prefs_;
Alex Deymo1b3556c2016-02-03 09:54:02 -0800150 BootControlInterface* boot_control_;
151 HardwareInterface* hardware_;
Darin Petkov73058b42010-10-06 16:32:19 -0700152
Jay Srinivasanedce2832012-10-24 18:57:47 -0700153 // Global context for the system.
154 SystemState* system_state_;
155
Sen Jiang5ae865b2017-04-18 14:24:40 -0700156 // Pointer to the MultiRangeHttpFetcher that does the http work.
157 std::unique_ptr<MultiRangeHttpFetcher> http_fetcher_;
Jay Srinivasanedce2832012-10-24 18:57:47 -0700158
Amin Hassani7ecda262017-07-11 17:10:50 -0700159 // If |true|, the update is user initiated (vs. periodic update checks). Hence
160 // the |delta_performer_| can decide not to use O_DSYNC flag for faster
161 // update.
Amin Hassanied37d682018-04-06 13:22:00 -0700162 bool interactive_;
Amin Hassani7ecda262017-07-11 17:10:50 -0700163
rspangler@google.com49fdf182009-10-10 00:57:34 +0000164 // The FileWriter that downloaded data should be written to. It will
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700165 // either point to *decompressing_file_writer_ or *delta_performer_.
rspangler@google.com49fdf182009-10-10 00:57:34 +0000166 FileWriter* writer_;
167
Ben Chan02f7c1d2014-10-18 15:18:02 -0700168 std::unique_ptr<DeltaPerformer> delta_performer_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000169
Darin Petkov9ce452b2010-11-17 14:33:28 -0800170 // Used by TransferTerminated to figure if this action terminated itself or
171 // was terminated by the action processor.
David Zeuthena99981f2013-04-29 13:42:47 -0700172 ErrorCode code_;
Darin Petkov9ce452b2010-11-17 14:33:28 -0800173
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700174 // For reporting status to outsiders
175 DownloadActionDelegate* delegate_;
Aaron Wood9321f502017-09-07 11:18:54 -0700176 uint64_t bytes_received_{0}; // per file/range
177 uint64_t bytes_received_previous_payloads_{0};
Sen Jiang5ae865b2017-04-18 14:24:40 -0700178 uint64_t bytes_total_{0};
Alex Deymo22ad8612015-11-20 17:59:11 -0300179 bool download_active_{false};
Darin Petkov9d911fa2010-08-19 09:36:08 -0700180
David Zeuthen8f191b22013-08-06 12:27:50 -0700181 // The file-id for the file we're sharing or the empty string
182 // if we're not using p2p to share.
183 std::string p2p_file_id_;
184
185 // The file descriptor for the p2p file used for caching the payload or -1
186 // if we're not using p2p to share.
187 int p2p_sharing_fd_;
188
189 // Set to |false| if p2p file is not visible.
190 bool p2p_visible_;
191
Sen Jiang5ae865b2017-04-18 14:24:40 -0700192 // Loaded from prefs before downloading any payload.
193 size_t resume_payload_index_{0};
194
195 // Offset of the payload in the download URL, used by UpdateAttempterAndroid.
196 int64_t base_offset_{0};
197
rspangler@google.com49fdf182009-10-10 00:57:34 +0000198 DISALLOW_COPY_AND_ASSIGN(DownloadAction);
199};
200
201// We want to be sure that we're compiled with large file support on linux,
202// just in case we find ourselves downloading large images.
Alex Vakulenko0103c362016-01-20 07:56:15 -0800203static_assert(8 == sizeof(off_t), "off_t not 64 bit");
rspangler@google.com49fdf182009-10-10 00:57:34 +0000204
205} // namespace chromeos_update_engine
206
Alex Deymo39910dc2015-11-09 17:04:30 -0800207#endif // UPDATE_ENGINE_PAYLOAD_CONSUMER_DOWNLOAD_ACTION_H_