libzypp  17.36.1
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 
42 namespace zyppng {
43 
44  class ProvidePrivate;
45  using AnyMap = std::unordered_map<std::string, boost::any>;
47 
52  {
53  public:
54 
56 
57  ProvideMediaHandle () = default;
58  ProvideMediaHandle ( Provide &parent, AttachedMediaInfo_Ptr mediaInfoRef );
59  std::shared_ptr<Provide> parent() const;
60  bool isValid () const;
61  std::string handle() const;
62  const zypp::Url &baseUrl() const;
63  const std::optional<zypp::Pathname> &localPath() const;
64  zyppng::AttachedMediaInfo_constPtr mediaInfo() const;
65  private:
66  ProvideWeakRef _parent;
67  AttachedMediaInfo_Ptr _mediaRef;
68  };
69 
75  {
76  public:
77 
78  struct Stats {
79  std::chrono::steady_clock::time_point _startTime;
80  std::chrono::steady_clock::time_point _lastPulseTime;
81  uint _itemsSinceStart = 0; //< How many items have been started since Provide::start() was called
82  uint _runningItems = 0; //< How many items are currently running
83  zypp::ByteCount _finishedBytes; //< The number of bytes that were finished completely
84  zypp::ByteCount _expectedBytes; //< The number of currently expected bytes
85  zypp::ByteCount _partialBytes; //< The number of bytes of items that were already partially downloaded but the item they belong to is not finished
86  zypp::ByteCount _perSecondSinceLastPulse; //< The download speed since the last pulse
87  zypp::ByteCount _perSecond; //< The download speed we are currently operating with
88  };
89 
90  ProvideStatus( ProvideRef parent );
91  virtual ~ProvideStatus(){}
92 
93  virtual void provideStart ();
94  virtual void provideDone (){}
95  virtual void itemStart ( ProvideItem &item ){}
96  virtual void itemDone ( ProvideItem &item );
97  virtual void itemFailed ( ProvideItem &item );
98  virtual void requestStart ( ProvideItem &item, uint32_t reqId, const zypp::Url &url, const AnyMap &extraData = {} ){}
99  virtual void requestDone ( ProvideItem &item, uint32_t reqId, const AnyMap &extraData = {} ){}
100  virtual void requestRedirect ( ProvideItem &item, uint32_t reqId, const zypp::Url &toUrl, const AnyMap &extraData = {} ){}
101  virtual void requestFailed ( ProvideItem &item, uint32_t reqId, const std::exception_ptr &err, const AnyMap &requestData = {} ){}
102  virtual void pulse ( );
103 
104  const Stats &stats() const;
105 
106  private:
108  ProvideWeakRef _provider;
109  };
110 
111  class Provide : public Base
112  {
114  template<class T> friend class ProvidePromise;
115  friend class ProvideItem;
116  friend class ProvideMediaHandle;
117  friend class ProvideStatus;
118  public:
119 
122  using Res = ProvideRes;
123 
124  static ProvideRef create( const zypp::Pathname &workDir = "" );
125 
130  expected<LazyMediaHandle> prepareMedia ( const std::vector<zypp::Url> &urls, const ProvideMediaSpec &request );
131  expected<LazyMediaHandle> prepareMedia ( const zypp::Url &url, const ProvideMediaSpec &request );
132 
134  AsyncOpRef<expected<MediaHandle>> attachMedia( const std::vector<zypp::Url> &urls, const ProvideMediaSpec &request );
135  AsyncOpRef<expected<MediaHandle>> attachMedia( const zypp::Url &url, const ProvideMediaSpec &request );
136 
137  AsyncOpRef<expected<ProvideRes>> provide( const std::vector<zypp::Url> &urls, const ProvideFileSpec &request );
138  AsyncOpRef<expected<ProvideRes>> provide( const zypp::Url &url, const ProvideFileSpec &request );
139  AsyncOpRef<expected<ProvideRes>> provide( const MediaHandle &attachHandle, const zypp::Pathname &fileName, const ProvideFileSpec &request );
140  AsyncOpRef<expected<ProvideRes>> provide( const LazyMediaHandle &attachHandle, const zypp::Pathname &fileName, const ProvideFileSpec &request );
141 
142 
146  AsyncOpRef<expected<zypp::CheckSum>> checksumForFile ( const zypp::Pathname &p, const std::string &algorithm );
147 
151  AsyncOpRef<expected<zypp::ManagedFile>> copyFile ( const zypp::Pathname &source, const zypp::Pathname &target );
152  AsyncOpRef<expected<zypp::ManagedFile>> copyFile ( ProvideRes &&source, const zypp::Pathname &target );
153 
154  void start();
155  void setWorkerPath( const zypp::Pathname &path );
156  bool isRunning() const;
157  bool ejectDevice ( const std::string &queueRef, const std::string &device );
158 
159  void setStatusTracker( ProvideStatusRef tracker );
160 
161  const zypp::Pathname &providerWorkdir () const;
162 
163  const zypp::media::CredManagerOptions &credManangerOptions () const;
164  void setCredManagerOptions( const zypp::media::CredManagerOptions & opt );
165 
166  SignalProxy<void()> sigIdle();
167 
168  enum Action {
169  ABORT, // abort and return error
170  RETRY, // retry
171  SKIP // abort and set skip request
172  };
173  using MediaChangeAction = std::optional<Action>;
174 
182 
187  SignalProxy< std::optional<zypp::media::AuthData> ( const zypp::Url &reqUrl, const std::string &triedUsername, const std::map<std::string, std::string> &extraValues ) > sigAuthRequired();
188 
189 
190  static auto copyResultToDest ( ProvideRef provider, const zypp::Pathname &targetPath ) {
191  return [ providerRef=std::move(provider), targetPath = targetPath ]( ProvideRes &&file ){
192  zypp::filesystem::assert_dir( targetPath.dirname () );
193  return providerRef->copyFile( std::move(file), targetPath );
194  };
195  }
196 
197 
198  private:
199  Provide( const zypp::Pathname &workDir );
200  };
201 
202 }
203 #endif
SignalProxy< void()> sigIdle()
Definition: provide.cc:1209
const zypp::Pathname & providerWorkdir() const
Definition: provide.cc:1193
zypp::ByteCount _finishedBytes
Definition: provide.h:83
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:1066
const zypp::media::CredManagerOptions & credManangerOptions() const
Definition: provide.cc:1198
virtual void pulse()
Definition: provide.cc:1260
ProvideStatus(ProvideRef parent)
Definition: provide.cc:1226
virtual void requestDone(ProvideItem &item, uint32_t reqId, const AnyMap &extraData={})
Definition: provide.h:99
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:95
AsyncOpRef< expected< zypp::CheckSum > > checksumForFile(const zypp::Pathname &p, const std::string &algorithm)
Definition: provide.cc:1120
AttachedMediaInfo_Ptr _mediaRef
Definition: provide.h:67
virtual void itemFailed(ProvideItem &item)
Definition: provide.cc:1250
virtual void requestFailed(ProvideItem &item, uint32_t reqId, const std::exception_ptr &err, const AnyMap &requestData={})
Definition: provide.h:101
DEFINE_PTR_TYPE(AttachedSyncMediaInfo)
virtual void provideDone()
Definition: provide.h:94
Definition: Arch.h:363
virtual ~ProvideStatus()
Definition: provide.h:91
static ProvideRef create(const zypp::Pathname &workDir="")
Definition: provide.cc:1002
zypp::ByteCount _perSecond
Definition: provide.h:87
std::chrono::steady_clock::time_point _lastPulseTime
Definition: provide.h:80
zyppng::AttachedMediaInfo_constPtr mediaInfo() const
Definition: provide.cc:990
const std::optional< zypp::Pathname > & localPath() const
Definition: provide.cc:982
void setCredManagerOptions(const zypp::media::CredManagerOptions &opt)
Definition: provide.cc:1204
expected< LazyMediaHandle > prepareMedia(const std::vector< zypp::Url > &urls, const ProvideMediaSpec &request)
Definition: provide.cc:1007
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:1214
virtual void requestRedirect(ProvideItem &item, uint32_t reqId, const zypp::Url &toUrl, const AnyMap &extraData={})
Definition: provide.h:100
zypp::ByteCount _expectedBytes
Definition: provide.h:84
virtual void itemDone(ProvideItem &item)
Definition: provide.cc:1237
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:1180
void setWorkerPath(const zypp::Pathname &path)
Definition: provide.cc:1175
ProvideWeakRef _provider
Definition: provide.h:108
std::optional< Action > MediaChangeAction
Definition: provide.h:173
virtual void provideStart()
Definition: provide.cc:1230
zypp::ByteCount _partialBytes
Definition: provide.h:85
Provide(const zypp::Pathname &workDir)
Definition: provide.cc:996
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:1043
const zypp::Url & baseUrl() const
Definition: provide.cc:974
zypp::ByteCount _perSecondSinceLastPulse
Definition: provide.h:86
ProvideWeakRef _parent
Definition: provide.h:66
void setStatusTracker(ProvideStatusRef tracker)
Definition: provide.cc:1188
std::chrono::steady_clock::time_point _startTime
Definition: provide.h:79
const Stats & stats() const
Definition: provide.cc:1255
AsyncOpRef< expected< zypp::ManagedFile > > copyFile(const zypp::Pathname &source, const zypp::Pathname &target)
Definition: provide.cc:1140
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:45
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:190
Url manipulation class.
Definition: Url.h:92
AsyncOpRef< expected< MediaHandle > > attachMediaIfNeeded(LazyMediaHandle lazyHandle)
Definition: provide.cc:1023
virtual void requestStart(ProvideItem &item, uint32_t reqId, const zypp::Url &url, const AnyMap &extraData={})
Definition: provide.h:98
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:1219