1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
10 #ifndef ZYPP_MediaSetAccess_H
11 #define ZYPP_MediaSetAccess_H
16 #include <boost/function.hpp>
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"
26 ///////////////////////////////////////////////////////////////////
28 { /////////////////////////////////////////////////////////////////
30 DEFINE_PTR_TYPE(MediaSetAccess);
32 ///////////////////////////////////////////////////////////////////
34 // CLASS NAME : MediaSetAccess
37 * Media access layer responsible for handling files distributed on a set
40 * This is provided as a means to handle CD or DVD sets accessible through
41 * dir, iso, nfs or other URL schemes other than cd/dvd (see
42 * \ref MediaManager for info on different implemented media backends).
43 * Currently it handles URLs containing cdN, CDN, dvdN, and DVDN strings,
44 * where N is the number of particular media in the set.
48 * "iso:/?iso=/path/to/iso/images/openSUSE-10.3-Alpha2plus-DVD-x86_64-DVD1.iso"
49 * "dir:/path/to/cdset/sources/openSUSE-10.3/Alpha2plus/CD1"
52 * MediaSetAccess accesses files on desired media by rewriting
53 * the original URL, replacing the digit (usually) 1 with requested media
54 * number and uses \ref MediaManager to get the files from the new URL.
56 * Additionaly, each media number can be assined a media verifier which
57 * checks if the media we are trying to access is the desired one. See
58 * \ref MediaVerifierBase for more info.
62 * Url url("dir:/path/to/cdset/sources/openSUSE-10.3/Alpha2plus/CD1");
64 * MediaSetAccess access(url);
66 * access.setVerifier(1, media1VerifierRef);
67 * access.setVerifier(2, media2VerifierRef);
69 * Pathname file1 = "/some/file/on/media1";
70 * access.provideFile(1, file1);
71 * Pathname file2 = "/some/file/on/media2";
72 * access.provideFile(2, file1);
76 class MediaSetAccess : public base::ReferenceCounted, private base::NonCopyable
78 friend std::ostream & operator<<( std::ostream & str, const MediaSetAccess & obj );
82 * Creates a callback enabled media access for specified \a url.
85 * \param prefered_attach_point Prefered attach (mount) point. Use, if
86 * you want to mount the media to a specific directory.
88 MediaSetAccess( const Url &url, const Pathname & prefered_attach_point = "" );
89 /** \overload Also taking a \ref label. */
90 MediaSetAccess( const std::string & label_r, const Url &url, const Pathname & prefered_attach_point = "" );
94 * Sets a \ref MediaVerifier verifier for given media number.
96 void setVerifier( unsigned media_nr, media::MediaVerifierRef verifier );
99 * The label identifing this media set and to be sent in a media change request.
101 const std::string & label() const
105 * Set the label identifing this media set and to be sent in a media change request.
107 void setLabel( const std::string & label_r )
108 { _label = label_r; }
111 * Provides a file from a media location.
113 * \param on_media_file location of the file on media
114 * \return local pathname of the requested file
116 * \throws MediaException if a problem occurs,
117 * see \ref media::MediaManager::provideFile()
119 Pathname provideFile( const OnMediaLocation & on_media_file );
122 * Provides \a file from media \a media_nr.
124 * \param file path to the file relative to media URL
125 * \param media_nr the media number in the media set
126 * \return local pathname of the requested file
128 * \throws MediaException if a problem occurs,
129 * see \ref media::MediaManager::provideFile()
131 Pathname provideFile(const Pathname & file, unsigned media_nr = 1 );
134 * Provides direcotry \a dir from media number \a media_nr.
136 * \param dir path to the directory relative to media URL
137 * \param recursive whether to provide the whole directory subtree
138 * \param media_nr the media number in the media set
139 * \return local pathname of the requested directory
141 * \throws MediaException if a problem occurs,
142 * see \ref media::MediaManager::provideDir()
143 * and \ref media::MediaManager::provideDirTree()
145 Pathname provideDir(const Pathname & dir, bool recursive, unsigned media_nr = 1);
148 * check if a file exists on the specified media
150 * \param file file to check
151 * \param media_nr Media number
153 bool doesFileExist(const Pathname & file, unsigned media_nr = 1 );
156 * Release all attached media of this set.
158 * \throws MediaNotOpenException for invalid access IDs.
163 * Replaces media number in specified url with given \a medianr.
165 * Media number in the URL is searched for with regex
166 * <tt> "^(.*(cd|dvd))([0-9]+)(\\.iso)$" </tt> for iso scheme and
167 * with <tt> "^(.*(cd|dvd))([0-9]+)(/?)$" </tt> for other schemes.
169 * For cd and dvd scheme it returns the original URL, as well as for
170 * URL which do not match the above regexes.
172 * \param url_r original URL
173 * \param medianr requested media number
174 * \return rewritten URL if applicable, the original URL otherwise
176 static Url rewriteUrl (const Url & url_r, const media::MediaNr medianr);
179 Pathname provideFileInternal(const Pathname & file, unsigned media_nr, bool checkonly, bool cached);
180 media::MediaAccessId getMediaAccessId (media::MediaNr medianr);
181 virtual std::ostream & dumpOn( std::ostream & str ) const;
184 /** Media or media set URL */
188 * Prefered mount point.
190 * \see MediaManager::open(Url,Pathname)
191 * \see MediaHandler::_attachPoint
193 Pathname _prefAttachPoint;
197 typedef std::map<media::MediaNr, media::MediaAccessId> MediaMap;
198 typedef std::map<media::MediaNr, media::MediaVerifierRef > VerifierMap;
200 /** Mapping between media number and Media Access ID */
202 /** Mapping between media number and corespondent verifier */
203 VerifierMap _verifiers;
205 ///////////////////////////////////////////////////////////////////
207 /** \relates MediaSetAccess Stream output */
208 inline std::ostream & operator<<( std::ostream & str, const MediaSetAccess & obj )
209 { return obj.dumpOn( str ); }
213 ///////////////////////////////////////////////////////////////////
214 #endif // ZYPP_SOURCE_MediaSetAccess_H