• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 //
16 
17 #ifndef UPDATE_ENGINE_PAYLOAD_CONSUMER_DOWNLOAD_ACTION_H_
18 #define UPDATE_ENGINE_PAYLOAD_CONSUMER_DOWNLOAD_ACTION_H_
19 
20 #include <fcntl.h>
21 #include <sys/stat.h>
22 #include <sys/types.h>
23 
24 #include <memory>
25 #include <string>
26 
27 #include "update_engine/common/action.h"
28 #include "update_engine/common/boot_control_interface.h"
29 #include "update_engine/common/http_fetcher.h"
30 #include "update_engine/payload_consumer/delta_performer.h"
31 #include "update_engine/payload_consumer/install_plan.h"
32 #include "update_engine/system_state.h"
33 
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
36 // DeltaPerformer that will apply the delta to the disk.
37 
38 namespace chromeos_update_engine {
39 
40 class DownloadActionDelegate {
41  public:
42   virtual ~DownloadActionDelegate() = default;
43 
44   // Called periodically after bytes are received. This method will be invoked
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;
62 };
63 
64 class PrefsInterface;
65 
66 class DownloadAction : public InstallPlanAction,
67                        public HttpFetcherDelegate {
68  public:
69   // Debugging/logging
StaticType()70   static std::string StaticType() { return "DownloadAction"; }
71 
72   // Takes ownership of the passed in HttpFetcher. Useful for testing.
73   // A good calling pattern is:
74   // DownloadAction(prefs, boot_contol, hardware, system_state,
75   //                new WhateverHttpFetcher);
76   DownloadAction(PrefsInterface* prefs,
77                  BootControlInterface* boot_control,
78                  HardwareInterface* hardware,
79                  SystemState* system_state,
80                  HttpFetcher* http_fetcher);
81   ~DownloadAction() override;
82 
83   // InstallPlanAction overrides.
84   void PerformAction() override;
85   void SuspendAction() override;
86   void ResumeAction() override;
87   void TerminateProcessing() override;
Type()88   std::string Type() const override { return StaticType(); }
89 
90   // Testing
SetTestFileWriter(FileWriter * writer)91   void SetTestFileWriter(FileWriter* writer) {
92     writer_ = writer;
93   }
94 
GetHTTPResponseCode()95   int GetHTTPResponseCode() { return http_fetcher_->http_response_code(); }
96 
97   // HttpFetcherDelegate methods (see http_fetcher.h)
98   void ReceivedBytes(HttpFetcher* fetcher,
99                      const void* bytes, size_t length) override;
100   void SeekToOffset(off_t offset) override;
101   void TransferComplete(HttpFetcher* fetcher, bool successful) override;
102   void TransferTerminated(HttpFetcher* fetcher) override;
103 
delegate()104   DownloadActionDelegate* delegate() const { return delegate_; }
set_delegate(DownloadActionDelegate * delegate)105   void set_delegate(DownloadActionDelegate* delegate) {
106     delegate_ = delegate;
107   }
108 
http_fetcher()109   HttpFetcher* http_fetcher() { return http_fetcher_.get(); }
110 
111   // Returns the p2p file id for the file being written or the empty
112   // string if we're not writing to a p2p file.
p2p_file_id()113   std::string p2p_file_id() { return p2p_file_id_; }
114 
115  private:
116   // Closes the file descriptor for the p2p file being written and
117   // clears |p2p_file_id_| to indicate that we're no longer sharing
118   // the file. If |delete_p2p_file| is True, also deletes the file.
119   // If there is no p2p file descriptor, this method does nothing.
120   void CloseP2PSharingFd(bool delete_p2p_file);
121 
122   // Starts sharing the p2p file. Must be called before
123   // WriteToP2PFile(). Returns True if this worked.
124   bool SetupP2PSharingFd();
125 
126   // Writes |length| bytes of payload from |data| into |file_offset|
127   // of the p2p file. Also does sanity checks; for example ensures we
128   // don't end up with a file with holes in it.
129   //
130   // This method does nothing if SetupP2PSharingFd() hasn't been
131   // called or if CloseP2PSharingFd() has been called.
132   void WriteToP2PFile(const void* data, size_t length, off_t file_offset);
133 
134   // The InstallPlan passed in
135   InstallPlan install_plan_;
136 
137   // SystemState required pointers.
138   PrefsInterface* prefs_;
139   BootControlInterface* boot_control_;
140   HardwareInterface* hardware_;
141 
142   // Global context for the system.
143   SystemState* system_state_;
144 
145   // Pointer to the HttpFetcher that does the http work.
146   std::unique_ptr<HttpFetcher> http_fetcher_;
147 
148   // The FileWriter that downloaded data should be written to. It will
149   // either point to *decompressing_file_writer_ or *delta_performer_.
150   FileWriter* writer_;
151 
152   std::unique_ptr<DeltaPerformer> delta_performer_;
153 
154   // Used by TransferTerminated to figure if this action terminated itself or
155   // was terminated by the action processor.
156   ErrorCode code_;
157 
158   // For reporting status to outsiders
159   DownloadActionDelegate* delegate_;
160   uint64_t bytes_received_;
161   bool download_active_{false};
162 
163   // The file-id for the file we're sharing or the empty string
164   // if we're not using p2p to share.
165   std::string p2p_file_id_;
166 
167   // The file descriptor for the p2p file used for caching the payload or -1
168   // if we're not using p2p to share.
169   int p2p_sharing_fd_;
170 
171   // Set to |false| if p2p file is not visible.
172   bool p2p_visible_;
173 
174   DISALLOW_COPY_AND_ASSIGN(DownloadAction);
175 };
176 
177 // We want to be sure that we're compiled with large file support on linux,
178 // just in case we find ourselves downloading large images.
179 static_assert(8 == sizeof(off_t), "off_t not 64 bit");
180 
181 }  // namespace chromeos_update_engine
182 
183 #endif  // UPDATE_ENGINE_PAYLOAD_CONSUMER_DOWNLOAD_ACTION_H_
184