blob: c0f06884083ccddc67dc0c1396e4a7559aefe977 [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
27#include <curl/curl.h>
28
Alex Deymo39910dc2015-11-09 17:04:30 -080029#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 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
Darin Petkov9d911fa2010-08-19 09:36:08 -070045 // Called right before starting the download with |active| set to
46 // true. Called after completing the download with |active| set to
47 // false.
48 virtual void SetDownloadStatus(bool active) = 0;
49
50 // Called periodically after bytes are received. This method will be
51 // invoked only if the download is active. |bytes_received| is the
52 // number of bytes downloaded thus far. |total| is the number of
53 // bytes expected.
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070054 virtual void BytesReceived(uint64_t bytes_received, uint64_t total) = 0;
55};
56
Darin Petkov73058b42010-10-06 16:32:19 -070057class PrefsInterface;
rspangler@google.com49fdf182009-10-10 00:57:34 +000058
Chris Sosad317e402013-06-12 13:47:09 -070059class DownloadAction : public InstallPlanAction,
rspangler@google.com49fdf182009-10-10 00:57:34 +000060 public HttpFetcherDelegate {
61 public:
62 // Takes ownership of the passed in HttpFetcher. Useful for testing.
63 // A good calling pattern is:
Jay Srinivasanf0572052012-10-23 18:12:56 -070064 // DownloadAction(prefs, system_state, new WhateverHttpFetcher);
65 DownloadAction(PrefsInterface* prefs,
66 SystemState* system_state,
67 HttpFetcher* http_fetcher);
Alex Deymo610277e2014-11-11 21:18:11 -080068 ~DownloadAction() override;
69 void PerformAction() override;
70 void TerminateProcessing() override;
rspangler@google.com49fdf182009-10-10 00:57:34 +000071
Andrew de los Reyesf9185172010-05-03 11:07:05 -070072 // Testing
73 void SetTestFileWriter(FileWriter* writer) {
74 writer_ = writer;
75 }
76
Darin Petkov1023a602010-08-30 13:47:51 -070077 int GetHTTPResponseCode() { return http_fetcher_->http_response_code(); }
78
rspangler@google.com49fdf182009-10-10 00:57:34 +000079 // Debugging/logging
adlr@google.comc98a7ed2009-12-04 18:54:03 +000080 static std::string StaticType() { return "DownloadAction"; }
Alex Deymo610277e2014-11-11 21:18:11 -080081 std::string Type() const override { return StaticType(); }
rspangler@google.com49fdf182009-10-10 00:57:34 +000082
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070083 // HttpFetcherDelegate methods (see http_fetcher.h)
Alex Deymo60ca1a72015-06-18 18:19:15 -070084 void ReceivedBytes(HttpFetcher* fetcher,
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080085 const void* bytes, size_t length) override;
Alex Deymo610277e2014-11-11 21:18:11 -080086 void SeekToOffset(off_t offset) override;
Alex Deymo60ca1a72015-06-18 18:19:15 -070087 void TransferComplete(HttpFetcher* fetcher, bool successful) override;
88 void TransferTerminated(HttpFetcher* fetcher) override;
rspangler@google.com49fdf182009-10-10 00:57:34 +000089
Darin Petkovf42cc1c2010-09-01 09:03:02 -070090 DownloadActionDelegate* delegate() const { return delegate_; }
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070091 void set_delegate(DownloadActionDelegate* delegate) {
92 delegate_ = delegate;
93 }
94
Darin Petkov9b230572010-10-08 10:20:09 -070095 HttpFetcher* http_fetcher() { return http_fetcher_.get(); }
96
David Zeuthen8f191b22013-08-06 12:27:50 -070097 // Returns the p2p file id for the file being written or the empty
98 // string if we're not writing to a p2p file.
Alex Vakulenkod2779df2014-06-16 13:19:00 -070099 std::string p2p_file_id() { return p2p_file_id_; }
David Zeuthen8f191b22013-08-06 12:27:50 -0700100
rspangler@google.com49fdf182009-10-10 00:57:34 +0000101 private:
David Zeuthen8f191b22013-08-06 12:27:50 -0700102 // Closes the file descriptor for the p2p file being written and
103 // clears |p2p_file_id_| to indicate that we're no longer sharing
104 // the file. If |delete_p2p_file| is True, also deletes the file.
105 // If there is no p2p file descriptor, this method does nothing.
106 void CloseP2PSharingFd(bool delete_p2p_file);
107
108 // Starts sharing the p2p file. Must be called before
109 // WriteToP2PFile(). Returns True if this worked.
110 bool SetupP2PSharingFd();
111
112 // Writes |length| bytes of payload from |data| into |file_offset|
113 // of the p2p file. Also does sanity checks; for example ensures we
114 // don't end up with a file with holes in it.
115 //
116 // This method does nothing if SetupP2PSharingFd() hasn't been
117 // called or if CloseP2PSharingFd() has been called.
Alex Deymo60ca1a72015-06-18 18:19:15 -0700118 void WriteToP2PFile(const void* data, size_t length, off_t file_offset);
David Zeuthen8f191b22013-08-06 12:27:50 -0700119
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700120 // The InstallPlan passed in
121 InstallPlan install_plan_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000122
Darin Petkov73058b42010-10-06 16:32:19 -0700123 // Update Engine preference store.
124 PrefsInterface* prefs_;
125
Jay Srinivasanedce2832012-10-24 18:57:47 -0700126 // Global context for the system.
127 SystemState* system_state_;
128
129 // Pointer to the HttpFetcher that does the http work.
Ben Chan02f7c1d2014-10-18 15:18:02 -0700130 std::unique_ptr<HttpFetcher> http_fetcher_;
Jay Srinivasanedce2832012-10-24 18:57:47 -0700131
rspangler@google.com49fdf182009-10-10 00:57:34 +0000132 // The FileWriter that downloaded data should be written to. It will
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700133 // either point to *decompressing_file_writer_ or *delta_performer_.
rspangler@google.com49fdf182009-10-10 00:57:34 +0000134 FileWriter* writer_;
135
Ben Chan02f7c1d2014-10-18 15:18:02 -0700136 std::unique_ptr<DeltaPerformer> delta_performer_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000137
Darin Petkov9ce452b2010-11-17 14:33:28 -0800138 // Used by TransferTerminated to figure if this action terminated itself or
139 // was terminated by the action processor.
David Zeuthena99981f2013-04-29 13:42:47 -0700140 ErrorCode code_;
Darin Petkov9ce452b2010-11-17 14:33:28 -0800141
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700142 // For reporting status to outsiders
143 DownloadActionDelegate* delegate_;
144 uint64_t bytes_received_;
Darin Petkov9d911fa2010-08-19 09:36:08 -0700145
David Zeuthen8f191b22013-08-06 12:27:50 -0700146 // The file-id for the file we're sharing or the empty string
147 // if we're not using p2p to share.
148 std::string p2p_file_id_;
149
150 // The file descriptor for the p2p file used for caching the payload or -1
151 // if we're not using p2p to share.
152 int p2p_sharing_fd_;
153
154 // Set to |false| if p2p file is not visible.
155 bool p2p_visible_;
156
rspangler@google.com49fdf182009-10-10 00:57:34 +0000157 DISALLOW_COPY_AND_ASSIGN(DownloadAction);
158};
159
160// We want to be sure that we're compiled with large file support on linux,
161// just in case we find ourselves downloading large images.
162COMPILE_ASSERT(8 == sizeof(off_t), off_t_not_64_bit);
163
164} // namespace chromeos_update_engine
165
Alex Deymo39910dc2015-11-09 17:04:30 -0800166#endif // UPDATE_ENGINE_PAYLOAD_CONSUMER_DOWNLOAD_ACTION_H_