1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
10 #ifndef ZYPP_MediaSetAccess_H
11 #define ZYPP_MediaSetAccess_H
16 #include <zypp/base/Function.h>
18 #include "zypp/base/ReferenceCounted.h"
19 #include "zypp/base/NonCopyable.h"
20 #include "zypp/base/Flags.h"
21 #include "zypp/base/PtrTypes.h"
22 #include "zypp/media/MediaManager.h"
23 #include "zypp/Pathname.h"
24 #include "zypp/CheckSum.h"
25 #include "zypp/OnMediaLocation.h"
27 ///////////////////////////////////////////////////////////////////
29 { /////////////////////////////////////////////////////////////////
31 DEFINE_PTR_TYPE(MediaSetAccess);
33 ///////////////////////////////////////////////////////////////////
35 // CLASS NAME : MediaSetAccess
38 * Media access layer responsible for handling files distributed on a set
39 * of media with media change and abort/retry/ingore user callback handling.
41 * This is provided as a means to handle CD or DVD sets accessible through
42 * dir, iso, nfs or other URL schemes other than cd/dvd (see
43 * \ref MediaManager for info on different implemented media backends).
44 * Currently it handles URLs containing cdN, CDN, dvdN, and DVDN strings,
45 * where N is the number of particular media in the set.
49 * "iso:/?iso=/path/to/iso/images/openSUSE-10.3-Alpha2plus-DVD-x86_64-DVD1.iso"
50 * "dir:/path/to/cdset/sources/openSUSE-10.3/Alpha2plus/CD1"
53 * MediaSetAccess accesses files on desired media by rewriting
54 * the original URL, replacing the digit (usually) 1 with requested media
55 * number and uses \ref MediaManager to get the files from the new URL.
57 * Additionaly, each media number can be assined a media verifier which
58 * checks if the media we are trying to access is the desired one. See
59 * \ref MediaVerifierBase for more info.
63 * Url url("dir:/path/to/cdset/sources/openSUSE-10.3/Alpha2plus/CD1");
65 * MediaSetAccess access(url);
67 * access.setVerifier(1, media1VerifierRef);
68 * access.setVerifier(2, media2VerifierRef);
70 * Pathname file1 = "/some/file/on/media1";
71 * Pathname providedfile1 = access.provideFile(file1, 1);
72 * Pathname file2 = "/some/file/on/media2";
73 * Pathname providedfile2 = access.provideFile(file1, 2);
77 class MediaSetAccess : public base::ReferenceCounted, private base::NonCopyable
79 friend std::ostream & operator<<( std::ostream & str, const MediaSetAccess & obj );
83 * Creates a callback enabled media access for specified \a url.
86 * \param prefered_attach_point Prefered attach (mount) point. Use, if
87 * you want to mount the media to a specific directory.
89 MediaSetAccess( const Url &url, const Pathname & prefered_attach_point = "" );
90 /** \overload Also taking a \ref label. */
91 MediaSetAccess( const std::string & label_r, const Url &url, const Pathname & prefered_attach_point = "" );
95 * Sets a \ref MediaVerifier verifier for given media number.
97 void setVerifier( unsigned media_nr, media::MediaVerifierRef verifier );
100 * The label identifing this media set and to be sent in a media change request.
102 const std::string & label() const
106 * Set the label identifing this media set and to be sent in a media change request.
108 void setLabel( const std::string & label_r )
109 { _label = label_r; }
111 enum ProvideFileOption
114 * The user is not asked anything, and the error
115 * exception is just propagated */
116 PROVIDE_DEFAULT = 0x0,
117 PROVIDE_NON_INTERACTIVE = 0x1
119 ZYPP_DECLARE_FLAGS(ProvideFileOptions,ProvideFileOption);
122 * Provides a file from a media location.
124 * \param resource location of the file on media
125 * \return local pathname of the requested file
127 * \throws MediaException if a problem occured and user has chosen to
128 * abort the operation. The calling code should take care
129 * to quit the current operation.
130 * \throws SkipRequestException if a problem occured and user has chosen
131 * to skip the current operation. The calling code should continue
132 * with the next one, if possible.
135 * If the resource is marked as optional, no Exception is thrown
136 * and Pathname() is returned
138 * \note interaction with the user does not ocurr if
139 * \ref ProvideFileOptions::NON_INTERACTIVE is set.
141 * \note OnMediaLocation::optional() hint has no effect on the transfer.
143 * \see zypp::media::MediaManager::provideFile()
145 Pathname provideFile( const OnMediaLocation & resource, ProvideFileOptions options = PROVIDE_DEFAULT );
148 * Provides \a file from media \a media_nr.
150 * \param file path to the file relative to media URL
151 * \param media_nr the media number in the media set
152 * \return local pathname of the requested file
154 * \note interaction with the user does not ocurr if
155 * \ref ProvideFileOptions::NON_INTERACTIVE is set.
157 * \note OnMediaLocation::optional() hint has no effect on the transfer.
159 * \throws MediaException if a problem occured and user has chosen to
160 * abort the operation. The calling code should take care
161 * to quit the current operation.
162 * \throws SkipRequestException if a problem occured and user has chosen
163 * to skip the current operation. The calling code should continue
164 * with the next one, if possible.
165 * \see zypp::media::MediaManager::provideFile()
167 Pathname provideFile(const Pathname & file, unsigned media_nr = 1, ProvideFileOptions options = PROVIDE_DEFAULT );
171 * The same as provideFile(Pathname,unsigned) but this method does not
172 * call the user callbacks, except of the case of
173 * wrong media in the drive, and it won't throw an exception in any case.
175 * \return Path to the provided file on success, an empty Pathname() otherwise.
176 * This method is obsolete.
178 * To avoid interaction with the user,
179 * use \ref provideFile with \ref ProvideFileOptions::NON_INTERACTIVE
180 * as an option. However you need to handle the exceptions yourself
181 * in case of error. If you need ignore functionality, try
182 * \ref Fetcher and set \ref OnMediaLocation::setOptional
184 ZYPP_DEPRECATED Pathname provideOptionalFile( const Pathname & file, unsigned media_nr = 1 );
187 * Release file from media.
188 * This signal that file is not needed anymore.
190 * \param resource location of the file on media
192 void releaseFile( const OnMediaLocation &resource );
196 * Release file from media.
197 * This signal that file is not needed anymore.
199 * \param file path to the file relative to media URL
200 * \param media_nr the media number in the media set
202 void releaseFile(const Pathname & file, unsigned media_nr = 1 );
205 * Provides direcotry \a dir from media number \a media_nr.
207 * \param dir path to the directory relative to media URL
208 * \param recursive whether to provide the whole directory subtree
209 * \param media_nr the media number in the media set
210 * \return local pathname of the requested directory
212 * \throws MediaException if a problem occured and user has chosen to
213 * abort the operation. The calling code should take care
214 * to quit the current operation.
215 * \todo throw SkipRequestException if a problem occured and user has chosen
216 * to skip the current operation. The calling code should continue
217 * with the next one, if possible.
218 * \see zypp::media::MediaManager::provideDir()
219 * \see zypp::media::MediaManager::provideDirTree()
221 Pathname provideDir(const Pathname & dir, bool recursive, unsigned media_nr = 1, ProvideFileOptions options = PROVIDE_DEFAULT );
224 * Checks if a file exists on the specified media, with user callbacks.
226 * \param file file to check
227 * \param media_nr Media number
229 * \throws MediaException if a problem occured and user has chosen to
230 * abort the operation. The calling code should take care
231 * to quit the current operation.
232 * \throws SkipRequestException if a problem occured and user has chosen
233 * to skip the current operation. The calling code should continue
234 * with the next one, if possible.
235 * \see zypp::media::MediaManager::doesFileExist(MediaAccessId,const Pathname&)
237 bool doesFileExist(const Pathname & file, unsigned media_nr = 1 );
240 * Fills \ref retlist with directory information.
242 void dirInfo( filesystem::DirContent &retlist, const Pathname &dirname,
243 bool dots = true, unsigned media_nr = 1 );
246 * Release all attached media of this set.
248 * \throws MediaNotOpenException for invalid access IDs.
253 * Replaces media number in specified url with given \a medianr.
255 * Media number in the URL is searched for with regex
256 * <tt> "^(.*(cd|dvd))([0-9]+)(\\.iso)$" </tt> for iso scheme and
257 * with <tt> "^(.*(cd|dvd))([0-9]+)(/?)$" </tt> for other schemes.
259 * For cd and dvd scheme it returns the original URL, as well as for
260 * URL which do not match the above regexes.
262 * \param url_r original URL
263 * \param medianr requested media number
264 * \return rewritten URL if applicable, the original URL otherwise
266 static Url rewriteUrl (const Url & url_r, const media::MediaNr medianr);
270 * Provides the \a file from medium number \a media_nr and returns its
273 * \note The method must not throw if \a checkonly is <tt>true</tt>.
275 * \throws MediaException \a checkonly is <tt>false</tt> and
276 * a problem occured and user has chosen to
277 * abort the operation. The calling code should take care
278 * to quit the current operation.
279 * \throws SkipRequestException \a checkonly is <tt>false</tt> and
280 * a problem occured and user has chosen
281 * to skip the current operation. The calling code should continue
282 * with the next one, if possible.
284 Pathname provideFileInternal( const OnMediaLocation &resource, ProvideFileOptions options );
286 typedef function<void( media::MediaAccessId, const Pathname & )> ProvideOperation;
288 void provide( ProvideOperation op, const OnMediaLocation &resource, ProvideFileOptions options );
290 media::MediaAccessId getMediaAccessId (media::MediaNr medianr);
291 virtual std::ostream & dumpOn( std::ostream & str ) const;
294 /** Media or media set URL */
298 * Prefered mount point.
300 * \see MediaManager::open(Url,Pathname)
301 * \see MediaHandler::_attachPoint
303 Pathname _prefAttachPoint;
307 typedef std::map<media::MediaNr, media::MediaAccessId> MediaMap;
308 typedef std::map<media::MediaNr, media::MediaVerifierRef > VerifierMap;
310 /** Mapping between media number and Media Access ID */
312 /** Mapping between media number and corespondent verifier */
313 VerifierMap _verifiers;
315 ///////////////////////////////////////////////////////////////////
316 ZYPP_DECLARE_OPERATORS_FOR_FLAGS(MediaSetAccess::ProvideFileOptions);
318 /** \relates MediaSetAccess Stream output */
319 inline std::ostream & operator<<( std::ostream & str, const MediaSetAccess & obj )
320 { return obj.dumpOn( str ); }
324 ///////////////////////////////////////////////////////////////////
325 #endif // ZYPP_SOURCE_MediaSetAccess_H