libzypp  17.37.5
MediaHandler.h
Go to the documentation of this file.
1 
2 /*---------------------------------------------------------------------\
3 | ____ _ __ __ ___ |
4 | |__ / \ / / . \ . \ |
5 | / / \ V /| _/ _/ |
6 | / /__ | | | | | | |
7 | /_____||_| |_| |_| |
8 | |
9 \---------------------------------------------------------------------*/
13 #ifndef ZYPP_MEDIA_MEDIAHANDLERL_H
14 #define ZYPP_MEDIA_MEDIAHANDLERL_H
15 
16 #include <any>
17 #include <iosfwd>
18 #include <string>
19 #include <list>
20 
21 #include <zypp/Pathname.h>
22 #include <zypp/PathInfo.h>
23 #include <zypp/base/PtrTypes.h>
24 
25 #include <zypp/media/MediaUrl.h>
26 
27 #include <zypp/media/MediaSource.h>
28 #include <zypp-media/MediaException>
29 #include <zypp-core/Globals.h>
30 #include <zypp-core/OnMediaLocation>
31 
32 #undef ZYPP_BASE_LOGGER_LOGGROUP
33 #define ZYPP_BASE_LOGGER_LOGGROUP "zypp::media"
34 
35 namespace zypp {
36  namespace media {
37 
38  class MediaManager;
39 
41 //
42 // CLASS NAME : MediaHandler
51 class MediaHandler {
52  friend std::ostream & operator<<( std::ostream & str, const MediaHandler & obj );
53 
54  public:
55  using Ptr = shared_ptr<MediaHandler>;
56  using constPtr = shared_ptr<const MediaHandler>;
57  static bool setAttachPrefix(const Pathname &attach_prefix);
58 
59  static std::string getRealPath(const std::string &path);
60  static Pathname getRealPath(const Pathname &path);
61 
62  private:
67 
71  mutable
73 
79 
91 
98 
104 
106  mutable time_t _attach_mtime;
107 
108  protected:
109 
113  const std::vector<MediaUrl> _urls;
114 
118  const MediaUrl _url;
119 
124 
125  public:
126 
133  bool dependsOnParent(MediaAccessId parentId,
134  bool exactIdMatch);
135  bool dependsOnParent();
136 
142  void resetParentId();
143 
147  Pathname attachPoint() const;
148 
154  void setAttachPoint(const Pathname &path, bool temp);
155 
160  void setAttachPoint(const AttachPointRef &ref);
161 
166 
172  void attachPointHint(const Pathname &path, bool temp);
173 
181  Pathname createAttachPoint() const;
188  Pathname createAttachPoint(const Pathname &attach_root) const;
189 
194  void removeAttachPoint();
195 
202  virtual bool checkAttachPoint(const Pathname &apoint) const;
203 
212  static bool checkAttachPoint(const Pathname &apoint,
213  bool empty_dir,
214  bool writeable);
215 
224  bool isUseableAttachPoint(const Pathname &path,
225  bool mtab=true) const;
226 
231  std::string mediaSourceName() const
232  {
233  return _mediaSource ? _mediaSource->name : "";
234  }
235 
240  void setMediaSource(const MediaSourceRef &ref);
241 
247  findAttachedMedia(const MediaSourceRef &media) const;
248 
261 
266  bool isSharedMedia() const;
267 
276  bool checkAttached(bool matchMountFs) const;
277 
286  void forceRelaseAllMedia(bool matchMountFs);
287  void forceRelaseAllMedia(const MediaSourceRef &ref,
288  bool matchMountFs);
289 
290  protected:
291 
293  //
294  // Real action interface to be overloaded by concrete handler.
295  //
297 
310  virtual void attachTo(bool next = false) = 0;
311 
327  virtual void disconnectFrom() { return; }
328 
341  virtual void releaseFrom( const std::string & ejectDev = "" ) = 0;
342 
349  virtual void forceEject( const std::string & device ) {}
350 
362  virtual void getFile( const OnMediaLocation &file ) const;
363 
375  virtual void getFileCopy( const OnMediaLocation & file, const Pathname & targetFilename ) const;
376 
377 
393  virtual void getDir( const Pathname & dirname, bool recurse_r ) const = 0;
394 
410  virtual void getDirInfo( std::list<std::string> & retlist,
411  const Pathname & dirname, bool dots = true ) const = 0;
412 
424  virtual void getDirInfo( filesystem::DirContent & retlist,
425  const Pathname & dirname, bool dots = true ) const = 0;
426 
435  virtual bool getDoesFileExist( const Pathname & filename ) const = 0;
436 
437  protected:
438 
447  void getDirectoryYast( std::list<std::string> & retlist,
448  const Pathname & dirname, bool dots = true ) const;
449 
459  const Pathname & dirname, bool dots = true ) const;
460 
461  public:
462 
474  MediaHandler ( MediaUrl primaryUrl_r,
475  std::vector<MediaUrl> urls_r,
476  const Pathname & attach_point_r,
477  Pathname urlpath_below_attachpoint_r,
478  const bool does_download_r );
479 
484  virtual ~MediaHandler();
485 
486  public:
487 
488 
490  //
491  // MediaAccess interface. Does common checks and logging.
492  // Invokes real action if necessary.
493  //
495 
499  bool downloads() const { return _does_download; }
500 
504  std::string protocol() const { return _url.url().getScheme(); }
505 
509  Url url() const { return _url.url(); }
510 
521  void attach(bool next);
522 
526  virtual bool isAttached() const { return _mediaSource != nullptr; }
527 
536  Pathname localRoot() const;
537 
543  Pathname localPath( const Pathname & pathname ) const;
544 
558  void disconnect();
559 
566  void release( const std::string & ejectDev = "" );
567 
576  void provideFile( const OnMediaLocation &file ) const;
577 
589  void provideFileCopy( const OnMediaLocation &srcFile, const Pathname& targetFilename ) const;
590 
600  void provideDir( const Pathname& dirname ) const;
601 
611  void provideDirTree( const Pathname& dirname ) const;
612 
620  void releaseFile( const Pathname & filename ) const { return releasePath( filename ); }
621 
629  void releaseDir( const Pathname & dirname ) const { return releasePath( dirname ); }
630 
643  void releasePath( const Pathname& pathname ) const;
644 
645  public:
646 
660  void dirInfo( std::list<std::string> & retlist,
661  const Pathname & dirname, bool dots = true ) const;
662 
675  void dirInfo( filesystem::DirContent & retlist,
676  const Pathname & dirname, bool dots = true ) const;
677 
686  bool doesFileExist( const Pathname & filename ) const;
687 
691  virtual bool hasMoreDevices();
692 
701  virtual void
702  getDetectedDevices(std::vector<std::string> & devices,
703  unsigned int & index) const;
704 
715  virtual void
716  precacheFiles ( const std::vector< OnMediaLocation > &files );
717 };
718 
720 
721  } // namespace media
722 } // namespace zypp
723 
724 
725 #endif // ZYPP_MEDIA_MEDIAHANDLERL_H
Attach point of a media source.
Definition: MediaSource.h:106
void resetParentId()
Called in case, where the media manager takes over the destruction of the parent id (e...
void releasePath(const Pathname &pathname) const
Remove pathname below localRoot IFF handler downloads files to the local filesystem.
Describes a resource file located on a medium.
virtual void getDir(const Pathname &dirname, bool recurse_r) const =0
Call concrete handler to provide directory content (not recursive!) below attach point.
AttachPoint _attachPointHint
The user provided attach preferred point.
Definition: MediaHandler.h:90
void provideDir(const Pathname &dirname) const
Use concrete handler to provide directory denoted by path below &#39;localRoot&#39; (not recursive!).
virtual bool checkAttachPoint(const Pathname &apoint) const
Verify if the specified directory as attach point (root) as requires by the particular media handler ...
AttachedMedia attachedMedia() const
Returns the attached media.
Pathname _relativeRoot
The relative root directory of the data on the media.
Definition: MediaHandler.h:97
static std::string getRealPath(const std::string &path)
std::string protocol() const
Protocol hint for MediaAccess.
Definition: MediaHandler.h:504
const MediaUrl _url
Primary Url.
Definition: MediaHandler.h:118
static Pathname _attachPrefix
User defined default attach point prefix.
Definition: MediaHandler.h:66
void setAttachPoint(const Pathname &path, bool temp)
Set a new attach point.
void dirInfo(std::list< std::string > &retlist, const Pathname &dirname, bool dots=true) const
Return content of directory on media via retlist.
virtual void disconnectFrom()
Call concrete handler to disconnect media.
Definition: MediaHandler.h:327
bool isUseableAttachPoint(const Pathname &path, bool mtab=true) const
Ask media manager, if the specified path is already used as attach point or if there are another atta...
String related utilities and Regular expression matching.
shared_ptr< const MediaHandler > constPtr
Definition: MediaHandler.h:56
void provideFileCopy(const OnMediaLocation &srcFile, const Pathname &targetFilename) const
Call concrete handler to provide a copy of a file under a different place in the file system (usually...
void provideDirTree(const Pathname &dirname) const
Use concrete handler to provide directory tree denoted by path below &#39;localRoot&#39; (recursive!!).
Provides API related macros.
bool doesFileExist(const Pathname &filename) const
check if a file exists
std::list< DirEntry > DirContent
Returned by readdir.
Definition: PathInfo.h:526
bool checkAttached(bool matchMountFs) const
Check actual mediaSource attachment against the current mount table of the system.
virtual void attachTo(bool next=false)=0
Call concrete handler to attach the media.
Pathname localPath(const Pathname &pathname) const
Files provided will be available at &#39;localPath(filename)&#39;.
virtual void getFileCopy(const OnMediaLocation &file, const Pathname &targetFilename) const
Call concrete handler to provide a file under a different place in the file system (usually not under...
void releaseDir(const Pathname &dirname) const
Remove directory tree below localRoot IFF handler downloads files to the local filesystem.
Definition: MediaHandler.h:629
std::string mediaSourceName() const
Get the media source name or an empty string.
Definition: MediaHandler.h:231
unsigned int MediaAccessId
Media manager access Id type.
Definition: MediaSource.h:30
const std::vector< MediaUrl > _urls
All mirrors including the primary.
Definition: MediaHandler.h:113
MediaSourceRef _mediaSource
The attached media source description reference.
Definition: MediaHandler.h:72
virtual void releaseFrom(const std::string &ejectDev="")=0
Call concrete handler to release the media.
Abstract base class for &#39;physical&#39; MediaHandler like MediaCD, etc.
Definition: MediaHandler.h:51
A simple structure containing references to a media source and its attach point.
Definition: MediaSource.h:134
MediaHandler(MediaUrl primaryUrl_r, std::vector< MediaUrl > urls_r, const Pathname &attach_point_r, Pathname urlpath_below_attachpoint_r, const bool does_download_r)
If the concrete media handler provides a nonempty attach_point, it must be an existing directory...
Definition: MediaHandler.cc:55
void provideFile(const OnMediaLocation &file) const
Use concrete handler to provide file denoted by path below &#39;localRoot&#39;.
void setMediaSource(const MediaSourceRef &ref)
Set new media source reference.
void disconnect()
Use concrete handler to isconnect media.
void attach(bool next)
Use concrete handler to attach the media.
bool isSharedMedia() const
Returns a hint if the media is shared or not.
virtual ~MediaHandler()
Contolling MediaAccess takes care, that attached media is released prior to deleting this...
static bool setAttachPrefix(const Pathname &attach_prefix)
void getDirectoryYast(std::list< std::string > &retlist, const Pathname &dirname, bool dots=true) const
Retrieve and if available scan dirname/directory.yast.
AttachPoint attachPointHint() const
Get the actual attach point hint.
virtual bool hasMoreDevices()
Check if the media has one more device available for attach(true).
void removeAttachPoint()
Remove unused attach point.
virtual void precacheFiles(const std::vector< OnMediaLocation > &files)
Tries to fetch the given files and precaches them.
Pathname localRoot() const
Return the local directory that corresponds to medias url, no matter if media isAttached or not...
virtual void forceEject(const std::string &device)
Call concrete handler to physically eject the media (i.e.
Definition: MediaHandler.h:349
void forceRelaseAllMedia(bool matchMountFs)
Call to this function will try to release all media matching the currenlty attached media source...
virtual void getDirInfo(std::list< std::string > &retlist, const Pathname &dirname, bool dots=true) const =0
Call concrete handler to provide a content list of directory on media via retlist.
virtual bool getDoesFileExist(const Pathname &filename) const =0
check if a file exists
AttachedMedia findAttachedMedia(const MediaSourceRef &media) const
Ask the media manager if specified media source is already attached.
void releaseFile(const Pathname &filename) const
Remove filename below localRoot IFF handler downloads files to the local filesystem.
Definition: MediaHandler.h:620
bool downloads() const
Hint if files are downloaded or not.
Definition: MediaHandler.h:499
Pathname attachPoint() const
Return the currently used attach point.
Url url() const
Primary Url used.
Definition: MediaHandler.h:509
virtual void getDetectedDevices(std::vector< std::string > &devices, unsigned int &index) const
Fill in a vector of detected ejectable devices and the index of the currently attached device within ...
MediaAccessId _parentId
Access Id of media handler we depend on.
Definition: MediaHandler.h:123
AttachPointRef _attachPoint
This is where the media will be actually attached ("mounted").
Definition: MediaHandler.h:78
time_t _attach_mtime
timestamp of the the last attach verification
Definition: MediaHandler.h:106
constexpr std::string_view device("device")
shared_ptr< MediaHandler > Ptr
Definition: MediaHandler.h:55
Pathname createAttachPoint() const
Try to create a default / temporary attach point.
virtual bool isAttached() const
True if media is attached.
Definition: MediaHandler.h:526
Easy-to use interface to the ZYPP dependency resolver.
Definition: Application.cc:19
void release(const std::string &ejectDev="")
Use concrete handler to release the media.
friend std::ostream & operator<<(std::ostream &str, const MediaHandler &obj)
virtual void getFile(const OnMediaLocation &file) const
Call concrete handler to provide file below attach point.
bool _does_download
True if concrete handler downloads files to the local filesystem.
Definition: MediaHandler.h:103
Url manipulation class.
Definition: Url.h:92