blob: 8e3809ab70609a43fac0d56ffd00ca5d62159c38 [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:
Darin Petkov9d911fa2010-08-19 09:36:08 -070031 // Called right before starting the download with |active| set to
32 // true. Called after completing the download with |active| set to
33 // false.
34 virtual void SetDownloadStatus(bool active) = 0;
35
36 // Called periodically after bytes are received. This method will be
37 // invoked only if the download is active. |bytes_received| is the
38 // number of bytes downloaded thus far. |total| is the number of
39 // bytes expected.
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070040 virtual void BytesReceived(uint64_t bytes_received, uint64_t total) = 0;
41};
42
Darin Petkov73058b42010-10-06 16:32:19 -070043class PrefsInterface;
rspangler@google.com49fdf182009-10-10 00:57:34 +000044
Chris Sosad317e402013-06-12 13:47:09 -070045class DownloadAction : public InstallPlanAction,
rspangler@google.com49fdf182009-10-10 00:57:34 +000046 public HttpFetcherDelegate {
47 public:
48 // Takes ownership of the passed in HttpFetcher. Useful for testing.
49 // A good calling pattern is:
Jay Srinivasanf0572052012-10-23 18:12:56 -070050 // DownloadAction(prefs, system_state, new WhateverHttpFetcher);
51 DownloadAction(PrefsInterface* prefs,
52 SystemState* system_state,
53 HttpFetcher* http_fetcher);
rspangler@google.com49fdf182009-10-10 00:57:34 +000054 virtual ~DownloadAction();
rspangler@google.com49fdf182009-10-10 00:57:34 +000055 void PerformAction();
56 void TerminateProcessing();
57
Andrew de los Reyesf9185172010-05-03 11:07:05 -070058 // Testing
59 void SetTestFileWriter(FileWriter* writer) {
60 writer_ = writer;
61 }
62
Darin Petkov1023a602010-08-30 13:47:51 -070063 int GetHTTPResponseCode() { return http_fetcher_->http_response_code(); }
64
rspangler@google.com49fdf182009-10-10 00:57:34 +000065 // Debugging/logging
adlr@google.comc98a7ed2009-12-04 18:54:03 +000066 static std::string StaticType() { return "DownloadAction"; }
67 std::string Type() const { return StaticType(); }
rspangler@google.com49fdf182009-10-10 00:57:34 +000068
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070069 // HttpFetcherDelegate methods (see http_fetcher.h)
rspangler@google.com49fdf182009-10-10 00:57:34 +000070 virtual void ReceivedBytes(HttpFetcher *fetcher,
71 const char* bytes, int length);
Andrew de los Reyes34e41a12010-10-26 20:07:58 -070072 virtual void SeekToOffset(off_t offset);
rspangler@google.com49fdf182009-10-10 00:57:34 +000073 virtual void TransferComplete(HttpFetcher *fetcher, bool successful);
Darin Petkov9ce452b2010-11-17 14:33:28 -080074 virtual void TransferTerminated(HttpFetcher *fetcher);
rspangler@google.com49fdf182009-10-10 00:57:34 +000075
Darin Petkovf42cc1c2010-09-01 09:03:02 -070076 DownloadActionDelegate* delegate() const { return delegate_; }
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070077 void set_delegate(DownloadActionDelegate* delegate) {
78 delegate_ = delegate;
79 }
80
Darin Petkov9b230572010-10-08 10:20:09 -070081 HttpFetcher* http_fetcher() { return http_fetcher_.get(); }
82
David Zeuthen8f191b22013-08-06 12:27:50 -070083 // Returns the p2p file id for the file being written or the empty
84 // string if we're not writing to a p2p file.
Alex Vakulenkod2779df2014-06-16 13:19:00 -070085 std::string p2p_file_id() { return p2p_file_id_; }
David Zeuthen8f191b22013-08-06 12:27:50 -070086
rspangler@google.com49fdf182009-10-10 00:57:34 +000087 private:
David Zeuthen8f191b22013-08-06 12:27:50 -070088 // Closes the file descriptor for the p2p file being written and
89 // clears |p2p_file_id_| to indicate that we're no longer sharing
90 // the file. If |delete_p2p_file| is True, also deletes the file.
91 // If there is no p2p file descriptor, this method does nothing.
92 void CloseP2PSharingFd(bool delete_p2p_file);
93
94 // Starts sharing the p2p file. Must be called before
95 // WriteToP2PFile(). Returns True if this worked.
96 bool SetupP2PSharingFd();
97
98 // Writes |length| bytes of payload from |data| into |file_offset|
99 // of the p2p file. Also does sanity checks; for example ensures we
100 // don't end up with a file with holes in it.
101 //
102 // This method does nothing if SetupP2PSharingFd() hasn't been
103 // called or if CloseP2PSharingFd() has been called.
104 void WriteToP2PFile(const char *data, size_t length, off_t file_offset);
105
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700106 // The InstallPlan passed in
107 InstallPlan install_plan_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000108
Darin Petkov73058b42010-10-06 16:32:19 -0700109 // Update Engine preference store.
110 PrefsInterface* prefs_;
111
Jay Srinivasanedce2832012-10-24 18:57:47 -0700112 // Global context for the system.
113 SystemState* system_state_;
114
115 // Pointer to the HttpFetcher that does the http work.
Ben Chan02f7c1d2014-10-18 15:18:02 -0700116 std::unique_ptr<HttpFetcher> http_fetcher_;
Jay Srinivasanedce2832012-10-24 18:57:47 -0700117
rspangler@google.com49fdf182009-10-10 00:57:34 +0000118 // The FileWriter that downloaded data should be written to. It will
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700119 // either point to *decompressing_file_writer_ or *delta_performer_.
rspangler@google.com49fdf182009-10-10 00:57:34 +0000120 FileWriter* writer_;
121
Ben Chan02f7c1d2014-10-18 15:18:02 -0700122 std::unique_ptr<DeltaPerformer> delta_performer_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000123
Darin Petkov9ce452b2010-11-17 14:33:28 -0800124 // Used by TransferTerminated to figure if this action terminated itself or
125 // was terminated by the action processor.
David Zeuthena99981f2013-04-29 13:42:47 -0700126 ErrorCode code_;
Darin Petkov9ce452b2010-11-17 14:33:28 -0800127
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700128 // For reporting status to outsiders
129 DownloadActionDelegate* delegate_;
130 uint64_t bytes_received_;
Darin Petkov9d911fa2010-08-19 09:36:08 -0700131
David Zeuthen8f191b22013-08-06 12:27:50 -0700132 // The file-id for the file we're sharing or the empty string
133 // if we're not using p2p to share.
134 std::string p2p_file_id_;
135
136 // The file descriptor for the p2p file used for caching the payload or -1
137 // if we're not using p2p to share.
138 int p2p_sharing_fd_;
139
140 // Set to |false| if p2p file is not visible.
141 bool p2p_visible_;
142
rspangler@google.com49fdf182009-10-10 00:57:34 +0000143 DISALLOW_COPY_AND_ASSIGN(DownloadAction);
144};
145
146// We want to be sure that we're compiled with large file support on linux,
147// just in case we find ourselves downloading large images.
148COMPILE_ASSERT(8 == sizeof(off_t), off_t_not_64_bit);
149
150} // namespace chromeos_update_engine
151
Gilad Arnoldcf175a02014-07-10 16:48:47 -0700152#endif // UPDATE_ENGINE_DOWNLOAD_ACTION_H_