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