libzypp  17.37.5
provide.h
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
9 #ifndef ZYPP_MEDIA_PROVIDE_H_INCLUDED
10 #define ZYPP_MEDIA_PROVIDE_H_INCLUDED
11 
12 #include <zypp-core/fs/PathInfo.h>
13 #include <zypp-core/ManagedFile.h>
15 #include <zypp-core/zyppng/base/Base>
16 #include <zypp-core/zyppng/async/AsyncOp>
18 #include <zypp-core/ByteCount.h>
19 #include <zypp-media/ng/LazyMediaHandle>
20 #include <zypp-media/ng/ProvideFwd>
21 #include <zypp-media/ng/ProvideRes>
22 #include <zypp-media/auth/AuthData>
23 #include <boost/any.hpp>
24 
25 namespace zypp {
26  class Url;
27  namespace media {
28  struct CredManagerOptions;
29  }
30 }
31 
44 namespace zyppng {
45 
46  class ProvidePrivate;
47  using AnyMap = std::unordered_map<std::string, boost::any>;
49 
54  {
55  public:
56 
58 
59  ProvideMediaHandle () = default;
60  ProvideMediaHandle ( Provide &parent, AttachedMediaInfo_Ptr mediaInfoRef );
61  std::shared_ptr<Provide> parent() const;
62  bool isValid () const;
63  std::string handle() const;
64  const zypp::Url &baseUrl() const;
65  const std::vector<zypp::Url> &mirrors() const;
66  const std::optional<zypp::Pathname> &localPath() const;
67  zyppng::AttachedMediaInfo_constPtr mediaInfo() const;
68  private:
69  ProvideWeakRef _parent;
70  AttachedMediaInfo_Ptr _mediaRef;
71  };
72 
78  {
79  public:
80 
81  struct Stats {
82  std::chrono::steady_clock::time_point _startTime;
83  std::chrono::steady_clock::time_point _lastPulseTime;
84  uint _itemsSinceStart = 0; //< How many items have been started since Provide::start() was called
85  uint _runningItems = 0; //< How many items are currently running
86  zypp::ByteCount _finishedBytes; //< The number of bytes that were finished completely
87  zypp::ByteCount _expectedBytes; //< The number of currently expected bytes
88  zypp::ByteCount _partialBytes; //< The number of bytes of items that were already partially downloaded but the item they belong to is not finished
89  zypp::ByteCount _perSecondSinceLastPulse; //< The download speed since the last pulse
90  zypp::ByteCount _perSecond; //< The download speed we are currently operating with
91  };
92 
93  ProvideStatus( ProvideRef parent );
94  virtual ~ProvideStatus(){}
95 
96  virtual void provideStart ();
97  virtual void provideDone (){}
98  virtual void itemStart ( ProvideItem &item ){}
99  virtual void itemDone ( ProvideItem &item );
100  virtual void itemFailed ( ProvideItem &item );
101  virtual void requestStart ( ProvideItem &item, uint32_t reqId, const zypp::Url &url, const AnyMap &extraData = {} ){}
102  virtual void requestDone ( ProvideItem &item, uint32_t reqId, const AnyMap &extraData = {} ){}
103  virtual void requestRedirect ( ProvideItem &item, uint32_t reqId, const zypp::Url &toUrl, const AnyMap &extraData = {} ){}
104  virtual void requestFailed ( ProvideItem &item, uint32_t reqId, const std::exception_ptr &err, const AnyMap &requestData = {} ){}
105  virtual void pulse ( );
106 
107  const Stats &stats() const;
108 
109  private:
111  ProvideWeakRef _provider;
112  };
113 
114  class Provide : public Base
115  {
117  template<class T> friend class ProvidePromise;
118  friend class ProvideItem;
119  friend class ProvideMediaHandle;
120  friend class ProvideStatus;
121  public:
122 
125  using Res = ProvideRes;
126 
127  static ProvideRef create( const zypp::Pathname &workDir = "" );
128 
133  expected<LazyMediaHandle> prepareMedia ( const std::vector<zypp::Url> &urls, const ProvideMediaSpec &request );
134  expected<LazyMediaHandle> prepareMedia ( const zypp::Url &url, const ProvideMediaSpec &request );
135 
137  AsyncOpRef<expected<MediaHandle>> attachMedia( const std::vector<zypp::Url> &urls, const ProvideMediaSpec &request );
138  AsyncOpRef<expected<MediaHandle>> attachMedia( const zypp::Url &url, const ProvideMediaSpec &request );
139 
140  AsyncOpRef<expected<ProvideRes>> provide( const std::vector<zypp::Url> &urls, const ProvideFileSpec &request );
141  AsyncOpRef<expected<ProvideRes>> provide( const zypp::Url &url, const ProvideFileSpec &request );
142  AsyncOpRef<expected<ProvideRes>> provide( const MediaHandle &attachHandle, const zypp::Pathname &fileName, const ProvideFileSpec &request );
143  AsyncOpRef<expected<ProvideRes>> provide( const LazyMediaHandle &attachHandle, const zypp::Pathname &fileName, const ProvideFileSpec &request );
144 
145 
149  AsyncOpRef<expected<zypp::CheckSum>> checksumForFile ( const zypp::Pathname &p, const std::string &algorithm );
150 
154  AsyncOpRef<expected<zypp::ManagedFile>> copyFile ( const zypp::Pathname &source, const zypp::Pathname &target );
155  AsyncOpRef<expected<zypp::ManagedFile>> copyFile ( ProvideRes &&source, const zypp::Pathname &target );
156 
157  void start();
158  void setWorkerPath( const zypp::Pathname &path );
159  bool isRunning() const;
160  bool ejectDevice ( const std::string &queueRef, const std::string &device );
161 
162  void setStatusTracker( ProvideStatusRef tracker );
163 
164  const zypp::Pathname &providerWorkdir () const;
165 
166  const zypp::media::CredManagerOptions &credManangerOptions () const;
167  void setCredManagerOptions( const zypp::media::CredManagerOptions & opt );
168 
169  SignalProxy<void()> sigIdle();
170 
171  enum Action {
172  ABORT, // abort and return error
173  RETRY, // retry
174  SKIP // abort and set skip request
175  };
176  using MediaChangeAction = std::optional<Action>;
177 
185 
190  SignalProxy< std::optional<zypp::media::AuthData> ( const zypp::Url &reqUrl, const std::string &triedUsername, const std::map<std::string, std::string> &extraValues ) > sigAuthRequired();
191 
192 
193  static auto copyResultToDest ( ProvideRef provider, const zypp::Pathname &targetPath ) {
194  return [ providerRef=std::move(provider), targetPath = targetPath ]( ProvideRes &&file ){
195  zypp::filesystem::assert_dir( targetPath.dirname () );
196  return providerRef->copyFile( std::move(file), targetPath );
197  };
198  }
199 
200 
201  private:
202  Provide( const zypp::Pathname &workDir );
203  };
204 
205 }
206 #endif
SignalProxy< void()> sigIdle()
Definition: provide.cc:1226
const zypp::Pathname & providerWorkdir() const
Definition: provide.cc:1210
zypp::ByteCount _finishedBytes
Definition: provide.h:86
int assert_dir(const Pathname &path, unsigned mode)
Like &#39;mkdir -p&#39;.
Definition: PathInfo.cc:324
AsyncOpRef< expected< ProvideRes > > provide(const std::vector< zypp::Url > &urls, const ProvideFileSpec &request)
Definition: provide.cc:1074
const zypp::media::CredManagerOptions & credManangerOptions() const
Definition: provide.cc:1215
virtual void pulse()
Definition: provide.cc:1277
ProvideStatus(ProvideRef parent)
Definition: provide.cc:1243
const std::vector< zypp::Url > & mirrors() const
Definition: provide.cc:982
virtual void requestDone(ProvideItem &item, uint32_t reqId, const AnyMap &extraData={})
Definition: provide.h:102
Store and operate with byte count.
Definition: ByteCount.h:31
A ProvideRes object is a reference counted ownership of a resource in the cache provided by a Provide...
Definition: provideres.h:35
virtual void itemStart(ProvideItem &item)
Definition: provide.h:98
AsyncOpRef< expected< zypp::CheckSum > > checksumForFile(const zypp::Pathname &p, const std::string &algorithm)
Definition: provide.cc:1137
AttachedMediaInfo_Ptr _mediaRef
Definition: provide.h:70
virtual void itemFailed(ProvideItem &item)
Definition: provide.cc:1267
virtual void requestFailed(ProvideItem &item, uint32_t reqId, const std::exception_ptr &err, const AnyMap &requestData={})
Definition: provide.h:104
DEFINE_PTR_TYPE(AttachedSyncMediaInfo)
virtual void provideDone()
Definition: provide.h:97
Definition: Arch.h:363
virtual ~ProvideStatus()
Definition: provide.h:94
static ProvideRef create(const zypp::Pathname &workDir="")
Definition: provide.cc:1010
zypp::ByteCount _perSecond
Definition: provide.h:90
std::chrono::steady_clock::time_point _lastPulseTime
Definition: provide.h:83
zyppng::AttachedMediaInfo_constPtr mediaInfo() const
Definition: provide.cc:998
const std::optional< zypp::Pathname > & localPath() const
Definition: provide.cc:990
void setCredManagerOptions(const zypp::media::CredManagerOptions &opt)
Definition: provide.cc:1221
expected< LazyMediaHandle > prepareMedia(const std::vector< zypp::Url > &urls, const ProvideMediaSpec &request)
Definition: provide.cc:1015
bool isRunning() const
SignalProxy< MediaChangeAction(const std::string &queueRef, const std::string &label, const int32_t mediaNr, const std::vector< std::string > &devices, const std::optional< std::string > &desc)> sigMediaChangeRequested()
Definition: provide.cc:1231
virtual void requestRedirect(ProvideItem &item, uint32_t reqId, const zypp::Url &toUrl, const AnyMap &extraData={})
Definition: provide.h:103
zypp::ByteCount _expectedBytes
Definition: provide.h:87
virtual void itemDone(ProvideItem &item)
Definition: provide.cc:1254
Pathname dirname() const
Return all but the last component od this path.
Definition: Pathname.h:126
bool ejectDevice(const std::string &queueRef, const std::string &device)
Definition: provide.cc:1197
void setWorkerPath(const zypp::Pathname &path)
Definition: provide.cc:1192
ProvideWeakRef _provider
Definition: provide.h:111
std::optional< Action > MediaChangeAction
Definition: provide.h:176
virtual void provideStart()
Definition: provide.cc:1247
zypp::ByteCount _partialBytes
Definition: provide.h:88
Provide(const zypp::Pathname &workDir)
Definition: provide.cc:1004
zypp::Url Url
Definition: url.h:15
std::shared_ptr< AsyncOp< T > > AsyncOpRef
Definition: asyncop.h:255
#define ZYPP_DECLARE_PRIVATE(Class)
Definition: zyppglobal.h:87
AsyncOpRef< expected< MediaHandle > > attachMedia(const std::vector< zypp::Url > &urls, const ProvideMediaSpec &request)
Definition: provide.cc:1051
const zypp::Url & baseUrl() const
Definition: provide.cc:974
zypp::ByteCount _perSecondSinceLastPulse
Definition: provide.h:89
ProvideWeakRef _parent
Definition: provide.h:69
void setStatusTracker(ProvideStatusRef tracker)
Definition: provide.cc:1205
std::chrono::steady_clock::time_point _startTime
Definition: provide.h:82
const Stats & stats() const
Definition: provide.cc:1272
AsyncOpRef< expected< zypp::ManagedFile > > copyFile(const zypp::Pathname &source, const zypp::Pathname &target)
Definition: provide.cc:1157
constexpr std::string_view device("device")
std::string handle() const
Definition: provide.cc:967
std::unordered_map< std::string, boost::any > AnyMap
Definition: provide.h:47
Easy-to use interface to the ZYPP dependency resolver.
Definition: Application.cc:19
std::shared_ptr< Provide > parent() const
Definition: provide.cc:957
static auto copyResultToDest(ProvideRef provider, const zypp::Pathname &targetPath)
Definition: provide.h:193
Url manipulation class.
Definition: Url.h:92
AsyncOpRef< expected< MediaHandle > > attachMediaIfNeeded(LazyMediaHandle lazyHandle)
Definition: provide.cc:1031
virtual void requestStart(ProvideItem &item, uint32_t reqId, const zypp::Url &url, const AnyMap &extraData={})
Definition: provide.h:101
SignalProxy< std::optional< zypp::media::AuthData > const zypp::Url &reqUrl, const std::string &triedUsername, const std::map< std::string, std::string > &extraValues) > sigAuthRequired()
Definition: provide.cc:1236