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 * If \p on_media_file is marked as optional, then
117 * in case of failure the user interaction callbacks
118 * will be ignored and the exception will throw
121 * \throws MediaException if a problem occurs,
122 * see \ref media::MediaManager::provideFile()
124 Pathname provideFile( const OnMediaLocation & on_media_file );
127 * Provides \a file from media \a media_nr.
129 * \param file path to the file relative to media URL
130 * \param media_nr the media number in the media set
131 * \return local pathname of the requested file
133 * \throws MediaException if a problem occurs,
134 * see \ref media::MediaManager::provideFile()
136 Pathname provideFile(const Pathname & file, unsigned media_nr = 1 );
139 * Release file from media.
140 * This signal that file is not needed anymore.
142 * \param on_media_file location of the file on media
144 void releaseFile( const OnMediaLocation & on_media_file );
148 * Release file from media.
149 * This signal that file is not needed anymore.
151 * \param file path to the file relative to media URL
152 * \param media_nr the media number in the media set
154 void releaseFile(const Pathname & file, unsigned media_nr = 1 );
157 * Provides direcotry \a dir from media number \a media_nr.
159 * \param dir path to the directory relative to media URL
160 * \param recursive whether to provide the whole directory subtree
161 * \param media_nr the media number in the media set
162 * \return local pathname of the requested directory
164 * \throws MediaException if a problem occurs,
165 * see \ref media::MediaManager::provideDir()
166 * and \ref media::MediaManager::provideDirTree()
168 Pathname provideDir(const Pathname & dir, bool recursive, unsigned media_nr = 1);
171 * check if a file exists on the specified media
173 * \param file file to check
174 * \param media_nr Media number
176 bool doesFileExist(const Pathname & file, unsigned media_nr = 1 );
179 * Release all attached media of this set.
181 * \throws MediaNotOpenException for invalid access IDs.
186 * Replaces media number in specified url with given \a medianr.
188 * Media number in the URL is searched for with regex
189 * <tt> "^(.*(cd|dvd))([0-9]+)(\\.iso)$" </tt> for iso scheme and
190 * with <tt> "^(.*(cd|dvd))([0-9]+)(/?)$" </tt> for other schemes.
192 * For cd and dvd scheme it returns the original URL, as well as for
193 * URL which do not match the above regexes.
195 * \param url_r original URL
196 * \param medianr requested media number
197 * \return rewritten URL if applicable, the original URL otherwise
199 static Url rewriteUrl (const Url & url_r, const media::MediaNr medianr);
202 Pathname provideFileInternal(const Pathname & file, unsigned media_nr, bool checkonly, bool cached);
203 media::MediaAccessId getMediaAccessId (media::MediaNr medianr);
204 virtual std::ostream & dumpOn( std::ostream & str ) const;
207 /** Media or media set URL */
211 * Prefered mount point.
213 * \see MediaManager::open(Url,Pathname)
214 * \see MediaHandler::_attachPoint
216 Pathname _prefAttachPoint;
220 typedef std::map<media::MediaNr, media::MediaAccessId> MediaMap;
221 typedef std::map<media::MediaNr, media::MediaVerifierRef > VerifierMap;
223 /** Mapping between media number and Media Access ID */
225 /** Mapping between media number and corespondent verifier */
226 VerifierMap _verifiers;
228 ///////////////////////////////////////////////////////////////////
230 /** \relates MediaSetAccess Stream output */
231 inline std::ostream & operator<<( std::ostream & str, const MediaSetAccess & obj )
232 { return obj.dumpOn( str ); }
236 ///////////////////////////////////////////////////////////////////
237 #endif // ZYPP_SOURCE_MediaSetAccess_H