blob: 3cf7e6ad5ec179cef3f034891ee6af8bb75e44ae [file] [log] [blame]
Darin Petkov7ed561b2011-10-04 02:59:03 -07001// Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
rspangler@google.com49fdf182009-10-10 00:57:34 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Gilad Arnoldcf175a02014-07-10 16:48:47 -07005#ifndef UPDATE_ENGINE_DOWNLOAD_ACTION_H_
6#define UPDATE_ENGINE_DOWNLOAD_ACTION_H_
rspangler@google.com49fdf182009-10-10 00:57:34 +00007
rspangler@google.com49fdf182009-10-10 00:57:34 +00008#include <fcntl.h>
Alex Vakulenko44cab302014-07-23 13:12:15 -07009#include <sys/stat.h>
10#include <sys/types.h>
rspangler@google.com49fdf182009-10-10 00:57:34 +000011
Ben Chan02f7c1d2014-10-18 15:18:02 -070012#include <memory>
rspangler@google.com49fdf182009-10-10 00:57:34 +000013#include <string>
14
15#include <curl/curl.h>
16
rspangler@google.com49fdf182009-10-10 00:57:34 +000017#include "update_engine/action.h"
Andrew de los Reyesf9185172010-05-03 11:07:05 -070018#include "update_engine/delta_performer.h"
rspangler@google.com49fdf182009-10-10 00:57:34 +000019#include "update_engine/http_fetcher.h"
adlr@google.comc98a7ed2009-12-04 18:54:03 +000020#include "update_engine/install_plan.h"
Jay Srinivasanf0572052012-10-23 18:12:56 -070021#include "update_engine/system_state.h"
rspangler@google.com49fdf182009-10-10 00:57:34 +000022
Darin Petkov7ed561b2011-10-04 02:59:03 -070023// The Download Action downloads a specified url to disk. The url should point
24// to an update in a delta payload format. The payload will be piped into a
Andrew de los Reyesf9185172010-05-03 11:07:05 -070025// DeltaPerformer that will apply the delta to the disk.
rspangler@google.com49fdf182009-10-10 00:57:34 +000026
rspangler@google.com49fdf182009-10-10 00:57:34 +000027namespace chromeos_update_engine {
28
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070029class DownloadActionDelegate {
30 public:
Alex Deymoe8948702014-11-11 21:44:45 -080031 virtual ~DownloadActionDelegate() = default;
32
Darin Petkov9d911fa2010-08-19 09:36:08 -070033 // Called right before starting the download with |active| set to
34 // true. Called after completing the download with |active| set to
35 // false.
36 virtual void SetDownloadStatus(bool active) = 0;
37
38 // Called periodically after bytes are received. This method will be
39 // invoked only if the download is active. |bytes_received| is the
40 // number of bytes downloaded thus far. |total| is the number of
41 // bytes expected.
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070042 virtual void BytesReceived(uint64_t bytes_received, uint64_t total) = 0;
43};
44
Darin Petkov73058b42010-10-06 16:32:19 -070045class PrefsInterface;
rspangler@google.com49fdf182009-10-10 00:57:34 +000046
Chris Sosad317e402013-06-12 13:47:09 -070047class DownloadAction : public InstallPlanAction,
rspangler@google.com49fdf182009-10-10 00:57:34 +000048 public HttpFetcherDelegate {
49 public:
50 // Takes ownership of the passed in HttpFetcher. Useful for testing.
51 // A good calling pattern is:
Jay Srinivasanf0572052012-10-23 18:12:56 -070052 // DownloadAction(prefs, system_state, new WhateverHttpFetcher);
53 DownloadAction(PrefsInterface* prefs,
54 SystemState* system_state,
55 HttpFetcher* http_fetcher);
Alex Deymo610277e2014-11-11 21:18:11 -080056 ~DownloadAction() override;
57 void PerformAction() override;
58 void TerminateProcessing() override;
rspangler@google.com49fdf182009-10-10 00:57:34 +000059
Andrew de los Reyesf9185172010-05-03 11:07:05 -070060 // Testing
61 void SetTestFileWriter(FileWriter* writer) {
62 writer_ = writer;
63 }
64
Darin Petkov1023a602010-08-30 13:47:51 -070065 int GetHTTPResponseCode() { return http_fetcher_->http_response_code(); }
66
rspangler@google.com49fdf182009-10-10 00:57:34 +000067 // Debugging/logging
adlr@google.comc98a7ed2009-12-04 18:54:03 +000068 static std::string StaticType() { return "DownloadAction"; }
Alex Deymo610277e2014-11-11 21:18:11 -080069 std::string Type() const override { return StaticType(); }
rspangler@google.com49fdf182009-10-10 00:57:34 +000070
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070071 // HttpFetcherDelegate methods (see http_fetcher.h)
Alex Deymo610277e2014-11-11 21:18:11 -080072 void ReceivedBytes(HttpFetcher *fetcher,
73 const char* bytes, int length) override;
74 void SeekToOffset(off_t offset) override;
75 void TransferComplete(HttpFetcher *fetcher, bool successful) override;
76 void TransferTerminated(HttpFetcher *fetcher) override;
rspangler@google.com49fdf182009-10-10 00:57:34 +000077
Darin Petkovf42cc1c2010-09-01 09:03:02 -070078 DownloadActionDelegate* delegate() const { return delegate_; }
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070079 void set_delegate(DownloadActionDelegate* delegate) {
80 delegate_ = delegate;
81 }
82
Darin Petkov9b230572010-10-08 10:20:09 -070083 HttpFetcher* http_fetcher() { return http_fetcher_.get(); }
84
David Zeuthen8f191b22013-08-06 12:27:50 -070085 // Returns the p2p file id for the file being written or the empty
86 // string if we're not writing to a p2p file.
Alex Vakulenkod2779df2014-06-16 13:19:00 -070087 std::string p2p_file_id() { return p2p_file_id_; }
David Zeuthen8f191b22013-08-06 12:27:50 -070088
rspangler@google.com49fdf182009-10-10 00:57:34 +000089 private:
David Zeuthen8f191b22013-08-06 12:27:50 -070090 // Closes the file descriptor for the p2p file being written and
91 // clears |p2p_file_id_| to indicate that we're no longer sharing
92 // the file. If |delete_p2p_file| is True, also deletes the file.
93 // If there is no p2p file descriptor, this method does nothing.
94 void CloseP2PSharingFd(bool delete_p2p_file);
95
96 // Starts sharing the p2p file. Must be called before
97 // WriteToP2PFile(). Returns True if this worked.
98 bool SetupP2PSharingFd();
99
100 // Writes |length| bytes of payload from |data| into |file_offset|
101 // of the p2p file. Also does sanity checks; for example ensures we
102 // don't end up with a file with holes in it.
103 //
104 // This method does nothing if SetupP2PSharingFd() hasn't been
105 // called or if CloseP2PSharingFd() has been called.
106 void WriteToP2PFile(const char *data, size_t length, off_t file_offset);
107
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700108 // The InstallPlan passed in
109 InstallPlan install_plan_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000110
Darin Petkov73058b42010-10-06 16:32:19 -0700111 // Update Engine preference store.
112 PrefsInterface* prefs_;
113
Jay Srinivasanedce2832012-10-24 18:57:47 -0700114 // Global context for the system.
115 SystemState* system_state_;
116
117 // Pointer to the HttpFetcher that does the http work.
Ben Chan02f7c1d2014-10-18 15:18:02 -0700118 std::unique_ptr<HttpFetcher> http_fetcher_;
Jay Srinivasanedce2832012-10-24 18:57:47 -0700119
rspangler@google.com49fdf182009-10-10 00:57:34 +0000120 // The FileWriter that downloaded data should be written to. It will
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700121 // either point to *decompressing_file_writer_ or *delta_performer_.
rspangler@google.com49fdf182009-10-10 00:57:34 +0000122 FileWriter* writer_;
123
Ben Chan02f7c1d2014-10-18 15:18:02 -0700124 std::unique_ptr<DeltaPerformer> delta_performer_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000125
Darin Petkov9ce452b2010-11-17 14:33:28 -0800126 // Used by TransferTerminated to figure if this action terminated itself or
127 // was terminated by the action processor.
David Zeuthena99981f2013-04-29 13:42:47 -0700128 ErrorCode code_;
Darin Petkov9ce452b2010-11-17 14:33:28 -0800129
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700130 // For reporting status to outsiders
131 DownloadActionDelegate* delegate_;
132 uint64_t bytes_received_;
Darin Petkov9d911fa2010-08-19 09:36:08 -0700133
David Zeuthen8f191b22013-08-06 12:27:50 -0700134 // The file-id for the file we're sharing or the empty string
135 // if we're not using p2p to share.
136 std::string p2p_file_id_;
137
138 // The file descriptor for the p2p file used for caching the payload or -1
139 // if we're not using p2p to share.
140 int p2p_sharing_fd_;
141
142 // Set to |false| if p2p file is not visible.
143 bool p2p_visible_;
144
rspangler@google.com49fdf182009-10-10 00:57:34 +0000145 DISALLOW_COPY_AND_ASSIGN(DownloadAction);
146};
147
148// We want to be sure that we're compiled with large file support on linux,
149// just in case we find ourselves downloading large images.
150COMPILE_ASSERT(8 == sizeof(off_t), off_t_not_64_bit);
151
152} // namespace chromeos_update_engine
153
Gilad Arnoldcf175a02014-07-10 16:48:47 -0700154#endif // UPDATE_ENGINE_DOWNLOAD_ACTION_H_