libzypp  13.10.6
MediaAccess.h
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
12 #ifndef ZYPP_MEDIA_MEDIAACCESS_H
13 #define ZYPP_MEDIA_MEDIAACCESS_H
14 
15 #include <iosfwd>
16 #include <map>
17 #include <list>
18 #include <string>
19 
21 #include "zypp/base/NonCopyable.h"
22 #include "zypp/base/PtrTypes.h"
23 #include "zypp/APIConfig.h"
24 
25 #include "zypp/Pathname.h"
26 #include "zypp/PathInfo.h"
27 
29 #include "zypp/media/MediaSource.h"
30 
31 #include "zypp/Url.h"
32 
33 namespace zypp {
34  namespace media {
35 
36  class MediaHandler;
37 
39  //
40  // CLASS NAME : MediaAccess
51  {
52  public:
53  typedef intrusive_ptr<MediaAccess> Ptr;
54  typedef intrusive_ptr<const MediaAccess> constPtr;
55 
56  private:
57 
58  static const Pathname _noPath;
59 
65 
66  friend class MediaManager;
67  friend class MediaManager_Impl;
68 
70 
71  bool isSharedMedia() const;
72 
73  void resetParentId();
74  bool dependsOnParent() const;
75 
76  bool dependsOnParent(MediaAccessId parentId,
77  bool exactIdMatch) const;
78  public:
79 
83  MediaAccess();
84 
95  void open( const Url& url, const Pathname & preferred_attach_point = "" );
96 
100  bool isOpen() const { return( _handler != 0 ); }
101 
106  bool downloads() const;
107 
111  std::string protocol() const;
112 
116  Url url() const;
117 
124  void close();
125 
126  public:
127 
138  void attach(bool next = false);
139 
146  bool isAttached() const;
147 
148  bool hasMoreDevices() const;
149 
158  virtual void
159  getDetectedDevices(std::vector<std::string> & devices,
160  unsigned int & index) const;
161 
162 
171  Pathname localRoot() const;
172 
179  Pathname localPath( const Pathname & pathname ) const;
180 
194  void disconnect();
195 
203  void release( const std::string & ejectDev = "" );
204 
222  void provideFile( const Pathname & filename ) const;
223 
231  void releaseFile( const Pathname & filename ) const;
232 
242  void provideDir( const Pathname & dirname ) const;
243 
253  void provideDirTree( const Pathname & dirname ) const;
254 
262  void releaseDir( const Pathname & dirname ) const;
263 
275  void releasePath( const Pathname & pathname ) const;
276 
280  void setDeltafile( const Pathname & filename ) const;
281 
282  public:
283 
297  void dirInfo( std::list<std::string> & retlist,
298  const Pathname & dirname, bool dots = true ) const;
299 
312  void dirInfo( filesystem::DirContent & retlist,
313  const Pathname & dirname, bool dots = true ) const;
314 
323  bool doesFileExist( const Pathname & filename ) const;
324 
328  virtual ~MediaAccess();
329 
330  public:
331 
332  virtual std::ostream & dumpOn( std::ostream & str ) const;
333 
334  public:
345  void getFile( const Url &from, const Pathname &to );
346 
347  public:
348 
369  class FileProvider {
370  FileProvider( const FileProvider & ); // no copy
371  FileProvider & operator=( const FileProvider & ); // no assign
372  private:
374  Pathname _file;
375  Pathname _local_file;
376  public:
380  FileProvider( MediaAccess::constPtr media_r, const Pathname & file_r )
381  : _media( media_r )
382  , _file( file_r )
383  , _local_file( "" )
384  {
385  if ( _file.empty() ) {
387  } else if ( _media ) {
388  try {
389  _media->provideFile( _file );
390  _local_file = _media->localPath( _file );
391  }
392  catch (const MediaException & excpt_r)
393  {
394  ZYPP_CAUGHT(excpt_r);
395  _media = NULL;
396  ZYPP_RETHROW(excpt_r);
397  }
398  }
399  }
400 
402  if ( _media )
403  {
404  try {
405  _media->releaseFile( _file );
406  }
407  catch (const MediaException &excpt_r)
408  {
409  ZYPP_CAUGHT(excpt_r);
410  }
411  catch(...) {} // No exception from dtor!
412  }
413  }
414 
415  public:
416 
421  Pathname localFile() const { return _local_file; }
422 
427  Pathname operator()() const {
428  if ( _media )
429  return _media->localPath( _file );
430  return Pathname();
431  }
432  };
433  };
434 
435  std::ostream & operator<<( std::ostream & str, const MediaAccess & obj );
436 
438 
439  } // namespace media
440 } // namespace zypp
441 
442 #endif // ZYPP_MEDIA_MEDIAACCESS_H
443 
MediaAccess()
constructor
Definition: MediaAccess.cc:53
void dirInfo(std::list< std::string > &retlist, const Pathname &dirname, bool dots=true) const
Return content of directory on media via retlist.
Definition: MediaAccess.cc:412
void open(const Url &url, const Pathname &preferred_attach_point="")
open url.
Definition: MediaAccess.cc:104
void provideFile(const Pathname &filename) const
Use concrete handler to provide file denoted by path below &#39;attach point&#39;.
Definition: MediaAccess.cc:340
Pathname localFile() const
If no error, expect operator() to return the local Pathname of the provided file. ...
Definition: MediaAccess.h:421
void close()
close url
Definition: MediaAccess.cc:232
void provideDir(const Pathname &dirname) const
Use concrete handler to provide directory denoted by path below &#39;attach point&#39; (not recursive!)...
Definition: MediaAccess.cc:373
Url url() const
Url if media is opened, otherwise empty.
Definition: MediaAccess.cc:222
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
Definition: Exception.h:320
Handle access to a medium.
Definition: MediaAccess.h:50
std::string protocol() const
Used Protocol if media is opened, otherwise &#39;unknown&#39;.
Definition: MediaAccess.cc:202
void provideDirTree(const Pathname &dirname) const
Use concrete handler to provide directory tree denoted by path below &#39;attach point&#39; (recursive!!)...
Definition: MediaAccess.cc:383
FileProvider(MediaAccess::constPtr media_r, const Pathname &file_r)
Definition: MediaAccess.h:380
bool downloads() const
Hint if files are downloaded or not.
Definition: MediaAccess.cc:211
void disconnect()
Use concrete handler to disconnect the media.
Definition: MediaAccess.cc:317
void release(const std::string &ejectDev="")
Use concrete handler to release the media.
Definition: MediaAccess.cc:327
void releaseDir(const Pathname &dirname) const
Remove directory tree below attach point IFF handler downloads files to the local filesystem...
Definition: MediaAccess.cc:393
bool doesFileExist(const Pathname &filename) const
check if a file exists
Definition: MediaAccess.cc:438
bool isOpen() const
True if media is open.
Definition: MediaAccess.h:100
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 ...
Definition: MediaAccess.cc:279
Pathname localRoot() const
Return the local directory that corresponds to medias url, no matter if media isAttached or not...
Definition: MediaAccess.cc:297
unsigned int MediaAccessId
Media manager access Id type.
Definition: MediaSource.h:29
std::ostream & operator<<(std::ostream &str, const MediaAccess &obj)
Definition: MediaAccess.cc:482
MediaHandler * _handler
handler for &#39;physical&#39; media == 0 if not open
Definition: MediaAccess.h:64
#define ZYPP_RETHROW(EXCPT)
Drops a logline and rethrows, updating the CodeLocation.
Definition: Exception.h:328
bool isSharedMedia() const
Definition: MediaAccess.cc:76
boost::noncopyable NonCopyable
Ensure derived classes cannot be copied.
Definition: NonCopyable.h:26
void releaseFile(const Pathname &filename) const
Remove filename below attach point IFF handler downloads files to the local filesystem.
Definition: MediaAccess.cc:360
Abstract base class for &#39;physical&#39; MediaHandler like MediaCD, etc.
Definition: MediaHandler.h:45
A simple structure containing references to a media source and its attach point.
Definition: MediaSource.h:133
virtual ~MediaAccess()
Destructor.
Definition: MediaAccess.cc:59
Just inherits Exception to separate media exceptions.
Provides API related macros.
FileProvider & operator=(const FileProvider &)
std::list< DirEntry > DirContent
Returned by readdir.
Definition: PathInfo.h:544
AttachedMedia attachedMedia() const
Definition: MediaAccess.cc:69
bool dependsOnParent() const
Definition: MediaAccess.cc:89
void attach(bool next=false)
Use concrete handler to attach the media.
Definition: MediaAccess.cc:256
static const Pathname _noPath
Definition: MediaAccess.h:58
virtual std::ostream & dumpOn(std::ostream &str) const
Overload to realize std::ostream &amp; operator&lt;&lt;.
Definition: MediaAccess.cc:448
Base class for reference counted objects.
void setDeltafile(const Pathname &filename) const
set a deltafile to be used in the next download
Definition: MediaAccess.cc:350
bool isAttached() const
True if media is attached.
Definition: MediaAccess.cc:266
Pathname localPath(const Pathname &pathname) const
Short for &#39;localRoot() + pathname&#39;, but returns an empty pathname if media is not open...
Definition: MediaAccess.cc:308
Helper class that provides file on construction and cleans up on destruction.
Definition: MediaAccess.h:369
#define ZYPP_CAUGHT(EXCPT)
Drops a logline telling the Exception was caught (in order to handle it).
Definition: Exception.h:324
Manages access to the &#39;physical&#39; media, e.g CDROM drives, Disk volumes, directory trees...
Definition: MediaManager.h:473
Pathname operator()() const
Return the local Pathname of the provided file or an empty Pathname on error.
Definition: MediaAccess.h:427
intrusive_ptr< const MediaAccess > constPtr
Definition: MediaAccess.h:54
intrusive_ptr< MediaAccess > Ptr
Definition: MediaAccess.h:53
void releasePath(const Pathname &pathname) const
Remove pathname below attach point IFF handler downloads files to the local filesystem.
Definition: MediaAccess.cc:402
bool hasMoreDevices() const
Definition: MediaAccess.cc:272
Url manipulation class.
Definition: Url.h:87
void getFile(const Url &from, const Pathname &to)
Get file from location at specified by URL and copy it to destination.
Definition: MediaAccess.cc:457