1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/media/MediaHandler.h
12 #ifndef ZYPP_MEDIA_MEDIAHANDLERL_H
13 #define ZYPP_MEDIA_MEDIAHANDLERL_H
19 #include "zypp/Pathname.h"
20 #include "zypp/PathInfo.h"
21 #include "zypp/base/PtrTypes.h"
25 #include "zypp/media/MediaSource.h"
26 #include "zypp/media/MediaException.h"
32 ///////////////////////////////////////////////////////////////////
34 // CLASS NAME : MediaHandler
36 * @short Abstract base class for 'physical' MediaHandler like MediaCD, etc.
38 * Handles the requests forwarded by @ref MediaAccess. The public interface
39 * contains nonvirtual methods, which should do common sanitychecks and
40 * logging. For the real action they call virtual methods overloaded by the
44 friend std::ostream & operator<<( std::ostream & str, const MediaHandler & obj );
47 typedef shared_ptr<MediaHandler> Ptr;
48 typedef shared_ptr<const MediaHandler> constPtr;
52 * MediaAccess (MediaManager) needs access to the
53 * attachedMedia() function to deliver a shared
54 * media source and its attach point to other
55 * media handler instances.
57 friend class MediaAccess;
60 * The attached media source.
62 MediaSourceRef _mediaSource;
65 * Returns the attached media. Used by MediaManager
66 * to find other handlers using the same source.
68 AttachedMedia attachedMedia() const;
71 * this is where the media will be actually "mounted"
72 * all files are provided 'below' this directory.
74 AttachPointRef _attachPoint;
77 * The relative root directory of the data on the media.
78 * See also _localRoot and urlpath_below_attachpoint_r
79 * constructor argument.
81 Pathname _relativeRoot;
84 * The local directory that corresponds to the media url.
85 * With NFS it's the '_attachPoint', as the directory on the
86 * server is mounted. With CD/DVD it's 'attach point+_relativeRoot'
87 * because the CDs root directory is mounted. And with CIFS
88 * it's '_url.path() without the shares name'.
93 * True if concrete handler downloads files to the local
94 * filesystem. If true releaseFile/Dir will delete them.
105 * Return the currently used attach point.
107 Pathname attachPoint() const;
110 * Set a new attach point and update localRoot.
112 void setAttachPoint(const Pathname &path, bool _temporary);
115 * Set a (shared) attach point and update localRoot.
117 void setAttachPoint(const AttachPointRef &ref);
120 * Try to create a default / temporary attach point.
121 * \return The name of the new attach point or empty path name.
123 Pathname createAttachPoint() const;
126 * Remove unused attach point.
128 void removeAttachPoint();
130 std::string mediaSourceName() const
132 return _mediaSource ? _mediaSource->name : "";
135 void setMediaSource(const MediaSourceRef &ref);
138 * Ask the media manager if specified media source
139 * is already attached.
142 findAttachedMedia(const MediaSourceRef &media) const;
146 ///////////////////////////////////////////////////////////////////
148 // Real action interface to be overloaded by concrete handler.
150 ///////////////////////////////////////////////////////////////////
153 * Call concrete handler to attach the media.
155 * Asserted that not already attached, and attachPoint is a directory.
157 * @param next try next available device in turn until end of device
158 * list is reached (for media which are accessible through multiple
159 * devices like cdroms).
161 * \throws MediaException
164 virtual void attachTo(bool next = false) = 0;
167 * Call concrete handler to disconnect media.
169 * Asserted that media is attached.
171 * This is useful for media which e.g. holds open a connection to a
172 * server like FTP. After calling disconnect() the media object still is
173 * valid and files are present.
175 * After calling disconnect() it's not possible to call provideFile() or
176 * provideDir() anymore.
178 * \throws MediaException
181 virtual void disconnectFrom() { return; }
184 * Call concrete handler to release the media.
186 * If eject is true, and the media is used in one handler
187 * instance only, physically eject the media (i.e. CD-ROM).
189 * Asserted that media is attached.
191 * \throws MediaException
194 virtual void releaseFrom( bool eject ) = 0;
197 * Call concrete handler to physically eject the media (i.e. CD-ROM)
198 * in case the media is not attached..
200 * Asserted that media is not attached.
202 virtual void forceEject() {}
205 * Call concrete handler to provide file below attach point.
207 * Default implementation provided, that returns whether a file
208 * is located at '_localRoot + filename'.
210 * Asserted that media is attached.
212 * \throws MediaException
215 virtual void getFile( const Pathname & filename ) const = 0;
218 * Call concrete handler to provide a file under a different place
219 * in the file system (usually not under attach point) as a copy.
220 * Media must be attached before by callee.
222 * Default implementation provided that calls getFile(srcFilename)
223 * and copies the result around.
225 * \throws MediaException
228 virtual void getFileCopy( const Pathname & srcFilename, const Pathname & targetFilename ) const;
232 * Call concrete handler to provide directory content (not recursive!)
233 * below attach point.
235 * Return E_not_supported_by_media if media does not support retrieval of
238 * Default implementation provided, that returns whether a directory
239 * is located at '_localRoot + dirname'.
241 * Asserted that media is attached.
243 * \throws MediaException
246 virtual void getDir( const Pathname & dirname, bool recurse_r ) const = 0;
249 * Call concrete handler to provide a content list of directory on media
250 * via retlist. If dots is false entries starting with '.' are not reported.
252 * Return E_not_supported_by_media if media does not support retrieval of
255 * Default implementation provided, that returns the content of a
256 * directory at '_localRoot + dirnname' retrieved via 'readdir'.
258 * Asserted that media is attached and retlist is empty.
260 * \throws MediaException
263 virtual void getDirInfo( std::list<std::string> & retlist,
264 const Pathname & dirname, bool dots = true ) const = 0;
267 * Basically the same as getDirInfo above. The content list is returned as
268 * filesystem::DirContent, which includes name and filetype of each directory
269 * entry. Retrieving the filetype usg. requires an additional ::stat call for
270 * each entry, thus it's more expensive than a simple readdir.
272 * Asserted that media is attached and retlist is empty.
274 * \throws MediaException
277 virtual void getDirInfo( filesystem::DirContent & retlist,
278 const Pathname & dirname, bool dots = true ) const = 0;
283 * Retrieve and if available scan dirname/directory.yast.
285 * Asserted that media is attached.
287 * \throws MediaException
290 void getDirectoryYast( std::list<std::string> & retlist,
291 const Pathname & dirname, bool dots = true ) const;
294 * Retrieve and if available scan dirname/directory.yast.
296 * Asserted that media is attached.
298 * \throws MediaException
301 void getDirectoryYast( filesystem::DirContent & retlist,
302 const Pathname & dirname, bool dots = true ) const;
307 * If the concrete media handler provides a nonempty
308 * attach_point, it must be an existing directory.
310 * On an empty attach_point, MediaHandler will create
311 * a temporay directory, which will be erased from
314 * On any error, the attach_point is set to an empty Pathname,
315 * which should lead to E_bad_attachpoint.
317 MediaHandler ( const Url& url_r,
318 const Pathname & attach_point_r,
319 const Pathname & urlpath_below_attachpoint_r,
320 const bool does_download_r );
323 * Contolling MediaAccess takes care, that attached media is released
324 * prior to deleting this.
326 virtual ~MediaHandler();
331 ///////////////////////////////////////////////////////////////////
333 // MediaAccess interface. Does common checks and logging.
334 // Invokes real action if necessary.
336 ///////////////////////////////////////////////////////////////////
339 * Protocol hint for MediaAccess.
341 std::string protocol() const { return _url.getScheme(); }
346 Url url() const { return _url; }
349 * Use concrete handler to attach the media.
351 * @param next try next available device in turn until end of device
352 * list is reached (for media which are accessible through multiple
353 * devices like cdroms).
355 * \throws MediaException
358 void attach(bool next);
361 * True if media is attached.
363 bool isAttached() const { return _mediaSource; }
366 * Return the local directory that corresponds to medias url,
367 * no matter if media isAttached or not. Files requested will
368 * be available at 'localRoot() + filename' or better
369 * 'localPath( filename )'.
371 * Returns empty pathname if E_bad_attachpoint
373 const Pathname & localRoot() const { return _localRoot; }
376 * Files provided will be available at 'localPath(filename)'.
378 * Returns empty pathname if E_bad_attachpoint
380 Pathname localPath( const Pathname & pathname ) const;
383 * Use concrete handler to isconnect media.
385 * This is useful for media which e.g. holds open a connection to a
386 * server like FTP. After calling disconnect() the media object still is
387 * valid and files are present.
389 * After calling disconnect() it's not possible to call provideFile() or
390 * provideDir() anymore.
392 * \throws MediaException
398 * Use concrete handler to release the media.
399 * @param eject if true, physically eject the media * (i.e. CD-ROM)
401 * \throws MediaException
404 void release( bool eject = false );
407 * Use concrete handler to provide file denoted by path below
408 * 'localRoot'. Filename is interpreted relative to the
409 * attached url and a path prefix is preserved.
411 * \throws MediaException
414 void provideFile( Pathname filename ) const;
417 * Call concrete handler to provide a copy of a file under a different place
418 * in the file system (usually not under attach point) as a copy.
419 * Media must be attached before by callee.
421 * @param srcFilename Filename of source file on the media
422 * @param targetFilename Filename for the target in the file system
424 * \throws MediaException
427 void provideFileCopy( Pathname srcFilename, Pathname targetFilename) const;
430 * Use concrete handler to provide directory denoted
431 * by path below 'localRoot' (not recursive!).
432 * dirname is interpreted relative to the
433 * attached url and a path prefix is preserved.
435 * \throws MediaException
438 void provideDir( Pathname dirname ) const;
441 * Use concrete handler to provide directory tree denoted
442 * by path below 'localRoot' (recursive!!).
443 * dirname is interpreted relative to the
444 * attached url and a path prefix is preserved.
446 * \throws MediaException
449 void provideDirTree( Pathname dirname ) const;
452 * Remove filename below localRoot IFF handler downloads files
453 * to the local filesystem. Never remove anything from media.
455 * \throws MediaException
458 void releaseFile( const Pathname & filename ) const { return releasePath( filename ); }
461 * Remove directory tree below localRoot IFF handler downloads files
462 * to the local filesystem. Never remove anything from media.
464 * \throws MediaException
467 void releaseDir( const Pathname & dirname ) const { return releasePath( dirname ); }
470 * Remove pathname below localRoot IFF handler downloads files
471 * to the local filesystem. Never remove anything from media.
473 * If pathname denotes a directory it is recursively removed.
474 * If pathname is empty or '/' everything below the localRoot
475 * is recursively removed.
476 * If pathname denotes a file it is unlinked.
478 * \throws MediaException
481 void releasePath( Pathname pathname ) const;
486 * Return content of directory on media via retlist. If dots is false
487 * entries starting with '.' are not reported.
489 * The request is forwarded to the concrete handler,
490 * which may atempt to retieve the content e.g. via 'readdir'
492 * <B>Caution:</B> This is not supported by all media types.
493 * Be prepared to handle E_not_supported_by_media.
495 * \throws MediaException
498 void dirInfo( std::list<std::string> & retlist,
499 const Pathname & dirname, bool dots = true ) const;
502 * Basically the same as dirInfo above. The content is returned as
503 * filesystem::DirContent, which includes name and filetype of each directory
504 * entry. Retrieving the filetype usg. requires an additional ::stat call for
505 * each entry, thus it's more expensive than a simple readdir.
507 * <B>Caution:</B> This is not supported by all media types.
508 * Be prepared to handle E_not_supported_by_media.
510 * \throws MediaException
513 void dirInfo( filesystem::DirContent & retlist,
514 const Pathname & dirname, bool dots = true ) const;
517 ///////////////////////////////////////////////////////////////////
519 #define MEDIA_HANDLER_API \
521 virtual void attachTo (bool next = false); \
522 virtual void releaseFrom( bool eject ); \
523 virtual void getFile( const Pathname & filename ) const; \
524 virtual void getDir( const Pathname & dirname, bool recurse_r ) const; \
525 virtual void getDirInfo( std::list<std::string> & retlist, \
526 const Pathname & dirname, bool dots = true ) const; \
527 virtual void getDirInfo( filesystem::DirContent & retlist, \
528 const Pathname & dirname, bool dots = true ) const;
534 #endif // ZYPP_MEDIA_MEDIAHANDLERL_H