| 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 |  | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 17 | #ifndef UPDATE_ENGINE_PAYLOAD_CONSUMER_DOWNLOAD_ACTION_H_ | 
|  | 18 | #define UPDATE_ENGINE_PAYLOAD_CONSUMER_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 |  | 
|  | 27 | #include <curl/curl.h> | 
|  | 28 |  | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 29 | #include "update_engine/common/action.h" | 
|  | 30 | #include "update_engine/common/http_fetcher.h" | 
|  | 31 | #include "update_engine/payload_consumer/delta_performer.h" | 
|  | 32 | #include "update_engine/payload_consumer/install_plan.h" | 
| Jay Srinivasan | f057205 | 2012-10-23 18:12:56 -0700 | [diff] [blame] | 33 | #include "update_engine/system_state.h" | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 34 |  | 
| Darin Petkov | 7ed561b | 2011-10-04 02:59:03 -0700 | [diff] [blame] | 35 | // 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 Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 37 | // DeltaPerformer that will apply the delta to the disk. | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 38 |  | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 39 | namespace chromeos_update_engine { | 
|  | 40 |  | 
| Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 41 | class DownloadActionDelegate { | 
|  | 42 | public: | 
| Alex Deymo | e894870 | 2014-11-11 21:44:45 -0800 | [diff] [blame] | 43 | virtual ~DownloadActionDelegate() = default; | 
|  | 44 |  | 
| Alex Deymo | 22ad861 | 2015-11-20 17:59:11 -0300 | [diff] [blame] | 45 | // Called periodically after bytes are received. This method will be invoked | 
| Alex Deymo | 542c19b | 2015-12-03 07:43:31 -0300 | [diff] [blame] | 46 | // 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 Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 63 | }; | 
|  | 64 |  | 
| Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 65 | class PrefsInterface; | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 66 |  | 
| Chris Sosa | d317e40 | 2013-06-12 13:47:09 -0700 | [diff] [blame] | 67 | class DownloadAction : public InstallPlanAction, | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 68 | public HttpFetcherDelegate { | 
|  | 69 | public: | 
|  | 70 | // Takes ownership of the passed in HttpFetcher. Useful for testing. | 
|  | 71 | // A good calling pattern is: | 
| Jay Srinivasan | f057205 | 2012-10-23 18:12:56 -0700 | [diff] [blame] | 72 | // DownloadAction(prefs, system_state, new WhateverHttpFetcher); | 
|  | 73 | DownloadAction(PrefsInterface* prefs, | 
|  | 74 | SystemState* system_state, | 
|  | 75 | HttpFetcher* http_fetcher); | 
| Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 76 | ~DownloadAction() override; | 
|  | 77 | void PerformAction() override; | 
|  | 78 | void TerminateProcessing() override; | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 79 |  | 
| Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 80 | // Testing | 
|  | 81 | void SetTestFileWriter(FileWriter* writer) { | 
|  | 82 | writer_ = writer; | 
|  | 83 | } | 
|  | 84 |  | 
| Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 85 | int GetHTTPResponseCode() { return http_fetcher_->http_response_code(); } | 
|  | 86 |  | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 87 | // Debugging/logging | 
| adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 88 | static std::string StaticType() { return "DownloadAction"; } | 
| Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 89 | std::string Type() const override { return StaticType(); } | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 90 |  | 
| Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 91 | // HttpFetcherDelegate methods (see http_fetcher.h) | 
| Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 92 | void ReceivedBytes(HttpFetcher* fetcher, | 
| Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 93 | const void* bytes, size_t length) override; | 
| Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 94 | void SeekToOffset(off_t offset) override; | 
| Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 95 | void TransferComplete(HttpFetcher* fetcher, bool successful) override; | 
|  | 96 | void TransferTerminated(HttpFetcher* fetcher) override; | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 97 |  | 
| Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 98 | DownloadActionDelegate* delegate() const { return delegate_; } | 
| Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 99 | void set_delegate(DownloadActionDelegate* delegate) { | 
|  | 100 | delegate_ = delegate; | 
|  | 101 | } | 
|  | 102 |  | 
| Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 103 | HttpFetcher* http_fetcher() { return http_fetcher_.get(); } | 
|  | 104 |  | 
| David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 105 | // Returns the p2p file id for the file being written or the empty | 
|  | 106 | // string if we're not writing to a p2p file. | 
| Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 107 | std::string p2p_file_id() { return p2p_file_id_; } | 
| David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 108 |  | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 109 | private: | 
| David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 110 | // Closes the file descriptor for the p2p file being written and | 
|  | 111 | // clears |p2p_file_id_| to indicate that we're no longer sharing | 
|  | 112 | // the file. If |delete_p2p_file| is True, also deletes the file. | 
|  | 113 | // If there is no p2p file descriptor, this method does nothing. | 
|  | 114 | void CloseP2PSharingFd(bool delete_p2p_file); | 
|  | 115 |  | 
|  | 116 | // Starts sharing the p2p file. Must be called before | 
|  | 117 | // WriteToP2PFile(). Returns True if this worked. | 
|  | 118 | bool SetupP2PSharingFd(); | 
|  | 119 |  | 
|  | 120 | // Writes |length| bytes of payload from |data| into |file_offset| | 
|  | 121 | // of the p2p file. Also does sanity checks; for example ensures we | 
|  | 122 | // don't end up with a file with holes in it. | 
|  | 123 | // | 
|  | 124 | // This method does nothing if SetupP2PSharingFd() hasn't been | 
|  | 125 | // called or if CloseP2PSharingFd() has been called. | 
| Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 126 | void WriteToP2PFile(const void* data, size_t length, off_t file_offset); | 
| David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 127 |  | 
| Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 128 | // The InstallPlan passed in | 
|  | 129 | InstallPlan install_plan_; | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 130 |  | 
| Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 131 | // Update Engine preference store. | 
|  | 132 | PrefsInterface* prefs_; | 
|  | 133 |  | 
| Jay Srinivasan | edce283 | 2012-10-24 18:57:47 -0700 | [diff] [blame] | 134 | // Global context for the system. | 
|  | 135 | SystemState* system_state_; | 
|  | 136 |  | 
|  | 137 | // Pointer to the HttpFetcher that does the http work. | 
| Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 138 | std::unique_ptr<HttpFetcher> http_fetcher_; | 
| Jay Srinivasan | edce283 | 2012-10-24 18:57:47 -0700 | [diff] [blame] | 139 |  | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 140 | // 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] | 141 | // either point to *decompressing_file_writer_ or *delta_performer_. | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 142 | FileWriter* writer_; | 
|  | 143 |  | 
| Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 144 | std::unique_ptr<DeltaPerformer> delta_performer_; | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 145 |  | 
| Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 146 | // Used by TransferTerminated to figure if this action terminated itself or | 
|  | 147 | // was terminated by the action processor. | 
| David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 148 | ErrorCode code_; | 
| Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 149 |  | 
| Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 150 | // For reporting status to outsiders | 
|  | 151 | DownloadActionDelegate* delegate_; | 
|  | 152 | uint64_t bytes_received_; | 
| Alex Deymo | 22ad861 | 2015-11-20 17:59:11 -0300 | [diff] [blame] | 153 | bool download_active_{false}; | 
| Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 154 |  | 
| David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 155 | // The file-id for the file we're sharing or the empty string | 
|  | 156 | // if we're not using p2p to share. | 
|  | 157 | std::string p2p_file_id_; | 
|  | 158 |  | 
|  | 159 | // The file descriptor for the p2p file used for caching the payload or -1 | 
|  | 160 | // if we're not using p2p to share. | 
|  | 161 | int p2p_sharing_fd_; | 
|  | 162 |  | 
|  | 163 | // Set to |false| if p2p file is not visible. | 
|  | 164 | bool p2p_visible_; | 
|  | 165 |  | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 166 | DISALLOW_COPY_AND_ASSIGN(DownloadAction); | 
|  | 167 | }; | 
|  | 168 |  | 
|  | 169 | // We want to be sure that we're compiled with large file support on linux, | 
|  | 170 | // just in case we find ourselves downloading large images. | 
| Alex Vakulenko | 0103c36 | 2016-01-20 07:56:15 -0800 | [diff] [blame] | 171 | static_assert(8 == sizeof(off_t), "off_t not 64 bit"); | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 172 |  | 
|  | 173 | }  // namespace chromeos_update_engine | 
|  | 174 |  | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 175 | #endif  // UPDATE_ENGINE_PAYLOAD_CONSUMER_DOWNLOAD_ACTION_H_ |