libzypp  10.5.0
MediaAccess.h
Go to the documentation of this file.
00001 /*---------------------------------------------------------------------\
00002 |                          ____ _   __ __ ___                          |
00003 |                         |__  / \ / / . \ . \                         |
00004 |                           / / \ V /|  _/  _/                         |
00005 |                          / /__ | | | | | |                           |
00006 |                         /_____||_| |_| |_|                           |
00007 |                                                                      |
00008 \---------------------------------------------------------------------*/
00012 #ifndef ZYPP_MEDIA_MEDIAACCESS_H
00013 #define ZYPP_MEDIA_MEDIAACCESS_H
00014 
00015 #include <iosfwd>
00016 #include <map>
00017 #include <list>
00018 #include <string>
00019 
00020 #include "zypp/base/ReferenceCounted.h"
00021 #include "zypp/base/NonCopyable.h"
00022 #include "zypp/base/PtrTypes.h"
00023 #include "zypp/base/Deprecated.h"
00024 
00025 #include "zypp/Pathname.h"
00026 #include "zypp/PathInfo.h"
00027 
00028 #include "zypp/media/MediaException.h"
00029 #include "zypp/media/MediaSource.h"
00030 
00031 #include "zypp/Url.h"
00032 
00033 namespace zypp {
00034   namespace media {
00035 
00036     class MediaHandler;
00037 
00039     //
00040     //  CLASS NAME : MediaAccess
00050     class MediaAccess : public base::ReferenceCounted, private base::NonCopyable
00051     {
00052     public:
00053         typedef intrusive_ptr<MediaAccess> Ptr;
00054         typedef intrusive_ptr<const MediaAccess> constPtr;
00055 
00056     private:
00057 
00058         static const Pathname _noPath;
00059 
00064         MediaHandler * _handler;
00065 
00066         friend class MediaManager;
00067         friend class MediaManager_Impl;
00068 
00069         AttachedMedia        attachedMedia() const;
00070 
00071         bool                 isSharedMedia() const;
00072 
00073         void                 resetParentId();
00074         bool                 dependsOnParent() const;
00075 
00076         bool                 dependsOnParent(MediaAccessId parentId,
00077                                              bool exactIdMatch) const;
00078     public:
00079 
00083         MediaAccess();
00084 
00095         void open( const Url& url, const Pathname & preferred_attach_point = "" );
00096 
00100         bool isOpen() const { return( _handler != 0 ); }
00101 
00106         bool        downloads() const;
00107 
00109         static
00110         ZYPP_DEPRECATED bool downloads(const Url &url)
00111         { return url.schemeIsDownloading(); }
00112 
00114         static
00115         ZYPP_DEPRECATED bool canBeVolatile(const Url &url)
00116         { return url.schemeIsVolatile(); }
00117 
00121         std::string protocol() const;
00122 
00126         Url url() const;
00127 
00134         void close();
00135 
00136     public:
00137 
00148         void attach(bool next = false);
00149 
00156         bool isAttached() const;
00157 
00158         bool hasMoreDevices() const;
00159 
00168         virtual void
00169         getDetectedDevices(std::vector<std::string> & devices,
00170                            unsigned int & index) const;
00171 
00172 
00181         Pathname localRoot() const;
00182 
00189         Pathname localPath( const Pathname & pathname ) const;
00190 
00204         void disconnect();
00205 
00213         void release( const std::string & ejectDev = "" );
00214 
00232         void provideFile( const Pathname & filename ) const;
00233 
00241         void releaseFile( const Pathname & filename ) const;
00242 
00252         void provideDir( const Pathname & dirname ) const;
00253 
00263         void provideDirTree( const Pathname & dirname ) const;
00264 
00272         void releaseDir( const Pathname & dirname ) const;
00273 
00285         void releasePath( const Pathname & pathname ) const;
00286 
00290         void setDeltafile( const Pathname & filename ) const;
00291 
00292     public:
00293 
00307         void dirInfo( std::list<std::string> & retlist,
00308                          const Pathname & dirname, bool dots = true ) const;
00309 
00322         void dirInfo( filesystem::DirContent & retlist,
00323                       const Pathname & dirname, bool dots = true ) const;
00324 
00333         bool doesFileExist( const Pathname & filename ) const;
00334 
00338         virtual ~MediaAccess();
00339 
00340     public:
00341 
00342         virtual std::ostream & dumpOn( std::ostream & str ) const;
00343 
00344     public:
00355         void getFile( const Url &from, const Pathname &to );
00356 
00357     public:
00358 
00379       class FileProvider {
00380         FileProvider( const FileProvider & );             // no copy
00381         FileProvider & operator=( const FileProvider & ); // no assign
00382         private:
00383           MediaAccess::constPtr _media;
00384           Pathname              _file;
00385           Pathname              _local_file;
00386         public:
00390           FileProvider( MediaAccess::constPtr media_r, const Pathname & file_r )
00391             : _media( media_r )
00392             , _file( file_r )
00393             , _local_file( "" )
00394           {
00395             if ( _file.empty() ) {
00396               ZYPP_THROW(MediaBadFilenameException(_file.asString()));
00397             } else if ( _media ) {
00398               try {
00399                 _media->provideFile( _file );
00400                 _local_file = _media->localPath( _file );
00401               }
00402               catch (const MediaException & excpt_r)
00403               {
00404                 ZYPP_CAUGHT(excpt_r);
00405                 _media = NULL;
00406                 ZYPP_RETHROW(excpt_r);
00407               }
00408             }
00409           }
00410 
00411           ~FileProvider() {
00412             if ( _media )
00413             {
00414               try {
00415                 _media->releaseFile( _file );
00416               }
00417               catch (const MediaException &excpt_r)
00418               {
00419                 ZYPP_CAUGHT(excpt_r);
00420               }
00421               catch(...) {} // No exception from dtor!
00422             }
00423           }
00424 
00425         public:
00426 
00431           Pathname localFile() const { return _local_file; }
00432 
00437           Pathname operator()() const {
00438             if ( _media )
00439               return _media->localPath( _file );
00440             return Pathname();
00441           }
00442       };
00443     };
00444 
00445     std::ostream & operator<<( std::ostream & str, const MediaAccess & obj );
00446 
00448 
00449   } // namespace media
00450 } // namespace zypp
00451 
00452 #endif // ZYPP_MEDIA_MEDIAACCESS_H
00453