- made MediaSetAccess::rewriteUrl() static
[platform/upstream/libzypp.git] / zypp / MediaSetAccess.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9
10 #ifndef ZYPP_MediaSetAccess_H
11 #define ZYPP_MediaSetAccess_H
12
13 #include <iosfwd>
14 #include <string>
15 #include <vector>
16 #include <boost/function.hpp>
17
18 #include "zypp/base/ReferenceCounted.h"
19 #include "zypp/base/NonCopyable.h"
20 #include "zypp/base/PtrTypes.h"
21 #include "zypp/media/MediaManager.h"
22 #include "zypp/Pathname.h"
23 #include "zypp/CheckSum.h"
24 #include "zypp/OnMediaLocation.h"
25
26 ///////////////////////////////////////////////////////////////////
27 namespace zypp
28 { /////////////////////////////////////////////////////////////////
29
30     DEFINE_PTR_TYPE(MediaSetAccess);
31
32     typedef boost::function<bool ( const Pathname &file )> FileChecker;
33
34     class NullFileChecker
35     {
36       public:
37       bool operator()( const Pathname &file );
38     };
39
40     class ChecksumFileChecker
41     {
42       public:
43       ChecksumFileChecker( const CheckSum &checksum );
44       bool operator()( const Pathname &file );
45       private:
46       CheckSum _checksum;
47     };
48
49     ///////////////////////////////////////////////////////////////////
50     //
51     //  CLASS NAME : MediaSetAccess
52     //
53     /**
54      * Media access layer responsible for handling media distributed as a set.
55      *
56      * This is provided as a means to handle CD or DVD sets accessible through
57      * dir, iso, nfs or other URL schemes other than cd/dvd (see
58      * \ref MediaManager for info on different implemented media backends).
59      * Currently it handles URLs containing cdN, CDN, dvdN, and DVDN strings,
60      * where N is the number of particular media in the set.
61      * 
62      * Examples:
63      * \code
64      * "iso:/?iso=/path/to/iso/images/openSUSE-10.3-Alpha2plus-DVD-x86_64-DVD1.iso"
65      * "dir:/path/to/cdset/sources/openSUSE-10.3/Alpha2plus/CD1"
66      * \endcode
67      * 
68      * MediaSetAccess accesses files on desired media by rewriting
69      * the original URL, replacing the digit (usually) 1 with requested media
70      * number and uses \ref MediaManager to get the files from the new URL.
71      * 
72      * Additionaly, each media number can be assined a media verifier which
73      * checks if the media we are trying to access is the desired one. See
74      * \ref MediaVerifierBase for more info.
75      * 
76      * Code example:
77      * \code
78      * Url url("dir:/path/to/cdset/sources/openSUSE-10.3/Alpha2plus/CD1");
79      * 
80      * MediaSetAccess access(url, "/");
81      * 
82      * access.setVerifier(1, media1VerifierRef);
83      * access.setVerifier(2, media2VerifierRef);
84      * 
85      * Pathname file1 = "/some/file/on/media1";
86      * access.provideFile(1, file1);
87      * Pathname file2 = "/some/file/on/media2";
88      * access.provideFile(2, file1);
89      *
90      * \endcode
91      */
92     class MediaSetAccess : public base::ReferenceCounted, private base::NonCopyable
93     {
94       friend std::ostream & operator<<( std::ostream & str, const MediaSetAccess & obj );
95
96     public:
97       /**
98        * creates a callback enabled media access  for \param url and \param path.
99        * with only 1 media no verified
100        */
101       MediaSetAccess( const Url &url, const Pathname &path );
102       ~MediaSetAccess();
103
104       /**
105        * Sets a \ref MediaVerifierRef verifier for given media number
106        */
107       void setVerifier( unsigned media_nr, media::MediaVerifierRef verifier );
108       
109       /**
110        * provide a file from a media location.
111        */
112       Pathname provideFile( const OnMediaLocation & on_media_file );
113
114       Pathname provideFile(const Pathname & file, unsigned media_nr = 1 );
115       Pathname provideFile(const Pathname & file, unsigned media_nr, const FileChecker checker );
116
117       static Url rewriteUrl (const Url & url_r, const media::MediaNr medianr);
118
119     protected:
120       Pathname provideFileInternal(const Pathname & file, unsigned media_nr, bool checkonly, bool cached);
121       media::MediaAccessId getMediaAccessId (media::MediaNr medianr);
122       virtual std::ostream & dumpOn( std::ostream & str ) const;
123
124     private:
125       /** Media or media set URL */
126       Url _url;
127       /** Path on the media relative to _url */
128       Pathname _path;
129
130       typedef std::map<media::MediaNr, media::MediaAccessId> MediaMap;
131       typedef std::map<media::MediaNr, media::MediaVerifierRef > VerifierMap;
132
133       /** Mapping between media number and Media Access ID */
134       MediaMap _medias;
135       /** Mapping between media number and corespondent verifier */
136       VerifierMap _verifiers;
137     };
138     ///////////////////////////////////////////////////////////////////
139
140     /** \relates MediaSetAccess Stream output */
141     inline std::ostream & operator<<( std::ostream & str, const MediaSetAccess & obj )
142     { return obj.dumpOn( str ); }
143
144
145 } // namespace zypp
146 ///////////////////////////////////////////////////////////////////
147 #endif // ZYPP_SOURCE_MediaSetAccess_H