blob: caa5a7b7d45346de7e8a97c76fe923ba92b23282 [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
Amin Hassaniec7bc112020-10-29 16:47:58 -070017#ifndef UPDATE_ENGINE_COMMON_DOWNLOAD_ACTION_H_
18#define UPDATE_ENGINE_COMMON_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"
rspangler@google.com49fdf182009-10-10 00:57:34 +000033
Darin Petkov7ed561b2011-10-04 02:59:03 -070034// The Download Action downloads a specified url to disk. The url should point
35// to an update in a delta payload format. The payload will be piped into a
Andrew de los Reyesf9185172010-05-03 11:07:05 -070036// DeltaPerformer that will apply the delta to the disk.
rspangler@google.com49fdf182009-10-10 00:57:34 +000037
rspangler@google.com49fdf182009-10-10 00:57:34 +000038namespace chromeos_update_engine {
39
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070040class DownloadActionDelegate {
41 public:
Alex Deymoe8948702014-11-11 21:44:45 -080042 virtual ~DownloadActionDelegate() = default;
43
Alex Deymo22ad8612015-11-20 17:59:11 -030044 // Called periodically after bytes are received. This method will be invoked
Alex Deymo542c19b2015-12-03 07:43:31 -030045 // only if the DownloadAction is running. |bytes_progressed| is the number of
46 // bytes downloaded since the last call of this method, |bytes_received|
47 // the number of bytes downloaded thus far and |total| is the number of bytes
48 // expected.
49 virtual void BytesReceived(uint64_t bytes_progressed,
50 uint64_t bytes_received,
51 uint64_t total) = 0;
52
53 // Returns whether the download should be canceled, in which case the
54 // |cancel_reason| error should be set to the reason why the download was
55 // canceled.
56 virtual bool ShouldCancel(ErrorCode* cancel_reason) = 0;
57
58 // Called once the complete payload has been downloaded. Note that any errors
59 // while applying or downloading the partial payload will result in this
60 // method not being called.
61 virtual void DownloadComplete() = 0;
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070062};
63
Darin Petkov73058b42010-10-06 16:32:19 -070064class PrefsInterface;
rspangler@google.com49fdf182009-10-10 00:57:34 +000065
Amin Hassani008c4582019-01-13 16:22:47 -080066class DownloadAction : public InstallPlanAction, public HttpFetcherDelegate {
rspangler@google.com49fdf182009-10-10 00:57:34 +000067 public:
Alex Deymof2858572016-02-25 11:20:13 -080068 // Debugging/logging
69 static std::string StaticType() { return "DownloadAction"; }
70
rspangler@google.com49fdf182009-10-10 00:57:34 +000071 // Takes ownership of the passed in HttpFetcher. Useful for testing.
72 // A good calling pattern is:
Amin Hassani538bd592020-11-04 20:46:08 -080073 // DownloadAction(prefs, boot_contol, hardware,
Amin Hassani7ecda262017-07-11 17:10:50 -070074 // new WhateverHttpFetcher, false);
Jay Srinivasanf0572052012-10-23 18:12:56 -070075 DownloadAction(PrefsInterface* prefs,
Alex Deymo1b3556c2016-02-03 09:54:02 -080076 BootControlInterface* boot_control,
77 HardwareInterface* hardware,
Amin Hassani7ecda262017-07-11 17:10:50 -070078 HttpFetcher* http_fetcher,
Amin Hassanied37d682018-04-06 13:22:00 -070079 bool interactive);
Alex Deymo610277e2014-11-11 21:18:11 -080080 ~DownloadAction() override;
Alex Deymof2858572016-02-25 11:20:13 -080081
82 // InstallPlanAction overrides.
Alex Deymo610277e2014-11-11 21:18:11 -080083 void PerformAction() override;
Alex Deymof2858572016-02-25 11:20:13 -080084 void SuspendAction() override;
85 void ResumeAction() override;
Alex Deymo610277e2014-11-11 21:18:11 -080086 void TerminateProcessing() override;
Alex Deymof2858572016-02-25 11:20:13 -080087 std::string Type() const override { return StaticType(); }
rspangler@google.com49fdf182009-10-10 00:57:34 +000088
Andrew de los Reyesf9185172010-05-03 11:07:05 -070089 // Testing
Kelvin Zhangead9fd72020-12-03 12:57:35 -050090 void SetTestFileWriter(DeltaPerformer* writer) { writer_ = writer; }
Andrew de los Reyesf9185172010-05-03 11:07:05 -070091
Darin Petkov1023a602010-08-30 13:47:51 -070092 int GetHTTPResponseCode() { return http_fetcher_->http_response_code(); }
93
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070094 // HttpFetcherDelegate methods (see http_fetcher.h)
Amin Hassani0cd9d772018-07-31 23:55:43 -070095 bool ReceivedBytes(HttpFetcher* fetcher,
96 const void* bytes,
97 size_t length) override;
Alex Deymo610277e2014-11-11 21:18:11 -080098 void SeekToOffset(off_t offset) override;
Alex Deymo60ca1a72015-06-18 18:19:15 -070099 void TransferComplete(HttpFetcher* fetcher, bool successful) override;
100 void TransferTerminated(HttpFetcher* fetcher) override;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000101
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700102 DownloadActionDelegate* delegate() const { return delegate_; }
Amin Hassani008c4582019-01-13 16:22:47 -0800103 void set_delegate(DownloadActionDelegate* delegate) { delegate_ = delegate; }
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700104
Sen Jiang5ae865b2017-04-18 14:24:40 -0700105 void set_base_offset(int64_t base_offset) { base_offset_ = base_offset; }
106
Darin Petkov9b230572010-10-08 10:20:09 -0700107 HttpFetcher* http_fetcher() { return http_fetcher_.get(); }
108
rspangler@google.com49fdf182009-10-10 00:57:34 +0000109 private:
Kelvin Zhangcc011d32020-07-10 18:20:08 -0400110 // Attempt to load cached manifest data from prefs
111 // return true on success, false otherwise.
112 bool LoadCachedManifest(int64_t manifest_size);
113
Sen Jiang6c736682017-03-10 15:01:36 -0800114 // Start downloading the current payload using delta_performer.
115 void StartDownloading();
116
Sen Jiang0affc2c2017-02-10 15:55:05 -0800117 // Pointer to the current payload in install_plan_.payloads.
118 InstallPlan::Payload* payload_{nullptr};
119
Amin Hassani538bd592020-11-04 20:46:08 -0800120 // Required pointers.
Darin Petkov73058b42010-10-06 16:32:19 -0700121 PrefsInterface* prefs_;
Alex Deymo1b3556c2016-02-03 09:54:02 -0800122 BootControlInterface* boot_control_;
123 HardwareInterface* hardware_;
Darin Petkov73058b42010-10-06 16:32:19 -0700124
Sen Jiang5ae865b2017-04-18 14:24:40 -0700125 // Pointer to the MultiRangeHttpFetcher that does the http work.
126 std::unique_ptr<MultiRangeHttpFetcher> http_fetcher_;
Jay Srinivasanedce2832012-10-24 18:57:47 -0700127
Amin Hassani7ecda262017-07-11 17:10:50 -0700128 // If |true|, the update is user initiated (vs. periodic update checks). Hence
129 // the |delta_performer_| can decide not to use O_DSYNC flag for faster
130 // update.
Amin Hassanied37d682018-04-06 13:22:00 -0700131 bool interactive_;
Amin Hassani7ecda262017-07-11 17:10:50 -0700132
rspangler@google.com49fdf182009-10-10 00:57:34 +0000133 // The FileWriter that downloaded data should be written to. It will
Kelvin Zhangead9fd72020-12-03 12:57:35 -0500134 // either point to a writer for unittest or *delta_performer_.
135 DeltaPerformer* writer_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000136
Ben Chan02f7c1d2014-10-18 15:18:02 -0700137 std::unique_ptr<DeltaPerformer> delta_performer_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000138
Darin Petkov9ce452b2010-11-17 14:33:28 -0800139 // Used by TransferTerminated to figure if this action terminated itself or
140 // was terminated by the action processor.
David Zeuthena99981f2013-04-29 13:42:47 -0700141 ErrorCode code_;
Darin Petkov9ce452b2010-11-17 14:33:28 -0800142
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700143 // For reporting status to outsiders
144 DownloadActionDelegate* delegate_;
Aaron Wood9321f502017-09-07 11:18:54 -0700145 uint64_t bytes_received_{0}; // per file/range
146 uint64_t bytes_received_previous_payloads_{0};
Sen Jiang5ae865b2017-04-18 14:24:40 -0700147 uint64_t bytes_total_{0};
Alex Deymo22ad8612015-11-20 17:59:11 -0300148 bool download_active_{false};
Darin Petkov9d911fa2010-08-19 09:36:08 -0700149
Sen Jiang5ae865b2017-04-18 14:24:40 -0700150 // Loaded from prefs before downloading any payload.
151 size_t resume_payload_index_{0};
152
153 // Offset of the payload in the download URL, used by UpdateAttempterAndroid.
154 int64_t base_offset_{0};
155
rspangler@google.com49fdf182009-10-10 00:57:34 +0000156 DISALLOW_COPY_AND_ASSIGN(DownloadAction);
157};
158
159// We want to be sure that we're compiled with large file support on linux,
160// just in case we find ourselves downloading large images.
Alex Vakulenko0103c362016-01-20 07:56:15 -0800161static_assert(8 == sizeof(off_t), "off_t not 64 bit");
rspangler@google.com49fdf182009-10-10 00:57:34 +0000162
163} // namespace chromeos_update_engine
164
Amin Hassaniec7bc112020-10-29 16:47:58 -0700165#endif // UPDATE_ENGINE_COMMON_DOWNLOAD_ACTION_H_