libzypp  17.31.31
devicedriver.h
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
9 
10 #ifndef ZYPP_MEDIA_NG_WORKER_DEVICEDRIVER_H_INCLUDED
11 #define ZYPP_MEDIA_NG_WORKER_DEVICEDRIVER_H_INCLUDED
12 
13 #include <zypp-media/ng/ProvideFwd>
14 #include <zypp-media/ng/worker/ProvideWorker>
15 #include <zypp-media/ng/HeaderValueMap>
16 #include <zypp-media/Mount>
17 #include <zypp-core/zyppng/base/Signals>
18 #include <zypp-core/zyppng/base/Base>
19 #include <zypp-core/zyppng/base/zyppglobal.h>
20 #include <zypp-core/zyppng/pipelines/Expected>
21 #include <any>
22 #include <unordered_map>
23 
24 namespace zyppng::worker
25 {
26 
27  ZYPP_FWD_DECL_TYPE_WITH_REFS (DeviceDriver);
28 
29  struct Device
30  {
31  std::string _name;
32  unsigned int _maj_nr = 0;
33  unsigned int _min_nr = 0;
35  bool _ephemeral = false;
36  std::unordered_map<std::string, std::any> _properties = {};
37  };
38 
40  {
41  std::shared_ptr<Device> _dev;
43  };
44 
45  struct AttachError
46  {
47  AttachError ( const uint code, const std::string &reason, const bool transient, const HeaderValueMap &extra = {} );
48  AttachError ( const uint code, const bool transient, const zypp::Exception &e );
49 
50  uint _code;
51  std::string _reason;
52  bool _transient;
54  };
55 
56  using AttachResult = expected<void, AttachError>;
57 
64  class DeviceDriver : public zyppng::Base
65  {
66  public:
67 
68  DeviceDriver ( WorkerCaps::WorkerType wType );
69 
73  void setProvider ( ProvideWorkerWeakRef workerRef );
74 
78  virtual AttachResult mountDevice ( const uint32_t id, const zypp::Url &mediaUrl, const std::string &attachId, const std::string &label, const HeaderValueMap &extras ) = 0;
79 
80 
81 
82  virtual zyppng::expected<WorkerCaps> initialize(const zyppng::worker::Configuration &conf);
83 
84 
93  bool detachMedia ( const std::string &attachId );
94 
98  void releaseIdleDevices ();
99 
105  virtual void detectDevices();
106 
111  std::vector<std::shared_ptr<Device>> &knownDevices();
112 
117  const std::vector<std::shared_ptr<Device>> &knownDevices() const;
118 
122  std::unordered_map<std::string, AttachedMedia> &attachedMedia();
123 
128  virtual bool isVolatile () const;
129 
133  void setAttachRoot ( const zypp::Pathname &root );
134 
138  zypp::Pathname attachRoot () const;
139 
143  virtual void immediateShutdown();
144 
148  ProvideWorkerRef parentWorker () const;
149 
153  const zyppng::worker::Configuration &config() const;
154 
155  protected:
159  virtual void unmountDevice ( Device &dev );
160 
164  zyppng::expected<void> isDesiredMedium ( const zypp::Url &deviceUrl, const zypp::Pathname &mountPoint, const zyppng::MediaDataVerifierRef &verifier, uint mediaNr = 1 );
165 
166 
167  zypp::Pathname createAttachPoint(const zypp::Pathname &attach_root) const;
168  void removeAttachPoint ( const zypp::Pathname &attach_pt ) const;
169  bool checkAttached ( const zypp::Pathname &mountPoint, const std::function<bool( const zypp::media::MountEntry &)> predicate );
170 
174  static const std::function<bool( const zypp::media::MountEntry &)> devicePredicate ( unsigned int majNr, unsigned int minNr );
175 
179  static const std::function<bool( const zypp::media::MountEntry &)> fstypePredicate ( const std::string &src, const std::vector<std::string> &fstypes );
180 
184  static const std::function<bool( const zypp::media::MountEntry &)> bindMountPredicate ( const std::string &src );
185 
186  private:
187  WorkerCaps::WorkerType _wType;
189  time_t _attach_mtime = 0; //< Timestamp of the mtab we did read
191  std::vector<std::shared_ptr<Device>> _sysDevs;
192  std::unordered_map<std::string, AttachedMedia> _attachedMedia;
193  ProvideWorkerWeakRef _parentWorker;
194  };
195 
196 
197 
198 
199 }
200 
201 #endif
DeviceDriver(WorkerCaps::WorkerType wType)
Definition: devicedriver.cc:25
bool detachMedia(const std::string &attachId)
Definition: devicedriver.cc:60
virtual AttachResult mountDevice(const uint32_t id, const zypp::Url &mediaUrl, const std::string &attachId, const std::string &label, const HeaderValueMap &extras)=0
std::unordered_map< std::string, AttachedMedia > & attachedMedia()
ProvideWorkerWeakRef _parentWorker
Definition: devicedriver.h:193
zypp::Pathname attachRoot() const
bool _ephemeral
If set to true the device is removed from the internal list after the last attachpoint was released...
Definition: devicedriver.h:35
std::unordered_map< std::string, std::any > _properties
Definition: devicedriver.h:36
std::vector< std::shared_ptr< Device > > _sysDevs
Definition: devicedriver.h:191
expected< void, AttachError > AttachResult
Definition: devicedriver.h:56
WorkerCaps::WorkerType _wType
Definition: devicedriver.h:187
virtual void immediateShutdown()
unsigned int _maj_nr
Major number of the device.
Definition: devicedriver.h:32
virtual zyppng::expected< WorkerCaps > initialize(const zyppng::worker::Configuration &conf)
Definition: devicedriver.cc:34
std::unordered_map< std::string, AttachedMedia > _attachedMedia
Definition: devicedriver.h:192
ProvideWorkerRef parentWorker() const
static const std::function< bool(const zypp::media::MountEntry &)> bindMountPredicate(const std::string &src)
const zyppng::worker::Configuration & config() const
unsigned int _min_nr
Minor number of the device.
Definition: devicedriver.h:33
std::string _name
Path of the device node or URL for e.g. nfs devices.
Definition: devicedriver.h:31
void removeAttachPoint(const zypp::Pathname &attach_pt) const
std::vector< std::shared_ptr< Device > > & knownDevices()
Definition: devicedriver.cc:90
zypp::Pathname createAttachPoint(const zypp::Pathname &attach_root) const
void setAttachRoot(const zypp::Pathname &root)
bool checkAttached(const zypp::Pathname &mountPoint, const std::function< bool(const zypp::media::MountEntry &)> predicate)
Base class for Exception.
Definition: Exception.h:145
static const std::function< bool(const zypp::media::MountEntry &)> devicePredicate(unsigned int majNr, unsigned int minNr)
Predicate predicate
Definition: PoolQuery.cc:313
ZYPP_FWD_DECL_TYPE_WITH_REFS(DeviceDriver)
MediaVerifierRef verifier
A "struct mntent" like mount entry structure, but using std::strings.
Definition: mount.h:34
zypp::Pathname _mountPoint
Mountpoint of the device, if empty dev is not mounted.
Definition: devicedriver.h:34
zypp::proto::Configuration Configuration
Definition: provideworker.h:33
void setProvider(ProvideWorkerWeakRef workerRef)
Definition: devicedriver.cc:29
zyppng::expected< void > isDesiredMedium(const zypp::Url &deviceUrl, const zypp::Pathname &mountPoint, const zyppng::MediaDataVerifierRef &verifier, uint mediaNr=1)
std::shared_ptr< Device > _dev
Definition: devicedriver.h:41
virtual bool isVolatile() const
static const std::function< bool(const zypp::media::MountEntry &)> fstypePredicate(const std::string &src, const std::vector< std::string > &fstypes)
zyppng::worker::Configuration _config
Definition: devicedriver.h:188
Url manipulation class.
Definition: Url.h:91
virtual void unmountDevice(Device &dev)
AttachError(const uint code, const std::string &reason, const bool transient, const HeaderValueMap &extra={})