Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // |
| 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.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 16 | |
Amin Hassani | ec7bc11 | 2020-10-29 16:47:58 -0700 | [diff] [blame] | 17 | #ifndef UPDATE_ENGINE_COMMON_DOWNLOAD_ACTION_H_ |
| 18 | #define UPDATE_ENGINE_COMMON_DOWNLOAD_ACTION_H_ |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 19 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 20 | #include <fcntl.h> |
Alex Vakulenko | 44cab30 | 2014-07-23 13:12:15 -0700 | [diff] [blame] | 21 | #include <sys/stat.h> |
| 22 | #include <sys/types.h> |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 23 | |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 24 | #include <memory> |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 25 | #include <string> |
| 26 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 27 | #include "update_engine/common/action.h" |
Alex Deymo | 1b3556c | 2016-02-03 09:54:02 -0800 | [diff] [blame] | 28 | #include "update_engine/common/boot_control_interface.h" |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 29 | #include "update_engine/common/http_fetcher.h" |
Sen Jiang | 5ae865b | 2017-04-18 14:24:40 -0700 | [diff] [blame] | 30 | #include "update_engine/common/multi_range_http_fetcher.h" |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 31 | #include "update_engine/payload_consumer/delta_performer.h" |
| 32 | #include "update_engine/payload_consumer/install_plan.h" |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 33 | |
Darin Petkov | 7ed561b | 2011-10-04 02:59:03 -0700 | [diff] [blame] | 34 | // 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 Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 36 | // DeltaPerformer that will apply the delta to the disk. |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 37 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 38 | namespace chromeos_update_engine { |
| 39 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 40 | class DownloadActionDelegate { |
| 41 | public: |
Alex Deymo | e894870 | 2014-11-11 21:44:45 -0800 | [diff] [blame] | 42 | virtual ~DownloadActionDelegate() = default; |
| 43 | |
Alex Deymo | 22ad861 | 2015-11-20 17:59:11 -0300 | [diff] [blame] | 44 | // Called periodically after bytes are received. This method will be invoked |
Alex Deymo | 542c19b | 2015-12-03 07:43:31 -0300 | [diff] [blame] | 45 | // 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 Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 62 | }; |
| 63 | |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 64 | class PrefsInterface; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 65 | |
Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame] | 66 | class DownloadAction : public InstallPlanAction, public HttpFetcherDelegate { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 67 | public: |
Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame] | 68 | // Debugging/logging |
| 69 | static std::string StaticType() { return "DownloadAction"; } |
| 70 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 71 | // Takes ownership of the passed in HttpFetcher. Useful for testing. |
| 72 | // A good calling pattern is: |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame^] | 73 | // DownloadAction(prefs, boot_contol, hardware, |
Amin Hassani | 7ecda26 | 2017-07-11 17:10:50 -0700 | [diff] [blame] | 74 | // new WhateverHttpFetcher, false); |
Jay Srinivasan | f057205 | 2012-10-23 18:12:56 -0700 | [diff] [blame] | 75 | DownloadAction(PrefsInterface* prefs, |
Alex Deymo | 1b3556c | 2016-02-03 09:54:02 -0800 | [diff] [blame] | 76 | BootControlInterface* boot_control, |
| 77 | HardwareInterface* hardware, |
Amin Hassani | 7ecda26 | 2017-07-11 17:10:50 -0700 | [diff] [blame] | 78 | HttpFetcher* http_fetcher, |
Amin Hassani | ed37d68 | 2018-04-06 13:22:00 -0700 | [diff] [blame] | 79 | bool interactive); |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 80 | ~DownloadAction() override; |
Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame] | 81 | |
| 82 | // InstallPlanAction overrides. |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 83 | void PerformAction() override; |
Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame] | 84 | void SuspendAction() override; |
| 85 | void ResumeAction() override; |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 86 | void TerminateProcessing() override; |
Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame] | 87 | std::string Type() const override { return StaticType(); } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 88 | |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 89 | // Testing |
Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame] | 90 | void SetTestFileWriter(FileWriter* writer) { writer_ = writer; } |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 91 | |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 92 | int GetHTTPResponseCode() { return http_fetcher_->http_response_code(); } |
| 93 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 94 | // HttpFetcherDelegate methods (see http_fetcher.h) |
Amin Hassani | 0cd9d77 | 2018-07-31 23:55:43 -0700 | [diff] [blame] | 95 | bool ReceivedBytes(HttpFetcher* fetcher, |
| 96 | const void* bytes, |
| 97 | size_t length) override; |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 98 | void SeekToOffset(off_t offset) override; |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 99 | void TransferComplete(HttpFetcher* fetcher, bool successful) override; |
| 100 | void TransferTerminated(HttpFetcher* fetcher) override; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 101 | |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 102 | DownloadActionDelegate* delegate() const { return delegate_; } |
Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame] | 103 | void set_delegate(DownloadActionDelegate* delegate) { delegate_ = delegate; } |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 104 | |
Sen Jiang | 5ae865b | 2017-04-18 14:24:40 -0700 | [diff] [blame] | 105 | void set_base_offset(int64_t base_offset) { base_offset_ = base_offset; } |
| 106 | |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 107 | HttpFetcher* http_fetcher() { return http_fetcher_.get(); } |
| 108 | |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 109 | // Returns the p2p file id for the file being written or the empty |
| 110 | // string if we're not writing to a p2p file. |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 111 | std::string p2p_file_id() { return p2p_file_id_; } |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 112 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 113 | private: |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 114 | // Closes the file descriptor for the p2p file being written and |
| 115 | // clears |p2p_file_id_| to indicate that we're no longer sharing |
| 116 | // the file. If |delete_p2p_file| is True, also deletes the file. |
| 117 | // If there is no p2p file descriptor, this method does nothing. |
| 118 | void CloseP2PSharingFd(bool delete_p2p_file); |
| 119 | |
| 120 | // Starts sharing the p2p file. Must be called before |
| 121 | // WriteToP2PFile(). Returns True if this worked. |
| 122 | bool SetupP2PSharingFd(); |
| 123 | |
| 124 | // Writes |length| bytes of payload from |data| into |file_offset| |
Saint Chou | a9997f0 | 2020-07-29 10:14:56 +0000 | [diff] [blame] | 125 | // of the p2p file. Also does validation checks; for example ensures we |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 126 | // don't end up with a file with holes in it. |
| 127 | // |
| 128 | // This method does nothing if SetupP2PSharingFd() hasn't been |
| 129 | // called or if CloseP2PSharingFd() has been called. |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 130 | void WriteToP2PFile(const void* data, size_t length, off_t file_offset); |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 131 | |
Kelvin Zhang | cc011d3 | 2020-07-10 18:20:08 -0400 | [diff] [blame] | 132 | // Attempt to load cached manifest data from prefs |
| 133 | // return true on success, false otherwise. |
| 134 | bool LoadCachedManifest(int64_t manifest_size); |
| 135 | |
Sen Jiang | 6c73668 | 2017-03-10 15:01:36 -0800 | [diff] [blame] | 136 | // Start downloading the current payload using delta_performer. |
| 137 | void StartDownloading(); |
| 138 | |
Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 139 | // Pointer to the current payload in install_plan_.payloads. |
| 140 | InstallPlan::Payload* payload_{nullptr}; |
| 141 | |
Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame^] | 142 | // Required pointers. |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 143 | PrefsInterface* prefs_; |
Alex Deymo | 1b3556c | 2016-02-03 09:54:02 -0800 | [diff] [blame] | 144 | BootControlInterface* boot_control_; |
| 145 | HardwareInterface* hardware_; |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 146 | |
Sen Jiang | 5ae865b | 2017-04-18 14:24:40 -0700 | [diff] [blame] | 147 | // Pointer to the MultiRangeHttpFetcher that does the http work. |
| 148 | std::unique_ptr<MultiRangeHttpFetcher> http_fetcher_; |
Jay Srinivasan | edce283 | 2012-10-24 18:57:47 -0700 | [diff] [blame] | 149 | |
Amin Hassani | 7ecda26 | 2017-07-11 17:10:50 -0700 | [diff] [blame] | 150 | // If |true|, the update is user initiated (vs. periodic update checks). Hence |
| 151 | // the |delta_performer_| can decide not to use O_DSYNC flag for faster |
| 152 | // update. |
Amin Hassani | ed37d68 | 2018-04-06 13:22:00 -0700 | [diff] [blame] | 153 | bool interactive_; |
Amin Hassani | 7ecda26 | 2017-07-11 17:10:50 -0700 | [diff] [blame] | 154 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 155 | // The FileWriter that downloaded data should be written to. It will |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 156 | // either point to *decompressing_file_writer_ or *delta_performer_. |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 157 | FileWriter* writer_; |
| 158 | |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 159 | std::unique_ptr<DeltaPerformer> delta_performer_; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 160 | |
Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 161 | // Used by TransferTerminated to figure if this action terminated itself or |
| 162 | // was terminated by the action processor. |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 163 | ErrorCode code_; |
Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 164 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 165 | // For reporting status to outsiders |
| 166 | DownloadActionDelegate* delegate_; |
Aaron Wood | 9321f50 | 2017-09-07 11:18:54 -0700 | [diff] [blame] | 167 | uint64_t bytes_received_{0}; // per file/range |
| 168 | uint64_t bytes_received_previous_payloads_{0}; |
Sen Jiang | 5ae865b | 2017-04-18 14:24:40 -0700 | [diff] [blame] | 169 | uint64_t bytes_total_{0}; |
Alex Deymo | 22ad861 | 2015-11-20 17:59:11 -0300 | [diff] [blame] | 170 | bool download_active_{false}; |
Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 171 | |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 172 | // The file-id for the file we're sharing or the empty string |
| 173 | // if we're not using p2p to share. |
| 174 | std::string p2p_file_id_; |
| 175 | |
| 176 | // The file descriptor for the p2p file used for caching the payload or -1 |
| 177 | // if we're not using p2p to share. |
| 178 | int p2p_sharing_fd_; |
| 179 | |
| 180 | // Set to |false| if p2p file is not visible. |
| 181 | bool p2p_visible_; |
| 182 | |
Sen Jiang | 5ae865b | 2017-04-18 14:24:40 -0700 | [diff] [blame] | 183 | // Loaded from prefs before downloading any payload. |
| 184 | size_t resume_payload_index_{0}; |
| 185 | |
| 186 | // Offset of the payload in the download URL, used by UpdateAttempterAndroid. |
| 187 | int64_t base_offset_{0}; |
| 188 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 189 | DISALLOW_COPY_AND_ASSIGN(DownloadAction); |
| 190 | }; |
| 191 | |
| 192 | // We want to be sure that we're compiled with large file support on linux, |
| 193 | // just in case we find ourselves downloading large images. |
Alex Vakulenko | 0103c36 | 2016-01-20 07:56:15 -0800 | [diff] [blame] | 194 | static_assert(8 == sizeof(off_t), "off_t not 64 bit"); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 195 | |
| 196 | } // namespace chromeos_update_engine |
| 197 | |
Amin Hassani | ec7bc11 | 2020-10-29 16:47:58 -0700 | [diff] [blame] | 198 | #endif // UPDATE_ENGINE_COMMON_DOWNLOAD_ACTION_H_ |